]>
Commit | Line | Data |
---|---|---|
4d85d7e9 J |
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; | |
0c83fd59 | 8 | use Symfony\Component\HttpFoundation\JsonResponse; |
4d85d7e9 | 9 | use Wallabag\CoreBundle\Entity\Config; |
1210dae1 | 10 | use Wallabag\UserBundle\Entity\User; |
d9085c63 | 11 | use Wallabag\CoreBundle\Form\Type\ChangePasswordType; |
c844dc0c | 12 | use Wallabag\CoreBundle\Form\Type\UserInformationType; |
e4977b8a | 13 | use Wallabag\CoreBundle\Form\Type\NewUserType; |
0c83fd59 J |
14 | use Wallabag\CoreBundle\Form\Type\RssType; |
15 | use Wallabag\CoreBundle\Tools\Utils; | |
4d85d7e9 J |
16 | |
17 | class ConfigController extends Controller | |
18 | { | |
19 | /** | |
20 | * @param Request $request | |
21 | * | |
22 | * @Route("/config", name="config") | |
4d85d7e9 J |
23 | */ |
24 | public function indexAction(Request $request) | |
25 | { | |
d9085c63 | 26 | $em = $this->getDoctrine()->getManager(); |
4d85d7e9 | 27 | $config = $this->getConfig(); |
fcb1fba5 | 28 | $userManager = $this->container->get('fos_user.user_manager'); |
c0d9eba0 | 29 | $user = $this->getUser(); |
4d85d7e9 | 30 | |
32da2a70 J |
31 | // handle basic config detail (this form is defined as a service) |
32 | $configForm = $this->createForm('config', $config); | |
d9085c63 | 33 | $configForm->handleRequest($request); |
4d85d7e9 | 34 | |
d9085c63 | 35 | if ($configForm->isValid()) { |
4d85d7e9 J |
36 | $em->persist($config); |
37 | $em->flush(); | |
38 | ||
32da2a70 J |
39 | // switch active theme |
40 | $activeTheme = $this->get('liip_theme.active_theme'); | |
41 | $activeTheme->setName($config->getTheme()); | |
42 | ||
4d85d7e9 J |
43 | $this->get('session')->getFlashBag()->add( |
44 | 'notice', | |
c89d35e8 | 45 | 'Config saved. Some parameters will be considered after disconnection.' |
4d85d7e9 J |
46 | ); |
47 | ||
48 | return $this->redirect($this->generateUrl('config')); | |
49 | } | |
50 | ||
d9085c63 J |
51 | // handle changing password |
52 | $pwdForm = $this->createForm(new ChangePasswordType()); | |
53 | $pwdForm->handleRequest($request); | |
54 | ||
55 | if ($pwdForm->isValid()) { | |
fcb1fba5 NL |
56 | $user->setPlainPassword($pwdForm->get('new_password')->getData()); |
57 | $userManager->updateUser($user, true); | |
d9085c63 J |
58 | |
59 | $this->get('session')->getFlashBag()->add( | |
60 | 'notice', | |
61 | 'Password updated' | |
62 | ); | |
63 | ||
64 | return $this->redirect($this->generateUrl('config')); | |
65 | } | |
66 | ||
c0d9eba0 | 67 | // handle changing user information |
3f7a6290 | 68 | $userForm = $this->createForm(new UserInformationType(), $user, array('validation_groups' => array('Profile'))); |
c0d9eba0 J |
69 | $userForm->handleRequest($request); |
70 | ||
71 | if ($userForm->isValid()) { | |
fcb1fba5 | 72 | $userManager->updateUser($user, true); |
c0d9eba0 J |
73 | |
74 | $this->get('session')->getFlashBag()->add( | |
75 | 'notice', | |
76 | 'Information updated' | |
77 | ); | |
78 | ||
79 | return $this->redirect($this->generateUrl('config')); | |
80 | } | |
81 | ||
0c83fd59 J |
82 | // handle rss information |
83 | $rssForm = $this->createForm(new RssType(), $config); | |
84 | $rssForm->handleRequest($request); | |
85 | ||
86 | if ($rssForm->isValid()) { | |
87 | $em->persist($config); | |
88 | $em->flush(); | |
89 | ||
90 | $this->get('session')->getFlashBag()->add( | |
91 | 'notice', | |
92 | 'RSS information updated' | |
93 | ); | |
94 | ||
95 | return $this->redirect($this->generateUrl('config')); | |
96 | } | |
97 | ||
e4977b8a | 98 | // handle adding new user |
fcb1fba5 | 99 | $newUser = $userManager->createUser(); |
0f30f48b JB |
100 | // enable created user by default |
101 | $newUser->setEnabled(true); | |
3f7a6290 | 102 | $newUserForm = $this->createForm(new NewUserType(), $newUser, array('validation_groups' => array('Profile'))); |
e4977b8a J |
103 | $newUserForm->handleRequest($request); |
104 | ||
fcb1fba5 NL |
105 | if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) { |
106 | $userManager->updateUser($newUser, true); | |
0bd2cb1e J |
107 | |
108 | $config = new Config($newUser); | |
109 | $config->setTheme($this->container->getParameter('theme')); | |
110 | $config->setItemsPerPage($this->container->getParameter('items_on_page')); | |
371ac69a | 111 | $config->setRssLimit($this->container->getParameter('rss_limit')); |
0bd2cb1e J |
112 | $config->setLanguage($this->container->getParameter('language')); |
113 | ||
114 | $em->persist($config); | |
115 | ||
e4977b8a J |
116 | $em->flush(); |
117 | ||
118 | $this->get('session')->getFlashBag()->add( | |
119 | 'notice', | |
120 | sprintf('User "%s" added', $newUser->getUsername()) | |
121 | ); | |
122 | ||
123 | return $this->redirect($this->generateUrl('config')); | |
124 | } | |
125 | ||
4d85d7e9 | 126 | return $this->render('WallabagCoreBundle:Config:index.html.twig', array( |
0c83fd59 J |
127 | 'form' => array( |
128 | 'config' => $configForm->createView(), | |
129 | 'rss' => $rssForm->createView(), | |
130 | 'pwd' => $pwdForm->createView(), | |
131 | 'user' => $userForm->createView(), | |
132 | 'new_user' => $newUserForm->createView(), | |
133 | ), | |
134 | 'rss' => array( | |
135 | 'username' => $user->getUsername(), | |
136 | 'token' => $config->getRssToken(), | |
9744e971 | 137 | ), |
4d85d7e9 J |
138 | )); |
139 | } | |
140 | ||
0c83fd59 J |
141 | /** |
142 | * @param Request $request | |
143 | * | |
144 | * @Route("/generate-token", name="generate_token") | |
145 | * | |
146 | * @return JsonResponse | |
147 | */ | |
148 | public function generateTokenAction(Request $request) | |
149 | { | |
150 | $config = $this->getConfig(); | |
151 | $config->setRssToken(Utils::generateToken()); | |
152 | ||
153 | $em = $this->getDoctrine()->getManager(); | |
154 | $em->persist($config); | |
155 | $em->flush(); | |
156 | ||
157 | if ($request->isXmlHttpRequest()) { | |
158 | return new JsonResponse(array('token' => $config->getRssToken())); | |
159 | } | |
160 | ||
161 | return $request->headers->get('referer') ? $this->redirect($request->headers->get('referer')) : $this->redirectToRoute('config'); | |
162 | } | |
163 | ||
d9085c63 J |
164 | /** |
165 | * Retrieve config for the current user. | |
166 | * If no config were found, create a new one. | |
167 | * | |
168 | * @return Wallabag\CoreBundle\Entity\Config | |
169 | */ | |
4d85d7e9 J |
170 | private function getConfig() |
171 | { | |
172 | $config = $this->getDoctrine() | |
173 | ->getRepository('WallabagCoreBundle:Config') | |
174 | ->findOneByUser($this->getUser()); | |
175 | ||
176 | if (!$config) { | |
177 | $config = new Config($this->getUser()); | |
178 | } | |
179 | ||
180 | return $config; | |
181 | } | |
182 | } |