]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/EntryController.php
Ability to reload an entry
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / EntryController.php
CommitLineData
9d50517c
NL
1<?php
2
ad4d1caa 3namespace Wallabag\CoreBundle\Controller;
9d50517c
NL
4
5use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6use Symfony\Bundle\FrameworkBundle\Controller\Controller;
163eae0b 7use Symfony\Component\HttpFoundation\Request;
2863bf2a 8use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
be463487 9use Wallabag\CoreBundle\Entity\Entry;
82d6d9cb
JB
10use Wallabag\CoreBundle\Form\Type\NewEntryType;
11use Wallabag\CoreBundle\Form\Type\EditEntryType;
26864574
NL
12use Wallabag\CoreBundle\Filter\EntryFilterType;
13use Pagerfanta\Adapter\DoctrineORMAdapter;
14use Pagerfanta\Pagerfanta;
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
82d6d9cb 46 $form = $this->createForm(new NewEntryType(), $entry);
b84a8055
NL
47
48 $form->handleRequest($request);
49
50 if ($form->isValid()) {
880a0e1c 51 $this->updateEntry($entry);
b84a8055
NL
52 $this->get('session')->getFlashBag()->add(
53 'notice',
54 'Entry saved'
55 );
56
57 return $this->redirect($this->generateUrl('homepage'));
58 }
59
053b9568 60 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array(
b84a8055
NL
61 'form' => $form->createView(),
62 ));
63 }
64
880a0e1c
NL
65 /**
66 * @param Request $request
67 *
68 * @Route("/bookmarklet", name="bookmarklet")
69 *
70 * @return \Symfony\Component\HttpFoundation\Response
71 */
72 public function addEntryViaBookmarklet(Request $request)
73 {
74 $entry = new Entry($this->getUser());
75 $entry->setUrl($request->get('url'));
76 $this->updateEntry($entry);
77
78 return $this->redirect($this->generateUrl('homepage'));
79 }
80
053b9568
NL
81 /**
82 * @param Request $request
83 *
84 * @Route("/new", name="new")
85 *
86 * @return \Symfony\Component\HttpFoundation\Response
87 */
88 public function addEntryAction(Request $request)
89 {
90 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
91 }
92
82d6d9cb
JB
93 /**
94 * Edit an entry content.
95 *
96 * @param Request $request
97 * @param Entry $entry
98 *
99 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
100 *
101 * @return \Symfony\Component\HttpFoundation\Response
102 */
103 public function editEntryAction(Request $request, Entry $entry)
104 {
105 $this->checkUserAction($entry);
106
107 $form = $this->createForm(new EditEntryType(), $entry);
108
109 $form->handleRequest($request);
110
111 if ($form->isValid()) {
112 $em = $this->getDoctrine()->getManager();
113 $em->persist($entry);
114 $em->flush();
115
116 $this->get('session')->getFlashBag()->add(
117 'notice',
118 'Entry updated'
119 );
120
121 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
122 }
123
124 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array(
125 'form' => $form->createView(),
126 ));
127 }
128
89659c9e
NL
129 /**
130 * Shows all entries for current user.
131 *
132 * @param Request $request
133 * @param int $page
134 *
135 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
136 *
137 * @return \Symfony\Component\HttpFoundation\Response
138 */
139 public function showAllAction(Request $request, $page)
140 {
2b7a4889 141 return $this->showEntries('all', $request, $page);
89659c9e
NL
142 }
143
9d50517c 144 /**
4346a860 145 * Shows unread entries for current user.
163eae0b 146 *
26864574
NL
147 * @param Request $request
148 * @param int $page
149 *
9fb6ac83 150 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
3d2b2d62 151 *
163eae0b 152 * @return \Symfony\Component\HttpFoundation\Response
9d50517c 153 */
26864574 154 public function showUnreadAction(Request $request, $page)
9d50517c 155 {
0ab7404f 156 return $this->showEntries('unread', $request, $page);
9d50517c 157 }
bd9f0815
NL
158
159 /**
4346a860 160 * Shows read entries for current user.
163eae0b 161 *
26864574
NL
162 * @param Request $request
163 * @param int $page
164 *
9fb6ac83 165 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
3d2b2d62 166 *
163eae0b 167 * @return \Symfony\Component\HttpFoundation\Response
bd9f0815 168 */
26864574 169 public function showArchiveAction(Request $request, $page)
bd9f0815 170 {
0ab7404f
JB
171 return $this->showEntries('archive', $request, $page);
172 }
173
174 /**
175 * Shows starred entries for current user.
176 *
177 * @param Request $request
178 * @param int $page
179 *
180 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
181 *
182 * @return \Symfony\Component\HttpFoundation\Response
183 */
184 public function showStarredAction(Request $request, $page)
185 {
186 return $this->showEntries('starred', $request, $page);
187 }
26864574 188
0ab7404f
JB
189 /**
190 * Global method to retrieve entries depending on the given type
191 * It returns the response to be send.
192 *
193 * @param string $type Entries type: unread, starred or archive
194 * @param Request $request
195 * @param int $page
196 *
197 * @return \Symfony\Component\HttpFoundation\Response
198 */
199 private function showEntries($type, Request $request, $page)
200 {
201 $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
202
203 switch ($type) {
204 case 'starred':
205 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
206 break;
207
208 case 'archive':
209 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
210 break;
211
212 case 'unread':
213 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
214 break;
215
2b7a4889
NL
216 case 'all':
217 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
218 break;
219
0ab7404f
JB
220 default:
221 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
222 }
223
d4ebe5c5 224 $form = $this->get('form.factory')->create(new EntryFilterType($repository, $this->getUser()));
9fb6ac83 225
26864574
NL
226 if ($request->query->has($form->getName())) {
227 // manually bind values from the request
228 $form->submit($request->query->get($form->getName()));
229
230 // build the query from the given form object
0ab7404f 231 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
26864574
NL
232 }
233
0ab7404f 234 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
26864574
NL
235 $entries = new Pagerfanta($pagerAdapter);
236
48ffc5a4 237 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
9fb6ac83 238 $entries->setCurrentPage($page);
bd9f0815
NL
239
240 return $this->render(
ad4d1caa 241 'WallabagCoreBundle:Entry:entries.html.twig',
9fb6ac83 242 array(
8ce32af6
JB
243 'form' => $form->createView(),
244 'entries' => $entries,
245 'currentPage' => $page,
9fb6ac83 246 )
bd9f0815 247 );
bd9f0815
NL
248 }
249
250 /**
4346a860 251 * Shows entry content.
163eae0b 252 *
3d2b2d62
J
253 * @param Entry $entry
254 *
bd9f0815 255 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
3d2b2d62 256 *
163eae0b 257 * @return \Symfony\Component\HttpFoundation\Response
bd9f0815 258 */
be463487 259 public function viewAction(Entry $entry)
bd9f0815 260 {
3d2b2d62
J
261 $this->checkUserAction($entry);
262
bd9f0815 263 return $this->render(
ad4d1caa 264 'WallabagCoreBundle:Entry:entry.html.twig',
bd9f0815
NL
265 array('entry' => $entry)
266 );
163eae0b
NL
267 }
268
831b02aa
JB
269 /**
270 * Reload an entry.
271 * Refetch content from the website and make it readable again.
272 *
273 * @param Entry $entry
274 *
275 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
276 *
277 * @return \Symfony\Component\HttpFoundation\RedirectResponse
278 */
279 public function reloadAction(Entry $entry)
280 {
281 $this->checkUserAction($entry);
282
283 $message = 'Entry reloaded';
284 if (false === $this->updateEntry($entry)) {
285 $message = 'Failed to reload entry';
286 }
287
288 $this->get('session')->getFlashBag()->add(
289 'notice',
290 $message
291 );
292
293 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
294 }
295
163eae0b 296 /**
4346a860 297 * Changes read status for an entry.
163eae0b 298 *
3d2b2d62
J
299 * @param Request $request
300 * @param Entry $entry
301 *
163eae0b 302 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
3d2b2d62 303 *
163eae0b
NL
304 * @return \Symfony\Component\HttpFoundation\RedirectResponse
305 */
be463487 306 public function toggleArchiveAction(Request $request, Entry $entry)
163eae0b 307 {
3d2b2d62
J
308 $this->checkUserAction($entry);
309
163eae0b
NL
310 $entry->toggleArchive();
311 $this->getDoctrine()->getManager()->flush();
312
313 $this->get('session')->getFlashBag()->add(
314 'notice',
8ce32af6 315 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived')
163eae0b
NL
316 );
317
318 return $this->redirect($request->headers->get('referer'));
319 }
320
321 /**
4346a860 322 * Changes favorite status for an entry.
163eae0b 323 *
3d2b2d62
J
324 * @param Request $request
325 * @param Entry $entry
326 *
163eae0b 327 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
3d2b2d62 328 *
163eae0b
NL
329 * @return \Symfony\Component\HttpFoundation\RedirectResponse
330 */
be463487 331 public function toggleStarAction(Request $request, Entry $entry)
163eae0b 332 {
3d2b2d62
J
333 $this->checkUserAction($entry);
334
163eae0b
NL
335 $entry->toggleStar();
336 $this->getDoctrine()->getManager()->flush();
337
338 $this->get('session')->getFlashBag()->add(
339 'notice',
8ce32af6 340 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred')
163eae0b
NL
341 );
342
343 return $this->redirect($request->headers->get('referer'));
344 }
345
346 /**
2863bf2a 347 * Deletes entry and redirect to the homepage or the last viewed page.
163eae0b 348 *
16a3d04c 349 * @param Entry $entry
3d2b2d62 350 *
163eae0b 351 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
3d2b2d62 352 *
163eae0b
NL
353 * @return \Symfony\Component\HttpFoundation\RedirectResponse
354 */
18d5f454 355 public function deleteEntryAction(Request $request, Entry $entry)
163eae0b 356 {
3d2b2d62
J
357 $this->checkUserAction($entry);
358
2863bf2a
JB
359 // generates the view url for this entry to check for redirection later
360 // to avoid redirecting to the deleted entry. Ugh.
361 $url = $this->generateUrl(
362 'view',
363 array('id' => $entry->getId()),
364 UrlGeneratorInterface::ABSOLUTE_URL
365 );
366
1d147791
NL
367 $em = $this->getDoctrine()->getManager();
368 $em->remove($entry);
369 $em->flush();
163eae0b
NL
370
371 $this->get('session')->getFlashBag()->add(
372 'notice',
373 'Entry deleted'
374 );
bd9f0815 375
2863bf2a
JB
376 // don't redirect user to the deleted entry
377 return $this->redirect($url !== $request->headers->get('referer') ?: $this->generateUrl('homepage'));
bd9f0815 378 }
3d2b2d62
J
379
380 /**
4346a860 381 * Check if the logged user can manage the given entry.
3d2b2d62
J
382 *
383 * @param Entry $entry
384 */
385 private function checkUserAction(Entry $entry)
386 {
387 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
82d6d9cb 388 throw $this->createAccessDeniedException('You can not access this entry.');
3d2b2d62
J
389 }
390 }
9d50517c 391}