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