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