]>
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\Service\Extractor; | |
10 | use Wallabag\CoreBundle\Form\Type\NewEntryType; | |
11 | use Wallabag\CoreBundle\Form\Type\EditEntryType; | |
12 | use Wallabag\CoreBundle\Filter\EntryFilterType; | |
13 | use Pagerfanta\Adapter\DoctrineORMAdapter; | |
14 | use Pagerfanta\Pagerfanta; | |
15 | ||
16 | class EntryController extends Controller | |
17 | { | |
18 | /** | |
19 | * @param Request $request | |
20 | * | |
21 | * @Route("/new-entry", name="new_entry") | |
22 | * | |
23 | * @return \Symfony\Component\HttpFoundation\Response | |
24 | */ | |
25 | public function addEntryFormAction(Request $request) | |
26 | { | |
27 | $entry = new Entry($this->getUser()); | |
28 | ||
29 | $form = $this->createForm(new NewEntryType(), $entry); | |
30 | ||
31 | $form->handleRequest($request); | |
32 | ||
33 | if ($form->isValid()) { | |
34 | $content = Extractor::extract($entry->getUrl()); | |
35 | ||
36 | $entry->setTitle($content->getTitle()); | |
37 | $entry->setContent($content->getBody()); | |
38 | ||
39 | $em = $this->getDoctrine()->getManager(); | |
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 | ||
51 | return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array( | |
52 | 'form' => $form->createView(), | |
53 | )); | |
54 | } | |
55 | ||
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 | ||
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 | ||
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 | { | |
116 | return $this->showEntries('all', $request, $page); | |
117 | } | |
118 | ||
119 | /** | |
120 | * Shows unread entries for current user. | |
121 | * | |
122 | * @param Request $request | |
123 | * @param int $page | |
124 | * | |
125 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) | |
126 | * | |
127 | * @return \Symfony\Component\HttpFoundation\Response | |
128 | */ | |
129 | public function showUnreadAction(Request $request, $page) | |
130 | { | |
131 | return $this->showEntries('unread', $request, $page); | |
132 | } | |
133 | ||
134 | /** | |
135 | * Shows read entries for current user. | |
136 | * | |
137 | * @param Request $request | |
138 | * @param int $page | |
139 | * | |
140 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) | |
141 | * | |
142 | * @return \Symfony\Component\HttpFoundation\Response | |
143 | */ | |
144 | public function showArchiveAction(Request $request, $page) | |
145 | { | |
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 | } | |
163 | ||
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 | ||
191 | case 'all': | |
192 | $qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); | |
193 | break; | |
194 | ||
195 | default: | |
196 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
197 | } | |
198 | ||
199 | $form = $this->get('form.factory')->create(new EntryFilterType()); | |
200 | ||
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 | |
206 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); | |
207 | } | |
208 | ||
209 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); | |
210 | $entries = new Pagerfanta($pagerAdapter); | |
211 | ||
212 | $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); | |
213 | $entries->setCurrentPage($page); | |
214 | ||
215 | return $this->render( | |
216 | 'WallabagCoreBundle:Entry:entries.html.twig', | |
217 | array( | |
218 | 'form' => $form->createView(), | |
219 | 'entries' => $entries, | |
220 | 'currentPage' => $page, | |
221 | ) | |
222 | ); | |
223 | } | |
224 | ||
225 | /** | |
226 | * Shows entry content. | |
227 | * | |
228 | * @param Entry $entry | |
229 | * | |
230 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") | |
231 | * | |
232 | * @return \Symfony\Component\HttpFoundation\Response | |
233 | */ | |
234 | public function viewAction(Entry $entry) | |
235 | { | |
236 | $this->checkUserAction($entry); | |
237 | ||
238 | return $this->render( | |
239 | 'WallabagCoreBundle:Entry:entry.html.twig', | |
240 | array('entry' => $entry) | |
241 | ); | |
242 | } | |
243 | ||
244 | /** | |
245 | * Changes read status for an entry. | |
246 | * | |
247 | * @param Request $request | |
248 | * @param Entry $entry | |
249 | * | |
250 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") | |
251 | * | |
252 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
253 | */ | |
254 | public function toggleArchiveAction(Request $request, Entry $entry) | |
255 | { | |
256 | $this->checkUserAction($entry); | |
257 | ||
258 | $entry->toggleArchive(); | |
259 | $this->getDoctrine()->getManager()->flush(); | |
260 | ||
261 | $this->get('session')->getFlashBag()->add( | |
262 | 'notice', | |
263 | 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived') | |
264 | ); | |
265 | ||
266 | return $this->redirect($request->headers->get('referer')); | |
267 | } | |
268 | ||
269 | /** | |
270 | * Changes favorite status for an entry. | |
271 | * | |
272 | * @param Request $request | |
273 | * @param Entry $entry | |
274 | * | |
275 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") | |
276 | * | |
277 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
278 | */ | |
279 | public function toggleStarAction(Request $request, Entry $entry) | |
280 | { | |
281 | $this->checkUserAction($entry); | |
282 | ||
283 | $entry->toggleStar(); | |
284 | $this->getDoctrine()->getManager()->flush(); | |
285 | ||
286 | $this->get('session')->getFlashBag()->add( | |
287 | 'notice', | |
288 | 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred') | |
289 | ); | |
290 | ||
291 | return $this->redirect($request->headers->get('referer')); | |
292 | } | |
293 | ||
294 | /** | |
295 | * Deletes entry. | |
296 | * | |
297 | * @param Request $request | |
298 | * @param Entry $entry | |
299 | * | |
300 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") | |
301 | * | |
302 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
303 | */ | |
304 | public function deleteEntryAction(Request $request, Entry $entry) | |
305 | { | |
306 | $this->checkUserAction($entry); | |
307 | ||
308 | $em = $this->getDoctrine()->getManager(); | |
309 | $em->remove($entry); | |
310 | $em->flush(); | |
311 | ||
312 | $this->get('session')->getFlashBag()->add( | |
313 | 'notice', | |
314 | 'Entry deleted' | |
315 | ); | |
316 | ||
317 | return $this->redirect($request->headers->get('referer')); | |
318 | } | |
319 | ||
320 | /** | |
321 | * Check if the logged user can manage the given entry. | |
322 | * | |
323 | * @param Entry $entry | |
324 | */ | |
325 | private function checkUserAction(Entry $entry) | |
326 | { | |
327 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | |
328 | throw $this->createAccessDeniedException('You can not access this entry.'); | |
329 | } | |
330 | } | |
331 | } |