]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/EntryController.php
Merge pull request #2500 from wallabag/add-check-composer
[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 // if refreshing entry failed, don't save it
334 if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) {
335 $bag = $this->get('session')->getFlashBag();
336 $bag->clear();
337 $bag->add('notice', 'flashes.entry.notice.entry_reloaded_failed');
338
339 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
340 }
341
342 $em = $this->getDoctrine()->getManager();
343 $em->persist($entry);
344 $em->flush();
345
346 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
347 }
348
349 /**
350 * Changes read status for an entry.
351 *
352 * @param Request $request
353 * @param Entry $entry
354 *
355 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
356 *
357 * @return \Symfony\Component\HttpFoundation\RedirectResponse
358 */
359 public function toggleArchiveAction(Request $request, Entry $entry)
360 {
361 $this->checkUserAction($entry);
362
363 $entry->toggleArchive();
364 $this->getDoctrine()->getManager()->flush();
365
366 $message = 'flashes.entry.notice.entry_unarchived';
367 if ($entry->isArchived()) {
368 $message = 'flashes.entry.notice.entry_archived';
369 }
370
371 $this->get('session')->getFlashBag()->add(
372 'notice',
373 $message
374 );
375
376 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
377
378 return $this->redirect($redirectUrl);
379 }
380
381 /**
382 * Changes starred status for an entry.
383 *
384 * @param Request $request
385 * @param Entry $entry
386 *
387 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
388 *
389 * @return \Symfony\Component\HttpFoundation\RedirectResponse
390 */
391 public function toggleStarAction(Request $request, Entry $entry)
392 {
393 $this->checkUserAction($entry);
394
395 $entry->toggleStar();
396 $this->getDoctrine()->getManager()->flush();
397
398 $message = 'flashes.entry.notice.entry_unstarred';
399 if ($entry->isStarred()) {
400 $message = 'flashes.entry.notice.entry_starred';
401 }
402
403 $this->get('session')->getFlashBag()->add(
404 'notice',
405 $message
406 );
407
408 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
409
410 return $this->redirect($redirectUrl);
411 }
412
413 /**
414 * Deletes entry and redirect to the homepage or the last viewed page.
415 *
416 * @param Entry $entry
417 *
418 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
419 *
420 * @return \Symfony\Component\HttpFoundation\RedirectResponse
421 */
422 public function deleteEntryAction(Request $request, Entry $entry)
423 {
424 $this->checkUserAction($entry);
425
426 // generates the view url for this entry to check for redirection later
427 // to avoid redirecting to the deleted entry. Ugh.
428 $url = $this->generateUrl(
429 'view',
430 ['id' => $entry->getId()],
431 UrlGeneratorInterface::ABSOLUTE_PATH
432 );
433
434 $em = $this->getDoctrine()->getManager();
435 $em->remove($entry);
436 $em->flush();
437
438 $this->get('session')->getFlashBag()->add(
439 'notice',
440 'flashes.entry.notice.entry_deleted'
441 );
442
443 // don't redirect user to the deleted entry (check that the referer doesn't end with the same url)
444 $referer = $request->headers->get('referer');
445 $to = (1 !== preg_match('#'.$url.'$#i', $referer) ? $referer : null);
446
447 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to);
448
449 return $this->redirect($redirectUrl);
450 }
451
452 /**
453 * Check if the logged user can manage the given entry.
454 *
455 * @param Entry $entry
456 */
457 private function checkUserAction(Entry $entry)
458 {
459 if (null === $this->getUser() || $this->getUser()->getId() != $entry->getUser()->getId()) {
460 throw $this->createAccessDeniedException('You can not access this entry.');
461 }
462 }
463
464 /**
465 * Check for existing entry, if it exists, redirect to it with a message.
466 *
467 * @param Entry $entry
468 *
469 * @return Entry|bool
470 */
471 private function checkIfEntryAlreadyExists(Entry $entry)
472 {
473 return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
474 }
475
476 /**
477 * Get public URL for entry (and generate it if necessary).
478 *
479 * @param Entry $entry
480 *
481 * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share")
482 *
483 * @return \Symfony\Component\HttpFoundation\Response
484 */
485 public function shareAction(Entry $entry)
486 {
487 $this->checkUserAction($entry);
488
489 if (null === $entry->getUuid()) {
490 $entry->generateUuid();
491
492 $em = $this->getDoctrine()->getManager();
493 $em->persist($entry);
494 $em->flush();
495 }
496
497 return $this->redirect($this->generateUrl('share_entry', [
498 'uuid' => $entry->getUuid(),
499 ]));
500 }
501
502 /**
503 * Disable public sharing for an entry.
504 *
505 * @param Entry $entry
506 *
507 * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share")
508 *
509 * @return \Symfony\Component\HttpFoundation\Response
510 */
511 public function deleteShareAction(Entry $entry)
512 {
513 $this->checkUserAction($entry);
514
515 $entry->cleanUuid();
516
517 $em = $this->getDoctrine()->getManager();
518 $em->persist($entry);
519 $em->flush();
520
521 return $this->redirect($this->generateUrl('view', [
522 'id' => $entry->getId(),
523 ]));
524 }
525
526 /**
527 * Ability to view a content publicly.
528 *
529 * @param Entry $entry
530 *
531 * @Route("/share/{uuid}", requirements={"uuid" = ".+"}, name="share_entry")
532 * @Cache(maxage="25200", smaxage="25200", public=true)
533 *
534 * @return \Symfony\Component\HttpFoundation\Response
535 */
536 public function shareEntryAction(Entry $entry)
537 {
538 if (!$this->get('craue_config')->get('share_public')) {
539 throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.');
540 }
541
542 return $this->render(
543 '@WallabagCore/themes/common/Entry/share.html.twig',
544 ['entry' => $entry]
545 );
546 }
547
548 /**
549 * Shows untagged articles for current user.
550 *
551 * @param Request $request
552 * @param int $page
553 *
554 * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"})
555 *
556 * @return \Symfony\Component\HttpFoundation\Response
557 */
558 public function showUntaggedEntriesAction(Request $request, $page)
559 {
560 return $this->showEntries('untagged', $request, $page);
561 }
562 }