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\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
;
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 $user = $this->getUser();
30 // handle basic config detail (this form is defined as a service)
31 $configForm = $this->createForm('config', $config);
32 $configForm->handleRequest($request);
34 if ($configForm->isValid()) {
35 $em->persist($config);
38 // switch active theme
39 $activeTheme = $this->get('liip_theme.active_theme');
40 $activeTheme->setName($config->getTheme());
42 $this->get('session')->getFlashBag()->add(
47 return $this->redirect($this->generateUrl('config'));
50 // handle changing password
51 $pwdForm = $this->createForm(new ChangePasswordType());
52 $pwdForm->handleRequest($request);
54 if ($pwdForm->isValid()) {
55 $user->setPassword($pwdForm->get('new_password')->getData());
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('validation_groups' => array('Profile')));
69 $userForm->handleRequest($request);
71 if ($userForm->isValid()) {
75 $this->get('session')->getFlashBag()->add(
80 return $this->redirect($this->generateUrl('config'));
83 // handle rss information
84 $rssForm = $this->createForm(new RssType(), $config);
85 $rssForm->handleRequest($request);
87 if ($rssForm->isValid()) {
88 $em->persist($config);
91 $this->get('session')->getFlashBag()->add(
93 'RSS information updated'
96 return $this->redirect($this->generateUrl('config'));
99 // handle adding new user
100 $newUser = new User();
101 // enable created user by default
102 $newUser->setEnabled(true);
103 $newUserForm = $this->createForm(new NewUserType(), $newUser, array('validation_groups' => array('Profile')));
104 $newUserForm->handleRequest($request);
106 if ($newUserForm->isValid()) {
107 $em->persist($newUser);
109 $config = new Config($newUser);
110 $config->setTheme($this->container
->getParameter('theme'));
111 $config->setItemsPerPage($this->container
->getParameter('items_on_page'));
112 $config->setRssLimit($this->container
->getParameter('rss_limit'));
113 $config->setLanguage($this->container
->getParameter('language'));
115 $em->persist($config);
119 $this->get('session')->getFlashBag()->add(
121 sprintf('User "%s" added', $newUser->getUsername())
124 return $this->redirect($this->generateUrl('config'));
127 return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
129 'config' => $configForm->createView(),
130 'rss' => $rssForm->createView(),
131 'pwd' => $pwdForm->createView(),
132 'user' => $userForm->createView(),
133 'new_user' => $newUserForm->createView(),
136 'username' => $user->getUsername(),
137 'token' => $config->getRssToken(),
143 * @param Request $request
145 * @Route("/generate-token", name="generate_token")
147 * @return JsonResponse
149 public function generateTokenAction(Request
$request)
151 $config = $this->getConfig();
152 $config->setRssToken(Utils
::generateToken());
154 $em = $this->getDoctrine()->getManager();
155 $em->persist($config);
158 if ($request->isXmlHttpRequest()) {
159 return new JsonResponse(array('token' => $config->getRssToken()));
162 return $request->headers
->get('referer') ? $this->redirect($request->headers
->get('referer')) : $this->redirectToRoute('config');
166 * Retrieve config for the current user.
167 * If no config were found, create a new one.
169 * @return Wallabag\CoreBundle\Entity\Config
171 private function getConfig()
173 $config = $this->getDoctrine()
174 ->getRepository('WallabagCoreBundle:Config')
175 ->findOneByUser($this->getUser());
178 $config = new Config($this->getUser());