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