]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Controller; |
9d50517c NL |
4 | |
5 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
6 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
163eae0b | 7 | use Symfony\Component\HttpFoundation\Request; |
be463487 | 8 | use Wallabag\CoreBundle\Entity\Entry; |
6b767d1c | 9 | use Wallabag\CoreBundle\Service\Extractor; |
82d6d9cb JB |
10 | use Wallabag\CoreBundle\Form\Type\NewEntryType; |
11 | use Wallabag\CoreBundle\Form\Type\EditEntryType; | |
9d50517c NL |
12 | |
13 | class EntryController extends Controller | |
14 | { | |
b84a8055 | 15 | /** |
3d2b2d62 J |
16 | * @param Request $request |
17 | * | |
b84a8055 | 18 | * @Route("/new", name="new_entry") |
3d2b2d62 | 19 | * |
b84a8055 NL |
20 | * @return \Symfony\Component\HttpFoundation\Response |
21 | */ | |
22 | public function addEntryAction(Request $request) | |
23 | { | |
3b815d2d | 24 | $entry = new Entry($this->getUser()); |
b84a8055 | 25 | |
82d6d9cb | 26 | $form = $this->createForm(new NewEntryType(), $entry); |
b84a8055 NL |
27 | |
28 | $form->handleRequest($request); | |
29 | ||
30 | if ($form->isValid()) { | |
6b767d1c | 31 | $content = Extractor::extract($entry->getUrl()); |
b84a8055 | 32 | |
6b767d1c NL |
33 | $entry->setTitle($content->getTitle()); |
34 | $entry->setContent($content->getBody()); | |
35 | ||
36 | $em = $this->getDoctrine()->getManager(); | |
b84a8055 NL |
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 | ||
ad4d1caa | 48 | return $this->render('WallabagCoreBundle:Entry:new.html.twig', array( |
b84a8055 NL |
49 | 'form' => $form->createView(), |
50 | )); | |
51 | } | |
52 | ||
82d6d9cb JB |
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 | ||
9d50517c | 89 | /** |
4346a860 | 90 | * Shows unread entries for current user. |
163eae0b | 91 | * |
9d50517c | 92 | * @Route("/unread", name="unread") |
3d2b2d62 | 93 | * |
163eae0b | 94 | * @return \Symfony\Component\HttpFoundation\Response |
9d50517c | 95 | */ |
bd9f0815 | 96 | public function showUnreadAction() |
9d50517c | 97 | { |
a8c90c5c | 98 | // TODO change pagination |
3b815d2d J |
99 | $entries = $this->getDoctrine() |
100 | ->getRepository('WallabagCoreBundle:Entry') | |
101 | ->findUnreadByUser($this->getUser()->getId(), 0); | |
9d50517c NL |
102 | |
103 | return $this->render( | |
ad4d1caa | 104 | 'WallabagCoreBundle:Entry:entries.html.twig', |
9d50517c NL |
105 | array('entries' => $entries) |
106 | ); | |
9d50517c | 107 | } |
bd9f0815 NL |
108 | |
109 | /** | |
4346a860 | 110 | * Shows read entries for current user. |
163eae0b | 111 | * |
bd9f0815 | 112 | * @Route("/archive", name="archive") |
3d2b2d62 | 113 | * |
163eae0b | 114 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 NL |
115 | */ |
116 | public function showArchiveAction() | |
117 | { | |
a8c90c5c | 118 | // TODO change pagination |
3b815d2d J |
119 | $entries = $this->getDoctrine() |
120 | ->getRepository('WallabagCoreBundle:Entry') | |
121 | ->findArchiveByUser($this->getUser()->getId(), 0); | |
bd9f0815 NL |
122 | |
123 | return $this->render( | |
ad4d1caa | 124 | 'WallabagCoreBundle:Entry:entries.html.twig', |
bd9f0815 NL |
125 | array('entries' => $entries) |
126 | ); | |
bd9f0815 NL |
127 | } |
128 | ||
129 | /** | |
4346a860 | 130 | * Shows starred entries for current user. |
163eae0b | 131 | * |
bd9f0815 | 132 | * @Route("/starred", name="starred") |
3d2b2d62 | 133 | * |
163eae0b | 134 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 NL |
135 | */ |
136 | public function showStarredAction() | |
137 | { | |
a8c90c5c | 138 | // TODO change pagination |
3b815d2d J |
139 | $entries = $this->getDoctrine() |
140 | ->getRepository('WallabagCoreBundle:Entry') | |
141 | ->findStarredByUser($this->getUser()->getId(), 0); | |
bd9f0815 NL |
142 | |
143 | return $this->render( | |
ad4d1caa | 144 | 'WallabagCoreBundle:Entry:entries.html.twig', |
bd9f0815 NL |
145 | array('entries' => $entries) |
146 | ); | |
bd9f0815 NL |
147 | } |
148 | ||
149 | /** | |
4346a860 | 150 | * Shows entry content. |
163eae0b | 151 | * |
3d2b2d62 J |
152 | * @param Entry $entry |
153 | * | |
bd9f0815 | 154 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") |
3d2b2d62 | 155 | * |
163eae0b | 156 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 | 157 | */ |
be463487 | 158 | public function viewAction(Entry $entry) |
bd9f0815 | 159 | { |
3d2b2d62 J |
160 | $this->checkUserAction($entry); |
161 | ||
bd9f0815 | 162 | return $this->render( |
ad4d1caa | 163 | 'WallabagCoreBundle:Entry:entry.html.twig', |
bd9f0815 NL |
164 | array('entry' => $entry) |
165 | ); | |
163eae0b NL |
166 | } |
167 | ||
168 | /** | |
4346a860 | 169 | * Changes read status for an entry. |
163eae0b | 170 | * |
3d2b2d62 J |
171 | * @param Request $request |
172 | * @param Entry $entry | |
173 | * | |
163eae0b | 174 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") |
3d2b2d62 | 175 | * |
163eae0b NL |
176 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
177 | */ | |
be463487 | 178 | public function toggleArchiveAction(Request $request, Entry $entry) |
163eae0b | 179 | { |
3d2b2d62 J |
180 | $this->checkUserAction($entry); |
181 | ||
163eae0b NL |
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 | /** | |
4346a860 | 194 | * Changes favorite status for an entry. |
163eae0b | 195 | * |
3d2b2d62 J |
196 | * @param Request $request |
197 | * @param Entry $entry | |
198 | * | |
163eae0b | 199 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") |
3d2b2d62 | 200 | * |
163eae0b NL |
201 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
202 | */ | |
be463487 | 203 | public function toggleStarAction(Request $request, Entry $entry) |
163eae0b | 204 | { |
3d2b2d62 J |
205 | $this->checkUserAction($entry); |
206 | ||
163eae0b NL |
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 | /** | |
4346a860 | 219 | * Deletes entry. |
163eae0b | 220 | * |
3d2b2d62 J |
221 | * @param Request $request |
222 | * @param Entry $entry | |
223 | * | |
163eae0b | 224 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") |
3d2b2d62 | 225 | * |
163eae0b NL |
226 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
227 | */ | |
be463487 | 228 | public function deleteEntryAction(Request $request, Entry $entry) |
163eae0b | 229 | { |
3d2b2d62 J |
230 | $this->checkUserAction($entry); |
231 | ||
1d147791 NL |
232 | $em = $this->getDoctrine()->getManager(); |
233 | $em->remove($entry); | |
234 | $em->flush(); | |
163eae0b NL |
235 | |
236 | $this->get('session')->getFlashBag()->add( | |
237 | 'notice', | |
238 | 'Entry deleted' | |
239 | ); | |
bd9f0815 | 240 | |
163eae0b | 241 | return $this->redirect($request->headers->get('referer')); |
bd9f0815 | 242 | } |
3d2b2d62 J |
243 | |
244 | /** | |
4346a860 | 245 | * Check if the logged user can manage the given entry. |
3d2b2d62 J |
246 | * |
247 | * @param Entry $entry | |
248 | */ | |
249 | private function checkUserAction(Entry $entry) | |
250 | { | |
251 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | |
82d6d9cb | 252 | throw $this->createAccessDeniedException('You can not access this entry.'); |
3d2b2d62 J |
253 | } |
254 | } | |
9d50517c | 255 | } |