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