]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Controller; |
9d50517c | 4 | |
619cc453 | 5 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
671a2b88 | 6 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; |
5847dd35 | 7 | use Pagerfanta\Pagerfanta; |
9d50517c NL |
8 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
9 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
5847dd35 | 10 | use Symfony\Component\HttpFoundation\RedirectResponse; |
163eae0b | 11 | use Symfony\Component\HttpFoundation\Request; |
5847dd35 | 12 | use Symfony\Component\HttpFoundation\Response; |
2863bf2a | 13 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
be463487 | 14 | use Wallabag\CoreBundle\Entity\Entry; |
5847dd35 | 15 | use Wallabag\CoreBundle\Form\Type\EditGroupSharesType; |
b0b893ea | 16 | use Wallabag\CoreBundle\Form\Type\EntryFilterType; |
619cc453 JB |
17 | use Wallabag\CoreBundle\Form\Type\EditEntryType; |
18 | use Wallabag\CoreBundle\Form\Type\NewEntryType; | |
222e09f1 | 19 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; |
e0597476 JB |
20 | use Wallabag\CoreBundle\Event\EntrySavedEvent; |
21 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; | |
ee122a75 | 22 | use Wallabag\CoreBundle\Form\Type\SearchEntryType; |
5847dd35 | 23 | use Wallabag\GroupBundle\Entity\Group; |
9d50517c NL |
24 | |
25 | class EntryController extends Controller | |
26 | { | |
ee122a75 NL |
27 | /** |
28 | * @param Request $request | |
29 | * @param int $page | |
30 | * | |
21e7ccef JB |
31 | * @Route("/search/{page}", name="search", defaults={"page" = 1}) |
32 | * | |
33 | * Default parameter for page is hardcoded (in duplication of the defaults from the Route) | |
34 | * because this controller is also called inside the layout template without any page as argument | |
ee122a75 NL |
35 | * |
36 | * @return \Symfony\Component\HttpFoundation\Response | |
37 | */ | |
21e7ccef | 38 | public function searchFormAction(Request $request, $page = 1, $currentRoute = null) |
ee122a75 | 39 | { |
21e7ccef JB |
40 | // fallback to retrieve currentRoute from query parameter instead of injected one (when using inside a template) |
41 | if (null === $currentRoute && $request->query->has('currentRoute')) { | |
42 | $currentRoute = $request->query->get('currentRoute'); | |
43 | } | |
44 | ||
ee122a75 NL |
45 | $form = $this->createForm(SearchEntryType::class); |
46 | ||
47 | $form->handleRequest($request); | |
48 | ||
21e7ccef | 49 | if ($form->isSubmitted() && $form->isValid()) { |
ee122a75 NL |
50 | return $this->showEntries('search', $request, $page); |
51 | } | |
52 | ||
53 | return $this->render('WallabagCoreBundle:Entry:search_form.html.twig', [ | |
54 | 'form' => $form->createView(), | |
49b042df | 55 | 'currentRoute' => $currentRoute, |
ee122a75 NL |
56 | ]); |
57 | } | |
58 | ||
880a0e1c | 59 | /** |
59b97fae | 60 | * Fetch content and update entry. |
7aba665e | 61 | * In case it fails, $entry->getContent will return an error message. |
59b97fae JB |
62 | * |
63 | * @param Entry $entry | |
64 | * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded | |
880a0e1c | 65 | */ |
59b97fae | 66 | private function updateEntry(Entry $entry, $prefixMessage = 'entry_saved') |
880a0e1c | 67 | { |
59b97fae JB |
68 | $message = 'flashes.entry.notice.'.$prefixMessage; |
69 | ||
880a0e1c | 70 | try { |
7aba665e | 71 | $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); |
880a0e1c | 72 | } catch (\Exception $e) { |
39ba51ca JB |
73 | $this->get('logger')->error('Error while saving an entry', [ |
74 | 'exception' => $e, | |
75 | 'entry' => $entry, | |
76 | ]); | |
77 | ||
59b97fae | 78 | $message = 'flashes.entry.notice.'.$prefixMessage.'_failed'; |
880a0e1c NL |
79 | } |
80 | ||
59b97fae | 81 | $this->get('session')->getFlashBag()->add('notice', $message); |
880a0e1c NL |
82 | } |
83 | ||
b84a8055 | 84 | /** |
3d2b2d62 J |
85 | * @param Request $request |
86 | * | |
053b9568 | 87 | * @Route("/new-entry", name="new_entry") |
3d2b2d62 | 88 | * |
b84a8055 NL |
89 | * @return \Symfony\Component\HttpFoundation\Response |
90 | */ | |
053b9568 | 91 | public function addEntryFormAction(Request $request) |
b84a8055 | 92 | { |
3b815d2d | 93 | $entry = new Entry($this->getUser()); |
b84a8055 | 94 | |
5c895a7f | 95 | $form = $this->createForm(NewEntryType::class, $entry); |
b84a8055 NL |
96 | |
97 | $form->handleRequest($request); | |
98 | ||
21e7ccef | 99 | if ($form->isSubmitted() && $form->isValid()) { |
b00a89e0 | 100 | $existingEntry = $this->checkIfEntryAlreadyExists($entry); |
dda57bb9 | 101 | |
5a4bbcc9 | 102 | if (false !== $existingEntry) { |
dda57bb9 NL |
103 | $this->get('session')->getFlashBag()->add( |
104 | 'notice', | |
4094ea47 | 105 | $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')]) |
dda57bb9 NL |
106 | ); |
107 | ||
4094ea47 | 108 | return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()])); |
dda57bb9 NL |
109 | } |
110 | ||
59b97fae | 111 | $this->updateEntry($entry); |
39ba51ca | 112 | |
59b97fae JB |
113 | $em = $this->getDoctrine()->getManager(); |
114 | $em->persist($entry); | |
115 | $em->flush(); | |
b84a8055 | 116 | |
e0597476 JB |
117 | // entry saved, dispatch event about it! |
118 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
119 | ||
b84a8055 NL |
120 | return $this->redirect($this->generateUrl('homepage')); |
121 | } | |
122 | ||
4094ea47 | 123 | return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', [ |
b84a8055 | 124 | 'form' => $form->createView(), |
4094ea47 | 125 | ]); |
b84a8055 NL |
126 | } |
127 | ||
880a0e1c NL |
128 | /** |
129 | * @param Request $request | |
130 | * | |
131 | * @Route("/bookmarklet", name="bookmarklet") | |
132 | * | |
133 | * @return \Symfony\Component\HttpFoundation\Response | |
134 | */ | |
5f8a7857 | 135 | public function addEntryViaBookmarkletAction(Request $request) |
880a0e1c NL |
136 | { |
137 | $entry = new Entry($this->getUser()); | |
138 | $entry->setUrl($request->get('url')); | |
f652f41d | 139 | |
b00a89e0 | 140 | if (false === $this->checkIfEntryAlreadyExists($entry)) { |
f652f41d | 141 | $this->updateEntry($entry); |
59b97fae JB |
142 | |
143 | $em = $this->getDoctrine()->getManager(); | |
144 | $em->persist($entry); | |
145 | $em->flush(); | |
e0597476 JB |
146 | |
147 | // entry saved, dispatch event about it! | |
148 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
f652f41d | 149 | } |
880a0e1c NL |
150 | |
151 | return $this->redirect($this->generateUrl('homepage')); | |
152 | } | |
153 | ||
053b9568 | 154 | /** |
053b9568 NL |
155 | * @Route("/new", name="new") |
156 | * | |
157 | * @return \Symfony\Component\HttpFoundation\Response | |
158 | */ | |
4d0ec0e7 | 159 | public function addEntryAction() |
053b9568 NL |
160 | { |
161 | return $this->render('WallabagCoreBundle:Entry:new.html.twig'); | |
162 | } | |
163 | ||
82d6d9cb JB |
164 | /** |
165 | * Edit an entry content. | |
166 | * | |
167 | * @param Request $request | |
168 | * @param Entry $entry | |
169 | * | |
170 | * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit") | |
171 | * | |
172 | * @return \Symfony\Component\HttpFoundation\Response | |
173 | */ | |
174 | public function editEntryAction(Request $request, Entry $entry) | |
175 | { | |
176 | $this->checkUserAction($entry); | |
177 | ||
5c895a7f | 178 | $form = $this->createForm(EditEntryType::class, $entry); |
82d6d9cb JB |
179 | |
180 | $form->handleRequest($request); | |
181 | ||
21e7ccef | 182 | if ($form->isSubmitted() && $form->isValid()) { |
82d6d9cb JB |
183 | $em = $this->getDoctrine()->getManager(); |
184 | $em->persist($entry); | |
185 | $em->flush(); | |
186 | ||
187 | $this->get('session')->getFlashBag()->add( | |
188 | 'notice', | |
4204a06b | 189 | 'flashes.entry.notice.entry_updated' |
82d6d9cb JB |
190 | ); |
191 | ||
4094ea47 | 192 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); |
82d6d9cb JB |
193 | } |
194 | ||
4094ea47 | 195 | return $this->render('WallabagCoreBundle:Entry:edit.html.twig', [ |
82d6d9cb | 196 | 'form' => $form->createView(), |
4094ea47 | 197 | ]); |
82d6d9cb JB |
198 | } |
199 | ||
89659c9e NL |
200 | /** |
201 | * Shows all entries for current user. | |
202 | * | |
203 | * @param Request $request | |
204 | * @param int $page | |
205 | * | |
206 | * @Route("/all/list/{page}", name="all", defaults={"page" = "1"}) | |
207 | * | |
208 | * @return \Symfony\Component\HttpFoundation\Response | |
209 | */ | |
210 | public function showAllAction(Request $request, $page) | |
211 | { | |
2b7a4889 | 212 | return $this->showEntries('all', $request, $page); |
89659c9e NL |
213 | } |
214 | ||
9d50517c | 215 | /** |
4346a860 | 216 | * Shows unread entries for current user. |
163eae0b | 217 | * |
26864574 NL |
218 | * @param Request $request |
219 | * @param int $page | |
220 | * | |
9fb6ac83 | 221 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) |
3d2b2d62 | 222 | * |
163eae0b | 223 | * @return \Symfony\Component\HttpFoundation\Response |
9d50517c | 224 | */ |
26864574 | 225 | public function showUnreadAction(Request $request, $page) |
9d50517c | 226 | { |
5c072d2b | 227 | // load the quickstart if no entry in database |
273b6f06 | 228 | if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUser($this->getUser()->getId()) == 0) { |
5c072d2b NL |
229 | return $this->redirect($this->generateUrl('quickstart')); |
230 | } | |
231 | ||
0ab7404f | 232 | return $this->showEntries('unread', $request, $page); |
9d50517c | 233 | } |
bd9f0815 NL |
234 | |
235 | /** | |
4346a860 | 236 | * Shows read entries for current user. |
163eae0b | 237 | * |
26864574 NL |
238 | * @param Request $request |
239 | * @param int $page | |
240 | * | |
9fb6ac83 | 241 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) |
3d2b2d62 | 242 | * |
163eae0b | 243 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 | 244 | */ |
26864574 | 245 | public function showArchiveAction(Request $request, $page) |
bd9f0815 | 246 | { |
0ab7404f JB |
247 | return $this->showEntries('archive', $request, $page); |
248 | } | |
249 | ||
250 | /** | |
251 | * Shows starred entries for current user. | |
252 | * | |
253 | * @param Request $request | |
254 | * @param int $page | |
255 | * | |
256 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) | |
257 | * | |
258 | * @return \Symfony\Component\HttpFoundation\Response | |
259 | */ | |
260 | public function showStarredAction(Request $request, $page) | |
261 | { | |
262 | return $this->showEntries('starred', $request, $page); | |
263 | } | |
26864574 | 264 | |
0ab7404f JB |
265 | /** |
266 | * Global method to retrieve entries depending on the given type | |
267 | * It returns the response to be send. | |
268 | * | |
269 | * @param string $type Entries type: unread, starred or archive | |
270 | * @param Request $request | |
271 | * @param int $page | |
272 | * | |
273 | * @return \Symfony\Component\HttpFoundation\Response | |
274 | */ | |
275 | private function showEntries($type, Request $request, $page) | |
276 | { | |
5c072d2b | 277 | $repository = $this->get('wallabag_core.entry_repository'); |
ee122a75 | 278 | $searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : ''); |
6ad8aabb | 279 | $currentRoute = (!is_null($request->query->get('currentRoute')) ? $request->query->get('currentRoute') : ''); |
0ab7404f JB |
280 | |
281 | switch ($type) { | |
ee122a75 | 282 | case 'search': |
49b042df | 283 | $qb = $repository->getBuilderForSearchByUser($this->getUser()->getId(), $searchTerm, $currentRoute); |
ee122a75 NL |
284 | |
285 | break; | |
b6520f0b NL |
286 | case 'untagged': |
287 | $qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId()); | |
288 | ||
289 | break; | |
0ab7404f JB |
290 | case 'starred': |
291 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); | |
292 | break; | |
293 | ||
294 | case 'archive': | |
295 | $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); | |
296 | break; | |
297 | ||
298 | case 'unread': | |
299 | $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); | |
300 | break; | |
301 | ||
2b7a4889 NL |
302 | case 'all': |
303 | $qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); | |
304 | break; | |
305 | ||
0ab7404f JB |
306 | default: |
307 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
308 | } | |
309 | ||
1d76102a | 310 | $form = $this->createForm(EntryFilterType::class); |
9fb6ac83 | 311 | |
26864574 NL |
312 | if ($request->query->has($form->getName())) { |
313 | // manually bind values from the request | |
314 | $form->submit($request->query->get($form->getName())); | |
315 | ||
316 | // build the query from the given form object | |
0ab7404f | 317 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); |
26864574 NL |
318 | } |
319 | ||
9deac0c5 | 320 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false); |
26864574 | 321 | |
53da8ad8 | 322 | $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare($pagerAdapter); |
891456ba | 323 | |
671a2b88 ML |
324 | try { |
325 | $entries->setCurrentPage($page); | |
326 | } catch (OutOfRangeCurrentPageException $e) { | |
327 | if ($page > 1) { | |
4094ea47 | 328 | return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302); |
671a2b88 ML |
329 | } |
330 | } | |
bd9f0815 NL |
331 | |
332 | return $this->render( | |
ee122a75 | 333 | 'WallabagCoreBundle:Entry:entries.html.twig', [ |
8ce32af6 JB |
334 | 'form' => $form->createView(), |
335 | 'entries' => $entries, | |
336 | 'currentPage' => $page, | |
ee122a75 | 337 | 'searchTerm' => $searchTerm, |
4094ea47 | 338 | ] |
bd9f0815 | 339 | ); |
bd9f0815 NL |
340 | } |
341 | ||
342 | /** | |
4346a860 | 343 | * Shows entry content. |
163eae0b | 344 | * |
3d2b2d62 J |
345 | * @param Entry $entry |
346 | * | |
bd9f0815 | 347 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") |
3d2b2d62 | 348 | * |
163eae0b | 349 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 | 350 | */ |
be463487 | 351 | public function viewAction(Entry $entry) |
bd9f0815 | 352 | { |
3d2b2d62 J |
353 | $this->checkUserAction($entry); |
354 | ||
bd9f0815 | 355 | return $this->render( |
ad4d1caa | 356 | 'WallabagCoreBundle:Entry:entry.html.twig', |
4094ea47 | 357 | ['entry' => $entry] |
bd9f0815 | 358 | ); |
163eae0b NL |
359 | } |
360 | ||
831b02aa JB |
361 | /** |
362 | * Reload an entry. | |
363 | * Refetch content from the website and make it readable again. | |
364 | * | |
365 | * @param Entry $entry | |
366 | * | |
367 | * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry") | |
368 | * | |
369 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
370 | */ | |
371 | public function reloadAction(Entry $entry) | |
372 | { | |
373 | $this->checkUserAction($entry); | |
374 | ||
59b97fae | 375 | $this->updateEntry($entry, 'entry_reloaded'); |
831b02aa | 376 | |
2297d60f JB |
377 | // if refreshing entry failed, don't save it |
378 | if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { | |
379 | $bag = $this->get('session')->getFlashBag(); | |
380 | $bag->clear(); | |
381 | $bag->add('notice', 'flashes.entry.notice.entry_reloaded_failed'); | |
382 | ||
383 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); | |
384 | } | |
385 | ||
59b97fae JB |
386 | $em = $this->getDoctrine()->getManager(); |
387 | $em->persist($entry); | |
388 | $em->flush(); | |
831b02aa | 389 | |
e0597476 JB |
390 | // entry saved, dispatch event about it! |
391 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
392 | ||
4094ea47 | 393 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); |
831b02aa JB |
394 | } |
395 | ||
163eae0b | 396 | /** |
4346a860 | 397 | * Changes read status for an entry. |
163eae0b | 398 | * |
3d2b2d62 J |
399 | * @param Request $request |
400 | * @param Entry $entry | |
401 | * | |
163eae0b | 402 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") |
3d2b2d62 | 403 | * |
163eae0b NL |
404 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
405 | */ | |
be463487 | 406 | public function toggleArchiveAction(Request $request, Entry $entry) |
163eae0b | 407 | { |
3d2b2d62 J |
408 | $this->checkUserAction($entry); |
409 | ||
163eae0b NL |
410 | $entry->toggleArchive(); |
411 | $this->getDoctrine()->getManager()->flush(); | |
412 | ||
4204a06b JB |
413 | $message = 'flashes.entry.notice.entry_unarchived'; |
414 | if ($entry->isArchived()) { | |
415 | $message = 'flashes.entry.notice.entry_archived'; | |
416 | } | |
417 | ||
163eae0b NL |
418 | $this->get('session')->getFlashBag()->add( |
419 | 'notice', | |
4204a06b | 420 | $message |
163eae0b NL |
421 | ); |
422 | ||
af497a64 NL |
423 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); |
424 | ||
425 | return $this->redirect($redirectUrl); | |
163eae0b NL |
426 | } |
427 | ||
428 | /** | |
eecd7e40 | 429 | * Changes starred status for an entry. |
163eae0b | 430 | * |
3d2b2d62 J |
431 | * @param Request $request |
432 | * @param Entry $entry | |
433 | * | |
163eae0b | 434 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") |
3d2b2d62 | 435 | * |
163eae0b NL |
436 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
437 | */ | |
be463487 | 438 | public function toggleStarAction(Request $request, Entry $entry) |
163eae0b | 439 | { |
3d2b2d62 J |
440 | $this->checkUserAction($entry); |
441 | ||
163eae0b NL |
442 | $entry->toggleStar(); |
443 | $this->getDoctrine()->getManager()->flush(); | |
444 | ||
4204a06b JB |
445 | $message = 'flashes.entry.notice.entry_unstarred'; |
446 | if ($entry->isStarred()) { | |
447 | $message = 'flashes.entry.notice.entry_starred'; | |
448 | } | |
449 | ||
163eae0b NL |
450 | $this->get('session')->getFlashBag()->add( |
451 | 'notice', | |
4204a06b | 452 | $message |
163eae0b NL |
453 | ); |
454 | ||
af497a64 NL |
455 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); |
456 | ||
457 | return $this->redirect($redirectUrl); | |
163eae0b NL |
458 | } |
459 | ||
460 | /** | |
2863bf2a | 461 | * Deletes entry and redirect to the homepage or the last viewed page. |
163eae0b | 462 | * |
16a3d04c | 463 | * @param Entry $entry |
3d2b2d62 | 464 | * |
163eae0b | 465 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") |
3d2b2d62 | 466 | * |
163eae0b NL |
467 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
468 | */ | |
18d5f454 | 469 | public function deleteEntryAction(Request $request, Entry $entry) |
163eae0b | 470 | { |
3d2b2d62 J |
471 | $this->checkUserAction($entry); |
472 | ||
2863bf2a JB |
473 | // generates the view url for this entry to check for redirection later |
474 | // to avoid redirecting to the deleted entry. Ugh. | |
475 | $url = $this->generateUrl( | |
476 | 'view', | |
4094ea47 | 477 | ['id' => $entry->getId()], |
ce0e9ec3 | 478 | UrlGeneratorInterface::ABSOLUTE_PATH |
2863bf2a JB |
479 | ); |
480 | ||
e0597476 JB |
481 | // entry deleted, dispatch event about it! |
482 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | |
483 | ||
1d147791 NL |
484 | $em = $this->getDoctrine()->getManager(); |
485 | $em->remove($entry); | |
486 | $em->flush(); | |
163eae0b NL |
487 | |
488 | $this->get('session')->getFlashBag()->add( | |
489 | 'notice', | |
4204a06b | 490 | 'flashes.entry.notice.entry_deleted' |
163eae0b | 491 | ); |
bd9f0815 | 492 | |
ce0e9ec3 JB |
493 | // don't redirect user to the deleted entry (check that the referer doesn't end with the same url) |
494 | $referer = $request->headers->get('referer'); | |
495 | $to = (1 !== preg_match('#'.$url.'$#i', $referer) ? $referer : null); | |
af497a64 NL |
496 | |
497 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to); | |
498 | ||
499 | return $this->redirect($redirectUrl); | |
bd9f0815 | 500 | } |
3d2b2d62 J |
501 | |
502 | /** | |
4346a860 | 503 | * Check if the logged user can manage the given entry. |
3d2b2d62 J |
504 | * |
505 | * @param Entry $entry | |
506 | */ | |
507 | private function checkUserAction(Entry $entry) | |
508 | { | |
f1be7af4 | 509 | if (null === $this->getUser() || $this->getUser()->getId() != $entry->getUser()->getId()) { |
82d6d9cb | 510 | throw $this->createAccessDeniedException('You can not access this entry.'); |
3d2b2d62 J |
511 | } |
512 | } | |
b00a89e0 NL |
513 | |
514 | /** | |
515 | * Check for existing entry, if it exists, redirect to it with a message. | |
516 | * | |
4094ea47 | 517 | * @param Entry $entry |
b00a89e0 | 518 | * |
4094ea47 | 519 | * @return Entry|bool |
b00a89e0 | 520 | */ |
4094ea47 | 521 | private function checkIfEntryAlreadyExists(Entry $entry) |
b00a89e0 NL |
522 | { |
523 | return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId()); | |
f3d0cb91 NL |
524 | } |
525 | ||
f1be7af4 NL |
526 | /** |
527 | * Get public URL for entry (and generate it if necessary). | |
528 | * | |
529 | * @param Entry $entry | |
530 | * | |
531 | * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share") | |
532 | * | |
533 | * @return \Symfony\Component\HttpFoundation\Response | |
534 | */ | |
535 | public function shareAction(Entry $entry) | |
536 | { | |
537 | $this->checkUserAction($entry); | |
538 | ||
7239082a NL |
539 | if (null === $entry->getUid()) { |
540 | $entry->generateUid(); | |
eddda878 JB |
541 | |
542 | $em = $this->getDoctrine()->getManager(); | |
543 | $em->persist($entry); | |
544 | $em->flush(); | |
f1be7af4 NL |
545 | } |
546 | ||
547 | return $this->redirect($this->generateUrl('share_entry', [ | |
7239082a | 548 | 'uid' => $entry->getUid(), |
f1be7af4 NL |
549 | ])); |
550 | } | |
551 | ||
552 | /** | |
553 | * Disable public sharing for an entry. | |
554 | * | |
555 | * @param Entry $entry | |
556 | * | |
557 | * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share") | |
558 | * | |
559 | * @return \Symfony\Component\HttpFoundation\Response | |
560 | */ | |
561 | public function deleteShareAction(Entry $entry) | |
562 | { | |
563 | $this->checkUserAction($entry); | |
564 | ||
7239082a | 565 | $entry->cleanUid(); |
eddda878 | 566 | |
f1be7af4 NL |
567 | $em = $this->getDoctrine()->getManager(); |
568 | $em->persist($entry); | |
569 | $em->flush(); | |
570 | ||
571 | return $this->redirect($this->generateUrl('view', [ | |
572 | 'id' => $entry->getId(), | |
573 | ])); | |
574 | } | |
575 | ||
d0545b6b | 576 | /** |
eddda878 | 577 | * Ability to view a content publicly. |
f3d0cb91 NL |
578 | * |
579 | * @param Entry $entry | |
580 | * | |
7239082a | 581 | * @Route("/share/{uid}", requirements={"uid" = ".+"}, name="share_entry") |
eddda878 | 582 | * @Cache(maxage="25200", smaxage="25200", public=true) |
f3d0cb91 NL |
583 | * |
584 | * @return \Symfony\Component\HttpFoundation\Response | |
585 | */ | |
d0545b6b | 586 | public function shareEntryAction(Entry $entry) |
f3d0cb91 | 587 | { |
eddda878 JB |
588 | if (!$this->get('craue_config')->get('share_public')) { |
589 | throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.'); | |
590 | } | |
591 | ||
f3d0cb91 | 592 | return $this->render( |
2ff9991a | 593 | '@WallabagCore/themes/common/Entry/share.html.twig', |
eddda878 | 594 | ['entry' => $entry] |
f3d0cb91 NL |
595 | ); |
596 | } | |
b6520f0b NL |
597 | |
598 | /** | |
599 | * Shows untagged articles for current user. | |
600 | * | |
601 | * @param Request $request | |
602 | * @param int $page | |
603 | * | |
604 | * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"}) | |
605 | * | |
606 | * @return \Symfony\Component\HttpFoundation\Response | |
607 | */ | |
608 | public function showUntaggedEntriesAction(Request $request, $page) | |
609 | { | |
610 | return $this->showEntries('untagged', $request, $page); | |
611 | } | |
5847dd35 TC |
612 | |
613 | /** | |
614 | * @Route("/group-articles/{group}/{page}", name="group-presentations", defaults={"page" = "1"}, requirements={"page": "\d+", "group": "\d+"}) | |
615 | * | |
616 | * @param Request $request | |
617 | * @param Group $group | |
618 | * @param int $page | |
619 | * @return Response | |
620 | */ | |
621 | public function showGroupSharedTemplatesAction(Request $request, Group $group, int $page) | |
622 | { | |
623 | if (!$this->getUser()->inGroup($group)) { | |
624 | throw $this->createAccessDeniedException(); | |
625 | } | |
626 | ||
627 | $repository = $this->get('wallabag_core.entry_repository'); | |
628 | ||
629 | /** @var QueryBuilder $entries */ | |
630 | $entries = $repository->findByGroup($group->getId()); | |
631 | ||
632 | $pagerAdapter = new DoctrineORMAdapter($entries->getQuery(), true, false); | |
633 | $pagerFanta = new Pagerfanta($pagerAdapter); | |
634 | $pagerFanta->setMaxPerPage(9); | |
635 | ||
636 | $form = $this->createForm(EntryFilterType::class); | |
637 | ||
638 | if ($request->query->has($form->getName())) { | |
639 | // manually bind values from the request | |
640 | $form->submit($request->query->get($form->getName())); | |
641 | ||
642 | // build the query from the given form object | |
643 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); | |
644 | } | |
645 | ||
646 | $searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : ''); | |
647 | ||
648 | try { | |
649 | $pagerFanta->setCurrentPage($page); | |
650 | } catch (OutOfRangeCurrentPageException $e) { | |
651 | if ($page > 1) { | |
652 | return $this->redirect($this->generateUrl('group-presentations', [ | |
653 | 'page' => $pagerFanta->getNbPages(), | |
654 | 'group' => $group->getId() | |
655 | ]), 302); | |
656 | } | |
657 | } | |
658 | ||
659 | return $this->render('WallabagCoreBundle:Entry:entries.html.twig', [ | |
660 | 'form' => $form->createView(), | |
661 | 'entries' => $pagerFanta, | |
662 | 'currentPage' => $page, | |
663 | 'searchTerm' => $searchTerm, | |
664 | ]); | |
665 | } | |
666 | ||
667 | /** | |
668 | * | |
669 | * @Route("/entry/group-shares/{entry}", name="group-shares-entry") | |
670 | * | |
671 | * @param Request $request | |
672 | * @param Entry $entry | |
673 | * @return Response | |
674 | */ | |
675 | public function groupShareAction(Request $request, Entry $entry) | |
676 | { | |
677 | $this->checkUserAction($entry); | |
678 | ||
679 | $form = $this->createForm(EditGroupSharesType::class, $entry, ['groups' => $this->getUser()->getGroups()]); | |
680 | ||
681 | $form->handleRequest($request); | |
682 | ||
683 | if ($form->isSubmitted() && $form->isValid()) { | |
684 | $em = $this->getDoctrine()->getManager(); | |
685 | $em->persist($entry); | |
686 | $em->flush(); | |
687 | ||
688 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); | |
689 | } | |
690 | ||
691 | return $this->render('WallabagCoreBundle:Entry:_share_groups.html.twig', [ | |
692 | 'form' => $form->createView(), | |
693 | 'entry' => $entry, | |
694 | ]); | |
695 | } | |
9d50517c | 696 | } |