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