]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/EntryController.php
Merge pull request #1547 from wallabag/v2-mistakes
[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
269 /**
4346a860 270 * Changes read status for an entry.
163eae0b 271 *
3d2b2d62
J
272 * @param Request $request
273 * @param Entry $entry
274 *
163eae0b 275 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
3d2b2d62 276 *
163eae0b
NL
277 * @return \Symfony\Component\HttpFoundation\RedirectResponse
278 */
be463487 279 public function toggleArchiveAction(Request $request, Entry $entry)
163eae0b 280 {
3d2b2d62
J
281 $this->checkUserAction($entry);
282
163eae0b
NL
283 $entry->toggleArchive();
284 $this->getDoctrine()->getManager()->flush();
285
286 $this->get('session')->getFlashBag()->add(
287 'notice',
8ce32af6 288 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived')
163eae0b
NL
289 );
290
291 return $this->redirect($request->headers->get('referer'));
292 }
293
294 /**
4346a860 295 * Changes favorite status for an entry.
163eae0b 296 *
3d2b2d62
J
297 * @param Request $request
298 * @param Entry $entry
299 *
163eae0b 300 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
3d2b2d62 301 *
163eae0b
NL
302 * @return \Symfony\Component\HttpFoundation\RedirectResponse
303 */
be463487 304 public function toggleStarAction(Request $request, Entry $entry)
163eae0b 305 {
3d2b2d62
J
306 $this->checkUserAction($entry);
307
163eae0b
NL
308 $entry->toggleStar();
309 $this->getDoctrine()->getManager()->flush();
310
311 $this->get('session')->getFlashBag()->add(
312 'notice',
8ce32af6 313 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred')
163eae0b
NL
314 );
315
316 return $this->redirect($request->headers->get('referer'));
317 }
318
319 /**
2863bf2a 320 * Deletes entry and redirect to the homepage or the last viewed page.
163eae0b 321 *
16a3d04c 322 * @param Entry $entry
3d2b2d62 323 *
163eae0b 324 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
3d2b2d62 325 *
163eae0b
NL
326 * @return \Symfony\Component\HttpFoundation\RedirectResponse
327 */
18d5f454 328 public function deleteEntryAction(Request $request, Entry $entry)
163eae0b 329 {
3d2b2d62
J
330 $this->checkUserAction($entry);
331
2863bf2a
JB
332 // generates the view url for this entry to check for redirection later
333 // to avoid redirecting to the deleted entry. Ugh.
334 $url = $this->generateUrl(
335 'view',
336 array('id' => $entry->getId()),
337 UrlGeneratorInterface::ABSOLUTE_URL
338 );
339
1d147791
NL
340 $em = $this->getDoctrine()->getManager();
341 $em->remove($entry);
342 $em->flush();
163eae0b
NL
343
344 $this->get('session')->getFlashBag()->add(
345 'notice',
346 'Entry deleted'
347 );
bd9f0815 348
2863bf2a 349 // don't redirect user to the deleted entry
e82160e5 350 return $this->redirect($url !== $request->headers->get('referer') ? $request->headers->get('referer') : $this->generateUrl('homepage'));
bd9f0815 351 }
3d2b2d62
J
352
353 /**
4346a860 354 * Check if the logged user can manage the given entry.
3d2b2d62
J
355 *
356 * @param Entry $entry
357 */
358 private function checkUserAction(Entry $entry)
359 {
360 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
82d6d9cb 361 throw $this->createAccessDeniedException('You can not access this entry.');
3d2b2d62
J
362 }
363 }
9d50517c 364}