]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Controller; |
9d50517c | 4 | |
619cc453 | 5 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
671a2b88 | 6 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; |
9d50517c NL |
7 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
8 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
163eae0b | 9 | use Symfony\Component\HttpFoundation\Request; |
2863bf2a | 10 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
be463487 | 11 | use Wallabag\CoreBundle\Entity\Entry; |
b0b893ea | 12 | use Wallabag\CoreBundle\Form\Type\EntryFilterType; |
619cc453 JB |
13 | use Wallabag\CoreBundle\Form\Type\EditEntryType; |
14 | use Wallabag\CoreBundle\Form\Type\NewEntryType; | |
9d50517c NL |
15 | |
16 | class EntryController extends Controller | |
17 | { | |
880a0e1c NL |
18 | /** |
19 | * @param Entry $entry | |
20 | */ | |
21 | private function updateEntry(Entry $entry) | |
22 | { | |
23 | try { | |
24 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | |
39ba51ca | 25 | |
880a0e1c NL |
26 | $em = $this->getDoctrine()->getManager(); |
27 | $em->persist($entry); | |
28 | $em->flush(); | |
29 | } catch (\Exception $e) { | |
39ba51ca JB |
30 | $this->get('logger')->error('Error while saving an entry', [ |
31 | 'exception' => $e, | |
32 | 'entry' => $entry, | |
33 | ]); | |
34 | ||
880a0e1c NL |
35 | return false; |
36 | } | |
37 | ||
38 | return true; | |
39 | } | |
40 | ||
b84a8055 | 41 | /** |
3d2b2d62 J |
42 | * @param Request $request |
43 | * | |
053b9568 | 44 | * @Route("/new-entry", name="new_entry") |
3d2b2d62 | 45 | * |
b84a8055 NL |
46 | * @return \Symfony\Component\HttpFoundation\Response |
47 | */ | |
053b9568 | 48 | public function addEntryFormAction(Request $request) |
b84a8055 | 49 | { |
3b815d2d | 50 | $entry = new Entry($this->getUser()); |
b84a8055 | 51 | |
5c895a7f | 52 | $form = $this->createForm(NewEntryType::class, $entry); |
b84a8055 NL |
53 | |
54 | $form->handleRequest($request); | |
55 | ||
56 | if ($form->isValid()) { | |
b00a89e0 | 57 | $existingEntry = $this->checkIfEntryAlreadyExists($entry); |
dda57bb9 | 58 | |
5a4bbcc9 | 59 | if (false !== $existingEntry) { |
dda57bb9 NL |
60 | $this->get('session')->getFlashBag()->add( |
61 | 'notice', | |
4094ea47 | 62 | $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')]) |
dda57bb9 NL |
63 | ); |
64 | ||
4094ea47 | 65 | return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()])); |
dda57bb9 NL |
66 | } |
67 | ||
39ba51ca JB |
68 | $message = 'flashes.entry.notice.entry_saved'; |
69 | if (false === $this->updateEntry($entry)) { | |
70 | $message = 'flashes.entry.notice.entry_saved_failed'; | |
71 | } | |
72 | ||
73 | $this->get('session')->getFlashBag()->add('notice', $message); | |
b84a8055 NL |
74 | |
75 | return $this->redirect($this->generateUrl('homepage')); | |
76 | } | |
77 | ||
4094ea47 | 78 | return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', [ |
b84a8055 | 79 | 'form' => $form->createView(), |
4094ea47 | 80 | ]); |
b84a8055 NL |
81 | } |
82 | ||
880a0e1c NL |
83 | /** |
84 | * @param Request $request | |
85 | * | |
86 | * @Route("/bookmarklet", name="bookmarklet") | |
87 | * | |
88 | * @return \Symfony\Component\HttpFoundation\Response | |
89 | */ | |
5f8a7857 | 90 | public function addEntryViaBookmarkletAction(Request $request) |
880a0e1c NL |
91 | { |
92 | $entry = new Entry($this->getUser()); | |
93 | $entry->setUrl($request->get('url')); | |
f652f41d | 94 | |
b00a89e0 | 95 | if (false === $this->checkIfEntryAlreadyExists($entry)) { |
f652f41d NL |
96 | $this->updateEntry($entry); |
97 | } | |
880a0e1c NL |
98 | |
99 | return $this->redirect($this->generateUrl('homepage')); | |
100 | } | |
101 | ||
053b9568 | 102 | /** |
053b9568 NL |
103 | * @Route("/new", name="new") |
104 | * | |
105 | * @return \Symfony\Component\HttpFoundation\Response | |
106 | */ | |
4d0ec0e7 | 107 | public function addEntryAction() |
053b9568 NL |
108 | { |
109 | return $this->render('WallabagCoreBundle:Entry:new.html.twig'); | |
110 | } | |
111 | ||
82d6d9cb JB |
112 | /** |
113 | * Edit an entry content. | |
114 | * | |
115 | * @param Request $request | |
116 | * @param Entry $entry | |
117 | * | |
118 | * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit") | |
119 | * | |
120 | * @return \Symfony\Component\HttpFoundation\Response | |
121 | */ | |
122 | public function editEntryAction(Request $request, Entry $entry) | |
123 | { | |
124 | $this->checkUserAction($entry); | |
125 | ||
5c895a7f | 126 | $form = $this->createForm(EditEntryType::class, $entry); |
82d6d9cb JB |
127 | |
128 | $form->handleRequest($request); | |
129 | ||
130 | if ($form->isValid()) { | |
131 | $em = $this->getDoctrine()->getManager(); | |
132 | $em->persist($entry); | |
133 | $em->flush(); | |
134 | ||
135 | $this->get('session')->getFlashBag()->add( | |
136 | 'notice', | |
4204a06b | 137 | 'flashes.entry.notice.entry_updated' |
82d6d9cb JB |
138 | ); |
139 | ||
4094ea47 | 140 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); |
82d6d9cb JB |
141 | } |
142 | ||
4094ea47 | 143 | return $this->render('WallabagCoreBundle:Entry:edit.html.twig', [ |
82d6d9cb | 144 | 'form' => $form->createView(), |
4094ea47 | 145 | ]); |
82d6d9cb JB |
146 | } |
147 | ||
89659c9e NL |
148 | /** |
149 | * Shows all entries for current user. | |
150 | * | |
151 | * @param Request $request | |
152 | * @param int $page | |
153 | * | |
154 | * @Route("/all/list/{page}", name="all", defaults={"page" = "1"}) | |
155 | * | |
156 | * @return \Symfony\Component\HttpFoundation\Response | |
157 | */ | |
158 | public function showAllAction(Request $request, $page) | |
159 | { | |
2b7a4889 | 160 | return $this->showEntries('all', $request, $page); |
89659c9e NL |
161 | } |
162 | ||
9d50517c | 163 | /** |
4346a860 | 164 | * Shows unread entries for current user. |
163eae0b | 165 | * |
26864574 NL |
166 | * @param Request $request |
167 | * @param int $page | |
168 | * | |
9fb6ac83 | 169 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) |
3d2b2d62 | 170 | * |
163eae0b | 171 | * @return \Symfony\Component\HttpFoundation\Response |
9d50517c | 172 | */ |
26864574 | 173 | public function showUnreadAction(Request $request, $page) |
9d50517c | 174 | { |
5c072d2b NL |
175 | // load the quickstart if no entry in database |
176 | if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUsername($this->getUser()->getId()) == 0) { | |
177 | return $this->redirect($this->generateUrl('quickstart')); | |
178 | } | |
179 | ||
0ab7404f | 180 | return $this->showEntries('unread', $request, $page); |
9d50517c | 181 | } |
bd9f0815 NL |
182 | |
183 | /** | |
4346a860 | 184 | * Shows read entries for current user. |
163eae0b | 185 | * |
26864574 NL |
186 | * @param Request $request |
187 | * @param int $page | |
188 | * | |
9fb6ac83 | 189 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) |
3d2b2d62 | 190 | * |
163eae0b | 191 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 | 192 | */ |
26864574 | 193 | public function showArchiveAction(Request $request, $page) |
bd9f0815 | 194 | { |
0ab7404f JB |
195 | return $this->showEntries('archive', $request, $page); |
196 | } | |
197 | ||
198 | /** | |
199 | * Shows starred entries for current user. | |
200 | * | |
201 | * @param Request $request | |
202 | * @param int $page | |
203 | * | |
204 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) | |
205 | * | |
206 | * @return \Symfony\Component\HttpFoundation\Response | |
207 | */ | |
208 | public function showStarredAction(Request $request, $page) | |
209 | { | |
210 | return $this->showEntries('starred', $request, $page); | |
211 | } | |
26864574 | 212 | |
0ab7404f JB |
213 | /** |
214 | * Global method to retrieve entries depending on the given type | |
215 | * It returns the response to be send. | |
216 | * | |
217 | * @param string $type Entries type: unread, starred or archive | |
218 | * @param Request $request | |
219 | * @param int $page | |
220 | * | |
221 | * @return \Symfony\Component\HttpFoundation\Response | |
222 | */ | |
223 | private function showEntries($type, Request $request, $page) | |
224 | { | |
5c072d2b | 225 | $repository = $this->get('wallabag_core.entry_repository'); |
0ab7404f JB |
226 | |
227 | switch ($type) { | |
228 | case 'starred': | |
229 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); | |
230 | break; | |
231 | ||
232 | case 'archive': | |
233 | $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); | |
234 | break; | |
235 | ||
236 | case 'unread': | |
237 | $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); | |
238 | break; | |
239 | ||
2b7a4889 NL |
240 | case 'all': |
241 | $qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); | |
242 | break; | |
243 | ||
0ab7404f JB |
244 | default: |
245 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
246 | } | |
247 | ||
1d76102a | 248 | $form = $this->createForm(EntryFilterType::class); |
9fb6ac83 | 249 | |
26864574 NL |
250 | if ($request->query->has($form->getName())) { |
251 | // manually bind values from the request | |
252 | $form->submit($request->query->get($form->getName())); | |
253 | ||
254 | // build the query from the given form object | |
0ab7404f | 255 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); |
26864574 NL |
256 | } |
257 | ||
0ab7404f | 258 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); |
26864574 | 259 | |
891456ba NL |
260 | $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries') |
261 | ->prepare($pagerAdapter, $page); | |
262 | ||
671a2b88 ML |
263 | try { |
264 | $entries->setCurrentPage($page); | |
265 | } catch (OutOfRangeCurrentPageException $e) { | |
266 | if ($page > 1) { | |
4094ea47 | 267 | return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302); |
671a2b88 ML |
268 | } |
269 | } | |
bd9f0815 NL |
270 | |
271 | return $this->render( | |
ad4d1caa | 272 | 'WallabagCoreBundle:Entry:entries.html.twig', |
4094ea47 | 273 | [ |
8ce32af6 JB |
274 | 'form' => $form->createView(), |
275 | 'entries' => $entries, | |
276 | 'currentPage' => $page, | |
4094ea47 | 277 | ] |
bd9f0815 | 278 | ); |
bd9f0815 NL |
279 | } |
280 | ||
281 | /** | |
4346a860 | 282 | * Shows entry content. |
163eae0b | 283 | * |
3d2b2d62 J |
284 | * @param Entry $entry |
285 | * | |
bd9f0815 | 286 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") |
3d2b2d62 | 287 | * |
163eae0b | 288 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 | 289 | */ |
be463487 | 290 | public function viewAction(Entry $entry) |
bd9f0815 | 291 | { |
3d2b2d62 J |
292 | $this->checkUserAction($entry); |
293 | ||
f3d0cb91 NL |
294 | $this->generateEntryUuid($entry); |
295 | ||
bd9f0815 | 296 | return $this->render( |
ad4d1caa | 297 | 'WallabagCoreBundle:Entry:entry.html.twig', |
4094ea47 | 298 | ['entry' => $entry] |
bd9f0815 | 299 | ); |
163eae0b NL |
300 | } |
301 | ||
831b02aa JB |
302 | /** |
303 | * Reload an entry. | |
304 | * Refetch content from the website and make it readable again. | |
305 | * | |
306 | * @param Entry $entry | |
307 | * | |
308 | * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry") | |
309 | * | |
310 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
311 | */ | |
312 | public function reloadAction(Entry $entry) | |
313 | { | |
314 | $this->checkUserAction($entry); | |
315 | ||
4204a06b | 316 | $message = 'flashes.entry.notice.entry_reloaded'; |
831b02aa | 317 | if (false === $this->updateEntry($entry)) { |
4204a06b | 318 | $message = 'flashes.entry.notice.entry_reload_failed'; |
831b02aa JB |
319 | } |
320 | ||
321 | $this->get('session')->getFlashBag()->add( | |
322 | 'notice', | |
323 | $message | |
324 | ); | |
325 | ||
4094ea47 | 326 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); |
831b02aa JB |
327 | } |
328 | ||
163eae0b | 329 | /** |
4346a860 | 330 | * Changes read status for an entry. |
163eae0b | 331 | * |
3d2b2d62 J |
332 | * @param Request $request |
333 | * @param Entry $entry | |
334 | * | |
163eae0b | 335 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") |
3d2b2d62 | 336 | * |
163eae0b NL |
337 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
338 | */ | |
be463487 | 339 | public function toggleArchiveAction(Request $request, Entry $entry) |
163eae0b | 340 | { |
3d2b2d62 J |
341 | $this->checkUserAction($entry); |
342 | ||
163eae0b NL |
343 | $entry->toggleArchive(); |
344 | $this->getDoctrine()->getManager()->flush(); | |
345 | ||
4204a06b JB |
346 | $message = 'flashes.entry.notice.entry_unarchived'; |
347 | if ($entry->isArchived()) { | |
348 | $message = 'flashes.entry.notice.entry_archived'; | |
349 | } | |
350 | ||
163eae0b NL |
351 | $this->get('session')->getFlashBag()->add( |
352 | 'notice', | |
4204a06b | 353 | $message |
163eae0b NL |
354 | ); |
355 | ||
af497a64 NL |
356 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); |
357 | ||
358 | return $this->redirect($redirectUrl); | |
163eae0b NL |
359 | } |
360 | ||
361 | /** | |
eecd7e40 | 362 | * Changes starred status for an entry. |
163eae0b | 363 | * |
3d2b2d62 J |
364 | * @param Request $request |
365 | * @param Entry $entry | |
366 | * | |
163eae0b | 367 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") |
3d2b2d62 | 368 | * |
163eae0b NL |
369 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
370 | */ | |
be463487 | 371 | public function toggleStarAction(Request $request, Entry $entry) |
163eae0b | 372 | { |
3d2b2d62 J |
373 | $this->checkUserAction($entry); |
374 | ||
163eae0b NL |
375 | $entry->toggleStar(); |
376 | $this->getDoctrine()->getManager()->flush(); | |
377 | ||
4204a06b JB |
378 | $message = 'flashes.entry.notice.entry_unstarred'; |
379 | if ($entry->isStarred()) { | |
380 | $message = 'flashes.entry.notice.entry_starred'; | |
381 | } | |
382 | ||
163eae0b NL |
383 | $this->get('session')->getFlashBag()->add( |
384 | 'notice', | |
4204a06b | 385 | $message |
163eae0b NL |
386 | ); |
387 | ||
af497a64 NL |
388 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); |
389 | ||
390 | return $this->redirect($redirectUrl); | |
163eae0b NL |
391 | } |
392 | ||
393 | /** | |
2863bf2a | 394 | * Deletes entry and redirect to the homepage or the last viewed page. |
163eae0b | 395 | * |
16a3d04c | 396 | * @param Entry $entry |
3d2b2d62 | 397 | * |
163eae0b | 398 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") |
3d2b2d62 | 399 | * |
163eae0b NL |
400 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
401 | */ | |
18d5f454 | 402 | public function deleteEntryAction(Request $request, Entry $entry) |
163eae0b | 403 | { |
3d2b2d62 J |
404 | $this->checkUserAction($entry); |
405 | ||
2863bf2a JB |
406 | // generates the view url for this entry to check for redirection later |
407 | // to avoid redirecting to the deleted entry. Ugh. | |
408 | $url = $this->generateUrl( | |
409 | 'view', | |
4094ea47 | 410 | ['id' => $entry->getId()], |
ce0e9ec3 | 411 | UrlGeneratorInterface::ABSOLUTE_PATH |
2863bf2a JB |
412 | ); |
413 | ||
1d147791 NL |
414 | $em = $this->getDoctrine()->getManager(); |
415 | $em->remove($entry); | |
416 | $em->flush(); | |
163eae0b NL |
417 | |
418 | $this->get('session')->getFlashBag()->add( | |
419 | 'notice', | |
4204a06b | 420 | 'flashes.entry.notice.entry_deleted' |
163eae0b | 421 | ); |
bd9f0815 | 422 | |
ce0e9ec3 JB |
423 | // don't redirect user to the deleted entry (check that the referer doesn't end with the same url) |
424 | $referer = $request->headers->get('referer'); | |
425 | $to = (1 !== preg_match('#'.$url.'$#i', $referer) ? $referer : null); | |
af497a64 NL |
426 | |
427 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to); | |
428 | ||
429 | return $this->redirect($redirectUrl); | |
bd9f0815 | 430 | } |
3d2b2d62 J |
431 | |
432 | /** | |
4346a860 | 433 | * Check if the logged user can manage the given entry. |
3d2b2d62 J |
434 | * |
435 | * @param Entry $entry | |
436 | */ | |
437 | private function checkUserAction(Entry $entry) | |
438 | { | |
439 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | |
82d6d9cb | 440 | throw $this->createAccessDeniedException('You can not access this entry.'); |
3d2b2d62 J |
441 | } |
442 | } | |
b00a89e0 NL |
443 | |
444 | /** | |
445 | * Check for existing entry, if it exists, redirect to it with a message. | |
446 | * | |
4094ea47 | 447 | * @param Entry $entry |
b00a89e0 | 448 | * |
4094ea47 | 449 | * @return Entry|bool |
b00a89e0 | 450 | */ |
4094ea47 | 451 | private function checkIfEntryAlreadyExists(Entry $entry) |
b00a89e0 NL |
452 | { |
453 | return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId()); | |
f3d0cb91 NL |
454 | } |
455 | ||
d0545b6b | 456 | /** |
f3d0cb91 NL |
457 | * Share entry content. |
458 | * | |
459 | * @param Entry $entry | |
460 | * | |
461 | * @Route("/share/{uuid}", requirements={"uuid" = ".+"}, name="share") | |
462 | * | |
463 | * @return \Symfony\Component\HttpFoundation\Response | |
464 | */ | |
d0545b6b | 465 | public function shareEntryAction(Entry $entry) |
f3d0cb91 NL |
466 | { |
467 | return $this->render( | |
468 | '@WallabagCore/themes/share.html.twig', | |
469 | array('entry' => $entry) | |
470 | ); | |
471 | } | |
472 | ||
473 | /** | |
474 | * @param Entry $entry | |
475 | */ | |
476 | private function generateEntryUuid(Entry $entry) | |
477 | { | |
478 | $entry->generateUuid(); | |
479 | $em = $this->getDoctrine()->getManager(); | |
480 | $em->persist($entry); | |
481 | $em->flush(); | |
b00a89e0 | 482 | } |
9d50517c | 483 | } |