]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/EntryController.php
Merge pull request #1317 from wallabag/v2-fix-typo
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / EntryController.php
CommitLineData
9d50517c
NL
1<?php
2
ad4d1caa 3namespace Wallabag\CoreBundle\Controller;
9d50517c
NL
4
5use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6use Symfony\Bundle\FrameworkBundle\Controller\Controller;
163eae0b 7use Symfony\Component\HttpFoundation\Request;
be463487 8use Wallabag\CoreBundle\Entity\Entry;
6b767d1c 9use Wallabag\CoreBundle\Service\Extractor;
82d6d9cb
JB
10use Wallabag\CoreBundle\Form\Type\NewEntryType;
11use Wallabag\CoreBundle\Form\Type\EditEntryType;
9d50517c
NL
12
13class 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 *
9fb6ac83 92 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
3d2b2d62 93 *
163eae0b 94 * @return \Symfony\Component\HttpFoundation\Response
9d50517c 95 */
9fb6ac83 96 public function showUnreadAction($page)
9d50517c 97 {
3b815d2d
J
98 $entries = $this->getDoctrine()
99 ->getRepository('WallabagCoreBundle:Entry')
9fb6ac83
FG
100 ->findUnreadByUser($this->getUser()->getId());
101
48ffc5a4 102 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
9fb6ac83 103 $entries->setCurrentPage($page);
9d50517c
NL
104
105 return $this->render(
ad4d1caa 106 'WallabagCoreBundle:Entry:entries.html.twig',
9fb6ac83
FG
107 array(
108 'entries' => $entries,
109 'currentPage' => $page
110 )
9d50517c 111 );
9d50517c 112 }
bd9f0815
NL
113
114 /**
4346a860 115 * Shows read entries for current user.
163eae0b 116 *
9fb6ac83 117 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
3d2b2d62 118 *
163eae0b 119 * @return \Symfony\Component\HttpFoundation\Response
bd9f0815 120 */
9fb6ac83 121 public function showArchiveAction($page)
bd9f0815 122 {
3b815d2d
J
123 $entries = $this->getDoctrine()
124 ->getRepository('WallabagCoreBundle:Entry')
9fb6ac83
FG
125 ->findArchiveByUser($this->getUser()->getId());
126
48ffc5a4 127 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
9fb6ac83 128 $entries->setCurrentPage($page);
bd9f0815
NL
129
130 return $this->render(
ad4d1caa 131 'WallabagCoreBundle:Entry:entries.html.twig',
9fb6ac83
FG
132 array(
133 'entries' => $entries,
134 'currentPage' => $page
135 )
bd9f0815 136 );
bd9f0815
NL
137 }
138
139 /**
4346a860 140 * Shows starred entries for current user.
163eae0b 141 *
9fb6ac83 142 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
3d2b2d62 143 *
163eae0b 144 * @return \Symfony\Component\HttpFoundation\Response
bd9f0815 145 */
9fb6ac83 146 public function showStarredAction($page)
bd9f0815 147 {
3b815d2d
J
148 $entries = $this->getDoctrine()
149 ->getRepository('WallabagCoreBundle:Entry')
9fb6ac83
FG
150 ->findStarredByUser($this->getUser()->getId());
151
48ffc5a4 152 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
9fb6ac83 153 $entries->setCurrentPage($page);
bd9f0815
NL
154
155 return $this->render(
ad4d1caa 156 'WallabagCoreBundle:Entry:entries.html.twig',
9fb6ac83
FG
157 array(
158 'entries' => $entries,
159 'currentPage' => $page
160 )
bd9f0815 161 );
bd9f0815
NL
162 }
163
164 /**
4346a860 165 * Shows entry content.
163eae0b 166 *
3d2b2d62
J
167 * @param Entry $entry
168 *
bd9f0815 169 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
3d2b2d62 170 *
163eae0b 171 * @return \Symfony\Component\HttpFoundation\Response
bd9f0815 172 */
be463487 173 public function viewAction(Entry $entry)
bd9f0815 174 {
3d2b2d62
J
175 $this->checkUserAction($entry);
176
bd9f0815 177 return $this->render(
ad4d1caa 178 'WallabagCoreBundle:Entry:entry.html.twig',
bd9f0815
NL
179 array('entry' => $entry)
180 );
163eae0b
NL
181 }
182
183 /**
4346a860 184 * Changes read status for an entry.
163eae0b 185 *
3d2b2d62
J
186 * @param Request $request
187 * @param Entry $entry
188 *
163eae0b 189 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
3d2b2d62 190 *
163eae0b
NL
191 * @return \Symfony\Component\HttpFoundation\RedirectResponse
192 */
be463487 193 public function toggleArchiveAction(Request $request, Entry $entry)
163eae0b 194 {
3d2b2d62
J
195 $this->checkUserAction($entry);
196
163eae0b
NL
197 $entry->toggleArchive();
198 $this->getDoctrine()->getManager()->flush();
199
200 $this->get('session')->getFlashBag()->add(
201 'notice',
202 'Entry archived'
203 );
204
205 return $this->redirect($request->headers->get('referer'));
206 }
207
208 /**
4346a860 209 * Changes favorite status for an entry.
163eae0b 210 *
3d2b2d62
J
211 * @param Request $request
212 * @param Entry $entry
213 *
163eae0b 214 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
3d2b2d62 215 *
163eae0b
NL
216 * @return \Symfony\Component\HttpFoundation\RedirectResponse
217 */
be463487 218 public function toggleStarAction(Request $request, Entry $entry)
163eae0b 219 {
3d2b2d62
J
220 $this->checkUserAction($entry);
221
163eae0b
NL
222 $entry->toggleStar();
223 $this->getDoctrine()->getManager()->flush();
224
225 $this->get('session')->getFlashBag()->add(
226 'notice',
227 'Entry starred'
228 );
229
230 return $this->redirect($request->headers->get('referer'));
231 }
232
233 /**
4346a860 234 * Deletes entry.
163eae0b 235 *
3d2b2d62
J
236 * @param Request $request
237 * @param Entry $entry
238 *
163eae0b 239 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
3d2b2d62 240 *
163eae0b
NL
241 * @return \Symfony\Component\HttpFoundation\RedirectResponse
242 */
be463487 243 public function deleteEntryAction(Request $request, Entry $entry)
163eae0b 244 {
3d2b2d62
J
245 $this->checkUserAction($entry);
246
1d147791
NL
247 $em = $this->getDoctrine()->getManager();
248 $em->remove($entry);
249 $em->flush();
163eae0b
NL
250
251 $this->get('session')->getFlashBag()->add(
252 'notice',
253 'Entry deleted'
254 );
bd9f0815 255
163eae0b 256 return $this->redirect($request->headers->get('referer'));
bd9f0815 257 }
3d2b2d62
J
258
259 /**
4346a860 260 * Check if the logged user can manage the given entry.
3d2b2d62
J
261 *
262 * @param Entry $entry
263 */
264 private function checkUserAction(Entry $entry)
265 {
266 if ($this->getUser()->getId() != $entry->getUser()->getId()) {
82d6d9cb 267 throw $this->createAccessDeniedException('You can not access this entry.');
3d2b2d62
J
268 }
269 }
9d50517c 270}