]>
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 | { | |
b84a8055 | 17 | /** |
3d2b2d62 J |
18 | * @param Request $request |
19 | * | |
053b9568 | 20 | * @Route("/new-entry", name="new_entry") |
3d2b2d62 | 21 | * |
b84a8055 NL |
22 | * @return \Symfony\Component\HttpFoundation\Response |
23 | */ | |
053b9568 | 24 | public function addEntryFormAction(Request $request) |
b84a8055 | 25 | { |
3b815d2d | 26 | $entry = new Entry($this->getUser()); |
b84a8055 | 27 | |
82d6d9cb | 28 | $form = $this->createForm(new NewEntryType(), $entry); |
b84a8055 NL |
29 | |
30 | $form->handleRequest($request); | |
31 | ||
32 | if ($form->isValid()) { | |
558d9aab | 33 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); |
6b767d1c NL |
34 | |
35 | $em = $this->getDoctrine()->getManager(); | |
b84a8055 NL |
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 | ||
053b9568 | 47 | return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array( |
b84a8055 NL |
48 | 'form' => $form->createView(), |
49 | )); | |
50 | } | |
51 | ||
053b9568 NL |
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 | ||
82d6d9cb JB |
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 | ||
89659c9e NL |
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 | { | |
2b7a4889 | 112 | return $this->showEntries('all', $request, $page); |
89659c9e NL |
113 | } |
114 | ||
9d50517c | 115 | /** |
4346a860 | 116 | * Shows unread entries for current user. |
163eae0b | 117 | * |
26864574 NL |
118 | * @param Request $request |
119 | * @param int $page | |
120 | * | |
9fb6ac83 | 121 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) |
3d2b2d62 | 122 | * |
163eae0b | 123 | * @return \Symfony\Component\HttpFoundation\Response |
9d50517c | 124 | */ |
26864574 | 125 | public function showUnreadAction(Request $request, $page) |
9d50517c | 126 | { |
0ab7404f | 127 | return $this->showEntries('unread', $request, $page); |
9d50517c | 128 | } |
bd9f0815 NL |
129 | |
130 | /** | |
4346a860 | 131 | * Shows read entries for current user. |
163eae0b | 132 | * |
26864574 NL |
133 | * @param Request $request |
134 | * @param int $page | |
135 | * | |
9fb6ac83 | 136 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) |
3d2b2d62 | 137 | * |
163eae0b | 138 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 | 139 | */ |
26864574 | 140 | public function showArchiveAction(Request $request, $page) |
bd9f0815 | 141 | { |
0ab7404f JB |
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 | } | |
26864574 | 159 | |
0ab7404f JB |
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 | ||
2b7a4889 NL |
187 | case 'all': |
188 | $qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); | |
189 | break; | |
190 | ||
0ab7404f JB |
191 | default: |
192 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
193 | } | |
194 | ||
195 | $form = $this->get('form.factory')->create(new EntryFilterType()); | |
9fb6ac83 | 196 | |
26864574 NL |
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 | |
0ab7404f | 202 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); |
26864574 NL |
203 | } |
204 | ||
0ab7404f | 205 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); |
26864574 NL |
206 | $entries = new Pagerfanta($pagerAdapter); |
207 | ||
48ffc5a4 | 208 | $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); |
9fb6ac83 | 209 | $entries->setCurrentPage($page); |
bd9f0815 NL |
210 | |
211 | return $this->render( | |
ad4d1caa | 212 | 'WallabagCoreBundle:Entry:entries.html.twig', |
9fb6ac83 | 213 | array( |
8ce32af6 JB |
214 | 'form' => $form->createView(), |
215 | 'entries' => $entries, | |
216 | 'currentPage' => $page, | |
9fb6ac83 | 217 | ) |
bd9f0815 | 218 | ); |
bd9f0815 NL |
219 | } |
220 | ||
221 | /** | |
4346a860 | 222 | * Shows entry content. |
163eae0b | 223 | * |
3d2b2d62 J |
224 | * @param Entry $entry |
225 | * | |
bd9f0815 | 226 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") |
3d2b2d62 | 227 | * |
163eae0b | 228 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 | 229 | */ |
be463487 | 230 | public function viewAction(Entry $entry) |
bd9f0815 | 231 | { |
3d2b2d62 J |
232 | $this->checkUserAction($entry); |
233 | ||
bd9f0815 | 234 | return $this->render( |
ad4d1caa | 235 | 'WallabagCoreBundle:Entry:entry.html.twig', |
bd9f0815 NL |
236 | array('entry' => $entry) |
237 | ); | |
163eae0b NL |
238 | } |
239 | ||
240 | /** | |
4346a860 | 241 | * Changes read status for an entry. |
163eae0b | 242 | * |
3d2b2d62 J |
243 | * @param Request $request |
244 | * @param Entry $entry | |
245 | * | |
163eae0b | 246 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") |
3d2b2d62 | 247 | * |
163eae0b NL |
248 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
249 | */ | |
be463487 | 250 | public function toggleArchiveAction(Request $request, Entry $entry) |
163eae0b | 251 | { |
3d2b2d62 J |
252 | $this->checkUserAction($entry); |
253 | ||
163eae0b NL |
254 | $entry->toggleArchive(); |
255 | $this->getDoctrine()->getManager()->flush(); | |
256 | ||
257 | $this->get('session')->getFlashBag()->add( | |
258 | 'notice', | |
8ce32af6 | 259 | 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived') |
163eae0b NL |
260 | ); |
261 | ||
262 | return $this->redirect($request->headers->get('referer')); | |
263 | } | |
264 | ||
265 | /** | |
4346a860 | 266 | * Changes favorite status for an entry. |
163eae0b | 267 | * |
3d2b2d62 J |
268 | * @param Request $request |
269 | * @param Entry $entry | |
270 | * | |
163eae0b | 271 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") |
3d2b2d62 | 272 | * |
163eae0b NL |
273 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
274 | */ | |
be463487 | 275 | public function toggleStarAction(Request $request, Entry $entry) |
163eae0b | 276 | { |
3d2b2d62 J |
277 | $this->checkUserAction($entry); |
278 | ||
163eae0b NL |
279 | $entry->toggleStar(); |
280 | $this->getDoctrine()->getManager()->flush(); | |
281 | ||
282 | $this->get('session')->getFlashBag()->add( | |
283 | 'notice', | |
8ce32af6 | 284 | 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred') |
163eae0b NL |
285 | ); |
286 | ||
287 | return $this->redirect($request->headers->get('referer')); | |
288 | } | |
289 | ||
290 | /** | |
16a3d04c | 291 | * Deletes entry and redirect to the homepage. |
163eae0b | 292 | * |
16a3d04c | 293 | * @param Entry $entry |
3d2b2d62 | 294 | * |
163eae0b | 295 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") |
3d2b2d62 | 296 | * |
163eae0b NL |
297 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
298 | */ | |
16a3d04c | 299 | public function deleteEntryAction(Entry $entry) |
163eae0b | 300 | { |
3d2b2d62 J |
301 | $this->checkUserAction($entry); |
302 | ||
1d147791 NL |
303 | $em = $this->getDoctrine()->getManager(); |
304 | $em->remove($entry); | |
305 | $em->flush(); | |
163eae0b NL |
306 | |
307 | $this->get('session')->getFlashBag()->add( | |
308 | 'notice', | |
309 | 'Entry deleted' | |
310 | ); | |
bd9f0815 | 311 | |
16a3d04c | 312 | return $this->redirect($this->generateUrl('homepage')); |
bd9f0815 | 313 | } |
3d2b2d62 J |
314 | |
315 | /** | |
4346a860 | 316 | * Check if the logged user can manage the given entry. |
3d2b2d62 J |
317 | * |
318 | * @param Entry $entry | |
319 | */ | |
320 | private function checkUserAction(Entry $entry) | |
321 | { | |
322 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | |
82d6d9cb | 323 | throw $this->createAccessDeniedException('You can not access this entry.'); |
3d2b2d62 J |
324 | } |
325 | } | |
9d50517c | 326 | } |