]>
Commit | Line | Data |
---|---|---|
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\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 Entries(); | |
23 | $entry->setUserId(1); | |
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 | * @return \Symfony\Component\HttpFoundation\Response | |
60 | */ | |
61 | public function showUnreadAction() | |
62 | { | |
63 | $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entries'); | |
64 | // TODO don't give the user ID like this | |
65 | // TODO change pagination | |
66 | $entries = $repository->findUnreadByUser(1, 0); | |
67 | ||
68 | return $this->render( | |
69 | 'WallabagCoreBundle:Entry:entries.html.twig', | |
70 | array('entries' => $entries) | |
71 | ); | |
72 | } | |
73 | ||
74 | /** | |
75 | * Shows read entries for current user | |
76 | * | |
77 | * @Route("/archive", name="archive") | |
78 | * @return \Symfony\Component\HttpFoundation\Response | |
79 | */ | |
80 | public function showArchiveAction() | |
81 | { | |
82 | $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entries'); | |
83 | // TODO don't give the user ID like this | |
84 | // TODO change pagination | |
85 | $entries = $repository->findArchiveByUser(1, 0); | |
86 | ||
87 | return $this->render( | |
88 | 'WallabagCoreBundle:Entry:entries.html.twig', | |
89 | array('entries' => $entries) | |
90 | ); | |
91 | } | |
92 | ||
93 | /** | |
94 | * Shows starred entries for current user | |
95 | * | |
96 | * @Route("/starred", name="starred") | |
97 | * @return \Symfony\Component\HttpFoundation\Response | |
98 | */ | |
99 | public function showStarredAction() | |
100 | { | |
101 | $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entries'); | |
102 | // TODO don't give the user ID like this | |
103 | // TODO change pagination | |
104 | $entries = $repository->findStarredByUser(1, 0); | |
105 | ||
106 | return $this->render( | |
107 | 'WallabagCoreBundle:Entry:entries.html.twig', | |
108 | array('entries' => $entries) | |
109 | ); | |
110 | } | |
111 | ||
112 | /** | |
113 | * Shows entry content | |
114 | * | |
115 | * @param Entries $entry | |
116 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") | |
117 | * @return \Symfony\Component\HttpFoundation\Response | |
118 | */ | |
119 | public function viewAction(Entries $entry) | |
120 | { | |
121 | return $this->render( | |
122 | 'WallabagCoreBundle:Entry:entry.html.twig', | |
123 | array('entry' => $entry) | |
124 | ); | |
125 | } | |
126 | ||
127 | /** | |
128 | * Changes read status for an entry | |
129 | * | |
130 | * @param Request $request | |
131 | * @param Entries $entry | |
132 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") | |
133 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
134 | */ | |
135 | public function toggleArchiveAction(Request $request, Entries $entry) | |
136 | { | |
137 | $entry->toggleArchive(); | |
138 | $this->getDoctrine()->getManager()->flush(); | |
139 | ||
140 | $this->get('session')->getFlashBag()->add( | |
141 | 'notice', | |
142 | 'Entry archived' | |
143 | ); | |
144 | ||
145 | return $this->redirect($request->headers->get('referer')); | |
146 | } | |
147 | ||
148 | /** | |
149 | * Changes favorite status for an entry | |
150 | * | |
151 | * @param Request $request | |
152 | * @param Entries $entry | |
153 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") | |
154 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
155 | */ | |
156 | public function toggleStarAction(Request $request, Entries $entry) | |
157 | { | |
158 | $entry->toggleStar(); | |
159 | $this->getDoctrine()->getManager()->flush(); | |
160 | ||
161 | $this->get('session')->getFlashBag()->add( | |
162 | 'notice', | |
163 | 'Entry starred' | |
164 | ); | |
165 | ||
166 | return $this->redirect($request->headers->get('referer')); | |
167 | } | |
168 | ||
169 | /** | |
170 | * Deletes entry | |
171 | * | |
172 | * @param Request $request | |
173 | * @param Entries $entry | |
174 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") | |
175 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
176 | */ | |
177 | public function deleteEntryAction(Request $request, Entries $entry) | |
178 | { | |
179 | $em = $this->getDoctrine()->getManager(); | |
180 | $entry->setDeleted(1); | |
181 | $em->persist($entry); | |
182 | $em->flush(); | |
183 | ||
184 | $this->get('session')->getFlashBag()->add( | |
185 | 'notice', | |
186 | 'Entry deleted' | |
187 | ); | |
188 | ||
189 | return $this->redirect($request->headers->get('referer')); | |
190 | } | |
191 | } |