]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/EntryController.php
Change share entry behavior
[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) {
229 case 'starred':
230 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
231 break;
232
233 case 'archive':
234 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
235 break;
236
237 case 'unread':
238 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
239 break;
240
2b7a4889
NL
241 case 'all':
242 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
243 break;
244
0ab7404f
JB
245 default:
246 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
247 }
248
1d76102a 249 $form = $this->createForm(EntryFilterType::class);
9fb6ac83 250
26864574
NL
251 if ($request->query->has($form->getName())) {
252 // manually bind values from the request
253 $form->submit($request->query->get($form->getName()));
254
255 // build the query from the given form object
0ab7404f 256 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
26864574
NL
257 }
258
0ab7404f 259 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
26864574 260
891456ba
NL
261 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')
262 ->prepare($pagerAdapter, $page);
263
671a2b88
ML
264 try {
265 $entries->setCurrentPage($page);
266 } catch (OutOfRangeCurrentPageException $e) {
267 if ($page > 1) {
4094ea47 268 return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302);
671a2b88
ML
269 }
270 }
bd9f0815
NL
271
272 return $this->render(
ad4d1caa 273 'WallabagCoreBundle:Entry:entries.html.twig',
4094ea47 274 [
8ce32af6
JB
275 'form' => $form->createView(),
276 'entries' => $entries,
277 'currentPage' => $page,
4094ea47 278 ]
bd9f0815 279 );
bd9f0815
NL
280 }
281
282 /**
4346a860 283 * Shows entry content.
163eae0b 284 *
3d2b2d62
J
285 * @param Entry $entry
286 *
bd9f0815 287 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
3d2b2d62 288 *
163eae0b 289 * @return \Symfony\Component\HttpFoundation\Response
bd9f0815 290 */
be463487 291 public function viewAction(Entry $entry)
bd9f0815 292 {
3d2b2d62
J
293 $this->checkUserAction($entry);
294
bd9f0815 295 return $this->render(
ad4d1caa 296 'WallabagCoreBundle:Entry:entry.html.twig',
4094ea47 297 ['entry' => $entry]
bd9f0815 298 );
163eae0b
NL
299 }
300
831b02aa
JB
301 /**
302 * Reload an entry.
303 * Refetch content from the website and make it readable again.
304 *
305 * @param Entry $entry
306 *
307 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
308 *
309 * @return \Symfony\Component\HttpFoundation\RedirectResponse
310 */
311 public function reloadAction(Entry $entry)
312 {
313 $this->checkUserAction($entry);
314
4204a06b 315 $message = 'flashes.entry.notice.entry_reloaded';
831b02aa 316 if (false === $this->updateEntry($entry)) {
4204a06b 317 $message = 'flashes.entry.notice.entry_reload_failed';
831b02aa
JB
318 }
319
320 $this->get('session')->getFlashBag()->add(
321 'notice',
322 $message
323 );
324
4094ea47 325 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
831b02aa
JB
326 }
327
163eae0b 328 /**
4346a860 329 * Changes read status for an entry.
163eae0b 330 *
3d2b2d62
J
331 * @param Request $request
332 * @param Entry $entry
333 *
163eae0b 334 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
3d2b2d62 335 *
163eae0b
NL
336 * @return \Symfony\Component\HttpFoundation\RedirectResponse
337 */
be463487 338 public function toggleArchiveAction(Request $request, Entry $entry)
163eae0b 339 {
3d2b2d62
J
340 $this->checkUserAction($entry);
341
163eae0b
NL
342 $entry->toggleArchive();
343 $this->getDoctrine()->getManager()->flush();
344
4204a06b
JB
345 $message = 'flashes.entry.notice.entry_unarchived';
346 if ($entry->isArchived()) {
347 $message = 'flashes.entry.notice.entry_archived';
348 }
349
163eae0b
NL
350 $this->get('session')->getFlashBag()->add(
351 'notice',
4204a06b 352 $message
163eae0b
NL
353 );
354
af497a64
NL
355 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
356
357 return $this->redirect($redirectUrl);
163eae0b
NL
358 }
359
360 /**
eecd7e40 361 * Changes starred status for an entry.
163eae0b 362 *
3d2b2d62
J
363 * @param Request $request
364 * @param Entry $entry
365 *
163eae0b 366 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
3d2b2d62 367 *
163eae0b
NL
368 * @return \Symfony\Component\HttpFoundation\RedirectResponse
369 */
be463487 370 public function toggleStarAction(Request $request, Entry $entry)
163eae0b 371 {
3d2b2d62
J
372 $this->checkUserAction($entry);
373
163eae0b
NL
374 $entry->toggleStar();
375 $this->getDoctrine()->getManager()->flush();
376
4204a06b
JB
377 $message = 'flashes.entry.notice.entry_unstarred';
378 if ($entry->isStarred()) {
379 $message = 'flashes.entry.notice.entry_starred';
380 }
381
163eae0b
NL
382 $this->get('session')->getFlashBag()->add(
383 'notice',
4204a06b 384 $message
163eae0b
NL
385 );
386
af497a64
NL
387 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
388
389 return $this->redirect($redirectUrl);
163eae0b
NL
390 }
391
392 /**
2863bf2a 393 * Deletes entry and redirect to the homepage or the last viewed page.
163eae0b 394 *
16a3d04c 395 * @param Entry $entry
3d2b2d62 396 *
163eae0b 397 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
3d2b2d62 398 *
163eae0b
NL
399 * @return \Symfony\Component\HttpFoundation\RedirectResponse
400 */
18d5f454 401 public function deleteEntryAction(Request $request, Entry $entry)
163eae0b 402 {
3d2b2d62
J
403 $this->checkUserAction($entry);
404
2863bf2a
JB
405 // generates the view url for this entry to check for redirection later
406 // to avoid redirecting to the deleted entry. Ugh.
407 $url = $this->generateUrl(
408 'view',
4094ea47 409 ['id' => $entry->getId()],
ce0e9ec3 410 UrlGeneratorInterface::ABSOLUTE_PATH
2863bf2a
JB
411 );
412
1d147791
NL
413 $em = $this->getDoctrine()->getManager();
414 $em->remove($entry);
415 $em->flush();
163eae0b
NL
416
417 $this->get('session')->getFlashBag()->add(
418 'notice',
4204a06b 419 'flashes.entry.notice.entry_deleted'
163eae0b 420 );
bd9f0815 421
ce0e9ec3
JB
422 // don't redirect user to the deleted entry (check that the referer doesn't end with the same url)
423 $referer = $request->headers->get('referer');
424 $to = (1 !== preg_match('#'.$url.'$#i', $referer) ? $referer : null);
af497a64
NL
425
426 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to);
427
428 return $this->redirect($redirectUrl);
bd9f0815 429 }
3d2b2d62
J
430
431 /**
4346a860 432 * Check if the logged user can manage the given entry.
3d2b2d62
J
433 *
434 * @param Entry $entry
435 */
436 private function checkUserAction(Entry $entry)
437 {
f1be7af4 438 if (null === $this->getUser() || $this->getUser()->getId() != $entry->getUser()->getId()) {
82d6d9cb 439 throw $this->createAccessDeniedException('You can not access this entry.');
3d2b2d62
J
440 }
441 }
b00a89e0
NL
442
443 /**
444 * Check for existing entry, if it exists, redirect to it with a message.
445 *
4094ea47 446 * @param Entry $entry
b00a89e0 447 *
4094ea47 448 * @return Entry|bool
b00a89e0 449 */
4094ea47 450 private function checkIfEntryAlreadyExists(Entry $entry)
b00a89e0
NL
451 {
452 return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
f3d0cb91
NL
453 }
454
f1be7af4
NL
455 /**
456 * Get public URL for entry (and generate it if necessary).
457 *
458 * @param Entry $entry
459 *
460 * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share")
461 *
462 * @return \Symfony\Component\HttpFoundation\Response
463 */
464 public function shareAction(Entry $entry)
465 {
466 $this->checkUserAction($entry);
467
468 if ('' === $entry->getUuid() || null === $entry->getUuid()) {
469 $this->generateEntryUuid($entry);
470 }
471
472 return $this->redirect($this->generateUrl('share_entry', [
473 'uuid' => $entry->getUuid(),
474 ]));
475 }
476
477 /**
478 * Disable public sharing for an entry.
479 *
480 * @param Entry $entry
481 *
482 * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share")
483 *
484 * @return \Symfony\Component\HttpFoundation\Response
485 */
486 public function deleteShareAction(Entry $entry)
487 {
488 $this->checkUserAction($entry);
489
490 $entry->cleanUuid();
491 $em = $this->getDoctrine()->getManager();
492 $em->persist($entry);
493 $em->flush();
494
495 return $this->redirect($this->generateUrl('view', [
496 'id' => $entry->getId(),
497 ]));
498 }
499
d0545b6b 500 /**
f3d0cb91
NL
501 * Share entry content.
502 *
503 * @param Entry $entry
504 *
f1be7af4 505 * @Route("/share/{uuid}", requirements={"uuid" = ".+"}, name="share_entry")
222e09f1 506 * @Cache(maxage="25200", public=true)
f3d0cb91
NL
507 *
508 * @return \Symfony\Component\HttpFoundation\Response
509 */
d0545b6b 510 public function shareEntryAction(Entry $entry)
f3d0cb91
NL
511 {
512 return $this->render(
513 '@WallabagCore/themes/share.html.twig',
514 array('entry' => $entry)
515 );
516 }
517
518 /**
519 * @param Entry $entry
520 */
521 private function generateEntryUuid(Entry $entry)
522 {
523 $entry->generateUuid();
524 $em = $this->getDoctrine()->getManager();
525 $em->persist($entry);
526 $em->flush();
b00a89e0 527 }
9d50517c 528}