]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/ConfigController.php
Changed behavior when we change language
[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\RedirectResponse;
9 use Symfony\Component\HttpFoundation\Request;
10 use Wallabag\CoreBundle\Entity\Config;
11 use Wallabag\CoreBundle\Entity\TaggingRule;
12 use Wallabag\CoreBundle\Form\Type\ConfigType;
13 use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
14 use Wallabag\CoreBundle\Form\Type\RssType;
15 use Wallabag\CoreBundle\Form\Type\TaggingRuleType;
16 use Wallabag\CoreBundle\Form\Type\UserInformationType;
17 use Wallabag\CoreBundle\Tools\Utils;
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(ConfigType::class, $config, ['action' => $this->generateUrl('config')]);
35 $configForm->handleRequest($request);
36
37 if ($configForm->isValid()) {
38 $em->persist($config);
39 $em->flush();
40
41 $request->getSession()->set('_locale', $config->getLanguage());
42
43 // switch active theme
44 $activeTheme = $this->get('liip_theme.active_theme');
45 $activeTheme->setName($config->getTheme());
46
47 $this->get('session')->getFlashBag()->add(
48 'notice',
49 'flashes.config.notice.config_saved'
50 );
51
52 return $this->redirect($this->generateUrl('config'));
53 }
54
55 // handle changing password
56 $pwdForm = $this->createForm(ChangePasswordType::class, null, ['action' => $this->generateUrl('config').'#set4']);
57 $pwdForm->handleRequest($request);
58
59 if ($pwdForm->isValid()) {
60 if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) {
61 $message = 'flashes.config.notice.password_not_updated_demo';
62 } else {
63 $message = 'flashes.config.notice.password_updated';
64
65 $user->setPlainPassword($pwdForm->get('new_password')->getData());
66 $userManager->updateUser($user, true);
67 }
68
69 $this->get('session')->getFlashBag()->add('notice', $message);
70
71 return $this->redirect($this->generateUrl('config').'#set4');
72 }
73
74 // handle changing user information
75 $userForm = $this->createForm(UserInformationType::class, $user, [
76 'validation_groups' => ['Profile'],
77 'action' => $this->generateUrl('config').'#set3',
78 ]);
79 $userForm->handleRequest($request);
80
81 if ($userForm->isValid()) {
82 $userManager->updateUser($user, true);
83
84 $this->get('session')->getFlashBag()->add(
85 'notice',
86 'flashes.config.notice.user_updated'
87 );
88
89 return $this->redirect($this->generateUrl('config').'#set3');
90 }
91
92 // handle rss information
93 $rssForm = $this->createForm(RssType::class, $config, ['action' => $this->generateUrl('config').'#set2']);
94 $rssForm->handleRequest($request);
95
96 if ($rssForm->isValid()) {
97 $em->persist($config);
98 $em->flush();
99
100 $this->get('session')->getFlashBag()->add(
101 'notice',
102 'flashes.config.notice.rss_updated'
103 );
104
105 return $this->redirect($this->generateUrl('config').'#set2');
106 }
107
108 // handle tagging rule
109 $taggingRule = new TaggingRule();
110 $action = $this->generateUrl('config').'#set5';
111
112 if ($request->query->has('tagging-rule')) {
113 $taggingRule = $this->getDoctrine()
114 ->getRepository('WallabagCoreBundle:TaggingRule')
115 ->find($request->query->get('tagging-rule'));
116
117 if ($this->getUser()->getId() !== $taggingRule->getConfig()->getUser()->getId()) {
118 return $this->redirect($action);
119 }
120
121 $action = $this->generateUrl('config').'?tagging-rule='.$taggingRule->getId().'#set5';
122 }
123
124 $newTaggingRule = $this->createForm(TaggingRuleType::class, $taggingRule, ['action' => $action]);
125 $newTaggingRule->handleRequest($request);
126
127 if ($newTaggingRule->isValid()) {
128 $taggingRule->setConfig($config);
129 $em->persist($taggingRule);
130 $em->flush();
131
132 $this->get('session')->getFlashBag()->add(
133 'notice',
134 'flashes.config.notice.tagging_rules_updated'
135 );
136
137 return $this->redirect($this->generateUrl('config').'#set5');
138 }
139
140 return $this->render('WallabagCoreBundle:Config:index.html.twig', [
141 'form' => [
142 'config' => $configForm->createView(),
143 'rss' => $rssForm->createView(),
144 'pwd' => $pwdForm->createView(),
145 'user' => $userForm->createView(),
146 'new_tagging_rule' => $newTaggingRule->createView(),
147 ],
148 'rss' => [
149 'username' => $user->getUsername(),
150 'token' => $config->getRssToken(),
151 ],
152 'twofactor_auth' => $this->getParameter('twofactor_auth'),
153 ]);
154 }
155
156 /**
157 * @param Request $request
158 *
159 * @Route("/generate-token", name="generate_token")
160 *
161 * @return RedirectResponse|JsonResponse
162 */
163 public function generateTokenAction(Request $request)
164 {
165 $config = $this->getConfig();
166 $config->setRssToken(Utils::generateToken());
167
168 $em = $this->getDoctrine()->getManager();
169 $em->persist($config);
170 $em->flush();
171
172 if ($request->isXmlHttpRequest()) {
173 return new JsonResponse(['token' => $config->getRssToken()]);
174 }
175
176 $this->get('session')->getFlashBag()->add(
177 'notice',
178 'flashes.config.notice.rss_token_updated'
179 );
180
181 return $this->redirect($this->generateUrl('config').'#set2');
182 }
183
184 /**
185 * Deletes a tagging rule and redirect to the config homepage.
186 *
187 * @param TaggingRule $rule
188 *
189 * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule")
190 *
191 * @return RedirectResponse
192 */
193 public function deleteTaggingRuleAction(TaggingRule $rule)
194 {
195 $this->validateRuleAction($rule);
196
197 $em = $this->getDoctrine()->getManager();
198 $em->remove($rule);
199 $em->flush();
200
201 $this->get('session')->getFlashBag()->add(
202 'notice',
203 'flashes.config.notice.tagging_rules_deleted'
204 );
205
206 return $this->redirect($this->generateUrl('config').'#set5');
207 }
208
209 /**
210 * Edit a tagging rule.
211 *
212 * @param TaggingRule $rule
213 *
214 * @Route("/tagging-rule/edit/{id}", requirements={"id" = "\d+"}, name="edit_tagging_rule")
215 *
216 * @return RedirectResponse
217 */
218 public function editTaggingRuleAction(TaggingRule $rule)
219 {
220 $this->validateRuleAction($rule);
221
222 return $this->redirect($this->generateUrl('config').'?tagging-rule='.$rule->getId().'#set5');
223 }
224
225 /**
226 * Validate that a rule can be edited/deleted by the current user.
227 *
228 * @param TaggingRule $rule
229 */
230 private function validateRuleAction(TaggingRule $rule)
231 {
232 if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) {
233 throw $this->createAccessDeniedException('You can not access this tagging rule.');
234 }
235 }
236
237 /**
238 * Retrieve config for the current user.
239 * If no config were found, create a new one.
240 *
241 * @return Config
242 */
243 private function getConfig()
244 {
245 $config = $this->getDoctrine()
246 ->getRepository('WallabagCoreBundle:Config')
247 ->findOneByUser($this->getUser());
248
249 // should NEVER HAPPEN ...
250 if (!$config) {
251 $config = new Config($this->getUser());
252 }
253
254 return $config;
255 }
256 }