]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/ConfigController.php
Add TaggingRule entity
[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\Request;
8 use Symfony\Component\HttpFoundation\JsonResponse;
9 use Wallabag\CoreBundle\Entity\Config;
10 use Wallabag\UserBundle\Entity\User;
11 use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
12 use Wallabag\CoreBundle\Form\Type\UserInformationType;
13 use Wallabag\CoreBundle\Form\Type\NewUserType;
14 use Wallabag\CoreBundle\Form\Type\RssType;
15 use Wallabag\CoreBundle\Tools\Utils;
16
17 class ConfigController extends Controller
18 {
19 /**
20 * @param Request $request
21 *
22 * @Route("/config", name="config")
23 */
24 public function indexAction(Request $request)
25 {
26 $em = $this->getDoctrine()->getManager();
27 $config = $this->getConfig();
28 $userManager = $this->container->get('fos_user.user_manager');
29 $user = $this->getUser();
30
31 // handle basic config detail (this form is defined as a service)
32 $configForm = $this->createForm('config', $config, array('action' => $this->generateUrl('config')));
33 $configForm->handleRequest($request);
34
35 if ($configForm->isValid()) {
36 $em->persist($config);
37 $em->flush();
38
39 // switch active theme
40 $activeTheme = $this->get('liip_theme.active_theme');
41 $activeTheme->setName($config->getTheme());
42
43 $this->get('session')->getFlashBag()->add(
44 'notice',
45 'Config saved. Some parameters will be considered after disconnection.'
46 );
47
48 return $this->redirect($this->generateUrl('config'));
49 }
50
51 // handle changing password
52 $pwdForm = $this->createForm(new ChangePasswordType(), null, array('action' => $this->generateUrl('config').'#set4'));
53 $pwdForm->handleRequest($request);
54
55 if ($pwdForm->isValid()) {
56 $user->setPlainPassword($pwdForm->get('new_password')->getData());
57 $userManager->updateUser($user, true);
58
59 $this->get('session')->getFlashBag()->add(
60 'notice',
61 'Password updated'
62 );
63
64 return $this->redirect($this->generateUrl('config'));
65 }
66
67 // handle changing user information
68 $userForm = $this->createForm(new UserInformationType(), $user, array(
69 'validation_groups' => array('Profile'),
70 'action' => $this->generateUrl('config').'#set3',
71 ));
72 $userForm->handleRequest($request);
73
74 if ($userForm->isValid()) {
75 $userManager->updateUser($user, true);
76
77 $this->get('session')->getFlashBag()->add(
78 'notice',
79 'Information updated'
80 );
81
82 return $this->redirect($this->generateUrl('config'));
83 }
84
85 // handle rss information
86 $rssForm = $this->createForm(new RssType(), $config, array('action' => $this->generateUrl('config').'#set2'));
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
101 // handle adding new user
102 $newUser = $userManager->createUser();
103 // enable created user by default
104 $newUser->setEnabled(true);
105 $newUserForm = $this->createForm(new NewUserType(), $newUser, array(
106 'validation_groups' => array('Profile'),
107 'action' => $this->generateUrl('config').'#set5',
108 ));
109 $newUserForm->handleRequest($request);
110
111 if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) {
112 $userManager->updateUser($newUser, true);
113
114 $config = new Config($newUser);
115 $config->setTheme($this->container->getParameter('theme'));
116 $config->setItemsPerPage($this->container->getParameter('items_on_page'));
117 $config->setRssLimit($this->container->getParameter('rss_limit'));
118 $config->setLanguage($this->container->getParameter('language'));
119
120 $em->persist($config);
121
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
132 return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
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(),
143 ),
144 ));
145 }
146
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
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 */
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 }