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