]>
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 | * | |
b84a8055 | 21 | * @Route("/new", name="new_entry") |
3d2b2d62 | 22 | * |
b84a8055 NL |
23 | * @return \Symfony\Component\HttpFoundation\Response |
24 | */ | |
25 | public function addEntryAction(Request $request) | |
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 | ||
ad4d1caa | 51 | return $this->render('WallabagCoreBundle:Entry:new.html.twig', array( |
b84a8055 NL |
52 | 'form' => $form->createView(), |
53 | )); | |
54 | } | |
55 | ||
82d6d9cb JB |
56 | /** |
57 | * Edit an entry content. | |
58 | * | |
59 | * @param Request $request | |
60 | * @param Entry $entry | |
61 | * | |
62 | * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit") | |
63 | * | |
64 | * @return \Symfony\Component\HttpFoundation\Response | |
65 | */ | |
66 | public function editEntryAction(Request $request, Entry $entry) | |
67 | { | |
68 | $this->checkUserAction($entry); | |
69 | ||
70 | $form = $this->createForm(new EditEntryType(), $entry); | |
71 | ||
72 | $form->handleRequest($request); | |
73 | ||
74 | if ($form->isValid()) { | |
75 | $em = $this->getDoctrine()->getManager(); | |
76 | $em->persist($entry); | |
77 | $em->flush(); | |
78 | ||
79 | $this->get('session')->getFlashBag()->add( | |
80 | 'notice', | |
81 | 'Entry updated' | |
82 | ); | |
83 | ||
84 | return $this->redirect($this->generateUrl('view', array('id' => $entry->getId()))); | |
85 | } | |
86 | ||
87 | return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array( | |
88 | 'form' => $form->createView(), | |
89 | )); | |
90 | } | |
91 | ||
9d50517c | 92 | /** |
4346a860 | 93 | * Shows unread entries for current user. |
163eae0b | 94 | * |
26864574 NL |
95 | * @param Request $request |
96 | * @param int $page | |
97 | * | |
9fb6ac83 | 98 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) |
3d2b2d62 | 99 | * |
163eae0b | 100 | * @return \Symfony\Component\HttpFoundation\Response |
9d50517c | 101 | */ |
26864574 | 102 | public function showUnreadAction(Request $request, $page) |
9d50517c | 103 | { |
26864574 NL |
104 | $form = $this->get('form.factory')->create(new EntryFilterType()); |
105 | ||
106 | $filterBuilder = $this->getDoctrine() | |
3b815d2d | 107 | ->getRepository('WallabagCoreBundle:Entry') |
9fb6ac83 FG |
108 | ->findUnreadByUser($this->getUser()->getId()); |
109 | ||
26864574 NL |
110 | if ($request->query->has($form->getName())) { |
111 | // manually bind values from the request | |
112 | $form->submit($request->query->get($form->getName())); | |
113 | ||
114 | // build the query from the given form object | |
115 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $filterBuilder); | |
116 | } | |
117 | ||
118 | $pagerAdapter = new DoctrineORMAdapter($filterBuilder->getQuery()); | |
119 | $entries = new Pagerfanta($pagerAdapter); | |
120 | ||
48ffc5a4 | 121 | $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); |
9fb6ac83 | 122 | $entries->setCurrentPage($page); |
9d50517c NL |
123 | |
124 | return $this->render( | |
ad4d1caa | 125 | 'WallabagCoreBundle:Entry:entries.html.twig', |
9fb6ac83 | 126 | array( |
26864574 | 127 | 'form' => $form->createView(), |
9fb6ac83 FG |
128 | 'entries' => $entries, |
129 | 'currentPage' => $page | |
130 | ) | |
9d50517c | 131 | ); |
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 | { |
26864574 NL |
146 | $form = $this->get('form.factory')->create(new EntryFilterType()); |
147 | ||
148 | $filterBuilder = $this->getDoctrine() | |
3b815d2d | 149 | ->getRepository('WallabagCoreBundle:Entry') |
9fb6ac83 FG |
150 | ->findArchiveByUser($this->getUser()->getId()); |
151 | ||
26864574 NL |
152 | if ($request->query->has($form->getName())) { |
153 | // manually bind values from the request | |
154 | $form->submit($request->query->get($form->getName())); | |
155 | ||
156 | // build the query from the given form object | |
157 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $filterBuilder); | |
158 | } | |
159 | ||
160 | $pagerAdapter = new DoctrineORMAdapter($filterBuilder->getQuery()); | |
161 | $entries = new Pagerfanta($pagerAdapter); | |
162 | ||
48ffc5a4 | 163 | $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); |
9fb6ac83 | 164 | $entries->setCurrentPage($page); |
bd9f0815 NL |
165 | |
166 | return $this->render( | |
ad4d1caa | 167 | 'WallabagCoreBundle:Entry:entries.html.twig', |
9fb6ac83 | 168 | array( |
26864574 | 169 | 'form' => $form->createView(), |
9fb6ac83 FG |
170 | 'entries' => $entries, |
171 | 'currentPage' => $page | |
172 | ) | |
bd9f0815 | 173 | ); |
bd9f0815 NL |
174 | } |
175 | ||
176 | /** | |
4346a860 | 177 | * Shows starred entries for current user. |
163eae0b | 178 | * |
26864574 NL |
179 | * @param Request $request |
180 | * @param int $page | |
181 | * | |
9fb6ac83 | 182 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) |
3d2b2d62 | 183 | * |
163eae0b | 184 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 | 185 | */ |
26864574 | 186 | public function showStarredAction(Request $request, $page) |
bd9f0815 | 187 | { |
26864574 NL |
188 | $form = $this->get('form.factory')->create(new EntryFilterType()); |
189 | ||
190 | $filterBuilder = $this->getDoctrine() | |
3b815d2d | 191 | ->getRepository('WallabagCoreBundle:Entry') |
9fb6ac83 FG |
192 | ->findStarredByUser($this->getUser()->getId()); |
193 | ||
26864574 NL |
194 | if ($request->query->has($form->getName())) { |
195 | // manually bind values from the request | |
196 | $form->submit($request->query->get($form->getName())); | |
197 | ||
198 | // build the query from the given form object | |
199 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $filterBuilder); | |
200 | } | |
201 | ||
202 | $pagerAdapter = new DoctrineORMAdapter($filterBuilder->getQuery()); | |
203 | $entries = new Pagerfanta($pagerAdapter); | |
204 | ||
48ffc5a4 | 205 | $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); |
9fb6ac83 | 206 | $entries->setCurrentPage($page); |
bd9f0815 NL |
207 | |
208 | return $this->render( | |
ad4d1caa | 209 | 'WallabagCoreBundle:Entry:entries.html.twig', |
9fb6ac83 | 210 | array( |
26864574 | 211 | 'form' => $form->createView(), |
9fb6ac83 FG |
212 | 'entries' => $entries, |
213 | 'currentPage' => $page | |
214 | ) | |
bd9f0815 | 215 | ); |
bd9f0815 NL |
216 | } |
217 | ||
218 | /** | |
4346a860 | 219 | * Shows entry content. |
163eae0b | 220 | * |
3d2b2d62 J |
221 | * @param Entry $entry |
222 | * | |
bd9f0815 | 223 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") |
3d2b2d62 | 224 | * |
163eae0b | 225 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 | 226 | */ |
be463487 | 227 | public function viewAction(Entry $entry) |
bd9f0815 | 228 | { |
3d2b2d62 J |
229 | $this->checkUserAction($entry); |
230 | ||
bd9f0815 | 231 | return $this->render( |
ad4d1caa | 232 | 'WallabagCoreBundle:Entry:entry.html.twig', |
bd9f0815 NL |
233 | array('entry' => $entry) |
234 | ); | |
163eae0b NL |
235 | } |
236 | ||
237 | /** | |
4346a860 | 238 | * Changes read status for an entry. |
163eae0b | 239 | * |
3d2b2d62 J |
240 | * @param Request $request |
241 | * @param Entry $entry | |
242 | * | |
163eae0b | 243 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") |
3d2b2d62 | 244 | * |
163eae0b NL |
245 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
246 | */ | |
be463487 | 247 | public function toggleArchiveAction(Request $request, Entry $entry) |
163eae0b | 248 | { |
3d2b2d62 J |
249 | $this->checkUserAction($entry); |
250 | ||
163eae0b NL |
251 | $entry->toggleArchive(); |
252 | $this->getDoctrine()->getManager()->flush(); | |
253 | ||
254 | $this->get('session')->getFlashBag()->add( | |
255 | 'notice', | |
256 | 'Entry archived' | |
257 | ); | |
258 | ||
259 | return $this->redirect($request->headers->get('referer')); | |
260 | } | |
261 | ||
262 | /** | |
4346a860 | 263 | * Changes favorite status for an entry. |
163eae0b | 264 | * |
3d2b2d62 J |
265 | * @param Request $request |
266 | * @param Entry $entry | |
267 | * | |
163eae0b | 268 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") |
3d2b2d62 | 269 | * |
163eae0b NL |
270 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
271 | */ | |
be463487 | 272 | public function toggleStarAction(Request $request, Entry $entry) |
163eae0b | 273 | { |
3d2b2d62 J |
274 | $this->checkUserAction($entry); |
275 | ||
163eae0b NL |
276 | $entry->toggleStar(); |
277 | $this->getDoctrine()->getManager()->flush(); | |
278 | ||
279 | $this->get('session')->getFlashBag()->add( | |
280 | 'notice', | |
281 | 'Entry starred' | |
282 | ); | |
283 | ||
284 | return $this->redirect($request->headers->get('referer')); | |
285 | } | |
286 | ||
287 | /** | |
4346a860 | 288 | * Deletes entry. |
163eae0b | 289 | * |
3d2b2d62 J |
290 | * @param Request $request |
291 | * @param Entry $entry | |
292 | * | |
163eae0b | 293 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") |
3d2b2d62 | 294 | * |
163eae0b NL |
295 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
296 | */ | |
be463487 | 297 | public function deleteEntryAction(Request $request, Entry $entry) |
163eae0b | 298 | { |
3d2b2d62 J |
299 | $this->checkUserAction($entry); |
300 | ||
1d147791 NL |
301 | $em = $this->getDoctrine()->getManager(); |
302 | $em->remove($entry); | |
303 | $em->flush(); | |
163eae0b NL |
304 | |
305 | $this->get('session')->getFlashBag()->add( | |
306 | 'notice', | |
307 | 'Entry deleted' | |
308 | ); | |
bd9f0815 | 309 | |
163eae0b | 310 | return $this->redirect($request->headers->get('referer')); |
bd9f0815 | 311 | } |
3d2b2d62 J |
312 | |
313 | /** | |
4346a860 | 314 | * Check if the logged user can manage the given entry. |
3d2b2d62 J |
315 | * |
316 | * @param Entry $entry | |
317 | */ | |
318 | private function checkUserAction(Entry $entry) | |
319 | { | |
320 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | |
82d6d9cb | 321 | throw $this->createAccessDeniedException('You can not access this entry.'); |
3d2b2d62 J |
322 | } |
323 | } | |
9d50517c | 324 | } |