]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Controller; |
9d50517c NL |
4 | |
5 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
6 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
163eae0b | 7 | use Symfony\Component\HttpFoundation\Request; |
be463487 | 8 | use Wallabag\CoreBundle\Entity\Entry; |
82d6d9cb JB |
9 | use Wallabag\CoreBundle\Form\Type\NewEntryType; |
10 | use Wallabag\CoreBundle\Form\Type\EditEntryType; | |
26864574 NL |
11 | use Wallabag\CoreBundle\Filter\EntryFilterType; |
12 | use Pagerfanta\Adapter\DoctrineORMAdapter; | |
13 | use Pagerfanta\Pagerfanta; | |
9d50517c NL |
14 | |
15 | class EntryController extends Controller | |
16 | { | |
880a0e1c NL |
17 | /** |
18 | * @param Entry $entry | |
19 | */ | |
20 | private function updateEntry(Entry $entry) | |
21 | { | |
22 | try { | |
23 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | |
24 | $em = $this->getDoctrine()->getManager(); | |
25 | $em->persist($entry); | |
26 | $em->flush(); | |
27 | } catch (\Exception $e) { | |
28 | return false; | |
29 | } | |
30 | ||
31 | return true; | |
32 | } | |
33 | ||
b84a8055 | 34 | /** |
3d2b2d62 J |
35 | * @param Request $request |
36 | * | |
053b9568 | 37 | * @Route("/new-entry", name="new_entry") |
3d2b2d62 | 38 | * |
b84a8055 NL |
39 | * @return \Symfony\Component\HttpFoundation\Response |
40 | */ | |
053b9568 | 41 | public function addEntryFormAction(Request $request) |
b84a8055 | 42 | { |
3b815d2d | 43 | $entry = new Entry($this->getUser()); |
b84a8055 | 44 | |
82d6d9cb | 45 | $form = $this->createForm(new NewEntryType(), $entry); |
b84a8055 NL |
46 | |
47 | $form->handleRequest($request); | |
48 | ||
49 | if ($form->isValid()) { | |
880a0e1c | 50 | $this->updateEntry($entry); |
b84a8055 NL |
51 | $this->get('session')->getFlashBag()->add( |
52 | 'notice', | |
53 | 'Entry saved' | |
54 | ); | |
55 | ||
56 | return $this->redirect($this->generateUrl('homepage')); | |
57 | } | |
58 | ||
053b9568 | 59 | return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array( |
b84a8055 NL |
60 | 'form' => $form->createView(), |
61 | )); | |
62 | } | |
63 | ||
880a0e1c NL |
64 | /** |
65 | * @param Request $request | |
66 | * | |
67 | * @Route("/bookmarklet", name="bookmarklet") | |
68 | * | |
69 | * @return \Symfony\Component\HttpFoundation\Response | |
70 | */ | |
71 | public function addEntryViaBookmarklet(Request $request) | |
72 | { | |
73 | $entry = new Entry($this->getUser()); | |
74 | $entry->setUrl($request->get('url')); | |
75 | $this->updateEntry($entry); | |
76 | ||
77 | return $this->redirect($this->generateUrl('homepage')); | |
78 | } | |
79 | ||
053b9568 NL |
80 | /** |
81 | * @param Request $request | |
82 | * | |
83 | * @Route("/new", name="new") | |
84 | * | |
85 | * @return \Symfony\Component\HttpFoundation\Response | |
86 | */ | |
87 | public function addEntryAction(Request $request) | |
88 | { | |
89 | return $this->render('WallabagCoreBundle:Entry:new.html.twig'); | |
90 | } | |
91 | ||
82d6d9cb JB |
92 | /** |
93 | * Edit an entry content. | |
94 | * | |
95 | * @param Request $request | |
96 | * @param Entry $entry | |
97 | * | |
98 | * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit") | |
99 | * | |
100 | * @return \Symfony\Component\HttpFoundation\Response | |
101 | */ | |
102 | public function editEntryAction(Request $request, Entry $entry) | |
103 | { | |
104 | $this->checkUserAction($entry); | |
105 | ||
106 | $form = $this->createForm(new EditEntryType(), $entry); | |
107 | ||
108 | $form->handleRequest($request); | |
109 | ||
110 | if ($form->isValid()) { | |
111 | $em = $this->getDoctrine()->getManager(); | |
112 | $em->persist($entry); | |
113 | $em->flush(); | |
114 | ||
115 | $this->get('session')->getFlashBag()->add( | |
116 | 'notice', | |
117 | 'Entry updated' | |
118 | ); | |
119 | ||
120 | return $this->redirect($this->generateUrl('view', array('id' => $entry->getId()))); | |
121 | } | |
122 | ||
123 | return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array( | |
124 | 'form' => $form->createView(), | |
125 | )); | |
126 | } | |
127 | ||
89659c9e NL |
128 | /** |
129 | * Shows all entries for current user. | |
130 | * | |
131 | * @param Request $request | |
132 | * @param int $page | |
133 | * | |
134 | * @Route("/all/list/{page}", name="all", defaults={"page" = "1"}) | |
135 | * | |
136 | * @return \Symfony\Component\HttpFoundation\Response | |
137 | */ | |
138 | public function showAllAction(Request $request, $page) | |
139 | { | |
2b7a4889 | 140 | return $this->showEntries('all', $request, $page); |
89659c9e NL |
141 | } |
142 | ||
9d50517c | 143 | /** |
4346a860 | 144 | * Shows unread entries for current user. |
163eae0b | 145 | * |
26864574 NL |
146 | * @param Request $request |
147 | * @param int $page | |
148 | * | |
9fb6ac83 | 149 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) |
3d2b2d62 | 150 | * |
163eae0b | 151 | * @return \Symfony\Component\HttpFoundation\Response |
9d50517c | 152 | */ |
26864574 | 153 | public function showUnreadAction(Request $request, $page) |
9d50517c | 154 | { |
0ab7404f | 155 | return $this->showEntries('unread', $request, $page); |
9d50517c | 156 | } |
bd9f0815 NL |
157 | |
158 | /** | |
4346a860 | 159 | * Shows read entries for current user. |
163eae0b | 160 | * |
26864574 NL |
161 | * @param Request $request |
162 | * @param int $page | |
163 | * | |
9fb6ac83 | 164 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) |
3d2b2d62 | 165 | * |
163eae0b | 166 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 | 167 | */ |
26864574 | 168 | public function showArchiveAction(Request $request, $page) |
bd9f0815 | 169 | { |
0ab7404f JB |
170 | return $this->showEntries('archive', $request, $page); |
171 | } | |
172 | ||
173 | /** | |
174 | * Shows starred entries for current user. | |
175 | * | |
176 | * @param Request $request | |
177 | * @param int $page | |
178 | * | |
179 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) | |
180 | * | |
181 | * @return \Symfony\Component\HttpFoundation\Response | |
182 | */ | |
183 | public function showStarredAction(Request $request, $page) | |
184 | { | |
185 | return $this->showEntries('starred', $request, $page); | |
186 | } | |
26864574 | 187 | |
0ab7404f JB |
188 | /** |
189 | * Global method to retrieve entries depending on the given type | |
190 | * It returns the response to be send. | |
191 | * | |
192 | * @param string $type Entries type: unread, starred or archive | |
193 | * @param Request $request | |
194 | * @param int $page | |
195 | * | |
196 | * @return \Symfony\Component\HttpFoundation\Response | |
197 | */ | |
198 | private function showEntries($type, Request $request, $page) | |
199 | { | |
200 | $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry'); | |
201 | ||
202 | switch ($type) { | |
203 | case 'starred': | |
204 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); | |
205 | break; | |
206 | ||
207 | case 'archive': | |
208 | $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); | |
209 | break; | |
210 | ||
211 | case 'unread': | |
212 | $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); | |
213 | break; | |
214 | ||
2b7a4889 NL |
215 | case 'all': |
216 | $qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); | |
217 | break; | |
218 | ||
0ab7404f JB |
219 | default: |
220 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
221 | } | |
222 | ||
d4ebe5c5 | 223 | $form = $this->get('form.factory')->create(new EntryFilterType($repository, $this->getUser())); |
9fb6ac83 | 224 | |
26864574 NL |
225 | if ($request->query->has($form->getName())) { |
226 | // manually bind values from the request | |
227 | $form->submit($request->query->get($form->getName())); | |
228 | ||
229 | // build the query from the given form object | |
0ab7404f | 230 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); |
26864574 NL |
231 | } |
232 | ||
0ab7404f | 233 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); |
26864574 NL |
234 | $entries = new Pagerfanta($pagerAdapter); |
235 | ||
48ffc5a4 | 236 | $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); |
9fb6ac83 | 237 | $entries->setCurrentPage($page); |
bd9f0815 NL |
238 | |
239 | return $this->render( | |
ad4d1caa | 240 | 'WallabagCoreBundle:Entry:entries.html.twig', |
9fb6ac83 | 241 | array( |
8ce32af6 JB |
242 | 'form' => $form->createView(), |
243 | 'entries' => $entries, | |
244 | 'currentPage' => $page, | |
9fb6ac83 | 245 | ) |
bd9f0815 | 246 | ); |
bd9f0815 NL |
247 | } |
248 | ||
249 | /** | |
4346a860 | 250 | * Shows entry content. |
163eae0b | 251 | * |
3d2b2d62 J |
252 | * @param Entry $entry |
253 | * | |
bd9f0815 | 254 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") |
3d2b2d62 | 255 | * |
163eae0b | 256 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 | 257 | */ |
be463487 | 258 | public function viewAction(Entry $entry) |
bd9f0815 | 259 | { |
3d2b2d62 J |
260 | $this->checkUserAction($entry); |
261 | ||
bd9f0815 | 262 | return $this->render( |
ad4d1caa | 263 | 'WallabagCoreBundle:Entry:entry.html.twig', |
bd9f0815 NL |
264 | array('entry' => $entry) |
265 | ); | |
163eae0b NL |
266 | } |
267 | ||
268 | /** | |
4346a860 | 269 | * Changes read status for an entry. |
163eae0b | 270 | * |
3d2b2d62 J |
271 | * @param Request $request |
272 | * @param Entry $entry | |
273 | * | |
163eae0b | 274 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") |
3d2b2d62 | 275 | * |
163eae0b NL |
276 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
277 | */ | |
be463487 | 278 | public function toggleArchiveAction(Request $request, Entry $entry) |
163eae0b | 279 | { |
3d2b2d62 J |
280 | $this->checkUserAction($entry); |
281 | ||
163eae0b NL |
282 | $entry->toggleArchive(); |
283 | $this->getDoctrine()->getManager()->flush(); | |
284 | ||
285 | $this->get('session')->getFlashBag()->add( | |
286 | 'notice', | |
8ce32af6 | 287 | 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived') |
163eae0b NL |
288 | ); |
289 | ||
290 | return $this->redirect($request->headers->get('referer')); | |
291 | } | |
292 | ||
293 | /** | |
4346a860 | 294 | * Changes favorite status for an entry. |
163eae0b | 295 | * |
3d2b2d62 J |
296 | * @param Request $request |
297 | * @param Entry $entry | |
298 | * | |
163eae0b | 299 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") |
3d2b2d62 | 300 | * |
163eae0b NL |
301 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
302 | */ | |
be463487 | 303 | public function toggleStarAction(Request $request, Entry $entry) |
163eae0b | 304 | { |
3d2b2d62 J |
305 | $this->checkUserAction($entry); |
306 | ||
163eae0b NL |
307 | $entry->toggleStar(); |
308 | $this->getDoctrine()->getManager()->flush(); | |
309 | ||
310 | $this->get('session')->getFlashBag()->add( | |
311 | 'notice', | |
8ce32af6 | 312 | 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred') |
163eae0b NL |
313 | ); |
314 | ||
315 | return $this->redirect($request->headers->get('referer')); | |
316 | } | |
317 | ||
318 | /** | |
16a3d04c | 319 | * Deletes entry and redirect to the homepage. |
163eae0b | 320 | * |
16a3d04c | 321 | * @param Entry $entry |
3d2b2d62 | 322 | * |
163eae0b | 323 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") |
3d2b2d62 | 324 | * |
163eae0b NL |
325 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
326 | */ | |
18d5f454 | 327 | public function deleteEntryAction(Request $request, Entry $entry) |
163eae0b | 328 | { |
3d2b2d62 J |
329 | $this->checkUserAction($entry); |
330 | ||
1d147791 NL |
331 | $em = $this->getDoctrine()->getManager(); |
332 | $em->remove($entry); | |
333 | $em->flush(); | |
163eae0b NL |
334 | |
335 | $this->get('session')->getFlashBag()->add( | |
336 | 'notice', | |
337 | 'Entry deleted' | |
338 | ); | |
bd9f0815 | 339 | |
18d5f454 | 340 | return $this->redirect($request->headers->get('referer')); |
bd9f0815 | 341 | } |
3d2b2d62 J |
342 | |
343 | /** | |
4346a860 | 344 | * Check if the logged user can manage the given entry. |
3d2b2d62 J |
345 | * |
346 | * @param Entry $entry | |
347 | */ | |
348 | private function checkUserAction(Entry $entry) | |
349 | { | |
350 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | |
82d6d9cb | 351 | throw $this->createAccessDeniedException('You can not access this entry.'); |
3d2b2d62 J |
352 | } |
353 | } | |
9d50517c | 354 | } |