]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/EntryController.php
log for authentication on API
[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 * @Route("/new", name="new_entry")
17 * @return \Symfony\Component\HttpFoundation\Response
18 */
19 public function addEntryAction(Request $request)
20 {
21 $entry = new Entry($this->getUser());
22
23 $form = $this->createFormBuilder($entry)
24 ->add('url', 'url')
25 ->add('save', 'submit')
26 ->getForm();
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 * Shows unread entries for current user
55 *
56 * @Route("/unread", name="unread")
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 * @return \Symfony\Component\HttpFoundation\Response
77 */
78 public function showArchiveAction()
79 {
80 // TODO change pagination
81 $entries = $this->getDoctrine()
82 ->getRepository('WallabagCoreBundle:Entry')
83 ->findArchiveByUser($this->getUser()->getId(), 0);
84
85 return $this->render(
86 'WallabagCoreBundle:Entry:entries.html.twig',
87 array('entries' => $entries)
88 );
89 }
90
91 /**
92 * Shows starred entries for current user
93 *
94 * @Route("/starred", name="starred")
95 * @return \Symfony\Component\HttpFoundation\Response
96 */
97 public function showStarredAction()
98 {
99 // TODO change pagination
100 $entries = $this->getDoctrine()
101 ->getRepository('WallabagCoreBundle:Entry')
102 ->findStarredByUser($this->getUser()->getId(), 0);
103
104 return $this->render(
105 'WallabagCoreBundle:Entry:entries.html.twig',
106 array('entries' => $entries)
107 );
108 }
109
110 /**
111 * Shows entry content
112 *
113 * @param Entry $entry
114 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
115 * @return \Symfony\Component\HttpFoundation\Response
116 */
117 public function viewAction(Entry $entry)
118 {
119 return $this->render(
120 'WallabagCoreBundle:Entry:entry.html.twig',
121 array('entry' => $entry)
122 );
123 }
124
125 /**
126 * Changes read status for an entry
127 *
128 * @param Request $request
129 * @param Entry $entry
130 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
131 * @return \Symfony\Component\HttpFoundation\RedirectResponse
132 */
133 public function toggleArchiveAction(Request $request, Entry $entry)
134 {
135 $entry->toggleArchive();
136 $this->getDoctrine()->getManager()->flush();
137
138 $this->get('session')->getFlashBag()->add(
139 'notice',
140 'Entry archived'
141 );
142
143 return $this->redirect($request->headers->get('referer'));
144 }
145
146 /**
147 * Changes favorite status for an entry
148 *
149 * @param Request $request
150 * @param Entry $entry
151 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
152 * @return \Symfony\Component\HttpFoundation\RedirectResponse
153 */
154 public function toggleStarAction(Request $request, Entry $entry)
155 {
156 $entry->toggleStar();
157 $this->getDoctrine()->getManager()->flush();
158
159 $this->get('session')->getFlashBag()->add(
160 'notice',
161 'Entry starred'
162 );
163
164 return $this->redirect($request->headers->get('referer'));
165 }
166
167 /**
168 * Deletes entry
169 *
170 * @param Request $request
171 * @param Entry $entry
172 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
173 * @return \Symfony\Component\HttpFoundation\RedirectResponse
174 */
175 public function deleteEntryAction(Request $request, Entry $entry)
176 {
177 $em = $this->getDoctrine()->getManager();
178 $entry->setDeleted(1);
179 $em->persist($entry);
180 $em->flush();
181
182 $this->get('session')->getFlashBag()->add(
183 'notice',
184 'Entry deleted'
185 );
186
187 return $this->redirect($request->headers->get('referer'));
188 }
189 }