]>
Commit | Line | Data |
---|---|---|
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 Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
9 | use Wallabag\CoreBundle\Entity\Entry; | |
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 Entry $entry | |
20 | */ | |
21 | private function updateEntry(Entry $entry) | |
22 | { | |
23 | try { | |
24 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | |
25 | $em = $this->getDoctrine()->getManager(); | |
26 | $em->persist($entry); | |
27 | $em->flush(); | |
28 | } catch (\Exception $e) { | |
29 | return false; | |
30 | } | |
31 | ||
32 | return true; | |
33 | } | |
34 | ||
35 | /** | |
36 | * @param Request $request | |
37 | * | |
38 | * @Route("/new-entry", name="new_entry") | |
39 | * | |
40 | * @return \Symfony\Component\HttpFoundation\Response | |
41 | */ | |
42 | public function addEntryFormAction(Request $request) | |
43 | { | |
44 | $entry = new Entry($this->getUser()); | |
45 | ||
46 | $form = $this->createForm(new NewEntryType(), $entry); | |
47 | ||
48 | $form->handleRequest($request); | |
49 | ||
50 | if ($form->isValid()) { | |
51 | // check for existing entry, if it exists, redirect to it with a message | |
52 | $existingEntry = $this->get('wallabag_core.entry_repository') | |
53 | ->existByUrlAndUserId($entry->getUrl(), $this->getUser()->getId()); | |
54 | ||
55 | if (false !== $existingEntry) { | |
56 | $this->get('session')->getFlashBag()->add( | |
57 | 'notice', | |
58 | 'Entry already saved on '.$existingEntry['createdAt']->format('d-m-Y') | |
59 | ); | |
60 | ||
61 | return $this->redirect($this->generateUrl('view', array('id' => $existingEntry['id']))); | |
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 addEntryViaBookmarklet(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(new EditEntryType(), $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->get('form.factory')->create(new EntryFilterType($repository, $this->getUser())); | |
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 | $entries->setCurrentPage($page); | |
257 | ||
258 | return $this->render( | |
259 | 'WallabagCoreBundle:Entry:entries.html.twig', | |
260 | array( | |
261 | 'form' => $form->createView(), | |
262 | 'entries' => $entries, | |
263 | 'currentPage' => $page, | |
264 | ) | |
265 | ); | |
266 | } | |
267 | ||
268 | /** | |
269 | * Shows entry content. | |
270 | * | |
271 | * @param Entry $entry | |
272 | * | |
273 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") | |
274 | * | |
275 | * @return \Symfony\Component\HttpFoundation\Response | |
276 | */ | |
277 | public function viewAction(Entry $entry) | |
278 | { | |
279 | $this->checkUserAction($entry); | |
280 | ||
281 | return $this->render( | |
282 | 'WallabagCoreBundle:Entry:entry.html.twig', | |
283 | array('entry' => $entry) | |
284 | ); | |
285 | } | |
286 | ||
287 | /** | |
288 | * Reload an entry. | |
289 | * Refetch content from the website and make it readable again. | |
290 | * | |
291 | * @param Entry $entry | |
292 | * | |
293 | * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry") | |
294 | * | |
295 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
296 | */ | |
297 | public function reloadAction(Entry $entry) | |
298 | { | |
299 | $this->checkUserAction($entry); | |
300 | ||
301 | $message = 'Entry reloaded'; | |
302 | if (false === $this->updateEntry($entry)) { | |
303 | $message = 'Failed to reload entry'; | |
304 | } | |
305 | ||
306 | $this->get('session')->getFlashBag()->add( | |
307 | 'notice', | |
308 | $message | |
309 | ); | |
310 | ||
311 | return $this->redirect($this->generateUrl('view', array('id' => $entry->getId()))); | |
312 | } | |
313 | ||
314 | /** | |
315 | * Changes read status for an entry. | |
316 | * | |
317 | * @param Request $request | |
318 | * @param Entry $entry | |
319 | * | |
320 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") | |
321 | * | |
322 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
323 | */ | |
324 | public function toggleArchiveAction(Request $request, Entry $entry) | |
325 | { | |
326 | $this->checkUserAction($entry); | |
327 | ||
328 | $entry->toggleArchive(); | |
329 | $this->getDoctrine()->getManager()->flush(); | |
330 | ||
331 | $this->get('session')->getFlashBag()->add( | |
332 | 'notice', | |
333 | 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived') | |
334 | ); | |
335 | ||
336 | return $this->redirect($request->headers->get('referer')); | |
337 | } | |
338 | ||
339 | /** | |
340 | * Changes favorite status for an entry. | |
341 | * | |
342 | * @param Request $request | |
343 | * @param Entry $entry | |
344 | * | |
345 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") | |
346 | * | |
347 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
348 | */ | |
349 | public function toggleStarAction(Request $request, Entry $entry) | |
350 | { | |
351 | $this->checkUserAction($entry); | |
352 | ||
353 | $entry->toggleStar(); | |
354 | $this->getDoctrine()->getManager()->flush(); | |
355 | ||
356 | $this->get('session')->getFlashBag()->add( | |
357 | 'notice', | |
358 | 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred') | |
359 | ); | |
360 | ||
361 | return $this->redirect($request->headers->get('referer')); | |
362 | } | |
363 | ||
364 | /** | |
365 | * Deletes entry and redirect to the homepage or the last viewed page. | |
366 | * | |
367 | * @param Entry $entry | |
368 | * | |
369 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") | |
370 | * | |
371 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
372 | */ | |
373 | public function deleteEntryAction(Request $request, Entry $entry) | |
374 | { | |
375 | $this->checkUserAction($entry); | |
376 | ||
377 | // generates the view url for this entry to check for redirection later | |
378 | // to avoid redirecting to the deleted entry. Ugh. | |
379 | $url = $this->generateUrl( | |
380 | 'view', | |
381 | array('id' => $entry->getId()), | |
382 | UrlGeneratorInterface::ABSOLUTE_URL | |
383 | ); | |
384 | ||
385 | $em = $this->getDoctrine()->getManager(); | |
386 | $em->remove($entry); | |
387 | $em->flush(); | |
388 | ||
389 | $this->get('session')->getFlashBag()->add( | |
390 | 'notice', | |
391 | 'Entry deleted' | |
392 | ); | |
393 | ||
394 | // don't redirect user to the deleted entry | |
395 | return $this->redirect($url !== $request->headers->get('referer') ? $request->headers->get('referer') : $this->generateUrl('homepage')); | |
396 | } | |
397 | ||
398 | /** | |
399 | * Check if the logged user can manage the given entry. | |
400 | * | |
401 | * @param Entry $entry | |
402 | */ | |
403 | private function checkUserAction(Entry $entry) | |
404 | { | |
405 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | |
406 | throw $this->createAccessDeniedException('You can not access this entry.'); | |
407 | } | |
408 | } | |
409 | } |