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