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