]>
Commit | Line | Data |
---|---|---|
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 Wallabag\CoreBundle\Entity\Entry; | |
9 | use Wallabag\CoreBundle\Form\Type\NewEntryType; | |
10 | use Wallabag\CoreBundle\Form\Type\EditEntryType; | |
11 | use Wallabag\CoreBundle\Filter\EntryFilterType; | |
12 | use Pagerfanta\Adapter\DoctrineORMAdapter; | |
13 | use Pagerfanta\Pagerfanta; | |
14 | ||
15 | class EntryController extends Controller | |
16 | { | |
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 | ||
34 | /** | |
35 | * @param Request $request | |
36 | * | |
37 | * @Route("/new-entry", name="new_entry") | |
38 | * | |
39 | * @return \Symfony\Component\HttpFoundation\Response | |
40 | */ | |
41 | public function addEntryFormAction(Request $request) | |
42 | { | |
43 | $entry = new Entry($this->getUser()); | |
44 | ||
45 | $form = $this->createForm(new NewEntryType(), $entry); | |
46 | ||
47 | $form->handleRequest($request); | |
48 | ||
49 | if ($form->isValid()) { | |
50 | $this->updateEntry($entry); | |
51 | $this->get('session')->getFlashBag()->add( | |
52 | 'notice', | |
53 | 'Entry saved' | |
54 | ); | |
55 | ||
56 | return $this->redirect($this->generateUrl('homepage')); | |
57 | } | |
58 | ||
59 | return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array( | |
60 | 'form' => $form->createView(), | |
61 | )); | |
62 | } | |
63 | ||
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 | ||
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 | ||
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 | ||
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 | { | |
140 | return $this->showEntries('all', $request, $page); | |
141 | } | |
142 | ||
143 | /** | |
144 | * Shows unread entries for current user. | |
145 | * | |
146 | * @param Request $request | |
147 | * @param int $page | |
148 | * | |
149 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) | |
150 | * | |
151 | * @return \Symfony\Component\HttpFoundation\Response | |
152 | */ | |
153 | public function showUnreadAction(Request $request, $page) | |
154 | { | |
155 | return $this->showEntries('unread', $request, $page); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Shows read entries for current user. | |
160 | * | |
161 | * @param Request $request | |
162 | * @param int $page | |
163 | * | |
164 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) | |
165 | * | |
166 | * @return \Symfony\Component\HttpFoundation\Response | |
167 | */ | |
168 | public function showArchiveAction(Request $request, $page) | |
169 | { | |
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 | } | |
187 | ||
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 | ||
215 | case 'all': | |
216 | $qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); | |
217 | break; | |
218 | ||
219 | default: | |
220 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
221 | } | |
222 | ||
223 | $form = $this->get('form.factory')->create(new EntryFilterType($repository, $this->getUser())); | |
224 | ||
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 | |
230 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); | |
231 | } | |
232 | ||
233 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); | |
234 | $entries = new Pagerfanta($pagerAdapter); | |
235 | ||
236 | $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); | |
237 | $entries->setCurrentPage($page); | |
238 | ||
239 | return $this->render( | |
240 | 'WallabagCoreBundle:Entry:entries.html.twig', | |
241 | array( | |
242 | 'form' => $form->createView(), | |
243 | 'entries' => $entries, | |
244 | 'currentPage' => $page, | |
245 | ) | |
246 | ); | |
247 | } | |
248 | ||
249 | /** | |
250 | * Shows entry content. | |
251 | * | |
252 | * @param Entry $entry | |
253 | * | |
254 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") | |
255 | * | |
256 | * @return \Symfony\Component\HttpFoundation\Response | |
257 | */ | |
258 | public function viewAction(Entry $entry) | |
259 | { | |
260 | $this->checkUserAction($entry); | |
261 | ||
262 | return $this->render( | |
263 | 'WallabagCoreBundle:Entry:entry.html.twig', | |
264 | array('entry' => $entry) | |
265 | ); | |
266 | } | |
267 | ||
268 | /** | |
269 | * Changes read status for an entry. | |
270 | * | |
271 | * @param Request $request | |
272 | * @param Entry $entry | |
273 | * | |
274 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") | |
275 | * | |
276 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
277 | */ | |
278 | public function toggleArchiveAction(Request $request, Entry $entry) | |
279 | { | |
280 | $this->checkUserAction($entry); | |
281 | ||
282 | $entry->toggleArchive(); | |
283 | $this->getDoctrine()->getManager()->flush(); | |
284 | ||
285 | $this->get('session')->getFlashBag()->add( | |
286 | 'notice', | |
287 | 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived') | |
288 | ); | |
289 | ||
290 | return $this->redirect($request->headers->get('referer')); | |
291 | } | |
292 | ||
293 | /** | |
294 | * Changes favorite status for an entry. | |
295 | * | |
296 | * @param Request $request | |
297 | * @param Entry $entry | |
298 | * | |
299 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") | |
300 | * | |
301 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
302 | */ | |
303 | public function toggleStarAction(Request $request, Entry $entry) | |
304 | { | |
305 | $this->checkUserAction($entry); | |
306 | ||
307 | $entry->toggleStar(); | |
308 | $this->getDoctrine()->getManager()->flush(); | |
309 | ||
310 | $this->get('session')->getFlashBag()->add( | |
311 | 'notice', | |
312 | 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred') | |
313 | ); | |
314 | ||
315 | return $this->redirect($request->headers->get('referer')); | |
316 | } | |
317 | ||
318 | /** | |
319 | * Deletes entry and redirect to the homepage. | |
320 | * | |
321 | * @param Entry $entry | |
322 | * | |
323 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") | |
324 | * | |
325 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
326 | */ | |
327 | public function deleteEntryAction(Request $request, Entry $entry) | |
328 | { | |
329 | $this->checkUserAction($entry); | |
330 | ||
331 | $em = $this->getDoctrine()->getManager(); | |
332 | $em->remove($entry); | |
333 | $em->flush(); | |
334 | ||
335 | $this->get('session')->getFlashBag()->add( | |
336 | 'notice', | |
337 | 'Entry deleted' | |
338 | ); | |
339 | ||
340 | return $this->redirect($request->headers->get('referer')); | |
341 | } | |
342 | ||
343 | /** | |
344 | * Check if the logged user can manage the given entry. | |
345 | * | |
346 | * @param Entry $entry | |
347 | */ | |
348 | private function checkUserAction(Entry $entry) | |
349 | { | |
350 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | |
351 | throw $this->createAccessDeniedException('You can not access this entry.'); | |
352 | } | |
353 | } | |
354 | } |