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