]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/EntryController.php
manage assets through npm
[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;
619cc453 7use Pagerfanta\Pagerfanta;
9d50517c
NL
8use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9use Symfony\Bundle\FrameworkBundle\Controller\Controller;
163eae0b 10use Symfony\Component\HttpFoundation\Request;
2863bf2a 11use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
be463487 12use Wallabag\CoreBundle\Entity\Entry;
b0b893ea 13use Wallabag\CoreBundle\Form\Type\EntryFilterType;
619cc453
JB
14use Wallabag\CoreBundle\Form\Type\EditEntryType;
15use Wallabag\CoreBundle\Form\Type\NewEntryType;
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());
5ecdfcd0 26
880a0e1c
NL
27 $em = $this->getDoctrine()->getManager();
28 $em->persist($entry);
29 $em->flush();
30 } catch (\Exception $e) {
5ecdfcd0
TC
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
5ecdfcd0
TC
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
NL
260 $entries = new Pagerfanta($pagerAdapter);
261
48ffc5a4 262 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
671a2b88
ML
263 try {
264 $entries->setCurrentPage($page);
265 } catch (OutOfRangeCurrentPageException $e) {
266 if ($page > 1) {
4094ea47 267 return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302);
671a2b88
ML
268 }
269 }
bd9f0815
NL
270
271 return $this->render(
ad4d1caa 272 'WallabagCoreBundle:Entry:entries.html.twig',
4094ea47 273 [
8ce32af6
JB
274 'form' => $form->createView(),
275 'entries' => $entries,
276 'currentPage' => $page,
4094ea47 277 ]
bd9f0815 278 );
bd9f0815
NL
279 }
280
281 /**
4346a860 282 * Shows entry content.
163eae0b 283 *
3d2b2d62
J
284 * @param Entry $entry
285 *
bd9f0815 286 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
3d2b2d62 287 *
163eae0b 288 * @return \Symfony\Component\HttpFoundation\Response
bd9f0815 289 */
be463487 290 public function viewAction(Entry $entry)
bd9f0815 291 {
3d2b2d62
J
292 $this->checkUserAction($entry);
293
bd9f0815 294 return $this->render(
ad4d1caa 295 'WallabagCoreBundle:Entry:entry.html.twig',
4094ea47 296 ['entry' => $entry]
bd9f0815 297 );
163eae0b
NL
298 }
299
831b02aa
JB
300 /**
301 * Reload an entry.
302 * Refetch content from the website and make it readable again.
303 *
304 * @param Entry $entry
305 *
306 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
307 *
308 * @return \Symfony\Component\HttpFoundation\RedirectResponse
309 */
310 public function reloadAction(Entry $entry)
311 {
312 $this->checkUserAction($entry);
313
4204a06b 314 $message = 'flashes.entry.notice.entry_reloaded';
831b02aa 315 if (false === $this->updateEntry($entry)) {
4204a06b 316 $message = 'flashes.entry.notice.entry_reload_failed';
831b02aa
JB
317 }
318
319 $this->get('session')->getFlashBag()->add(
320 'notice',
321 $message
322 );
323
4094ea47 324 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
831b02aa
JB
325 }
326
163eae0b 327 /**
4346a860 328 * Changes read status for an entry.
163eae0b 329 *
3d2b2d62
J
330 * @param Request $request
331 * @param Entry $entry
332 *
163eae0b 333 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
3d2b2d62 334 *
163eae0b
NL
335 * @return \Symfony\Component\HttpFoundation\RedirectResponse
336 */
be463487 337 public function toggleArchiveAction(Request $request, Entry $entry)
163eae0b 338 {
3d2b2d62
J
339 $this->checkUserAction($entry);
340
163eae0b
NL
341 $entry->toggleArchive();
342 $this->getDoctrine()->getManager()->flush();
343
4204a06b
JB
344 $message = 'flashes.entry.notice.entry_unarchived';
345 if ($entry->isArchived()) {
346 $message = 'flashes.entry.notice.entry_archived';
347 }
348
163eae0b
NL
349 $this->get('session')->getFlashBag()->add(
350 'notice',
4204a06b 351 $message
163eae0b
NL
352 );
353
af497a64
NL
354 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
355
356 return $this->redirect($redirectUrl);
163eae0b
NL
357 }
358
359 /**
4346a860 360 * Changes favorite status for an entry.
163eae0b 361 *
3d2b2d62
J
362 * @param Request $request
363 * @param Entry $entry
364 *
163eae0b 365 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
3d2b2d62 366 *
163eae0b
NL
367 * @return \Symfony\Component\HttpFoundation\RedirectResponse
368 */
be463487 369 public function toggleStarAction(Request $request, Entry $entry)
163eae0b 370 {
3d2b2d62
J
371 $this->checkUserAction($entry);
372
163eae0b
NL
373 $entry->toggleStar();
374 $this->getDoctrine()->getManager()->flush();
375
4204a06b
JB
376 $message = 'flashes.entry.notice.entry_unstarred';
377 if ($entry->isStarred()) {
378 $message = 'flashes.entry.notice.entry_starred';
379 }
380
163eae0b
NL
381 $this->get('session')->getFlashBag()->add(
382 'notice',
4204a06b 383 $message
163eae0b
NL
384 );
385
af497a64
NL
386 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
387
388 return $this->redirect($redirectUrl);
163eae0b
NL
389 }
390
391 /**
2863bf2a 392 * Deletes entry and redirect to the homepage or the last viewed page.
163eae0b 393 *
16a3d04c 394 * @param Entry $entry
3d2b2d62 395 *
163eae0b 396 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
3d2b2d62 397 *
163eae0b
NL
398 * @return \Symfony\Component\HttpFoundation\RedirectResponse
399 */
18d5f454 400 public function deleteEntryAction(Request $request, Entry $entry)
163eae0b 401 {
3d2b2d62
J
402 $this->checkUserAction($entry);
403
2863bf2a
JB
404 // generates the view url for this entry to check for redirection later
405 // to avoid redirecting to the deleted entry. Ugh.
406 $url = $this->generateUrl(
407 'view',
4094ea47 408 ['id' => $entry->getId()],
2863bf2a
JB
409 UrlGeneratorInterface::ABSOLUTE_URL
410 );
411
1d147791
NL
412 $em = $this->getDoctrine()->getManager();
413 $em->remove($entry);
414 $em->flush();
163eae0b
NL
415
416 $this->get('session')->getFlashBag()->add(
417 'notice',
4204a06b 418 'flashes.entry.notice.entry_deleted'
163eae0b 419 );
bd9f0815 420
2863bf2a 421 // don't redirect user to the deleted entry
345d7426 422 $to = ($url !== $request->headers->get('referer') ? $request->headers->get('referer') : null);
af497a64
NL
423
424 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to);
425
426 return $this->redirect($redirectUrl);
bd9f0815 427 }
3d2b2d62
J
428
429 /**
4346a860 430 * Check if the logged user can manage the given entry.
3d2b2d62
J
431 *
432 * @param Entry $entry
433 */
434 private function checkUserAction(Entry $entry)
435 {
436 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
82d6d9cb 437 throw $this->createAccessDeniedException('You can not access this entry.');
3d2b2d62
J
438 }
439 }
b00a89e0
NL
440
441 /**
442 * Check for existing entry, if it exists, redirect to it with a message.
443 *
4094ea47 444 * @param Entry $entry
b00a89e0 445 *
4094ea47 446 * @return Entry|bool
b00a89e0 447 */
4094ea47 448 private function checkIfEntryAlreadyExists(Entry $entry)
b00a89e0
NL
449 {
450 return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
451 }
9d50517c 452}