]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Controller; |
9d50517c NL |
4 | |
5 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
6 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
163eae0b | 7 | use Symfony\Component\HttpFoundation\Request; |
be463487 | 8 | use Wallabag\CoreBundle\Entity\Entry; |
6b767d1c | 9 | use Wallabag\CoreBundle\Service\Extractor; |
82d6d9cb JB |
10 | use Wallabag\CoreBundle\Form\Type\NewEntryType; |
11 | use Wallabag\CoreBundle\Form\Type\EditEntryType; | |
26864574 NL |
12 | use Wallabag\CoreBundle\Filter\EntryFilterType; |
13 | use Pagerfanta\Adapter\DoctrineORMAdapter; | |
14 | use Pagerfanta\Pagerfanta; | |
9d50517c NL |
15 | |
16 | class EntryController extends Controller | |
17 | { | |
b84a8055 | 18 | /** |
3d2b2d62 J |
19 | * @param Request $request |
20 | * | |
053b9568 | 21 | * @Route("/new-entry", name="new_entry") |
3d2b2d62 | 22 | * |
b84a8055 NL |
23 | * @return \Symfony\Component\HttpFoundation\Response |
24 | */ | |
053b9568 | 25 | public function addEntryFormAction(Request $request) |
b84a8055 | 26 | { |
3b815d2d | 27 | $entry = new Entry($this->getUser()); |
b84a8055 | 28 | |
82d6d9cb | 29 | $form = $this->createForm(new NewEntryType(), $entry); |
b84a8055 NL |
30 | |
31 | $form->handleRequest($request); | |
32 | ||
33 | if ($form->isValid()) { | |
6b767d1c | 34 | $content = Extractor::extract($entry->getUrl()); |
b84a8055 | 35 | |
6b767d1c NL |
36 | $entry->setTitle($content->getTitle()); |
37 | $entry->setContent($content->getBody()); | |
38 | ||
39 | $em = $this->getDoctrine()->getManager(); | |
b84a8055 NL |
40 | $em->persist($entry); |
41 | $em->flush(); | |
42 | ||
43 | $this->get('session')->getFlashBag()->add( | |
44 | 'notice', | |
45 | 'Entry saved' | |
46 | ); | |
47 | ||
48 | return $this->redirect($this->generateUrl('homepage')); | |
49 | } | |
50 | ||
053b9568 | 51 | return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array( |
b84a8055 NL |
52 | 'form' => $form->createView(), |
53 | )); | |
54 | } | |
55 | ||
053b9568 NL |
56 | /** |
57 | * @param Request $request | |
58 | * | |
59 | * @Route("/new", name="new") | |
60 | * | |
61 | * @return \Symfony\Component\HttpFoundation\Response | |
62 | */ | |
63 | public function addEntryAction(Request $request) | |
64 | { | |
65 | return $this->render('WallabagCoreBundle:Entry:new.html.twig'); | |
66 | } | |
67 | ||
82d6d9cb JB |
68 | /** |
69 | * Edit an entry content. | |
70 | * | |
71 | * @param Request $request | |
72 | * @param Entry $entry | |
73 | * | |
74 | * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit") | |
75 | * | |
76 | * @return \Symfony\Component\HttpFoundation\Response | |
77 | */ | |
78 | public function editEntryAction(Request $request, Entry $entry) | |
79 | { | |
80 | $this->checkUserAction($entry); | |
81 | ||
82 | $form = $this->createForm(new EditEntryType(), $entry); | |
83 | ||
84 | $form->handleRequest($request); | |
85 | ||
86 | if ($form->isValid()) { | |
87 | $em = $this->getDoctrine()->getManager(); | |
88 | $em->persist($entry); | |
89 | $em->flush(); | |
90 | ||
91 | $this->get('session')->getFlashBag()->add( | |
92 | 'notice', | |
93 | 'Entry updated' | |
94 | ); | |
95 | ||
96 | return $this->redirect($this->generateUrl('view', array('id' => $entry->getId()))); | |
97 | } | |
98 | ||
99 | return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array( | |
100 | 'form' => $form->createView(), | |
101 | )); | |
102 | } | |
103 | ||
9d50517c | 104 | /** |
4346a860 | 105 | * Shows unread entries for current user. |
163eae0b | 106 | * |
26864574 NL |
107 | * @param Request $request |
108 | * @param int $page | |
109 | * | |
9fb6ac83 | 110 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) |
3d2b2d62 | 111 | * |
163eae0b | 112 | * @return \Symfony\Component\HttpFoundation\Response |
9d50517c | 113 | */ |
26864574 | 114 | public function showUnreadAction(Request $request, $page) |
9d50517c | 115 | { |
0ab7404f | 116 | return $this->showEntries('unread', $request, $page); |
9d50517c | 117 | } |
bd9f0815 NL |
118 | |
119 | /** | |
4346a860 | 120 | * Shows read entries for current user. |
163eae0b | 121 | * |
26864574 NL |
122 | * @param Request $request |
123 | * @param int $page | |
124 | * | |
9fb6ac83 | 125 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) |
3d2b2d62 | 126 | * |
163eae0b | 127 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 | 128 | */ |
26864574 | 129 | public function showArchiveAction(Request $request, $page) |
bd9f0815 | 130 | { |
0ab7404f JB |
131 | return $this->showEntries('archive', $request, $page); |
132 | } | |
133 | ||
134 | /** | |
135 | * Shows starred entries for current user. | |
136 | * | |
137 | * @param Request $request | |
138 | * @param int $page | |
139 | * | |
140 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) | |
141 | * | |
142 | * @return \Symfony\Component\HttpFoundation\Response | |
143 | */ | |
144 | public function showStarredAction(Request $request, $page) | |
145 | { | |
146 | return $this->showEntries('starred', $request, $page); | |
147 | } | |
26864574 | 148 | |
0ab7404f JB |
149 | /** |
150 | * Global method to retrieve entries depending on the given type | |
151 | * It returns the response to be send. | |
152 | * | |
153 | * @param string $type Entries type: unread, starred or archive | |
154 | * @param Request $request | |
155 | * @param int $page | |
156 | * | |
157 | * @return \Symfony\Component\HttpFoundation\Response | |
158 | */ | |
159 | private function showEntries($type, Request $request, $page) | |
160 | { | |
161 | $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry'); | |
162 | ||
163 | switch ($type) { | |
164 | case 'starred': | |
165 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); | |
166 | break; | |
167 | ||
168 | case 'archive': | |
169 | $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); | |
170 | break; | |
171 | ||
172 | case 'unread': | |
173 | $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); | |
174 | break; | |
175 | ||
176 | default: | |
177 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
178 | } | |
179 | ||
180 | $form = $this->get('form.factory')->create(new EntryFilterType()); | |
9fb6ac83 | 181 | |
26864574 NL |
182 | if ($request->query->has($form->getName())) { |
183 | // manually bind values from the request | |
184 | $form->submit($request->query->get($form->getName())); | |
185 | ||
186 | // build the query from the given form object | |
0ab7404f | 187 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); |
26864574 NL |
188 | } |
189 | ||
0ab7404f | 190 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); |
26864574 NL |
191 | $entries = new Pagerfanta($pagerAdapter); |
192 | ||
48ffc5a4 | 193 | $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); |
9fb6ac83 | 194 | $entries->setCurrentPage($page); |
bd9f0815 NL |
195 | |
196 | return $this->render( | |
ad4d1caa | 197 | 'WallabagCoreBundle:Entry:entries.html.twig', |
9fb6ac83 | 198 | array( |
8ce32af6 JB |
199 | 'form' => $form->createView(), |
200 | 'entries' => $entries, | |
201 | 'currentPage' => $page, | |
9fb6ac83 | 202 | ) |
bd9f0815 | 203 | ); |
bd9f0815 NL |
204 | } |
205 | ||
206 | /** | |
4346a860 | 207 | * Shows entry content. |
163eae0b | 208 | * |
3d2b2d62 J |
209 | * @param Entry $entry |
210 | * | |
bd9f0815 | 211 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") |
3d2b2d62 | 212 | * |
163eae0b | 213 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 | 214 | */ |
be463487 | 215 | public function viewAction(Entry $entry) |
bd9f0815 | 216 | { |
3d2b2d62 J |
217 | $this->checkUserAction($entry); |
218 | ||
bd9f0815 | 219 | return $this->render( |
ad4d1caa | 220 | 'WallabagCoreBundle:Entry:entry.html.twig', |
bd9f0815 NL |
221 | array('entry' => $entry) |
222 | ); | |
163eae0b NL |
223 | } |
224 | ||
225 | /** | |
4346a860 | 226 | * Changes read status for an entry. |
163eae0b | 227 | * |
3d2b2d62 J |
228 | * @param Request $request |
229 | * @param Entry $entry | |
230 | * | |
163eae0b | 231 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") |
3d2b2d62 | 232 | * |
163eae0b NL |
233 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
234 | */ | |
be463487 | 235 | public function toggleArchiveAction(Request $request, Entry $entry) |
163eae0b | 236 | { |
3d2b2d62 J |
237 | $this->checkUserAction($entry); |
238 | ||
163eae0b NL |
239 | $entry->toggleArchive(); |
240 | $this->getDoctrine()->getManager()->flush(); | |
241 | ||
242 | $this->get('session')->getFlashBag()->add( | |
243 | 'notice', | |
8ce32af6 | 244 | 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived') |
163eae0b NL |
245 | ); |
246 | ||
247 | return $this->redirect($request->headers->get('referer')); | |
248 | } | |
249 | ||
250 | /** | |
4346a860 | 251 | * Changes favorite status for an entry. |
163eae0b | 252 | * |
3d2b2d62 J |
253 | * @param Request $request |
254 | * @param Entry $entry | |
255 | * | |
163eae0b | 256 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") |
3d2b2d62 | 257 | * |
163eae0b NL |
258 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
259 | */ | |
be463487 | 260 | public function toggleStarAction(Request $request, Entry $entry) |
163eae0b | 261 | { |
3d2b2d62 J |
262 | $this->checkUserAction($entry); |
263 | ||
163eae0b NL |
264 | $entry->toggleStar(); |
265 | $this->getDoctrine()->getManager()->flush(); | |
266 | ||
267 | $this->get('session')->getFlashBag()->add( | |
268 | 'notice', | |
8ce32af6 | 269 | 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred') |
163eae0b NL |
270 | ); |
271 | ||
272 | return $this->redirect($request->headers->get('referer')); | |
273 | } | |
274 | ||
275 | /** | |
4346a860 | 276 | * Deletes entry. |
163eae0b | 277 | * |
3d2b2d62 J |
278 | * @param Request $request |
279 | * @param Entry $entry | |
280 | * | |
163eae0b | 281 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") |
3d2b2d62 | 282 | * |
163eae0b NL |
283 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
284 | */ | |
be463487 | 285 | public function deleteEntryAction(Request $request, Entry $entry) |
163eae0b | 286 | { |
3d2b2d62 J |
287 | $this->checkUserAction($entry); |
288 | ||
1d147791 NL |
289 | $em = $this->getDoctrine()->getManager(); |
290 | $em->remove($entry); | |
291 | $em->flush(); | |
163eae0b NL |
292 | |
293 | $this->get('session')->getFlashBag()->add( | |
294 | 'notice', | |
295 | 'Entry deleted' | |
296 | ); | |
bd9f0815 | 297 | |
163eae0b | 298 | return $this->redirect($request->headers->get('referer')); |
bd9f0815 | 299 | } |
3d2b2d62 J |
300 | |
301 | /** | |
4346a860 | 302 | * Check if the logged user can manage the given entry. |
3d2b2d62 J |
303 | * |
304 | * @param Entry $entry | |
305 | */ | |
306 | private function checkUserAction(Entry $entry) | |
307 | { | |
308 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | |
82d6d9cb | 309 | throw $this->createAccessDeniedException('You can not access this entry.'); |
3d2b2d62 J |
310 | } |
311 | } | |
9d50517c | 312 | } |