]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/EntryController.php
Update bundle & stock file
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / EntryController.php
CommitLineData
9d50517c
NL
1<?php
2
ad4d1caa 3namespace Wallabag\CoreBundle\Controller;
9d50517c 4
619cc453
JB
5use Pagerfanta\Adapter\DoctrineORMAdapter;
6use Pagerfanta\Pagerfanta;
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;
26864574 12use Wallabag\CoreBundle\Filter\EntryFilterType;
619cc453
JB
13use Wallabag\CoreBundle\Form\Type\EditEntryType;
14use Wallabag\CoreBundle\Form\Type\NewEntryType;
9d50517c
NL
15
16class EntryController extends Controller
17{
880a0e1c
NL
18 /**
19 * @param Entry $entry
20 */
21 private function updateEntry(Entry $entry)
22 {
23 try {
24 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
25 $em = $this->getDoctrine()->getManager();
26 $em->persist($entry);
27 $em->flush();
28 } catch (\Exception $e) {
29 return false;
30 }
31
32 return true;
33 }
34
b84a8055 35 /**
3d2b2d62
J
36 * @param Request $request
37 *
053b9568 38 * @Route("/new-entry", name="new_entry")
3d2b2d62 39 *
b84a8055
NL
40 * @return \Symfony\Component\HttpFoundation\Response
41 */
053b9568 42 public function addEntryFormAction(Request $request)
b84a8055 43 {
3b815d2d 44 $entry = new Entry($this->getUser());
b84a8055 45
5c895a7f 46 $form = $this->createForm(NewEntryType::class, $entry);
b84a8055
NL
47
48 $form->handleRequest($request);
49
50 if ($form->isValid()) {
5a4bbcc9
JB
51 // check for existing entry, if it exists, redirect to it with a message
52 $existingEntry = $this->get('wallabag_core.entry_repository')
53 ->existByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
dda57bb9 54
5a4bbcc9 55 if (false !== $existingEntry) {
dda57bb9
NL
56 $this->get('session')->getFlashBag()->add(
57 'notice',
5a4bbcc9 58 'Entry already saved on '.$existingEntry['createdAt']->format('d-m-Y')
dda57bb9
NL
59 );
60
5a4bbcc9 61 return $this->redirect($this->generateUrl('view', array('id' => $existingEntry['id'])));
dda57bb9
NL
62 }
63
880a0e1c 64 $this->updateEntry($entry);
b84a8055
NL
65 $this->get('session')->getFlashBag()->add(
66 'notice',
67 'Entry saved'
68 );
69
70 return $this->redirect($this->generateUrl('homepage'));
71 }
72
053b9568 73 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array(
b84a8055
NL
74 'form' => $form->createView(),
75 ));
76 }
77
880a0e1c
NL
78 /**
79 * @param Request $request
80 *
81 * @Route("/bookmarklet", name="bookmarklet")
82 *
83 * @return \Symfony\Component\HttpFoundation\Response
84 */
85 public function addEntryViaBookmarklet(Request $request)
86 {
87 $entry = new Entry($this->getUser());
88 $entry->setUrl($request->get('url'));
89 $this->updateEntry($entry);
90
91 return $this->redirect($this->generateUrl('homepage'));
92 }
93
053b9568
NL
94 /**
95 * @param Request $request
96 *
97 * @Route("/new", name="new")
98 *
99 * @return \Symfony\Component\HttpFoundation\Response
100 */
101 public function addEntryAction(Request $request)
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',
131 'Entry updated'
132 );
133
134 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
135 }
136
137 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array(
138 'form' => $form->createView(),
139 ));
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 {
0ab7404f 169 return $this->showEntries('unread', $request, $page);
9d50517c 170 }
bd9f0815
NL
171
172 /**
4346a860 173 * Shows read entries for current user.
163eae0b 174 *
26864574
NL
175 * @param Request $request
176 * @param int $page
177 *
9fb6ac83 178 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
3d2b2d62 179 *
163eae0b 180 * @return \Symfony\Component\HttpFoundation\Response
bd9f0815 181 */
26864574 182 public function showArchiveAction(Request $request, $page)
bd9f0815 183 {
0ab7404f
JB
184 return $this->showEntries('archive', $request, $page);
185 }
186
187 /**
188 * Shows starred entries for current user.
189 *
190 * @param Request $request
191 * @param int $page
192 *
193 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
194 *
195 * @return \Symfony\Component\HttpFoundation\Response
196 */
197 public function showStarredAction(Request $request, $page)
198 {
199 return $this->showEntries('starred', $request, $page);
200 }
26864574 201
0ab7404f
JB
202 /**
203 * Global method to retrieve entries depending on the given type
204 * It returns the response to be send.
205 *
206 * @param string $type Entries type: unread, starred or archive
207 * @param Request $request
208 * @param int $page
209 *
210 * @return \Symfony\Component\HttpFoundation\Response
211 */
212 private function showEntries($type, Request $request, $page)
213 {
214 $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
215
216 switch ($type) {
217 case 'starred':
218 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
219 break;
220
221 case 'archive':
222 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
223 break;
224
225 case 'unread':
226 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
227 break;
228
2b7a4889
NL
229 case 'all':
230 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
231 break;
232
0ab7404f
JB
233 default:
234 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
235 }
236
5c895a7f 237 $form = $this->createForm(new EntryFilterType($repository, $this->getUser()));
9fb6ac83 238
26864574
NL
239 if ($request->query->has($form->getName())) {
240 // manually bind values from the request
241 $form->submit($request->query->get($form->getName()));
242
243 // build the query from the given form object
0ab7404f 244 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
26864574
NL
245 }
246
0ab7404f 247 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
26864574
NL
248 $entries = new Pagerfanta($pagerAdapter);
249
48ffc5a4 250 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
9fb6ac83 251 $entries->setCurrentPage($page);
bd9f0815
NL
252
253 return $this->render(
ad4d1caa 254 'WallabagCoreBundle:Entry:entries.html.twig',
9fb6ac83 255 array(
8ce32af6
JB
256 'form' => $form->createView(),
257 'entries' => $entries,
258 'currentPage' => $page,
9fb6ac83 259 )
bd9f0815 260 );
bd9f0815
NL
261 }
262
263 /**
4346a860 264 * Shows entry content.
163eae0b 265 *
3d2b2d62
J
266 * @param Entry $entry
267 *
bd9f0815 268 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
3d2b2d62 269 *
163eae0b 270 * @return \Symfony\Component\HttpFoundation\Response
bd9f0815 271 */
be463487 272 public function viewAction(Entry $entry)
bd9f0815 273 {
3d2b2d62
J
274 $this->checkUserAction($entry);
275
bd9f0815 276 return $this->render(
ad4d1caa 277 'WallabagCoreBundle:Entry:entry.html.twig',
bd9f0815
NL
278 array('entry' => $entry)
279 );
163eae0b
NL
280 }
281
831b02aa
JB
282 /**
283 * Reload an entry.
284 * Refetch content from the website and make it readable again.
285 *
286 * @param Entry $entry
287 *
288 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
289 *
290 * @return \Symfony\Component\HttpFoundation\RedirectResponse
291 */
292 public function reloadAction(Entry $entry)
293 {
294 $this->checkUserAction($entry);
295
296 $message = 'Entry reloaded';
297 if (false === $this->updateEntry($entry)) {
298 $message = 'Failed to reload entry';
299 }
300
301 $this->get('session')->getFlashBag()->add(
302 'notice',
303 $message
304 );
305
306 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
307 }
308
163eae0b 309 /**
4346a860 310 * Changes read status for an entry.
163eae0b 311 *
3d2b2d62
J
312 * @param Request $request
313 * @param Entry $entry
314 *
163eae0b 315 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
3d2b2d62 316 *
163eae0b
NL
317 * @return \Symfony\Component\HttpFoundation\RedirectResponse
318 */
be463487 319 public function toggleArchiveAction(Request $request, Entry $entry)
163eae0b 320 {
3d2b2d62
J
321 $this->checkUserAction($entry);
322
163eae0b
NL
323 $entry->toggleArchive();
324 $this->getDoctrine()->getManager()->flush();
325
326 $this->get('session')->getFlashBag()->add(
327 'notice',
8ce32af6 328 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived')
163eae0b
NL
329 );
330
331 return $this->redirect($request->headers->get('referer'));
332 }
333
334 /**
4346a860 335 * Changes favorite status for an entry.
163eae0b 336 *
3d2b2d62
J
337 * @param Request $request
338 * @param Entry $entry
339 *
163eae0b 340 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
3d2b2d62 341 *
163eae0b
NL
342 * @return \Symfony\Component\HttpFoundation\RedirectResponse
343 */
be463487 344 public function toggleStarAction(Request $request, Entry $entry)
163eae0b 345 {
3d2b2d62
J
346 $this->checkUserAction($entry);
347
163eae0b
NL
348 $entry->toggleStar();
349 $this->getDoctrine()->getManager()->flush();
350
351 $this->get('session')->getFlashBag()->add(
352 'notice',
8ce32af6 353 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred')
163eae0b
NL
354 );
355
356 return $this->redirect($request->headers->get('referer'));
357 }
358
359 /**
2863bf2a 360 * Deletes entry and redirect to the homepage or the last viewed page.
163eae0b 361 *
16a3d04c 362 * @param Entry $entry
3d2b2d62 363 *
163eae0b 364 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
3d2b2d62 365 *
163eae0b
NL
366 * @return \Symfony\Component\HttpFoundation\RedirectResponse
367 */
18d5f454 368 public function deleteEntryAction(Request $request, Entry $entry)
163eae0b 369 {
3d2b2d62
J
370 $this->checkUserAction($entry);
371
2863bf2a
JB
372 // generates the view url for this entry to check for redirection later
373 // to avoid redirecting to the deleted entry. Ugh.
374 $url = $this->generateUrl(
375 'view',
376 array('id' => $entry->getId()),
377 UrlGeneratorInterface::ABSOLUTE_URL
378 );
379
1d147791
NL
380 $em = $this->getDoctrine()->getManager();
381 $em->remove($entry);
382 $em->flush();
163eae0b
NL
383
384 $this->get('session')->getFlashBag()->add(
385 'notice',
386 'Entry deleted'
387 );
bd9f0815 388
2863bf2a 389 // don't redirect user to the deleted entry
e82160e5 390 return $this->redirect($url !== $request->headers->get('referer') ? $request->headers->get('referer') : $this->generateUrl('homepage'));
bd9f0815 391 }
3d2b2d62
J
392
393 /**
4346a860 394 * Check if the logged user can manage the given entry.
3d2b2d62
J
395 *
396 * @param Entry $entry
397 */
398 private function checkUserAction(Entry $entry)
399 {
400 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
82d6d9cb 401 throw $this->createAccessDeniedException('You can not access this entry.');
3d2b2d62
J
402 }
403 }
9d50517c 404}