]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/EntryController.php
Add basic title edition
[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", name="unread")
93 *
94 * @return \Symfony\Component\HttpFoundation\Response
95 */
96 public function showUnreadAction()
97 {
98 // TODO change pagination
99 $entries = $this->getDoctrine()
100 ->getRepository('WallabagCoreBundle:Entry')
101 ->findUnreadByUser($this->getUser()->getId(), 0);
102
103 return $this->render(
104 'WallabagCoreBundle:Entry:entries.html.twig',
105 array('entries' => $entries)
106 );
107 }
108
109 /**
110 * Shows read entries for current user.
111 *
112 * @Route("/archive", name="archive")
113 *
114 * @return \Symfony\Component\HttpFoundation\Response
115 */
116 public function showArchiveAction()
117 {
118 // TODO change pagination
119 $entries = $this->getDoctrine()
120 ->getRepository('WallabagCoreBundle:Entry')
121 ->findArchiveByUser($this->getUser()->getId(), 0);
122
123 return $this->render(
124 'WallabagCoreBundle:Entry:entries.html.twig',
125 array('entries' => $entries)
126 );
127 }
128
129 /**
130 * Shows starred entries for current user.
131 *
132 * @Route("/starred", name="starred")
133 *
134 * @return \Symfony\Component\HttpFoundation\Response
135 */
136 public function showStarredAction()
137 {
138 // TODO change pagination
139 $entries = $this->getDoctrine()
140 ->getRepository('WallabagCoreBundle:Entry')
141 ->findStarredByUser($this->getUser()->getId(), 0);
142
143 return $this->render(
144 'WallabagCoreBundle:Entry:entries.html.twig',
145 array('entries' => $entries)
146 );
147 }
148
149 /**
150 * Shows entry content.
151 *
152 * @param Entry $entry
153 *
154 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
155 *
156 * @return \Symfony\Component\HttpFoundation\Response
157 */
158 public function viewAction(Entry $entry)
159 {
160 $this->checkUserAction($entry);
161
162 return $this->render(
163 'WallabagCoreBundle:Entry:entry.html.twig',
164 array('entry' => $entry)
165 );
166 }
167
168 /**
169 * Changes read status for an entry.
170 *
171 * @param Request $request
172 * @param Entry $entry
173 *
174 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
175 *
176 * @return \Symfony\Component\HttpFoundation\RedirectResponse
177 */
178 public function toggleArchiveAction(Request $request, Entry $entry)
179 {
180 $this->checkUserAction($entry);
181
182 $entry->toggleArchive();
183 $this->getDoctrine()->getManager()->flush();
184
185 $this->get('session')->getFlashBag()->add(
186 'notice',
187 'Entry archived'
188 );
189
190 return $this->redirect($request->headers->get('referer'));
191 }
192
193 /**
194 * Changes favorite status for an entry.
195 *
196 * @param Request $request
197 * @param Entry $entry
198 *
199 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
200 *
201 * @return \Symfony\Component\HttpFoundation\RedirectResponse
202 */
203 public function toggleStarAction(Request $request, Entry $entry)
204 {
205 $this->checkUserAction($entry);
206
207 $entry->toggleStar();
208 $this->getDoctrine()->getManager()->flush();
209
210 $this->get('session')->getFlashBag()->add(
211 'notice',
212 'Entry starred'
213 );
214
215 return $this->redirect($request->headers->get('referer'));
216 }
217
218 /**
219 * Deletes entry.
220 *
221 * @param Request $request
222 * @param Entry $entry
223 *
224 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
225 *
226 * @return \Symfony\Component\HttpFoundation\RedirectResponse
227 */
228 public function deleteEntryAction(Request $request, Entry $entry)
229 {
230 $this->checkUserAction($entry);
231
232 $em = $this->getDoctrine()->getManager();
233 $em->remove($entry);
234 $em->flush();
235
236 $this->get('session')->getFlashBag()->add(
237 'notice',
238 'Entry deleted'
239 );
240
241 return $this->redirect($request->headers->get('referer'));
242 }
243
244 /**
245 * Check if the logged user can manage the given entry.
246 *
247 * @param Entry $entry
248 */
249 private function checkUserAction(Entry $entry)
250 {
251 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
252 throw $this->createAccessDeniedException('You can not access this entry.');
253 }
254 }
255 }