]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/EntryController.php
Merge pull request #1250 from frankoa/v2_pagination_and_api
[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
13 class EntryController extends Controller
14 {
15 /**
16 * @param Request $request
17 *
18 * @Route("/new", name="new_entry")
19 *
20 * @return \Symfony\Component\HttpFoundation\Response
21 */
22 public function addEntryAction(Request $request)
23 {
24 $entry = new Entry($this->getUser());
25
26 $form = $this->createForm(new NewEntryType(), $entry);
27
28 $form->handleRequest($request);
29
30 if ($form->isValid()) {
31 $content = Extractor::extract($entry->getUrl());
32
33 $entry->setTitle($content->getTitle());
34 $entry->setContent($content->getBody());
35
36 $em = $this->getDoctrine()->getManager();
37 $em->persist($entry);
38 $em->flush();
39
40 $this->get('session')->getFlashBag()->add(
41 'notice',
42 'Entry saved'
43 );
44
45 return $this->redirect($this->generateUrl('homepage'));
46 }
47
48 return $this->render('WallabagCoreBundle:Entry:new.html.twig', array(
49 'form' => $form->createView(),
50 ));
51 }
52
53 /**
54 * Edit an entry content.
55 *
56 * @param Request $request
57 * @param Entry $entry
58 *
59 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
60 *
61 * @return \Symfony\Component\HttpFoundation\Response
62 */
63 public function editEntryAction(Request $request, Entry $entry)
64 {
65 $this->checkUserAction($entry);
66
67 $form = $this->createForm(new EditEntryType(), $entry);
68
69 $form->handleRequest($request);
70
71 if ($form->isValid()) {
72 $em = $this->getDoctrine()->getManager();
73 $em->persist($entry);
74 $em->flush();
75
76 $this->get('session')->getFlashBag()->add(
77 'notice',
78 'Entry updated'
79 );
80
81 return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
82 }
83
84 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array(
85 'form' => $form->createView(),
86 ));
87 }
88
89 /**
90 * Shows unread entries for current user.
91 *
92 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
93 *
94 * @return \Symfony\Component\HttpFoundation\Response
95 */
96 public function showUnreadAction($page)
97 {
98 $entries = $this->getDoctrine()
99 ->getRepository('WallabagCoreBundle:Entry')
100 ->findUnreadByUser($this->getUser()->getId());
101
102 $entries->setCurrentPage($page);
103
104 return $this->render(
105 'WallabagCoreBundle:Entry:entries.html.twig',
106 array(
107 'entries' => $entries,
108 'currentPage' => $page
109 )
110 );
111 }
112
113 /**
114 * Shows read entries for current user.
115 *
116 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
117 *
118 * @return \Symfony\Component\HttpFoundation\Response
119 */
120 public function showArchiveAction($page)
121 {
122 $entries = $this->getDoctrine()
123 ->getRepository('WallabagCoreBundle:Entry')
124 ->findArchiveByUser($this->getUser()->getId());
125
126 $entries->setCurrentPage($page);
127
128 return $this->render(
129 'WallabagCoreBundle:Entry:entries.html.twig',
130 array(
131 'entries' => $entries,
132 'currentPage' => $page
133 )
134 );
135 }
136
137 /**
138 * Shows starred entries for current user.
139 *
140 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
141 *
142 * @return \Symfony\Component\HttpFoundation\Response
143 */
144 public function showStarredAction($page)
145 {
146 $entries = $this->getDoctrine()
147 ->getRepository('WallabagCoreBundle:Entry')
148 ->findStarredByUser($this->getUser()->getId());
149
150 $entries->setCurrentPage($page);
151
152 return $this->render(
153 'WallabagCoreBundle:Entry:entries.html.twig',
154 array(
155 'entries' => $entries,
156 'currentPage' => $page
157 )
158 );
159 }
160
161 /**
162 * Shows entry content.
163 *
164 * @param Entry $entry
165 *
166 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
167 *
168 * @return \Symfony\Component\HttpFoundation\Response
169 */
170 public function viewAction(Entry $entry)
171 {
172 $this->checkUserAction($entry);
173
174 return $this->render(
175 'WallabagCoreBundle:Entry:entry.html.twig',
176 array('entry' => $entry)
177 );
178 }
179
180 /**
181 * Changes read status for an entry.
182 *
183 * @param Request $request
184 * @param Entry $entry
185 *
186 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
187 *
188 * @return \Symfony\Component\HttpFoundation\RedirectResponse
189 */
190 public function toggleArchiveAction(Request $request, Entry $entry)
191 {
192 $this->checkUserAction($entry);
193
194 $entry->toggleArchive();
195 $this->getDoctrine()->getManager()->flush();
196
197 $this->get('session')->getFlashBag()->add(
198 'notice',
199 'Entry archived'
200 );
201
202 return $this->redirect($request->headers->get('referer'));
203 }
204
205 /**
206 * Changes favorite status for an entry.
207 *
208 * @param Request $request
209 * @param Entry $entry
210 *
211 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
212 *
213 * @return \Symfony\Component\HttpFoundation\RedirectResponse
214 */
215 public function toggleStarAction(Request $request, Entry $entry)
216 {
217 $this->checkUserAction($entry);
218
219 $entry->toggleStar();
220 $this->getDoctrine()->getManager()->flush();
221
222 $this->get('session')->getFlashBag()->add(
223 'notice',
224 'Entry starred'
225 );
226
227 return $this->redirect($request->headers->get('referer'));
228 }
229
230 /**
231 * Deletes entry.
232 *
233 * @param Request $request
234 * @param Entry $entry
235 *
236 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
237 *
238 * @return \Symfony\Component\HttpFoundation\RedirectResponse
239 */
240 public function deleteEntryAction(Request $request, Entry $entry)
241 {
242 $this->checkUserAction($entry);
243
244 $em = $this->getDoctrine()->getManager();
245 $em->remove($entry);
246 $em->flush();
247
248 $this->get('session')->getFlashBag()->add(
249 'notice',
250 'Entry deleted'
251 );
252
253 return $this->redirect($request->headers->get('referer'));
254 }
255
256 /**
257 * Check if the logged user can manage the given entry.
258 *
259 * @param Entry $entry
260 */
261 private function checkUserAction(Entry $entry)
262 {
263 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
264 throw $this->createAccessDeniedException('You can not access this entry.');
265 }
266 }
267 }