]>
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 | 31 | // handle basic config detail (this form is defined as a service) |
33fe61f9 | 32 | $configForm = $this->createForm('config', $config, array('action' => $this->generateUrl('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 | 51 | // handle changing password |
33fe61f9 | 52 | $pwdForm = $this->createForm(new ChangePasswordType(), null, array('action' => $this->generateUrl('config').'#set4')); |
d9085c63 J |
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 |
33fe61f9 JB |
68 | $userForm = $this->createForm(new UserInformationType(), $user, array( |
69 | 'validation_groups' => array('Profile'), | |
70 | 'action' => $this->generateUrl('config').'#set3', | |
71 | )); | |
c0d9eba0 J |
72 | $userForm->handleRequest($request); |
73 | ||
74 | if ($userForm->isValid()) { | |
fcb1fba5 | 75 | $userManager->updateUser($user, true); |
c0d9eba0 J |
76 | |
77 | $this->get('session')->getFlashBag()->add( | |
78 | 'notice', | |
79 | 'Information updated' | |
80 | ); | |
81 | ||
82 | return $this->redirect($this->generateUrl('config')); | |
83 | } | |
84 | ||
0c83fd59 | 85 | // handle rss information |
33fe61f9 | 86 | $rssForm = $this->createForm(new RssType(), $config, array('action' => $this->generateUrl('config').'#set2')); |
0c83fd59 J |
87 | $rssForm->handleRequest($request); |
88 | ||
89 | if ($rssForm->isValid()) { | |
90 | $em->persist($config); | |
91 | $em->flush(); | |
92 | ||
93 | $this->get('session')->getFlashBag()->add( | |
94 | 'notice', | |
95 | 'RSS information updated' | |
96 | ); | |
97 | ||
98 | return $this->redirect($this->generateUrl('config')); | |
99 | } | |
100 | ||
e4977b8a | 101 | // handle adding new user |
fcb1fba5 | 102 | $newUser = $userManager->createUser(); |
0f30f48b JB |
103 | // enable created user by default |
104 | $newUser->setEnabled(true); | |
33fe61f9 JB |
105 | $newUserForm = $this->createForm(new NewUserType(), $newUser, array( |
106 | 'validation_groups' => array('Profile'), | |
107 | 'action' => $this->generateUrl('config').'#set5', | |
108 | )); | |
e4977b8a J |
109 | $newUserForm->handleRequest($request); |
110 | ||
fcb1fba5 NL |
111 | if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) { |
112 | $userManager->updateUser($newUser, true); | |
0bd2cb1e J |
113 | |
114 | $config = new Config($newUser); | |
115 | $config->setTheme($this->container->getParameter('theme')); | |
116 | $config->setItemsPerPage($this->container->getParameter('items_on_page')); | |
371ac69a | 117 | $config->setRssLimit($this->container->getParameter('rss_limit')); |
0bd2cb1e J |
118 | $config->setLanguage($this->container->getParameter('language')); |
119 | ||
120 | $em->persist($config); | |
121 | ||
e4977b8a J |
122 | $em->flush(); |
123 | ||
124 | $this->get('session')->getFlashBag()->add( | |
125 | 'notice', | |
126 | sprintf('User "%s" added', $newUser->getUsername()) | |
127 | ); | |
128 | ||
129 | return $this->redirect($this->generateUrl('config')); | |
130 | } | |
131 | ||
4d85d7e9 | 132 | return $this->render('WallabagCoreBundle:Config:index.html.twig', array( |
0c83fd59 J |
133 | 'form' => array( |
134 | 'config' => $configForm->createView(), | |
135 | 'rss' => $rssForm->createView(), | |
136 | 'pwd' => $pwdForm->createView(), | |
137 | 'user' => $userForm->createView(), | |
138 | 'new_user' => $newUserForm->createView(), | |
139 | ), | |
140 | 'rss' => array( | |
141 | 'username' => $user->getUsername(), | |
142 | 'token' => $config->getRssToken(), | |
9744e971 | 143 | ), |
4d85d7e9 J |
144 | )); |
145 | } | |
146 | ||
0c83fd59 J |
147 | /** |
148 | * @param Request $request | |
149 | * | |
150 | * @Route("/generate-token", name="generate_token") | |
151 | * | |
152 | * @return JsonResponse | |
153 | */ | |
154 | public function generateTokenAction(Request $request) | |
155 | { | |
156 | $config = $this->getConfig(); | |
157 | $config->setRssToken(Utils::generateToken()); | |
158 | ||
159 | $em = $this->getDoctrine()->getManager(); | |
160 | $em->persist($config); | |
161 | $em->flush(); | |
162 | ||
163 | if ($request->isXmlHttpRequest()) { | |
164 | return new JsonResponse(array('token' => $config->getRssToken())); | |
165 | } | |
166 | ||
167 | return $request->headers->get('referer') ? $this->redirect($request->headers->get('referer')) : $this->redirectToRoute('config'); | |
168 | } | |
169 | ||
d9085c63 J |
170 | /** |
171 | * Retrieve config for the current user. | |
172 | * If no config were found, create a new one. | |
173 | * | |
174 | * @return Wallabag\CoreBundle\Entity\Config | |
175 | */ | |
4d85d7e9 J |
176 | private function getConfig() |
177 | { | |
178 | $config = $this->getDoctrine() | |
179 | ->getRepository('WallabagCoreBundle:Config') | |
180 | ->findOneByUser($this->getUser()); | |
181 | ||
182 | if (!$config) { | |
183 | $config = new Config($this->getUser()); | |
184 | } | |
185 | ||
186 | return $config; | |
187 | } | |
188 | } |