]>
Commit | Line | Data |
---|---|---|
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/list/{page}", name="unread", defaults={"page" = "1"}) | |
93 | * | |
94 | * @return \Symfony\Component\HttpFoundation\Response | |
95 | */ | |
96 | public function showUnreadAction($page) | |
97 | { | |
98 | $entries = $this->getDoctrine() | |
99 | ->getRepository('WallabagCoreBundle:Entry') | |
100 | ->findUnreadByUser($this->getUser()->getId()); | |
101 | ||
102 | $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); | |
103 | $entries->setCurrentPage($page); | |
104 | ||
105 | return $this->render( | |
106 | 'WallabagCoreBundle:Entry:entries.html.twig', | |
107 | array( | |
108 | 'entries' => $entries, | |
109 | 'currentPage' => $page | |
110 | ) | |
111 | ); | |
112 | } | |
113 | ||
114 | /** | |
115 | * Shows read entries for current user. | |
116 | * | |
117 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) | |
118 | * | |
119 | * @return \Symfony\Component\HttpFoundation\Response | |
120 | */ | |
121 | public function showArchiveAction($page) | |
122 | { | |
123 | $entries = $this->getDoctrine() | |
124 | ->getRepository('WallabagCoreBundle:Entry') | |
125 | ->findArchiveByUser($this->getUser()->getId()); | |
126 | ||
127 | $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); | |
128 | $entries->setCurrentPage($page); | |
129 | ||
130 | return $this->render( | |
131 | 'WallabagCoreBundle:Entry:entries.html.twig', | |
132 | array( | |
133 | 'entries' => $entries, | |
134 | 'currentPage' => $page | |
135 | ) | |
136 | ); | |
137 | } | |
138 | ||
139 | /** | |
140 | * Shows starred entries for current user. | |
141 | * | |
142 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) | |
143 | * | |
144 | * @return \Symfony\Component\HttpFoundation\Response | |
145 | */ | |
146 | public function showStarredAction($page) | |
147 | { | |
148 | $entries = $this->getDoctrine() | |
149 | ->getRepository('WallabagCoreBundle:Entry') | |
150 | ->findStarredByUser($this->getUser()->getId()); | |
151 | ||
152 | $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); | |
153 | $entries->setCurrentPage($page); | |
154 | ||
155 | return $this->render( | |
156 | 'WallabagCoreBundle:Entry:entries.html.twig', | |
157 | array( | |
158 | 'entries' => $entries, | |
159 | 'currentPage' => $page | |
160 | ) | |
161 | ); | |
162 | } | |
163 | ||
164 | /** | |
165 | * Shows entry content. | |
166 | * | |
167 | * @param Entry $entry | |
168 | * | |
169 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") | |
170 | * | |
171 | * @return \Symfony\Component\HttpFoundation\Response | |
172 | */ | |
173 | public function viewAction(Entry $entry) | |
174 | { | |
175 | $this->checkUserAction($entry); | |
176 | ||
177 | return $this->render( | |
178 | 'WallabagCoreBundle:Entry:entry.html.twig', | |
179 | array('entry' => $entry) | |
180 | ); | |
181 | } | |
182 | ||
183 | /** | |
184 | * Changes read status for an entry. | |
185 | * | |
186 | * @param Request $request | |
187 | * @param Entry $entry | |
188 | * | |
189 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") | |
190 | * | |
191 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
192 | */ | |
193 | public function toggleArchiveAction(Request $request, Entry $entry) | |
194 | { | |
195 | $this->checkUserAction($entry); | |
196 | ||
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 | /** | |
209 | * Changes favorite status for an entry. | |
210 | * | |
211 | * @param Request $request | |
212 | * @param Entry $entry | |
213 | * | |
214 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") | |
215 | * | |
216 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
217 | */ | |
218 | public function toggleStarAction(Request $request, Entry $entry) | |
219 | { | |
220 | $this->checkUserAction($entry); | |
221 | ||
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 | /** | |
234 | * Deletes entry. | |
235 | * | |
236 | * @param Request $request | |
237 | * @param Entry $entry | |
238 | * | |
239 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") | |
240 | * | |
241 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
242 | */ | |
243 | public function deleteEntryAction(Request $request, Entry $entry) | |
244 | { | |
245 | $this->checkUserAction($entry); | |
246 | ||
247 | $em = $this->getDoctrine()->getManager(); | |
248 | $em->remove($entry); | |
249 | $em->flush(); | |
250 | ||
251 | $this->get('session')->getFlashBag()->add( | |
252 | 'notice', | |
253 | 'Entry deleted' | |
254 | ); | |
255 | ||
256 | return $this->redirect($request->headers->get('referer')); | |
257 | } | |
258 | ||
259 | /** | |
260 | * Check if the logged user can manage the given entry. | |
261 | * | |
262 | * @param Entry $entry | |
263 | */ | |
264 | private function checkUserAction(Entry $entry) | |
265 | { | |
266 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | |
267 | throw $this->createAccessDeniedException('You can not access this entry.'); | |
268 | } | |
269 | } | |
270 | } |