]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
5 | use Pagerfanta\Adapter\DoctrineORMAdapter; | |
6 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; | |
7 | use Pagerfanta\Pagerfanta; | |
8 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
9 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
10 | use Symfony\Component\HttpFoundation\Request; | |
11 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
12 | use Wallabag\CoreBundle\Entity\Entry; | |
13 | use Wallabag\CoreBundle\Form\Type\EntryFilterType; | |
14 | use Wallabag\CoreBundle\Form\Type\EditEntryType; | |
15 | use Wallabag\CoreBundle\Form\Type\NewEntryType; | |
16 | ||
17 | class EntryController extends Controller | |
18 | { | |
19 | /** | |
20 | * @param Entry $entry | |
21 | */ | |
22 | private function updateEntry(Entry $entry) | |
23 | { | |
24 | try { | |
25 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | |
26 | $em = $this->getDoctrine()->getManager(); | |
27 | $em->persist($entry); | |
28 | $em->flush(); | |
29 | } catch (\Exception $e) { | |
30 | return false; | |
31 | } | |
32 | ||
33 | return true; | |
34 | } | |
35 | ||
36 | /** | |
37 | * @param Request $request | |
38 | * | |
39 | * @Route("/new-entry", name="new_entry") | |
40 | * | |
41 | * @return \Symfony\Component\HttpFoundation\Response | |
42 | */ | |
43 | public function addEntryFormAction(Request $request) | |
44 | { | |
45 | $entry = new Entry($this->getUser()); | |
46 | ||
47 | $form = $this->createForm(NewEntryType::class, $entry); | |
48 | ||
49 | $form->handleRequest($request); | |
50 | ||
51 | if ($form->isValid()) { | |
52 | // check for existing entry, if it exists, redirect to it with a message | |
53 | $existingEntry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId()); | |
54 | ||
55 | if (false !== $existingEntry) { | |
56 | $this->get('session')->getFlashBag()->add( | |
57 | 'notice', | |
58 | 'Entry already saved on '.$existingEntry->getCreatedAt()->format('d-m-Y') | |
59 | ); | |
60 | ||
61 | return $this->redirect($this->generateUrl('view', array('id' => $existingEntry->getId()))); | |
62 | } | |
63 | ||
64 | $this->updateEntry($entry); | |
65 | $this->get('session')->getFlashBag()->add( | |
66 | 'notice', | |
67 | 'Entry saved' | |
68 | ); | |
69 | ||
70 | return $this->redirect($this->generateUrl('homepage')); | |
71 | } | |
72 | ||
73 | return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array( | |
74 | 'form' => $form->createView(), | |
75 | )); | |
76 | } | |
77 | ||
78 | /** | |
79 | * @param Request $request | |
80 | * | |
81 | * @Route("/bookmarklet", name="bookmarklet") | |
82 | * | |
83 | * @return \Symfony\Component\HttpFoundation\Response | |
84 | */ | |
85 | public function addEntryViaBookmarkletAction(Request $request) | |
86 | { | |
87 | $entry = new Entry($this->getUser()); | |
88 | $entry->setUrl($request->get('url')); | |
89 | $this->updateEntry($entry); | |
90 | ||
91 | return $this->redirect($this->generateUrl('homepage')); | |
92 | } | |
93 | ||
94 | /** | |
95 | * @param Request $request | |
96 | * | |
97 | * @Route("/new", name="new") | |
98 | * | |
99 | * @return \Symfony\Component\HttpFoundation\Response | |
100 | */ | |
101 | public function addEntryAction(Request $request) | |
102 | { | |
103 | return $this->render('WallabagCoreBundle:Entry:new.html.twig'); | |
104 | } | |
105 | ||
106 | /** | |
107 | * Edit an entry content. | |
108 | * | |
109 | * @param Request $request | |
110 | * @param Entry $entry | |
111 | * | |
112 | * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit") | |
113 | * | |
114 | * @return \Symfony\Component\HttpFoundation\Response | |
115 | */ | |
116 | public function editEntryAction(Request $request, Entry $entry) | |
117 | { | |
118 | $this->checkUserAction($entry); | |
119 | ||
120 | $form = $this->createForm(EditEntryType::class, $entry); | |
121 | ||
122 | $form->handleRequest($request); | |
123 | ||
124 | if ($form->isValid()) { | |
125 | $em = $this->getDoctrine()->getManager(); | |
126 | $em->persist($entry); | |
127 | $em->flush(); | |
128 | ||
129 | $this->get('session')->getFlashBag()->add( | |
130 | 'notice', | |
131 | 'Entry updated' | |
132 | ); | |
133 | ||
134 | return $this->redirect($this->generateUrl('view', array('id' => $entry->getId()))); | |
135 | } | |
136 | ||
137 | return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array( | |
138 | 'form' => $form->createView(), | |
139 | )); | |
140 | } | |
141 | ||
142 | /** | |
143 | * Shows all entries for current user. | |
144 | * | |
145 | * @param Request $request | |
146 | * @param int $page | |
147 | * | |
148 | * @Route("/all/list/{page}", name="all", defaults={"page" = "1"}) | |
149 | * | |
150 | * @return \Symfony\Component\HttpFoundation\Response | |
151 | */ | |
152 | public function showAllAction(Request $request, $page) | |
153 | { | |
154 | return $this->showEntries('all', $request, $page); | |
155 | } | |
156 | ||
157 | /** | |
158 | * Shows unread entries for current user. | |
159 | * | |
160 | * @param Request $request | |
161 | * @param int $page | |
162 | * | |
163 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) | |
164 | * | |
165 | * @return \Symfony\Component\HttpFoundation\Response | |
166 | */ | |
167 | public function showUnreadAction(Request $request, $page) | |
168 | { | |
169 | // load the quickstart if no entry in database | |
170 | if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUsername($this->getUser()->getId()) == 0) { | |
171 | return $this->redirect($this->generateUrl('quickstart')); | |
172 | } | |
173 | ||
174 | return $this->showEntries('unread', $request, $page); | |
175 | } | |
176 | ||
177 | /** | |
178 | * Shows read entries for current user. | |
179 | * | |
180 | * @param Request $request | |
181 | * @param int $page | |
182 | * | |
183 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) | |
184 | * | |
185 | * @return \Symfony\Component\HttpFoundation\Response | |
186 | */ | |
187 | public function showArchiveAction(Request $request, $page) | |
188 | { | |
189 | return $this->showEntries('archive', $request, $page); | |
190 | } | |
191 | ||
192 | /** | |
193 | * Shows starred entries for current user. | |
194 | * | |
195 | * @param Request $request | |
196 | * @param int $page | |
197 | * | |
198 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) | |
199 | * | |
200 | * @return \Symfony\Component\HttpFoundation\Response | |
201 | */ | |
202 | public function showStarredAction(Request $request, $page) | |
203 | { | |
204 | return $this->showEntries('starred', $request, $page); | |
205 | } | |
206 | ||
207 | /** | |
208 | * Global method to retrieve entries depending on the given type | |
209 | * It returns the response to be send. | |
210 | * | |
211 | * @param string $type Entries type: unread, starred or archive | |
212 | * @param Request $request | |
213 | * @param int $page | |
214 | * | |
215 | * @return \Symfony\Component\HttpFoundation\Response | |
216 | */ | |
217 | private function showEntries($type, Request $request, $page) | |
218 | { | |
219 | $repository = $this->get('wallabag_core.entry_repository'); | |
220 | ||
221 | switch ($type) { | |
222 | case 'starred': | |
223 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); | |
224 | break; | |
225 | ||
226 | case 'archive': | |
227 | $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); | |
228 | break; | |
229 | ||
230 | case 'unread': | |
231 | $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); | |
232 | break; | |
233 | ||
234 | case 'all': | |
235 | $qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); | |
236 | break; | |
237 | ||
238 | default: | |
239 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
240 | } | |
241 | ||
242 | $form = $this->createForm(EntryFilterType::class); | |
243 | ||
244 | if ($request->query->has($form->getName())) { | |
245 | // manually bind values from the request | |
246 | $form->submit($request->query->get($form->getName())); | |
247 | ||
248 | // build the query from the given form object | |
249 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); | |
250 | } | |
251 | ||
252 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); | |
253 | $entries = new Pagerfanta($pagerAdapter); | |
254 | ||
255 | $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); | |
256 | try { | |
257 | $entries->setCurrentPage($page); | |
258 | } catch (OutOfRangeCurrentPageException $e) { | |
259 | if ($page > 1) { | |
260 | return $this->redirect($this->generateUrl($type, array('page' => $entries->getNbPages())), 302); | |
261 | } | |
262 | } | |
263 | ||
264 | return $this->render( | |
265 | 'WallabagCoreBundle:Entry:entries.html.twig', | |
266 | array( | |
267 | 'form' => $form->createView(), | |
268 | 'entries' => $entries, | |
269 | 'currentPage' => $page, | |
270 | ) | |
271 | ); | |
272 | } | |
273 | ||
274 | /** | |
275 | * Shows entry content. | |
276 | * | |
277 | * @param Entry $entry | |
278 | * | |
279 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") | |
280 | * | |
281 | * @return \Symfony\Component\HttpFoundation\Response | |
282 | */ | |
283 | public function viewAction(Entry $entry) | |
284 | { | |
285 | $this->checkUserAction($entry); | |
286 | ||
287 | return $this->render( | |
288 | 'WallabagCoreBundle:Entry:entry.html.twig', | |
289 | array('entry' => $entry) | |
290 | ); | |
291 | } | |
292 | ||
293 | /** | |
294 | * Reload an entry. | |
295 | * Refetch content from the website and make it readable again. | |
296 | * | |
297 | * @param Entry $entry | |
298 | * | |
299 | * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry") | |
300 | * | |
301 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
302 | */ | |
303 | public function reloadAction(Entry $entry) | |
304 | { | |
305 | $this->checkUserAction($entry); | |
306 | ||
307 | $message = 'Entry reloaded'; | |
308 | if (false === $this->updateEntry($entry)) { | |
309 | $message = 'Failed to reload entry'; | |
310 | } | |
311 | ||
312 | $this->get('session')->getFlashBag()->add( | |
313 | 'notice', | |
314 | $message | |
315 | ); | |
316 | ||
317 | return $this->redirect($this->generateUrl('view', array('id' => $entry->getId()))); | |
318 | } | |
319 | ||
320 | /** | |
321 | * Changes read status for an entry. | |
322 | * | |
323 | * @param Request $request | |
324 | * @param Entry $entry | |
325 | * | |
326 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") | |
327 | * | |
328 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
329 | */ | |
330 | public function toggleArchiveAction(Request $request, Entry $entry) | |
331 | { | |
332 | $this->checkUserAction($entry); | |
333 | ||
334 | $entry->toggleArchive(); | |
335 | $this->getDoctrine()->getManager()->flush(); | |
336 | ||
337 | $this->get('session')->getFlashBag()->add( | |
338 | 'notice', | |
339 | 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived') | |
340 | ); | |
341 | ||
342 | return $this->redirect($request->headers->get('referer')); | |
343 | } | |
344 | ||
345 | /** | |
346 | * Changes favorite status for an entry. | |
347 | * | |
348 | * @param Request $request | |
349 | * @param Entry $entry | |
350 | * | |
351 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") | |
352 | * | |
353 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
354 | */ | |
355 | public function toggleStarAction(Request $request, Entry $entry) | |
356 | { | |
357 | $this->checkUserAction($entry); | |
358 | ||
359 | $entry->toggleStar(); | |
360 | $this->getDoctrine()->getManager()->flush(); | |
361 | ||
362 | $this->get('session')->getFlashBag()->add( | |
363 | 'notice', | |
364 | 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred') | |
365 | ); | |
366 | ||
367 | return $this->redirect($request->headers->get('referer')); | |
368 | } | |
369 | ||
370 | /** | |
371 | * Deletes entry and redirect to the homepage or the last viewed page. | |
372 | * | |
373 | * @param Entry $entry | |
374 | * | |
375 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") | |
376 | * | |
377 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
378 | */ | |
379 | public function deleteEntryAction(Request $request, Entry $entry) | |
380 | { | |
381 | $this->checkUserAction($entry); | |
382 | ||
383 | // generates the view url for this entry to check for redirection later | |
384 | // to avoid redirecting to the deleted entry. Ugh. | |
385 | $url = $this->generateUrl( | |
386 | 'view', | |
387 | array('id' => $entry->getId()), | |
388 | UrlGeneratorInterface::ABSOLUTE_URL | |
389 | ); | |
390 | ||
391 | $em = $this->getDoctrine()->getManager(); | |
392 | $em->remove($entry); | |
393 | $em->flush(); | |
394 | ||
395 | $this->get('session')->getFlashBag()->add( | |
396 | 'notice', | |
397 | 'Entry deleted' | |
398 | ); | |
399 | ||
400 | // don't redirect user to the deleted entry | |
401 | return $this->redirect($url !== $request->headers->get('referer') ? $request->headers->get('referer') : $this->generateUrl('homepage')); | |
402 | } | |
403 | ||
404 | /** | |
405 | * Check if the logged user can manage the given entry. | |
406 | * | |
407 | * @param Entry $entry | |
408 | */ | |
409 | private function checkUserAction(Entry $entry) | |
410 | { | |
411 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | |
412 | throw $this->createAccessDeniedException('You can not access this entry.'); | |
413 | } | |
414 | } | |
415 | } |