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