]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/EntryController.php
Merge pull request #1072 from wallabag/v2-entry-test
[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\Helper\Url;
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->createFormBuilder($entry)
26 ->add('url', 'url')
27 ->add('save', 'submit')
28 ->getForm();
29
30 $form->handleRequest($request);
31
32 if ($form->isValid()) {
33 $content = Extractor::extract($entry->getUrl());
34
35 $entry->setTitle($content->getTitle());
36 $entry->setContent($content->getBody());
37
38 $em = $this->getDoctrine()->getManager();
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
50 return $this->render('WallabagCoreBundle:Entry:new.html.twig', array(
51 'form' => $form->createView(),
52 ));
53 }
54
55 /**
56 * Shows unread entries for current user
57 *
58 * @Route("/unread", name="unread")
59 *
60 * @return \Symfony\Component\HttpFoundation\Response
61 */
62 public function showUnreadAction()
63 {
64 // TODO change pagination
65 $entries = $this->getDoctrine()
66 ->getRepository('WallabagCoreBundle:Entry')
67 ->findUnreadByUser($this->getUser()->getId(), 0);
68
69 return $this->render(
70 'WallabagCoreBundle:Entry:entries.html.twig',
71 array('entries' => $entries)
72 );
73 }
74
75 /**
76 * Shows read entries for current user
77 *
78 * @Route("/archive", name="archive")
79 *
80 * @return \Symfony\Component\HttpFoundation\Response
81 */
82 public function showArchiveAction()
83 {
84 // TODO change pagination
85 $entries = $this->getDoctrine()
86 ->getRepository('WallabagCoreBundle:Entry')
87 ->findArchiveByUser($this->getUser()->getId(), 0);
88
89 return $this->render(
90 'WallabagCoreBundle:Entry:entries.html.twig',
91 array('entries' => $entries)
92 );
93 }
94
95 /**
96 * Shows starred entries for current user
97 *
98 * @Route("/starred", name="starred")
99 *
100 * @return \Symfony\Component\HttpFoundation\Response
101 */
102 public function showStarredAction()
103 {
104 // TODO change pagination
105 $entries = $this->getDoctrine()
106 ->getRepository('WallabagCoreBundle:Entry')
107 ->findStarredByUser($this->getUser()->getId(), 0);
108
109 return $this->render(
110 'WallabagCoreBundle:Entry:entries.html.twig',
111 array('entries' => $entries)
112 );
113 }
114
115 /**
116 * Shows entry content
117 *
118 * @param Entry $entry
119 *
120 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
121 *
122 * @return \Symfony\Component\HttpFoundation\Response
123 */
124 public function viewAction(Entry $entry)
125 {
126 $this->checkUserAction($entry);
127
128 return $this->render(
129 'WallabagCoreBundle:Entry:entry.html.twig',
130 array('entry' => $entry)
131 );
132 }
133
134 /**
135 * Changes read status for an entry
136 *
137 * @param Request $request
138 * @param Entry $entry
139 *
140 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
141 *
142 * @return \Symfony\Component\HttpFoundation\RedirectResponse
143 */
144 public function toggleArchiveAction(Request $request, Entry $entry)
145 {
146 $this->checkUserAction($entry);
147
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 *
162 * @param Request $request
163 * @param Entry $entry
164 *
165 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
166 *
167 * @return \Symfony\Component\HttpFoundation\RedirectResponse
168 */
169 public function toggleStarAction(Request $request, Entry $entry)
170 {
171 $this->checkUserAction($entry);
172
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 *
187 * @param Request $request
188 * @param Entry $entry
189 *
190 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
191 *
192 * @return \Symfony\Component\HttpFoundation\RedirectResponse
193 */
194 public function deleteEntryAction(Request $request, Entry $entry)
195 {
196 $this->checkUserAction($entry);
197
198 $entry->setDeleted(1);
199 $this->getDoctrine()->getManager()->flush();
200
201 $this->get('session')->getFlashBag()->add(
202 'notice',
203 'Entry deleted'
204 );
205
206 return $this->redirect($request->headers->get('referer'));
207 }
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 }
220 }