]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/ConfigController.php
f51ccaa486c9fe43dbfeca1d1cd5c1c740ed2b5c
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / ConfigController.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\JsonResponse;
8 use Symfony\Component\HttpFoundation\Request;
9 use Wallabag\CoreBundle\Entity\Config;
10 use Wallabag\CoreBundle\Entity\TaggingRule;
11 use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
12 use Wallabag\CoreBundle\Form\Type\NewUserType;
13 use Wallabag\CoreBundle\Form\Type\RssType;
14 use Wallabag\CoreBundle\Form\Type\TaggingRuleType;
15 use Wallabag\CoreBundle\Form\Type\UserInformationType;
16 use Wallabag\CoreBundle\Tools\Utils;
17 use Wallabag\UserBundle\Entity\User;
18
19 class ConfigController extends Controller
20 {
21 /**
22 * @param Request $request
23 *
24 * @Route("/config", name="config")
25 */
26 public function indexAction(Request $request)
27 {
28 $em = $this->getDoctrine()->getManager();
29 $config = $this->getConfig();
30 $userManager = $this->container->get('fos_user.user_manager');
31 $user = $this->getUser();
32
33 // handle basic config detail (this form is defined as a service)
34 $configForm = $this->createForm('config', $config, array('action' => $this->generateUrl('config')));
35 $configForm->handleRequest($request);
36
37 if ($configForm->isValid()) {
38 $em->persist($config);
39 $em->flush();
40
41 // switch active theme
42 $activeTheme = $this->get('liip_theme.active_theme');
43 $activeTheme->setName($config->getTheme());
44
45 $this->get('session')->getFlashBag()->add(
46 'notice',
47 'Config saved. Some parameters will be considered after disconnection.'
48 );
49
50 return $this->redirect($this->generateUrl('config'));
51 }
52
53 // handle changing password
54 $pwdForm = $this->createForm(new ChangePasswordType(), null, array('action' => $this->generateUrl('config').'#set4'));
55 $pwdForm->handleRequest($request);
56
57 if ($pwdForm->isValid()) {
58 $user->setPlainPassword($pwdForm->get('new_password')->getData());
59 $userManager->updateUser($user, true);
60
61 $this->get('session')->getFlashBag()->add(
62 'notice',
63 'Password updated'
64 );
65
66 return $this->redirect($this->generateUrl('config'));
67 }
68
69 // handle changing user information
70 $userForm = $this->createForm(new UserInformationType(), $user, array(
71 'validation_groups' => array('Profile'),
72 'action' => $this->generateUrl('config').'#set3',
73 ));
74 $userForm->handleRequest($request);
75
76 if ($userForm->isValid()) {
77 $userManager->updateUser($user, true);
78
79 $this->get('session')->getFlashBag()->add(
80 'notice',
81 'Information updated'
82 );
83
84 return $this->redirect($this->generateUrl('config'));
85 }
86
87 // handle rss information
88 $rssForm = $this->createForm(new RssType(), $config, array('action' => $this->generateUrl('config').'#set2'));
89 $rssForm->handleRequest($request);
90
91 if ($rssForm->isValid()) {
92 $em->persist($config);
93 $em->flush();
94
95 $this->get('session')->getFlashBag()->add(
96 'notice',
97 'RSS information updated'
98 );
99
100 return $this->redirect($this->generateUrl('config'));
101 }
102
103 // handle tagging rule
104 $taggingRule = new TaggingRule();
105 $newTaggingRule = $this->createForm(new TaggingRuleType(), $taggingRule, array('action' => $this->generateUrl('config').'#set5'));
106 $newTaggingRule->handleRequest($request);
107
108 if ($newTaggingRule->isValid()) {
109 $taggingRule->setConfig($config);
110 $em->persist($taggingRule);
111 $em->flush();
112
113 $this->get('session')->getFlashBag()->add(
114 'notice',
115 'Tagging rules updated'
116 );
117
118 return $this->redirect($this->generateUrl('config'));
119 }
120
121 // handle adding new user
122 $newUser = $userManager->createUser();
123 // enable created user by default
124 $newUser->setEnabled(true);
125 $newUserForm = $this->createForm(new NewUserType(), $newUser, array(
126 'validation_groups' => array('Profile'),
127 'action' => $this->generateUrl('config').'#set5',
128 ));
129 $newUserForm->handleRequest($request);
130
131 if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) {
132 $userManager->updateUser($newUser, true);
133
134 $config = new Config($newUser);
135 $config->setTheme($this->container->getParameter('theme'));
136 $config->setItemsPerPage($this->container->getParameter('items_on_page'));
137 $config->setRssLimit($this->container->getParameter('rss_limit'));
138 $config->setLanguage($this->container->getParameter('language'));
139
140 $em->persist($config);
141
142 $em->flush();
143
144 $this->get('session')->getFlashBag()->add(
145 'notice',
146 sprintf('User "%s" added', $newUser->getUsername())
147 );
148
149 return $this->redirect($this->generateUrl('config'));
150 }
151
152 return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
153 'form' => array(
154 'config' => $configForm->createView(),
155 'rss' => $rssForm->createView(),
156 'pwd' => $pwdForm->createView(),
157 'user' => $userForm->createView(),
158 'new_user' => $newUserForm->createView(),
159 'new_tagging_rule' => $newTaggingRule->createView(),
160 ),
161 'rss' => array(
162 'username' => $user->getUsername(),
163 'token' => $config->getRssToken(),
164 ),
165 ));
166 }
167
168 /**
169 * @param Request $request
170 *
171 * @Route("/generate-token", name="generate_token")
172 *
173 * @return JsonResponse
174 */
175 public function generateTokenAction(Request $request)
176 {
177 $config = $this->getConfig();
178 $config->setRssToken(Utils::generateToken());
179
180 $em = $this->getDoctrine()->getManager();
181 $em->persist($config);
182 $em->flush();
183
184 if ($request->isXmlHttpRequest()) {
185 return new JsonResponse(array('token' => $config->getRssToken()));
186 }
187
188 return $request->headers->get('referer') ? $this->redirect($request->headers->get('referer')) : $this->redirectToRoute('config');
189 }
190
191 /**
192 * Deletes a tagging rule and redirect to the config homepage.
193 *
194 * @param TaggingRule $rule
195 *
196 * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule")
197 *
198 * @return \Symfony\Component\HttpFoundation\RedirectResponse
199 */
200 public function deleteTaggingRule(TaggingRule $rule)
201 {
202 if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) {
203 throw $this->createAccessDeniedException('You can not access this tagging ryle.');
204 }
205
206 $em = $this->getDoctrine()->getManager();
207 $em->remove($rule);
208 $em->flush();
209
210 $this->get('session')->getFlashBag()->add(
211 'notice',
212 'Tagging rule deleted'
213 );
214
215 return $this->redirect($this->generateUrl('config'));
216 }
217
218 /**
219 * Retrieve config for the current user.
220 * If no config were found, create a new one.
221 *
222 * @return Wallabag\CoreBundle\Entity\Config
223 */
224 private function getConfig()
225 {
226 $config = $this->getDoctrine()
227 ->getRepository('WallabagCoreBundle:Config')
228 ->findOneByUser($this->getUser());
229
230 if (!$config) {
231 $config = new Config($this->getUser());
232 }
233
234 return $config;
235 }
236 }