]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/EntryController.php
Merge pull request #2239 from wallabag/documentation-2.1
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / EntryController.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Controller;
4
5 use Pagerfanta\Adapter\DoctrineORMAdapter;
6 use Pagerfanta\Exception\OutOfRangeCurrentPageException;
7 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9 use Symfony\Component\HttpFoundation\Request;
10 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
11 use Wallabag\CoreBundle\Entity\Entry;
12 use Wallabag\CoreBundle\Form\Type\EntryFilterType;
13 use Wallabag\CoreBundle\Form\Type\EditEntryType;
14 use Wallabag\CoreBundle\Form\Type\NewEntryType;
15 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
16
17 class EntryController extends Controller
18 {
19 /**
20 * Fetch content and update entry.
21 * In case it fails, entry will return to avod loosing the data.
22 *
23 * @param Entry $entry
24 * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded
25 *
26 * @return Entry
27 */
28 private function updateEntry(Entry $entry, $prefixMessage = 'entry_saved')
29 {
30 // put default title in case of fetching content failed
31 $entry->setTitle('No title found');
32
33 $message = 'flashes.entry.notice.'.$prefixMessage;
34
35 try {
36 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
37 } catch (\Exception $e) {
38 $this->get('logger')->error('Error while saving an entry', [
39 'exception' => $e,
40 'entry' => $entry,
41 ]);
42
43 $message = 'flashes.entry.notice.'.$prefixMessage.'_failed';
44 }
45
46 $this->get('session')->getFlashBag()->add('notice', $message);
47
48 return $entry;
49 }
50
51 /**
52 * @param Request $request
53 *
54 * @Route("/new-entry", name="new_entry")
55 *
56 * @return \Symfony\Component\HttpFoundation\Response
57 */
58 public function addEntryFormAction(Request $request)
59 {
60 $entry = new Entry($this->getUser());
61
62 $form = $this->createForm(NewEntryType::class, $entry);
63
64 $form->handleRequest($request);
65
66 if ($form->isValid()) {
67 $existingEntry = $this->checkIfEntryAlreadyExists($entry);
68
69 if (false !== $existingEntry) {
70 $this->get('session')->getFlashBag()->add(
71 'notice',
72 $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')])
73 );
74
75 return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()]));
76 }
77
78 $this->updateEntry($entry);
79
80 $em = $this->getDoctrine()->getManager();
81 $em->persist($entry);
82 $em->flush();
83
84 return $this->redirect($this->generateUrl('homepage'));
85 }
86
87 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', [
88 'form' => $form->createView(),
89 ]);
90 }
91
92 /**
93 * @param Request $request
94 *
95 * @Route("/bookmarklet", name="bookmarklet")
96 *
97 * @return \Symfony\Component\HttpFoundation\Response
98 */
99 public function addEntryViaBookmarkletAction(Request $request)
100 {
101 $entry = new Entry($this->getUser());
102 $entry->setUrl($request->get('url'));
103
104 if (false === $this->checkIfEntryAlreadyExists($entry)) {
105 $this->updateEntry($entry);
106
107 $em = $this->getDoctrine()->getManager();
108 $em->persist($entry);
109 $em->flush();
110 }
111
112 return $this->redirect($this->generateUrl('homepage'));
113 }
114
115 /**
116 * @Route("/new", name="new")
117 *
118 * @return \Symfony\Component\HttpFoundation\Response
119 */
120 public function addEntryAction()
121 {
122 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
123 }
124
125 /**
126 * Edit an entry content.
127 *
128 * @param Request $request
129 * @param Entry $entry
130 *
131 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
132 *
133 * @return \Symfony\Component\HttpFoundation\Response
134 */
135 public function editEntryAction(Request $request, Entry $entry)
136 {
137 $this->checkUserAction($entry);
138
139 $form = $this->createForm(EditEntryType::class, $entry);
140
141 $form->handleRequest($request);
142
143 if ($form->isValid()) {
144 $em = $this->getDoctrine()->getManager();
145 $em->persist($entry);
146 $em->flush();
147
148 $this->get('session')->getFlashBag()->add(
149 'notice',
150 'flashes.entry.notice.entry_updated'
151 );
152
153 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
154 }
155
156 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', [
157 'form' => $form->createView(),
158 ]);
159 }
160
161 /**
162 * Shows all entries for current user.
163 *
164 * @param Request $request
165 * @param int $page
166 *
167 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
168 *
169 * @return \Symfony\Component\HttpFoundation\Response
170 */
171 public function showAllAction(Request $request, $page)
172 {
173 return $this->showEntries('all', $request, $page);
174 }
175
176 /**
177 * Shows unread entries for current user.
178 *
179 * @param Request $request
180 * @param int $page
181 *
182 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
183 *
184 * @return \Symfony\Component\HttpFoundation\Response
185 */
186 public function showUnreadAction(Request $request, $page)
187 {
188 // load the quickstart if no entry in database
189 if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUsername($this->getUser()->getId()) == 0) {
190 return $this->redirect($this->generateUrl('quickstart'));
191 }
192
193 return $this->showEntries('unread', $request, $page);
194 }
195
196 /**
197 * Shows read entries for current user.
198 *
199 * @param Request $request
200 * @param int $page
201 *
202 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
203 *
204 * @return \Symfony\Component\HttpFoundation\Response
205 */
206 public function showArchiveAction(Request $request, $page)
207 {
208 return $this->showEntries('archive', $request, $page);
209 }
210
211 /**
212 * Shows starred entries for current user.
213 *
214 * @param Request $request
215 * @param int $page
216 *
217 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
218 *
219 * @return \Symfony\Component\HttpFoundation\Response
220 */
221 public function showStarredAction(Request $request, $page)
222 {
223 return $this->showEntries('starred', $request, $page);
224 }
225
226 /**
227 * Global method to retrieve entries depending on the given type
228 * It returns the response to be send.
229 *
230 * @param string $type Entries type: unread, starred or archive
231 * @param Request $request
232 * @param int $page
233 *
234 * @return \Symfony\Component\HttpFoundation\Response
235 */
236 private function showEntries($type, Request $request, $page)
237 {
238 $repository = $this->get('wallabag_core.entry_repository');
239
240 switch ($type) {
241 case 'untagged':
242 $qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId());
243
244 break;
245 case 'starred':
246 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
247 break;
248
249 case 'archive':
250 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
251 break;
252
253 case 'unread':
254 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
255 break;
256
257 case 'all':
258 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
259 break;
260
261 default:
262 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
263 }
264
265 $form = $this->createForm(EntryFilterType::class);
266
267 if ($request->query->has($form->getName())) {
268 // manually bind values from the request
269 $form->submit($request->query->get($form->getName()));
270
271 // build the query from the given form object
272 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
273 }
274
275 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
276
277 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')
278 ->prepare($pagerAdapter, $page);
279
280 try {
281 $entries->setCurrentPage($page);
282 } catch (OutOfRangeCurrentPageException $e) {
283 if ($page > 1) {
284 return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302);
285 }
286 }
287
288 return $this->render(
289 'WallabagCoreBundle:Entry:entries.html.twig',
290 [
291 'form' => $form->createView(),
292 'entries' => $entries,
293 'currentPage' => $page,
294 ]
295 );
296 }
297
298 /**
299 * Shows entry content.
300 *
301 * @param Entry $entry
302 *
303 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
304 *
305 * @return \Symfony\Component\HttpFoundation\Response
306 */
307 public function viewAction(Entry $entry)
308 {
309 $this->checkUserAction($entry);
310
311 return $this->render(
312 'WallabagCoreBundle:Entry:entry.html.twig',
313 ['entry' => $entry]
314 );
315 }
316
317 /**
318 * Reload an entry.
319 * Refetch content from the website and make it readable again.
320 *
321 * @param Entry $entry
322 *
323 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
324 *
325 * @return \Symfony\Component\HttpFoundation\RedirectResponse
326 */
327 public function reloadAction(Entry $entry)
328 {
329 $this->checkUserAction($entry);
330
331 $this->updateEntry($entry, 'entry_reloaded');
332
333 $em = $this->getDoctrine()->getManager();
334 $em->persist($entry);
335 $em->flush();
336
337 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
338 }
339
340 /**
341 * Changes read status for an entry.
342 *
343 * @param Request $request
344 * @param Entry $entry
345 *
346 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
347 *
348 * @return \Symfony\Component\HttpFoundation\RedirectResponse
349 */
350 public function toggleArchiveAction(Request $request, Entry $entry)
351 {
352 $this->checkUserAction($entry);
353
354 $entry->toggleArchive();
355 $this->getDoctrine()->getManager()->flush();
356
357 $message = 'flashes.entry.notice.entry_unarchived';
358 if ($entry->isArchived()) {
359 $message = 'flashes.entry.notice.entry_archived';
360 }
361
362 $this->get('session')->getFlashBag()->add(
363 'notice',
364 $message
365 );
366
367 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
368
369 return $this->redirect($redirectUrl);
370 }
371
372 /**
373 * Changes starred status for an entry.
374 *
375 * @param Request $request
376 * @param Entry $entry
377 *
378 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
379 *
380 * @return \Symfony\Component\HttpFoundation\RedirectResponse
381 */
382 public function toggleStarAction(Request $request, Entry $entry)
383 {
384 $this->checkUserAction($entry);
385
386 $entry->toggleStar();
387 $this->getDoctrine()->getManager()->flush();
388
389 $message = 'flashes.entry.notice.entry_unstarred';
390 if ($entry->isStarred()) {
391 $message = 'flashes.entry.notice.entry_starred';
392 }
393
394 $this->get('session')->getFlashBag()->add(
395 'notice',
396 $message
397 );
398
399 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
400
401 return $this->redirect($redirectUrl);
402 }
403
404 /**
405 * Deletes entry and redirect to the homepage or the last viewed page.
406 *
407 * @param Entry $entry
408 *
409 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
410 *
411 * @return \Symfony\Component\HttpFoundation\RedirectResponse
412 */
413 public function deleteEntryAction(Request $request, Entry $entry)
414 {
415 $this->checkUserAction($entry);
416
417 // generates the view url for this entry to check for redirection later
418 // to avoid redirecting to the deleted entry. Ugh.
419 $url = $this->generateUrl(
420 'view',
421 ['id' => $entry->getId()],
422 UrlGeneratorInterface::ABSOLUTE_PATH
423 );
424
425 $em = $this->getDoctrine()->getManager();
426 $em->remove($entry);
427 $em->flush();
428
429 $this->get('session')->getFlashBag()->add(
430 'notice',
431 'flashes.entry.notice.entry_deleted'
432 );
433
434 // don't redirect user to the deleted entry (check that the referer doesn't end with the same url)
435 $referer = $request->headers->get('referer');
436 $to = (1 !== preg_match('#'.$url.'$#i', $referer) ? $referer : null);
437
438 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to);
439
440 return $this->redirect($redirectUrl);
441 }
442
443 /**
444 * Check if the logged user can manage the given entry.
445 *
446 * @param Entry $entry
447 */
448 private function checkUserAction(Entry $entry)
449 {
450 if (null === $this->getUser() || $this->getUser()->getId() != $entry->getUser()->getId()) {
451 throw $this->createAccessDeniedException('You can not access this entry.');
452 }
453 }
454
455 /**
456 * Check for existing entry, if it exists, redirect to it with a message.
457 *
458 * @param Entry $entry
459 *
460 * @return Entry|bool
461 */
462 private function checkIfEntryAlreadyExists(Entry $entry)
463 {
464 return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
465 }
466
467 /**
468 * Get public URL for entry (and generate it if necessary).
469 *
470 * @param Entry $entry
471 *
472 * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share")
473 *
474 * @return \Symfony\Component\HttpFoundation\Response
475 */
476 public function shareAction(Entry $entry)
477 {
478 $this->checkUserAction($entry);
479
480 if (null === $entry->getUuid()) {
481 $entry->generateUuid();
482
483 $em = $this->getDoctrine()->getManager();
484 $em->persist($entry);
485 $em->flush();
486 }
487
488 return $this->redirect($this->generateUrl('share_entry', [
489 'uuid' => $entry->getUuid(),
490 ]));
491 }
492
493 /**
494 * Disable public sharing for an entry.
495 *
496 * @param Entry $entry
497 *
498 * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share")
499 *
500 * @return \Symfony\Component\HttpFoundation\Response
501 */
502 public function deleteShareAction(Entry $entry)
503 {
504 $this->checkUserAction($entry);
505
506 $entry->cleanUuid();
507
508 $em = $this->getDoctrine()->getManager();
509 $em->persist($entry);
510 $em->flush();
511
512 return $this->redirect($this->generateUrl('view', [
513 'id' => $entry->getId(),
514 ]));
515 }
516
517 /**
518 * Ability to view a content publicly.
519 *
520 * @param Entry $entry
521 *
522 * @Route("/share/{uuid}", requirements={"uuid" = ".+"}, name="share_entry")
523 * @Cache(maxage="25200", smaxage="25200", public=true)
524 *
525 * @return \Symfony\Component\HttpFoundation\Response
526 */
527 public function shareEntryAction(Entry $entry)
528 {
529 if (!$this->get('craue_config')->get('share_public')) {
530 throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.');
531 }
532
533 return $this->render(
534 '@WallabagCore/themes/share.html.twig',
535 ['entry' => $entry]
536 );
537 }
538
539 /**
540 * Shows untagged articles for current user.
541 *
542 * @param Request $request
543 * @param int $page
544 *
545 * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"})
546 *
547 * @return \Symfony\Component\HttpFoundation\Response
548 */
549 public function showUntaggedEntriesAction(Request $request, $page)
550 {
551 return $this->showEntries('untagged', $request, $page);
552 }
553 }