]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
5 | use Pagerfanta\Adapter\DoctrineORMAdapter; | |
6 | use Pagerfanta\Pagerfanta; | |
7 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
8 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
9 | use Symfony\Component\HttpFoundation\Request; | |
10 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
11 | use Wallabag\CoreBundle\Entity\Entry; | |
12 | use Wallabag\CoreBundle\Filter\EntryFilterType; | |
13 | use Wallabag\CoreBundle\Form\Type\EditEntryType; | |
14 | use Wallabag\CoreBundle\Form\Type\NewEntryType; | |
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(NewEntryType::class, $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(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 | return $this->showEntries('unread', $request, $page); | |
170 | } | |
171 | ||
172 | /** | |
173 | * Shows read entries for current user. | |
174 | * | |
175 | * @param Request $request | |
176 | * @param int $page | |
177 | * | |
178 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) | |
179 | * | |
180 | * @return \Symfony\Component\HttpFoundation\Response | |
181 | */ | |
182 | public function showArchiveAction(Request $request, $page) | |
183 | { | |
184 | return $this->showEntries('archive', $request, $page); | |
185 | } | |
186 | ||
187 | /** | |
188 | * Shows starred entries for current user. | |
189 | * | |
190 | * @param Request $request | |
191 | * @param int $page | |
192 | * | |
193 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) | |
194 | * | |
195 | * @return \Symfony\Component\HttpFoundation\Response | |
196 | */ | |
197 | public function showStarredAction(Request $request, $page) | |
198 | { | |
199 | return $this->showEntries('starred', $request, $page); | |
200 | } | |
201 | ||
202 | /** | |
203 | * Global method to retrieve entries depending on the given type | |
204 | * It returns the response to be send. | |
205 | * | |
206 | * @param string $type Entries type: unread, starred or archive | |
207 | * @param Request $request | |
208 | * @param int $page | |
209 | * | |
210 | * @return \Symfony\Component\HttpFoundation\Response | |
211 | */ | |
212 | private function showEntries($type, Request $request, $page) | |
213 | { | |
214 | $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry'); | |
215 | ||
216 | switch ($type) { | |
217 | case 'starred': | |
218 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); | |
219 | break; | |
220 | ||
221 | case 'archive': | |
222 | $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); | |
223 | break; | |
224 | ||
225 | case 'unread': | |
226 | $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); | |
227 | break; | |
228 | ||
229 | case 'all': | |
230 | $qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); | |
231 | break; | |
232 | ||
233 | default: | |
234 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
235 | } | |
236 | ||
237 | $form = $this->createForm(new EntryFilterType($repository, $this->getUser())); | |
238 | ||
239 | if ($request->query->has($form->getName())) { | |
240 | // manually bind values from the request | |
241 | $form->submit($request->query->get($form->getName())); | |
242 | ||
243 | // build the query from the given form object | |
244 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); | |
245 | } | |
246 | ||
247 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); | |
248 | $entries = new Pagerfanta($pagerAdapter); | |
249 | ||
250 | $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); | |
251 | $entries->setCurrentPage($page); | |
252 | ||
253 | return $this->render( | |
254 | 'WallabagCoreBundle:Entry:entries.html.twig', | |
255 | array( | |
256 | 'form' => $form->createView(), | |
257 | 'entries' => $entries, | |
258 | 'currentPage' => $page, | |
259 | ) | |
260 | ); | |
261 | } | |
262 | ||
263 | /** | |
264 | * Shows entry content. | |
265 | * | |
266 | * @param Entry $entry | |
267 | * | |
268 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") | |
269 | * | |
270 | * @return \Symfony\Component\HttpFoundation\Response | |
271 | */ | |
272 | public function viewAction(Entry $entry) | |
273 | { | |
274 | $this->checkUserAction($entry); | |
275 | ||
276 | return $this->render( | |
277 | 'WallabagCoreBundle:Entry:entry.html.twig', | |
278 | array('entry' => $entry) | |
279 | ); | |
280 | } | |
281 | ||
282 | /** | |
283 | * Reload an entry. | |
284 | * Refetch content from the website and make it readable again. | |
285 | * | |
286 | * @param Entry $entry | |
287 | * | |
288 | * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry") | |
289 | * | |
290 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
291 | */ | |
292 | public function reloadAction(Entry $entry) | |
293 | { | |
294 | $this->checkUserAction($entry); | |
295 | ||
296 | $message = 'Entry reloaded'; | |
297 | if (false === $this->updateEntry($entry)) { | |
298 | $message = 'Failed to reload entry'; | |
299 | } | |
300 | ||
301 | $this->get('session')->getFlashBag()->add( | |
302 | 'notice', | |
303 | $message | |
304 | ); | |
305 | ||
306 | return $this->redirect($this->generateUrl('view', array('id' => $entry->getId()))); | |
307 | } | |
308 | ||
309 | /** | |
310 | * Changes read status for an entry. | |
311 | * | |
312 | * @param Request $request | |
313 | * @param Entry $entry | |
314 | * | |
315 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") | |
316 | * | |
317 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
318 | */ | |
319 | public function toggleArchiveAction(Request $request, Entry $entry) | |
320 | { | |
321 | $this->checkUserAction($entry); | |
322 | ||
323 | $entry->toggleArchive(); | |
324 | $this->getDoctrine()->getManager()->flush(); | |
325 | ||
326 | $this->get('session')->getFlashBag()->add( | |
327 | 'notice', | |
328 | 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived') | |
329 | ); | |
330 | ||
331 | return $this->redirect($request->headers->get('referer')); | |
332 | } | |
333 | ||
334 | /** | |
335 | * Changes favorite status for an entry. | |
336 | * | |
337 | * @param Request $request | |
338 | * @param Entry $entry | |
339 | * | |
340 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") | |
341 | * | |
342 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
343 | */ | |
344 | public function toggleStarAction(Request $request, Entry $entry) | |
345 | { | |
346 | $this->checkUserAction($entry); | |
347 | ||
348 | $entry->toggleStar(); | |
349 | $this->getDoctrine()->getManager()->flush(); | |
350 | ||
351 | $this->get('session')->getFlashBag()->add( | |
352 | 'notice', | |
353 | 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred') | |
354 | ); | |
355 | ||
356 | return $this->redirect($request->headers->get('referer')); | |
357 | } | |
358 | ||
359 | /** | |
360 | * Deletes entry and redirect to the homepage or the last viewed page. | |
361 | * | |
362 | * @param Entry $entry | |
363 | * | |
364 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") | |
365 | * | |
366 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
367 | */ | |
368 | public function deleteEntryAction(Request $request, Entry $entry) | |
369 | { | |
370 | $this->checkUserAction($entry); | |
371 | ||
372 | // generates the view url for this entry to check for redirection later | |
373 | // to avoid redirecting to the deleted entry. Ugh. | |
374 | $url = $this->generateUrl( | |
375 | 'view', | |
376 | array('id' => $entry->getId()), | |
377 | UrlGeneratorInterface::ABSOLUTE_URL | |
378 | ); | |
379 | ||
380 | $em = $this->getDoctrine()->getManager(); | |
381 | $em->remove($entry); | |
382 | $em->flush(); | |
383 | ||
384 | $this->get('session')->getFlashBag()->add( | |
385 | 'notice', | |
386 | 'Entry deleted' | |
387 | ); | |
388 | ||
389 | // don't redirect user to the deleted entry | |
390 | return $this->redirect($url !== $request->headers->get('referer') ? $request->headers->get('referer') : $this->generateUrl('homepage')); | |
391 | } | |
392 | ||
393 | /** | |
394 | * Check if the logged user can manage the given entry. | |
395 | * | |
396 | * @param Entry $entry | |
397 | */ | |
398 | private function checkUserAction(Entry $entry) | |
399 | { | |
400 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | |
401 | throw $this->createAccessDeniedException('You can not access this entry.'); | |
402 | } | |
403 | } | |
404 | } |