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