]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/EntryController.php
Merge pull request #1389 from wallabag/v2-fix-duplicate
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / EntryController.php
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 unread entries for current user.
106 *
107 * @param Request $request
108 * @param int $page
109 *
110 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
111 *
112 * @return \Symfony\Component\HttpFoundation\Response
113 */
114 public function showUnreadAction(Request $request, $page)
115 {
116 return $this->showEntries('unread', $request, $page);
117 }
118
119 /**
120 * Shows read entries for current user.
121 *
122 * @param Request $request
123 * @param int $page
124 *
125 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
126 *
127 * @return \Symfony\Component\HttpFoundation\Response
128 */
129 public function showArchiveAction(Request $request, $page)
130 {
131 return $this->showEntries('archive', $request, $page);
132 }
133
134 /**
135 * Shows starred entries for current user.
136 *
137 * @param Request $request
138 * @param int $page
139 *
140 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
141 *
142 * @return \Symfony\Component\HttpFoundation\Response
143 */
144 public function showStarredAction(Request $request, $page)
145 {
146 return $this->showEntries('starred', $request, $page);
147 }
148
149 /**
150 * Global method to retrieve entries depending on the given type
151 * It returns the response to be send.
152 *
153 * @param string $type Entries type: unread, starred or archive
154 * @param Request $request
155 * @param int $page
156 *
157 * @return \Symfony\Component\HttpFoundation\Response
158 */
159 private function showEntries($type, Request $request, $page)
160 {
161 $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
162
163 switch ($type) {
164 case 'starred':
165 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
166 break;
167
168 case 'archive':
169 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
170 break;
171
172 case 'unread':
173 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
174 break;
175
176 default:
177 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
178 }
179
180 $form = $this->get('form.factory')->create(new EntryFilterType());
181
182 if ($request->query->has($form->getName())) {
183 // manually bind values from the request
184 $form->submit($request->query->get($form->getName()));
185
186 // build the query from the given form object
187 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
188 }
189
190 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
191 $entries = new Pagerfanta($pagerAdapter);
192
193 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
194 $entries->setCurrentPage($page);
195
196 return $this->render(
197 'WallabagCoreBundle:Entry:entries.html.twig',
198 array(
199 'form' => $form->createView(),
200 'entries' => $entries,
201 'currentPage' => $page,
202 )
203 );
204 }
205
206 /**
207 * Shows entry content.
208 *
209 * @param Entry $entry
210 *
211 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
212 *
213 * @return \Symfony\Component\HttpFoundation\Response
214 */
215 public function viewAction(Entry $entry)
216 {
217 $this->checkUserAction($entry);
218
219 return $this->render(
220 'WallabagCoreBundle:Entry:entry.html.twig',
221 array('entry' => $entry)
222 );
223 }
224
225 /**
226 * Changes read status for an entry.
227 *
228 * @param Request $request
229 * @param Entry $entry
230 *
231 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
232 *
233 * @return \Symfony\Component\HttpFoundation\RedirectResponse
234 */
235 public function toggleArchiveAction(Request $request, Entry $entry)
236 {
237 $this->checkUserAction($entry);
238
239 $entry->toggleArchive();
240 $this->getDoctrine()->getManager()->flush();
241
242 $this->get('session')->getFlashBag()->add(
243 'notice',
244 'Entry '.($entry->isArchived() ? 'archived' : 'unarchived')
245 );
246
247 return $this->redirect($request->headers->get('referer'));
248 }
249
250 /**
251 * Changes favorite status for an entry.
252 *
253 * @param Request $request
254 * @param Entry $entry
255 *
256 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
257 *
258 * @return \Symfony\Component\HttpFoundation\RedirectResponse
259 */
260 public function toggleStarAction(Request $request, Entry $entry)
261 {
262 $this->checkUserAction($entry);
263
264 $entry->toggleStar();
265 $this->getDoctrine()->getManager()->flush();
266
267 $this->get('session')->getFlashBag()->add(
268 'notice',
269 'Entry '.($entry->isStarred() ? 'starred' : 'unstarred')
270 );
271
272 return $this->redirect($request->headers->get('referer'));
273 }
274
275 /**
276 * Deletes entry.
277 *
278 * @param Request $request
279 * @param Entry $entry
280 *
281 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
282 *
283 * @return \Symfony\Component\HttpFoundation\RedirectResponse
284 */
285 public function deleteEntryAction(Request $request, Entry $entry)
286 {
287 $this->checkUserAction($entry);
288
289 $em = $this->getDoctrine()->getManager();
290 $em->remove($entry);
291 $em->flush();
292
293 $this->get('session')->getFlashBag()->add(
294 'notice',
295 'Entry deleted'
296 );
297
298 return $this->redirect($request->headers->get('referer'));
299 }
300
301 /**
302 * Check if the logged user can manage the given entry.
303 *
304 * @param Entry $entry
305 */
306 private function checkUserAction(Entry $entry)
307 {
308 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
309 throw $this->createAccessDeniedException('You can not access this entry.');
310 }
311 }
312 }