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);
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 $newUserForm = $this->createForm(new NewUserType(), $newUser);
102 $newUserForm->handleRequest($request);
104 if ($newUserForm->isValid()) {
105 $em->persist($newUser);
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'));
113 $em->persist($config);
117 $this->get('session')->getFlashBag()->add(
119 sprintf('User "%s" added', $newUser->getUsername())
122 return $this->redirect($this->generateUrl('config'));
125 return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
127 'config' => $configForm->createView(),
128 'rss' => $rssForm->createView(),
129 'pwd' => $pwdForm->createView(),
130 'user' => $userForm->createView(),
131 'new_user' => $newUserForm->createView(),
134 'username' => $user->getUsername(),
135 'token' => $config->getRssToken(),
141 * @param Request $request
143 * @Route("/generate-token", name="generate_token")
145 * @return JsonResponse
147 public function generateTokenAction(Request
$request)
149 $config = $this->getConfig();
150 $config->setRssToken(Utils
::generateToken());
152 $em = $this->getDoctrine()->getManager();
153 $em->persist($config);
156 if ($request->isXmlHttpRequest()) {
157 return new JsonResponse(array('token' => $config->getRssToken()));
160 return $request->headers
->get('referer') ? $this->redirect($request->headers
->get('referer')) : $this->redirectToRoute('config');
164 * Retrieve config for the current user.
165 * If no config were found, create a new one.
167 * @return Wallabag\CoreBundle\Entity\Config
169 private function getConfig()
171 $config = $this->getDoctrine()
172 ->getRepository('WallabagCoreBundle:Config')
173 ->findOneByUser($this->getUser());
176 $config = new Config($this->getUser());