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