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