]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/EntryController.php
Integrate graby
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / EntryController.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Controller;
4
5 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7 use Symfony\Component\HttpFoundation\Request;
8 use Wallabag\CoreBundle\Entity\Entry;
9 use Wallabag\CoreBundle\Service\Extractor;
10 use Wallabag\CoreBundle\Form\Type\NewEntryType;
11 use Wallabag\CoreBundle\Form\Type\EditEntryType;
12 use Wallabag\CoreBundle\Filter\EntryFilterType;
13 use Pagerfanta\Adapter\DoctrineORMAdapter;
14 use Pagerfanta\Pagerfanta;
15
16 class EntryController extends Controller
17 {
18 /**
19 * @param Request $request
20 *
21 * @Route("/new-entry", name="new_entry")
22 *
23 * @return \Symfony\Component\HttpFoundation\Response
24 */
25 public function addEntryFormAction(Request $request)
26 {
27 $entry = new Entry($this->getUser());
28
29 $form = $this->createForm(new NewEntryType(), $entry);
30
31 $form->handleRequest($request);
32
33 if ($form->isValid()) {
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 }
42
43 $em = $this->getDoctrine()->getManager();
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
55 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array(
56 'form' => $form->createView(),
57 ));
58 }
59
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
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
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 {
120 return $this->showEntries('all', $request, $page);
121 }
122
123 /**
124 * Shows unread entries for current user.
125 *
126 * @param Request $request
127 * @param int $page
128 *
129 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
130 *
131 * @return \Symfony\Component\HttpFoundation\Response
132 */
133 public function showUnreadAction(Request $request, $page)
134 {
135 return $this->showEntries('unread', $request, $page);
136 }
137
138 /**
139 * Shows read entries for current user.
140 *
141 * @param Request $request
142 * @param int $page
143 *
144 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
145 *
146 * @return \Symfony\Component\HttpFoundation\Response
147 */
148 public function showArchiveAction(Request $request, $page)
149 {
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 }
167
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
195 case 'all':
196 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
197 break;
198
199 default:
200 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
201 }
202
203 $form = $this->get('form.factory')->create(new EntryFilterType());
204
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
210 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
211 }
212
213 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
214 $entries = new Pagerfanta($pagerAdapter);
215
216 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
217 $entries->setCurrentPage($page);
218
219 return $this->render(
220 'WallabagCoreBundle:Entry:entries.html.twig',
221 array(
222 'form' => $form->createView(),
223 'entries' => $entries,
224 'currentPage' => $page,
225 )
226 );
227 }
228
229 /**
230 * Shows entry content.
231 *
232 * @param Entry $entry
233 *
234 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
235 *
236 * @return \Symfony\Component\HttpFoundation\Response
237 */
238 public function viewAction(Entry $entry)
239 {
240 $this->checkUserAction($entry);
241
242 return $this->render(
243 'WallabagCoreBundle:Entry:entry.html.twig',
244 array('entry' => $entry)
245 );
246 }
247
248 /**
249 * Changes read status for an entry.
250 *
251 * @param Request $request
252 * @param Entry $entry
253 *
254 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
255 *
256 * @return \Symfony\Component\HttpFoundation\RedirectResponse
257 */
258 public function toggleArchiveAction(Request $request, Entry $entry)
259 {
260 $this->checkUserAction($entry);
261
262 $entry->toggleArchive();
263 $this->getDoctrine()->getManager()->flush();
264
265 $this->get('session')->getFlashBag()->add(
266 'notice',
267 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived')
268 );
269
270 return $this->redirect($request->headers->get('referer'));
271 }
272
273 /**
274 * Changes favorite status for an entry.
275 *
276 * @param Request $request
277 * @param Entry $entry
278 *
279 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
280 *
281 * @return \Symfony\Component\HttpFoundation\RedirectResponse
282 */
283 public function toggleStarAction(Request $request, Entry $entry)
284 {
285 $this->checkUserAction($entry);
286
287 $entry->toggleStar();
288 $this->getDoctrine()->getManager()->flush();
289
290 $this->get('session')->getFlashBag()->add(
291 'notice',
292 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred')
293 );
294
295 return $this->redirect($request->headers->get('referer'));
296 }
297
298 /**
299 * Deletes entry and redirect to the homepage.
300 *
301 * @param Entry $entry
302 *
303 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
304 *
305 * @return \Symfony\Component\HttpFoundation\RedirectResponse
306 */
307 public function deleteEntryAction(Entry $entry)
308 {
309 $this->checkUserAction($entry);
310
311 $em = $this->getDoctrine()->getManager();
312 $em->remove($entry);
313 $em->flush();
314
315 $this->get('session')->getFlashBag()->add(
316 'notice',
317 'Entry deleted'
318 );
319
320 return $this->redirect($this->generateUrl('homepage'));
321 }
322
323 /**
324 * Check if the logged user can manage the given entry.
325 *
326 * @param Entry $entry
327 */
328 private function checkUserAction(Entry $entry)
329 {
330 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
331 throw $this->createAccessDeniedException('You can not access this entry.');
332 }
333 }
334 }