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