]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/EntryController.php
Convert array + phpDoc
[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());
26 $em = $this->getDoctrine()->getManager();
27 $em->persist($entry);
28 $em->flush();
29 } catch (\Exception $e) {
30 return false;
31 }
32
33 return true;
34 }
35
b84a8055 36 /**
3d2b2d62
J
37 * @param Request $request
38 *
053b9568 39 * @Route("/new-entry", name="new_entry")
3d2b2d62 40 *
b84a8055
NL
41 * @return \Symfony\Component\HttpFoundation\Response
42 */
053b9568 43 public function addEntryFormAction(Request $request)
b84a8055 44 {
3b815d2d 45 $entry = new Entry($this->getUser());
b84a8055 46
5c895a7f 47 $form = $this->createForm(NewEntryType::class, $entry);
b84a8055
NL
48
49 $form->handleRequest($request);
50
51 if ($form->isValid()) {
b00a89e0 52 $existingEntry = $this->checkIfEntryAlreadyExists($entry);
dda57bb9 53
5a4bbcc9 54 if (false !== $existingEntry) {
dda57bb9
NL
55 $this->get('session')->getFlashBag()->add(
56 'notice',
4094ea47 57 $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')])
dda57bb9
NL
58 );
59
4094ea47 60 return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()]));
dda57bb9
NL
61 }
62
880a0e1c 63 $this->updateEntry($entry);
b84a8055
NL
64 $this->get('session')->getFlashBag()->add(
65 'notice',
4204a06b 66 'flashes.entry.notice.entry_saved'
b84a8055
NL
67 );
68
69 return $this->redirect($this->generateUrl('homepage'));
70 }
71
4094ea47 72 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', [
b84a8055 73 'form' => $form->createView(),
4094ea47 74 ]);
b84a8055
NL
75 }
76
880a0e1c
NL
77 /**
78 * @param Request $request
79 *
80 * @Route("/bookmarklet", name="bookmarklet")
81 *
82 * @return \Symfony\Component\HttpFoundation\Response
83 */
5f8a7857 84 public function addEntryViaBookmarkletAction(Request $request)
880a0e1c
NL
85 {
86 $entry = new Entry($this->getUser());
87 $entry->setUrl($request->get('url'));
f652f41d 88
b00a89e0 89 if (false === $this->checkIfEntryAlreadyExists($entry)) {
f652f41d
NL
90 $this->updateEntry($entry);
91 }
880a0e1c
NL
92
93 return $this->redirect($this->generateUrl('homepage'));
94 }
95
053b9568 96 /**
053b9568
NL
97 * @Route("/new", name="new")
98 *
99 * @return \Symfony\Component\HttpFoundation\Response
100 */
4d0ec0e7 101 public function addEntryAction()
053b9568
NL
102 {
103 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
104 }
105
82d6d9cb
JB
106 /**
107 * Edit an entry content.
108 *
109 * @param Request $request
110 * @param Entry $entry
111 *
112 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
113 *
114 * @return \Symfony\Component\HttpFoundation\Response
115 */
116 public function editEntryAction(Request $request, Entry $entry)
117 {
118 $this->checkUserAction($entry);
119
5c895a7f 120 $form = $this->createForm(EditEntryType::class, $entry);
82d6d9cb
JB
121
122 $form->handleRequest($request);
123
124 if ($form->isValid()) {
125 $em = $this->getDoctrine()->getManager();
126 $em->persist($entry);
127 $em->flush();
128
129 $this->get('session')->getFlashBag()->add(
130 'notice',
4204a06b 131 'flashes.entry.notice.entry_updated'
82d6d9cb
JB
132 );
133
4094ea47 134 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
82d6d9cb
JB
135 }
136
4094ea47 137 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', [
82d6d9cb 138 'form' => $form->createView(),
4094ea47 139 ]);
82d6d9cb
JB
140 }
141
89659c9e
NL
142 /**
143 * Shows all entries for current user.
144 *
145 * @param Request $request
146 * @param int $page
147 *
148 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
149 *
150 * @return \Symfony\Component\HttpFoundation\Response
151 */
152 public function showAllAction(Request $request, $page)
153 {
2b7a4889 154 return $this->showEntries('all', $request, $page);
89659c9e
NL
155 }
156
9d50517c 157 /**
4346a860 158 * Shows unread entries for current user.
163eae0b 159 *
26864574
NL
160 * @param Request $request
161 * @param int $page
162 *
9fb6ac83 163 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
3d2b2d62 164 *
163eae0b 165 * @return \Symfony\Component\HttpFoundation\Response
9d50517c 166 */
26864574 167 public function showUnreadAction(Request $request, $page)
9d50517c 168 {
5c072d2b
NL
169 // load the quickstart if no entry in database
170 if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUsername($this->getUser()->getId()) == 0) {
171 return $this->redirect($this->generateUrl('quickstart'));
172 }
173
0ab7404f 174 return $this->showEntries('unread', $request, $page);
9d50517c 175 }
bd9f0815
NL
176
177 /**
4346a860 178 * Shows read entries for current user.
163eae0b 179 *
26864574
NL
180 * @param Request $request
181 * @param int $page
182 *
9fb6ac83 183 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
3d2b2d62 184 *
163eae0b 185 * @return \Symfony\Component\HttpFoundation\Response
bd9f0815 186 */
26864574 187 public function showArchiveAction(Request $request, $page)
bd9f0815 188 {
0ab7404f
JB
189 return $this->showEntries('archive', $request, $page);
190 }
191
192 /**
193 * Shows starred entries for current user.
194 *
195 * @param Request $request
196 * @param int $page
197 *
198 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
199 *
200 * @return \Symfony\Component\HttpFoundation\Response
201 */
202 public function showStarredAction(Request $request, $page)
203 {
204 return $this->showEntries('starred', $request, $page);
205 }
26864574 206
0ab7404f
JB
207 /**
208 * Global method to retrieve entries depending on the given type
209 * It returns the response to be send.
210 *
211 * @param string $type Entries type: unread, starred or archive
212 * @param Request $request
213 * @param int $page
214 *
215 * @return \Symfony\Component\HttpFoundation\Response
216 */
217 private function showEntries($type, Request $request, $page)
218 {
5c072d2b 219 $repository = $this->get('wallabag_core.entry_repository');
0ab7404f
JB
220
221 switch ($type) {
222 case 'starred':
223 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
224 break;
225
226 case 'archive':
227 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
228 break;
229
230 case 'unread':
231 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
232 break;
233
2b7a4889
NL
234 case 'all':
235 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
236 break;
237
0ab7404f
JB
238 default:
239 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
240 }
241
1d76102a 242 $form = $this->createForm(EntryFilterType::class);
9fb6ac83 243
26864574
NL
244 if ($request->query->has($form->getName())) {
245 // manually bind values from the request
246 $form->submit($request->query->get($form->getName()));
247
248 // build the query from the given form object
0ab7404f 249 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
26864574
NL
250 }
251
0ab7404f 252 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
26864574
NL
253 $entries = new Pagerfanta($pagerAdapter);
254
48ffc5a4 255 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
671a2b88
ML
256 try {
257 $entries->setCurrentPage($page);
258 } catch (OutOfRangeCurrentPageException $e) {
259 if ($page > 1) {
4094ea47 260 return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302);
671a2b88
ML
261 }
262 }
bd9f0815
NL
263
264 return $this->render(
ad4d1caa 265 'WallabagCoreBundle:Entry:entries.html.twig',
4094ea47 266 [
8ce32af6
JB
267 'form' => $form->createView(),
268 'entries' => $entries,
269 'currentPage' => $page,
4094ea47 270 ]
bd9f0815 271 );
bd9f0815
NL
272 }
273
274 /**
4346a860 275 * Shows entry content.
163eae0b 276 *
3d2b2d62
J
277 * @param Entry $entry
278 *
bd9f0815 279 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
3d2b2d62 280 *
163eae0b 281 * @return \Symfony\Component\HttpFoundation\Response
bd9f0815 282 */
be463487 283 public function viewAction(Entry $entry)
bd9f0815 284 {
3d2b2d62
J
285 $this->checkUserAction($entry);
286
bd9f0815 287 return $this->render(
ad4d1caa 288 'WallabagCoreBundle:Entry:entry.html.twig',
4094ea47 289 ['entry' => $entry]
bd9f0815 290 );
163eae0b
NL
291 }
292
831b02aa
JB
293 /**
294 * Reload an entry.
295 * Refetch content from the website and make it readable again.
296 *
297 * @param Entry $entry
298 *
299 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
300 *
301 * @return \Symfony\Component\HttpFoundation\RedirectResponse
302 */
303 public function reloadAction(Entry $entry)
304 {
305 $this->checkUserAction($entry);
306
4204a06b 307 $message = 'flashes.entry.notice.entry_reloaded';
831b02aa 308 if (false === $this->updateEntry($entry)) {
4204a06b 309 $message = 'flashes.entry.notice.entry_reload_failed';
831b02aa
JB
310 }
311
312 $this->get('session')->getFlashBag()->add(
313 'notice',
314 $message
315 );
316
4094ea47 317 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
831b02aa
JB
318 }
319
163eae0b 320 /**
4346a860 321 * Changes read status for an entry.
163eae0b 322 *
3d2b2d62
J
323 * @param Request $request
324 * @param Entry $entry
325 *
163eae0b 326 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
3d2b2d62 327 *
163eae0b
NL
328 * @return \Symfony\Component\HttpFoundation\RedirectResponse
329 */
be463487 330 public function toggleArchiveAction(Request $request, Entry $entry)
163eae0b 331 {
3d2b2d62
J
332 $this->checkUserAction($entry);
333
163eae0b
NL
334 $entry->toggleArchive();
335 $this->getDoctrine()->getManager()->flush();
336
4204a06b
JB
337 $message = 'flashes.entry.notice.entry_unarchived';
338 if ($entry->isArchived()) {
339 $message = 'flashes.entry.notice.entry_archived';
340 }
341
163eae0b
NL
342 $this->get('session')->getFlashBag()->add(
343 'notice',
4204a06b 344 $message
163eae0b
NL
345 );
346
347 return $this->redirect($request->headers->get('referer'));
348 }
349
350 /**
4346a860 351 * Changes favorite status for an entry.
163eae0b 352 *
3d2b2d62
J
353 * @param Request $request
354 * @param Entry $entry
355 *
163eae0b 356 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
3d2b2d62 357 *
163eae0b
NL
358 * @return \Symfony\Component\HttpFoundation\RedirectResponse
359 */
be463487 360 public function toggleStarAction(Request $request, Entry $entry)
163eae0b 361 {
3d2b2d62
J
362 $this->checkUserAction($entry);
363
163eae0b
NL
364 $entry->toggleStar();
365 $this->getDoctrine()->getManager()->flush();
366
4204a06b
JB
367 $message = 'flashes.entry.notice.entry_unstarred';
368 if ($entry->isStarred()) {
369 $message = 'flashes.entry.notice.entry_starred';
370 }
371
163eae0b
NL
372 $this->get('session')->getFlashBag()->add(
373 'notice',
4204a06b 374 $message
163eae0b
NL
375 );
376
377 return $this->redirect($request->headers->get('referer'));
378 }
379
380 /**
2863bf2a 381 * Deletes entry and redirect to the homepage or the last viewed page.
163eae0b 382 *
16a3d04c 383 * @param Entry $entry
3d2b2d62 384 *
163eae0b 385 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
3d2b2d62 386 *
163eae0b
NL
387 * @return \Symfony\Component\HttpFoundation\RedirectResponse
388 */
18d5f454 389 public function deleteEntryAction(Request $request, Entry $entry)
163eae0b 390 {
3d2b2d62
J
391 $this->checkUserAction($entry);
392
2863bf2a
JB
393 // generates the view url for this entry to check for redirection later
394 // to avoid redirecting to the deleted entry. Ugh.
395 $url = $this->generateUrl(
396 'view',
4094ea47 397 ['id' => $entry->getId()],
2863bf2a
JB
398 UrlGeneratorInterface::ABSOLUTE_URL
399 );
400
1d147791
NL
401 $em = $this->getDoctrine()->getManager();
402 $em->remove($entry);
403 $em->flush();
163eae0b
NL
404
405 $this->get('session')->getFlashBag()->add(
406 'notice',
4204a06b 407 'flashes.entry.notice.entry_deleted'
163eae0b 408 );
bd9f0815 409
2863bf2a 410 // don't redirect user to the deleted entry
e82160e5 411 return $this->redirect($url !== $request->headers->get('referer') ? $request->headers->get('referer') : $this->generateUrl('homepage'));
bd9f0815 412 }
3d2b2d62
J
413
414 /**
4346a860 415 * Check if the logged user can manage the given entry.
3d2b2d62
J
416 *
417 * @param Entry $entry
418 */
419 private function checkUserAction(Entry $entry)
420 {
421 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
82d6d9cb 422 throw $this->createAccessDeniedException('You can not access this entry.');
3d2b2d62
J
423 }
424 }
b00a89e0
NL
425
426 /**
427 * Check for existing entry, if it exists, redirect to it with a message.
428 *
4094ea47 429 * @param Entry $entry
b00a89e0 430 *
4094ea47 431 * @return Entry|bool
b00a89e0 432 */
4094ea47 433 private function checkIfEntryAlreadyExists(Entry $entry)
b00a89e0
NL
434 {
435 return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
436 }
9d50517c 437}