3 namespace Wallabag\CoreBundle\Controller
;
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
;
17 class ConfigController
extends Controller
20 * @param Request $request
22 * @Route("/config", name="config")
24 public function indexAction(Request
$request)
26 $em = $this->getDoctrine()->getManager();
27 $config = $this->getConfig();
28 $userManager = $this->container
->get('fos_user.user_manager');
29 $user = $this->getUser();
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);
35 if ($configForm->isValid()) {
36 $em->persist($config);
39 // switch active theme
40 $activeTheme = $this->get('liip_theme.active_theme');
41 $activeTheme->setName($config->getTheme());
43 $this->get('session')->getFlashBag()->add(
45 'Config saved. Some parameters will be considered after disconnection.'
48 return $this->redirect($this->generateUrl('config'));
51 // handle changing password
52 $pwdForm = $this->createForm(new ChangePasswordType(), null, array('action' => $this->generateUrl('config').'#set4'));
53 $pwdForm->handleRequest($request);
55 if ($pwdForm->isValid()) {
56 $user->setPlainPassword($pwdForm->get('new_password')->getData());
57 $userManager->updateUser($user, true);
59 $this->get('session')->getFlashBag()->add(
64 return $this->redirect($this->generateUrl('config'));
67 // handle changing user information
68 $userForm = $this->createForm(new UserInformationType(), $user, array(
69 'validation_groups' => array('Profile'),
70 'action' => $this->generateUrl('config').'#set3',
72 $userForm->handleRequest($request);
74 if ($userForm->isValid()) {
75 $userManager->updateUser($user, true);
77 $this->get('session')->getFlashBag()->add(
82 return $this->redirect($this->generateUrl('config'));
85 // handle rss information
86 $rssForm = $this->createForm(new RssType(), $config, array('action' => $this->generateUrl('config').'#set2'));
87 $rssForm->handleRequest($request);
89 if ($rssForm->isValid()) {
90 $em->persist($config);
93 $this->get('session')->getFlashBag()->add(
95 'RSS information updated'
98 return $this->redirect($this->generateUrl('config'));
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',
109 $newUserForm->handleRequest($request);
111 if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) {
112 $userManager->updateUser($newUser, true);
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'));
120 $em->persist($config);
124 $this->get('session')->getFlashBag()->add(
126 sprintf('User "%s" added', $newUser->getUsername())
129 return $this->redirect($this->generateUrl('config'));
132 return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
134 'config' => $configForm->createView(),
135 'rss' => $rssForm->createView(),
136 'pwd' => $pwdForm->createView(),
137 'user' => $userForm->createView(),
138 'new_user' => $newUserForm->createView(),
141 'username' => $user->getUsername(),
142 'token' => $config->getRssToken(),
148 * @param Request $request
150 * @Route("/generate-token", name="generate_token")
152 * @return JsonResponse
154 public function generateTokenAction(Request
$request)
156 $config = $this->getConfig();
157 $config->setRssToken(Utils
::generateToken());
159 $em = $this->getDoctrine()->getManager();
160 $em->persist($config);
163 if ($request->isXmlHttpRequest()) {
164 return new JsonResponse(array('token' => $config->getRssToken()));
167 return $request->headers
->get('referer') ? $this->redirect($request->headers
->get('referer')) : $this->redirectToRoute('config');
171 * Retrieve config for the current user.
172 * If no config were found, create a new one.
174 * @return Wallabag\CoreBundle\Entity\Config
176 private function getConfig()
178 $config = $this->getDoctrine()
179 ->getRepository('WallabagCoreBundle:Config')
180 ->findOneByUser($this->getUser());
183 $config = new Config($this->getUser());