]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/EntryController.php
filters: adapt queryBuilder for 'all' view
[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;
be463487 8use Wallabag\CoreBundle\Entity\Entry;
6b767d1c 9use Wallabag\CoreBundle\Service\Extractor;
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{
b84a8055 18 /**
3d2b2d62
J
19 * @param Request $request
20 *
053b9568 21 * @Route("/new-entry", name="new_entry")
3d2b2d62 22 *
b84a8055
NL
23 * @return \Symfony\Component\HttpFoundation\Response
24 */
053b9568 25 public function addEntryFormAction(Request $request)
b84a8055 26 {
3b815d2d 27 $entry = new Entry($this->getUser());
b84a8055 28
82d6d9cb 29 $form = $this->createForm(new NewEntryType(), $entry);
b84a8055
NL
30
31 $form->handleRequest($request);
32
33 if ($form->isValid()) {
6b767d1c 34 $content = Extractor::extract($entry->getUrl());
b84a8055 35
6b767d1c
NL
36 $entry->setTitle($content->getTitle());
37 $entry->setContent($content->getBody());
38
39 $em = $this->getDoctrine()->getManager();
b84a8055
NL
40 $em->persist($entry);
41 $em->flush();
42
43 $this->get('session')->getFlashBag()->add(
44 'notice',
45 'Entry saved'
46 );
47
48 return $this->redirect($this->generateUrl('homepage'));
49 }
50
053b9568 51 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array(
b84a8055
NL
52 'form' => $form->createView(),
53 ));
54 }
55
053b9568
NL
56 /**
57 * @param Request $request
58 *
59 * @Route("/new", name="new")
60 *
61 * @return \Symfony\Component\HttpFoundation\Response
62 */
63 public function addEntryAction(Request $request)
64 {
65 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
66 }
67
82d6d9cb
JB
68 /**
69 * Edit an entry content.
70 *
71 * @param Request $request
72 * @param Entry $entry
73 *
74 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
75 *
76 * @return \Symfony\Component\HttpFoundation\Response
77 */
78 public function editEntryAction(Request $request, Entry $entry)
79 {
80 $this->checkUserAction($entry);
81
82 $form = $this->createForm(new EditEntryType(), $entry);
83
84 $form->handleRequest($request);
85
86 if ($form->isValid()) {
87 $em = $this->getDoctrine()->getManager();
88 $em->persist($entry);
89 $em->flush();
90
91 $this->get('session')->getFlashBag()->add(
92 'notice',
93 'Entry updated'
94 );
95
96 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
97 }
98
99 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array(
100 'form' => $form->createView(),
101 ));
102 }
103
89659c9e
NL
104 /**
105 * Shows all entries for current user.
106 *
107 * @param Request $request
108 * @param int $page
109 *
110 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
111 *
112 * @return \Symfony\Component\HttpFoundation\Response
113 */
114 public function showAllAction(Request $request, $page)
115 {
2b7a4889 116 return $this->showEntries('all', $request, $page);
89659c9e
NL
117 }
118
9d50517c 119 /**
4346a860 120 * Shows unread entries for current user.
163eae0b 121 *
26864574
NL
122 * @param Request $request
123 * @param int $page
124 *
9fb6ac83 125 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
3d2b2d62 126 *
163eae0b 127 * @return \Symfony\Component\HttpFoundation\Response
9d50517c 128 */
26864574 129 public function showUnreadAction(Request $request, $page)
9d50517c 130 {
0ab7404f 131 return $this->showEntries('unread', $request, $page);
9d50517c 132 }
bd9f0815
NL
133
134 /**
4346a860 135 * Shows read entries for current user.
163eae0b 136 *
26864574
NL
137 * @param Request $request
138 * @param int $page
139 *
9fb6ac83 140 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
3d2b2d62 141 *
163eae0b 142 * @return \Symfony\Component\HttpFoundation\Response
bd9f0815 143 */
26864574 144 public function showArchiveAction(Request $request, $page)
bd9f0815 145 {
0ab7404f
JB
146 return $this->showEntries('archive', $request, $page);
147 }
148
149 /**
150 * Shows starred entries for current user.
151 *
152 * @param Request $request
153 * @param int $page
154 *
155 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
156 *
157 * @return \Symfony\Component\HttpFoundation\Response
158 */
159 public function showStarredAction(Request $request, $page)
160 {
161 return $this->showEntries('starred', $request, $page);
162 }
26864574 163
0ab7404f
JB
164 /**
165 * Global method to retrieve entries depending on the given type
166 * It returns the response to be send.
167 *
168 * @param string $type Entries type: unread, starred or archive
169 * @param Request $request
170 * @param int $page
171 *
172 * @return \Symfony\Component\HttpFoundation\Response
173 */
174 private function showEntries($type, Request $request, $page)
175 {
176 $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
177
178 switch ($type) {
179 case 'starred':
180 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
181 break;
182
183 case 'archive':
184 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
185 break;
186
187 case 'unread':
188 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
189 break;
190
2b7a4889
NL
191 case 'all':
192 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
193 break;
194
0ab7404f
JB
195 default:
196 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
197 }
198
199 $form = $this->get('form.factory')->create(new EntryFilterType());
9fb6ac83 200
26864574
NL
201 if ($request->query->has($form->getName())) {
202 // manually bind values from the request
203 $form->submit($request->query->get($form->getName()));
204
205 // build the query from the given form object
0ab7404f 206 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
26864574
NL
207 }
208
0ab7404f 209 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
26864574
NL
210 $entries = new Pagerfanta($pagerAdapter);
211
48ffc5a4 212 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
9fb6ac83 213 $entries->setCurrentPage($page);
bd9f0815
NL
214
215 return $this->render(
ad4d1caa 216 'WallabagCoreBundle:Entry:entries.html.twig',
9fb6ac83 217 array(
8ce32af6
JB
218 'form' => $form->createView(),
219 'entries' => $entries,
220 'currentPage' => $page,
9fb6ac83 221 )
bd9f0815 222 );
9fb6ac83 223
26864574
NL
224 if ($request->query->has($form->getName())) {
225 // manually bind values from the request
226 $form->submit($request->query->get($form->getName()));
227
228 // build the query from the given form object
229 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $filterBuilder);
230 }
231
232 $pagerAdapter = new DoctrineORMAdapter($filterBuilder->getQuery());
233 $entries = new Pagerfanta($pagerAdapter);
234
48ffc5a4 235 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
9fb6ac83 236 $entries->setCurrentPage($page);
bd9f0815
NL
237
238 return $this->render(
ad4d1caa 239 'WallabagCoreBundle:Entry:entries.html.twig',
9fb6ac83 240 array(
8ce32af6
JB
241 'form' => $form->createView(),
242 'entries' => $entries,
243 'currentPage' => $page,
9fb6ac83 244 )
bd9f0815 245 );
bd9f0815
NL
246 }
247
248 /**
4346a860 249 * Shows entry content.
163eae0b 250 *
3d2b2d62
J
251 * @param Entry $entry
252 *
bd9f0815 253 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
3d2b2d62 254 *
163eae0b 255 * @return \Symfony\Component\HttpFoundation\Response
bd9f0815 256 */
be463487 257 public function viewAction(Entry $entry)
bd9f0815 258 {
3d2b2d62
J
259 $this->checkUserAction($entry);
260
bd9f0815 261 return $this->render(
ad4d1caa 262 'WallabagCoreBundle:Entry:entry.html.twig',
bd9f0815
NL
263 array('entry' => $entry)
264 );
163eae0b
NL
265 }
266
267 /**
4346a860 268 * Changes read status for an entry.
163eae0b 269 *
3d2b2d62
J
270 * @param Request $request
271 * @param Entry $entry
272 *
163eae0b 273 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
3d2b2d62 274 *
163eae0b
NL
275 * @return \Symfony\Component\HttpFoundation\RedirectResponse
276 */
be463487 277 public function toggleArchiveAction(Request $request, Entry $entry)
163eae0b 278 {
3d2b2d62
J
279 $this->checkUserAction($entry);
280
163eae0b
NL
281 $entry->toggleArchive();
282 $this->getDoctrine()->getManager()->flush();
283
284 $this->get('session')->getFlashBag()->add(
285 'notice',
8ce32af6 286 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived')
163eae0b
NL
287 );
288
289 return $this->redirect($request->headers->get('referer'));
290 }
291
292 /**
4346a860 293 * Changes favorite status for an entry.
163eae0b 294 *
3d2b2d62
J
295 * @param Request $request
296 * @param Entry $entry
297 *
163eae0b 298 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
3d2b2d62 299 *
163eae0b
NL
300 * @return \Symfony\Component\HttpFoundation\RedirectResponse
301 */
be463487 302 public function toggleStarAction(Request $request, Entry $entry)
163eae0b 303 {
3d2b2d62
J
304 $this->checkUserAction($entry);
305
163eae0b
NL
306 $entry->toggleStar();
307 $this->getDoctrine()->getManager()->flush();
308
309 $this->get('session')->getFlashBag()->add(
310 'notice',
8ce32af6 311 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred')
163eae0b
NL
312 );
313
314 return $this->redirect($request->headers->get('referer'));
315 }
316
317 /**
4346a860 318 * Deletes entry.
163eae0b 319 *
3d2b2d62
J
320 * @param Request $request
321 * @param Entry $entry
322 *
163eae0b 323 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
3d2b2d62 324 *
163eae0b
NL
325 * @return \Symfony\Component\HttpFoundation\RedirectResponse
326 */
be463487 327 public function deleteEntryAction(Request $request, Entry $entry)
163eae0b 328 {
3d2b2d62
J
329 $this->checkUserAction($entry);
330
1d147791
NL
331 $em = $this->getDoctrine()->getManager();
332 $em->remove($entry);
333 $em->flush();
163eae0b
NL
334
335 $this->get('session')->getFlashBag()->add(
336 'notice',
337 'Entry deleted'
338 );
bd9f0815 339
163eae0b 340 return $this->redirect($request->headers->get('referer'));
bd9f0815 341 }
3d2b2d62
J
342
343 /**
4346a860 344 * Check if the logged user can manage the given entry.
3d2b2d62
J
345 *
346 * @param Entry $entry
347 */
348 private function checkUserAction(Entry $entry)
349 {
350 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
82d6d9cb 351 throw $this->createAccessDeniedException('You can not access this entry.');
3d2b2d62
J
352 }
353 }
9d50517c 354}