]>
Commit | Line | Data |
---|---|---|
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\CoreBundle\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 | $user = $this->getUser(); | |
29 | ||
30 | // handle basic config detail (this form is defined as a service) | |
31 | $configForm = $this->createForm('config', $config); | |
32 | $configForm->handleRequest($request); | |
33 | ||
34 | if ($configForm->isValid()) { | |
35 | $em->persist($config); | |
36 | $em->flush(); | |
37 | ||
38 | // switch active theme | |
39 | $activeTheme = $this->get('liip_theme.active_theme'); | |
40 | $activeTheme->setName($config->getTheme()); | |
41 | ||
42 | $this->get('session')->getFlashBag()->add( | |
43 | 'notice', | |
44 | 'Config saved' | |
45 | ); | |
46 | ||
47 | return $this->redirect($this->generateUrl('config')); | |
48 | } | |
49 | ||
50 | // handle changing password | |
51 | $pwdForm = $this->createForm(new ChangePasswordType()); | |
52 | $pwdForm->handleRequest($request); | |
53 | ||
54 | if ($pwdForm->isValid()) { | |
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 | ||
67 | // handle changing user information | |
68 | $userForm = $this->createForm(new UserInformationType(), $user, array('validation_groups' => array('Profile'))); | |
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 | ||
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 | ||
99 | // handle adding new user | |
100 | $newUser = new User(); | |
101 | $newUserForm = $this->createForm(new NewUserType(), $newUser, array('validation_groups' => array('Profile'))); | |
102 | $newUserForm->handleRequest($request); | |
103 | ||
104 | if ($newUserForm->isValid()) { | |
105 | $em->persist($newUser); | |
106 | ||
107 | $config = new Config($newUser); | |
108 | $config->setTheme($this->container->getParameter('theme')); | |
109 | $config->setItemsPerPage($this->container->getParameter('items_on_page')); | |
110 | $config->setRssLimit($this->container->getParameter('rss_limit')); | |
111 | $config->setLanguage($this->container->getParameter('language')); | |
112 | ||
113 | $em->persist($config); | |
114 | ||
115 | $em->flush(); | |
116 | ||
117 | $this->get('session')->getFlashBag()->add( | |
118 | 'notice', | |
119 | sprintf('User "%s" added', $newUser->getUsername()) | |
120 | ); | |
121 | ||
122 | return $this->redirect($this->generateUrl('config')); | |
123 | } | |
124 | ||
125 | return $this->render('WallabagCoreBundle:Config:index.html.twig', array( | |
126 | 'form' => array( | |
127 | 'config' => $configForm->createView(), | |
128 | 'rss' => $rssForm->createView(), | |
129 | 'pwd' => $pwdForm->createView(), | |
130 | 'user' => $userForm->createView(), | |
131 | 'new_user' => $newUserForm->createView(), | |
132 | ), | |
133 | 'rss' => array( | |
134 | 'username' => $user->getUsername(), | |
135 | 'token' => $config->getRssToken(), | |
136 | ), | |
137 | )); | |
138 | } | |
139 | ||
140 | /** | |
141 | * @param Request $request | |
142 | * | |
143 | * @Route("/generate-token", name="generate_token") | |
144 | * | |
145 | * @return JsonResponse | |
146 | */ | |
147 | public function generateTokenAction(Request $request) | |
148 | { | |
149 | $config = $this->getConfig(); | |
150 | $config->setRssToken(Utils::generateToken()); | |
151 | ||
152 | $em = $this->getDoctrine()->getManager(); | |
153 | $em->persist($config); | |
154 | $em->flush(); | |
155 | ||
156 | if ($request->isXmlHttpRequest()) { | |
157 | return new JsonResponse(array('token' => $config->getRssToken())); | |
158 | } | |
159 | ||
160 | return $request->headers->get('referer') ? $this->redirect($request->headers->get('referer')) : $this->redirectToRoute('config'); | |
161 | } | |
162 | ||
163 | /** | |
164 | * Retrieve config for the current user. | |
165 | * If no config were found, create a new one. | |
166 | * | |
167 | * @return Wallabag\CoreBundle\Entity\Config | |
168 | */ | |
169 | private function getConfig() | |
170 | { | |
171 | $config = $this->getDoctrine() | |
172 | ->getRepository('WallabagCoreBundle:Config') | |
173 | ->findOneByUser($this->getUser()); | |
174 | ||
175 | if (!$config) { | |
176 | $config = new Config($this->getUser()); | |
177 | } | |
178 | ||
179 | return $config; | |
180 | } | |
181 | } |