]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/EntryController.php
Fix duplicate article when added via the bookmarklet
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / EntryController.php
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 $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', array('%date%' => $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 'flashes.entry.notice.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
90 // check for existing entry, if it exists, redirect to it with a message
91 $existingEntry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
92
93 if (false === $existingEntry) {
94 $this->updateEntry($entry);
95 }
96
97 return $this->redirect($this->generateUrl('homepage'));
98 }
99
100 /**
101 * @Route("/new", name="new")
102 *
103 * @return \Symfony\Component\HttpFoundation\Response
104 */
105 public function addEntryAction()
106 {
107 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
108 }
109
110 /**
111 * Edit an entry content.
112 *
113 * @param Request $request
114 * @param Entry $entry
115 *
116 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
117 *
118 * @return \Symfony\Component\HttpFoundation\Response
119 */
120 public function editEntryAction(Request $request, Entry $entry)
121 {
122 $this->checkUserAction($entry);
123
124 $form = $this->createForm(EditEntryType::class, $entry);
125
126 $form->handleRequest($request);
127
128 if ($form->isValid()) {
129 $em = $this->getDoctrine()->getManager();
130 $em->persist($entry);
131 $em->flush();
132
133 $this->get('session')->getFlashBag()->add(
134 'notice',
135 'flashes.entry.notice.entry_updated'
136 );
137
138 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
139 }
140
141 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array(
142 'form' => $form->createView(),
143 ));
144 }
145
146 /**
147 * Shows all entries for current user.
148 *
149 * @param Request $request
150 * @param int $page
151 *
152 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
153 *
154 * @return \Symfony\Component\HttpFoundation\Response
155 */
156 public function showAllAction(Request $request, $page)
157 {
158 return $this->showEntries('all', $request, $page);
159 }
160
161 /**
162 * Shows unread entries for current user.
163 *
164 * @param Request $request
165 * @param int $page
166 *
167 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
168 *
169 * @return \Symfony\Component\HttpFoundation\Response
170 */
171 public function showUnreadAction(Request $request, $page)
172 {
173 // load the quickstart if no entry in database
174 if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUsername($this->getUser()->getId()) == 0) {
175 return $this->redirect($this->generateUrl('quickstart'));
176 }
177
178 return $this->showEntries('unread', $request, $page);
179 }
180
181 /**
182 * Shows read entries for current user.
183 *
184 * @param Request $request
185 * @param int $page
186 *
187 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
188 *
189 * @return \Symfony\Component\HttpFoundation\Response
190 */
191 public function showArchiveAction(Request $request, $page)
192 {
193 return $this->showEntries('archive', $request, $page);
194 }
195
196 /**
197 * Shows starred entries for current user.
198 *
199 * @param Request $request
200 * @param int $page
201 *
202 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
203 *
204 * @return \Symfony\Component\HttpFoundation\Response
205 */
206 public function showStarredAction(Request $request, $page)
207 {
208 return $this->showEntries('starred', $request, $page);
209 }
210
211 /**
212 * Global method to retrieve entries depending on the given type
213 * It returns the response to be send.
214 *
215 * @param string $type Entries type: unread, starred or archive
216 * @param Request $request
217 * @param int $page
218 *
219 * @return \Symfony\Component\HttpFoundation\Response
220 */
221 private function showEntries($type, Request $request, $page)
222 {
223 $repository = $this->get('wallabag_core.entry_repository');
224
225 switch ($type) {
226 case 'starred':
227 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
228 break;
229
230 case 'archive':
231 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
232 break;
233
234 case 'unread':
235 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
236 break;
237
238 case 'all':
239 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
240 break;
241
242 default:
243 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
244 }
245
246 $form = $this->createForm(EntryFilterType::class);
247
248 if ($request->query->has($form->getName())) {
249 // manually bind values from the request
250 $form->submit($request->query->get($form->getName()));
251
252 // build the query from the given form object
253 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
254 }
255
256 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
257 $entries = new Pagerfanta($pagerAdapter);
258
259 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
260 try {
261 $entries->setCurrentPage($page);
262 } catch (OutOfRangeCurrentPageException $e) {
263 if ($page > 1) {
264 return $this->redirect($this->generateUrl($type, array('page' => $entries->getNbPages())), 302);
265 }
266 }
267
268 return $this->render(
269 'WallabagCoreBundle:Entry:entries.html.twig',
270 array(
271 'form' => $form->createView(),
272 'entries' => $entries,
273 'currentPage' => $page,
274 )
275 );
276 }
277
278 /**
279 * Shows entry content.
280 *
281 * @param Entry $entry
282 *
283 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
284 *
285 * @return \Symfony\Component\HttpFoundation\Response
286 */
287 public function viewAction(Entry $entry)
288 {
289 $this->checkUserAction($entry);
290
291 return $this->render(
292 'WallabagCoreBundle:Entry:entry.html.twig',
293 array('entry' => $entry)
294 );
295 }
296
297 /**
298 * Reload an entry.
299 * Refetch content from the website and make it readable again.
300 *
301 * @param Entry $entry
302 *
303 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
304 *
305 * @return \Symfony\Component\HttpFoundation\RedirectResponse
306 */
307 public function reloadAction(Entry $entry)
308 {
309 $this->checkUserAction($entry);
310
311 $message = 'flashes.entry.notice.entry_reloaded';
312 if (false === $this->updateEntry($entry)) {
313 $message = 'flashes.entry.notice.entry_reload_failed';
314 }
315
316 $this->get('session')->getFlashBag()->add(
317 'notice',
318 $message
319 );
320
321 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
322 }
323
324 /**
325 * Changes read status for an entry.
326 *
327 * @param Request $request
328 * @param Entry $entry
329 *
330 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
331 *
332 * @return \Symfony\Component\HttpFoundation\RedirectResponse
333 */
334 public function toggleArchiveAction(Request $request, Entry $entry)
335 {
336 $this->checkUserAction($entry);
337
338 $entry->toggleArchive();
339 $this->getDoctrine()->getManager()->flush();
340
341 $message = 'flashes.entry.notice.entry_unarchived';
342 if ($entry->isArchived()) {
343 $message = 'flashes.entry.notice.entry_archived';
344 }
345
346 $this->get('session')->getFlashBag()->add(
347 'notice',
348 $message
349 );
350
351 return $this->redirect($request->headers->get('referer'));
352 }
353
354 /**
355 * Changes favorite status for an entry.
356 *
357 * @param Request $request
358 * @param Entry $entry
359 *
360 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
361 *
362 * @return \Symfony\Component\HttpFoundation\RedirectResponse
363 */
364 public function toggleStarAction(Request $request, Entry $entry)
365 {
366 $this->checkUserAction($entry);
367
368 $entry->toggleStar();
369 $this->getDoctrine()->getManager()->flush();
370
371 $message = 'flashes.entry.notice.entry_unstarred';
372 if ($entry->isStarred()) {
373 $message = 'flashes.entry.notice.entry_starred';
374 }
375
376 $this->get('session')->getFlashBag()->add(
377 'notice',
378 $message
379 );
380
381 return $this->redirect($request->headers->get('referer'));
382 }
383
384 /**
385 * Deletes entry and redirect to the homepage or the last viewed page.
386 *
387 * @param Entry $entry
388 *
389 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
390 *
391 * @return \Symfony\Component\HttpFoundation\RedirectResponse
392 */
393 public function deleteEntryAction(Request $request, Entry $entry)
394 {
395 $this->checkUserAction($entry);
396
397 // generates the view url for this entry to check for redirection later
398 // to avoid redirecting to the deleted entry. Ugh.
399 $url = $this->generateUrl(
400 'view',
401 array('id' => $entry->getId()),
402 UrlGeneratorInterface::ABSOLUTE_URL
403 );
404
405 $em = $this->getDoctrine()->getManager();
406 $em->remove($entry);
407 $em->flush();
408
409 $this->get('session')->getFlashBag()->add(
410 'notice',
411 'flashes.entry.notice.entry_deleted'
412 );
413
414 // don't redirect user to the deleted entry
415 return $this->redirect($url !== $request->headers->get('referer') ? $request->headers->get('referer') : $this->generateUrl('homepage'));
416 }
417
418 /**
419 * Check if the logged user can manage the given entry.
420 *
421 * @param Entry $entry
422 */
423 private function checkUserAction(Entry $entry)
424 {
425 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
426 throw $this->createAccessDeniedException('You can not access this entry.');
427 }
428 }
429 }