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