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