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\TaggingRule
;
11 use Wallabag\UserBundle\Entity\User
;
12 use Wallabag\CoreBundle\Form\Type\ChangePasswordType
;
13 use Wallabag\CoreBundle\Form\Type\UserInformationType
;
14 use Wallabag\CoreBundle\Form\Type\TaggingRuleType
;
15 use Wallabag\CoreBundle\Form\Type\NewUserType
;
16 use Wallabag\CoreBundle\Form\Type\RssType
;
17 use Wallabag\CoreBundle\Tools\Utils
;
19 class ConfigController
extends Controller
22 * @param Request $request
24 * @Route("/config", name="config")
26 public function indexAction(Request
$request)
28 $em = $this->getDoctrine()->getManager();
29 $config = $this->getConfig();
30 $userManager = $this->container
->get('fos_user.user_manager');
31 $user = $this->getUser();
33 // handle basic config detail (this form is defined as a service)
34 $configForm = $this->createForm('config', $config, array('action' => $this->generateUrl('config')));
35 $configForm->handleRequest($request);
37 if ($configForm->isValid()) {
38 $em->persist($config);
41 // switch active theme
42 $activeTheme = $this->get('liip_theme.active_theme');
43 $activeTheme->setName($config->getTheme());
45 $this->get('session')->getFlashBag()->add(
47 'Config saved. Some parameters will be considered after disconnection.'
50 return $this->redirect($this->generateUrl('config'));
53 // handle changing password
54 $pwdForm = $this->createForm(new ChangePasswordType(), null, array('action' => $this->generateUrl('config').'#set4'));
55 $pwdForm->handleRequest($request);
57 if ($pwdForm->isValid()) {
58 $user->setPlainPassword($pwdForm->get('new_password')->getData());
59 $userManager->updateUser($user, true);
61 $this->get('session')->getFlashBag()->add(
66 return $this->redirect($this->generateUrl('config'));
69 // handle changing user information
70 $userForm = $this->createForm(new UserInformationType(), $user, array(
71 'validation_groups' => array('Profile'),
72 'action' => $this->generateUrl('config').'#set3',
74 $userForm->handleRequest($request);
76 if ($userForm->isValid()) {
77 $userManager->updateUser($user, true);
79 $this->get('session')->getFlashBag()->add(
84 return $this->redirect($this->generateUrl('config'));
87 // handle rss information
88 $rssForm = $this->createForm(new RssType(), $config, array('action' => $this->generateUrl('config').'#set2'));
89 $rssForm->handleRequest($request);
91 if ($rssForm->isValid()) {
92 $em->persist($config);
95 $this->get('session')->getFlashBag()->add(
97 'RSS information updated'
100 return $this->redirect($this->generateUrl('config'));
103 // handle tagging rule
104 $taggingRule = new TaggingRule();
105 $newTaggingRule = $this->createForm(new TaggingRuleType(), $taggingRule, array('action' => $this->generateUrl('config').'#set5'));
106 $newTaggingRule->handleRequest($request);
108 if ($newTaggingRule->isValid()) {
109 $taggingRule->setConfig($config);
110 $em->persist($taggingRule);
113 $this->get('session')->getFlashBag()->add(
115 'Tagging rules updated'
118 return $this->redirect($this->generateUrl('config'));
121 // handle adding new user
122 $newUser = $userManager->createUser();
123 // enable created user by default
124 $newUser->setEnabled(true);
125 $newUserForm = $this->createForm(new NewUserType(), $newUser, array(
126 'validation_groups' => array('Profile'),
127 'action' => $this->generateUrl('config').'#set5',
129 $newUserForm->handleRequest($request);
131 if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) {
132 $userManager->updateUser($newUser, true);
134 $config = new Config($newUser);
135 $config->setTheme($this->container
->getParameter('theme'));
136 $config->setItemsPerPage($this->container
->getParameter('items_on_page'));
137 $config->setRssLimit($this->container
->getParameter('rss_limit'));
138 $config->setLanguage($this->container
->getParameter('language'));
140 $em->persist($config);
144 $this->get('session')->getFlashBag()->add(
146 sprintf('User "%s" added', $newUser->getUsername())
149 return $this->redirect($this->generateUrl('config'));
152 return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
154 'config' => $configForm->createView(),
155 'rss' => $rssForm->createView(),
156 'pwd' => $pwdForm->createView(),
157 'user' => $userForm->createView(),
158 'new_user' => $newUserForm->createView(),
159 'new_tagging_rule' => $newTaggingRule->createView(),
162 'username' => $user->getUsername(),
163 'token' => $config->getRssToken(),
169 * @param Request $request
171 * @Route("/generate-token", name="generate_token")
173 * @return JsonResponse
175 public function generateTokenAction(Request
$request)
177 $config = $this->getConfig();
178 $config->setRssToken(Utils
::generateToken());
180 $em = $this->getDoctrine()->getManager();
181 $em->persist($config);
184 if ($request->isXmlHttpRequest()) {
185 return new JsonResponse(array('token' => $config->getRssToken()));
188 return $request->headers
->get('referer') ? $this->redirect($request->headers
->get('referer')) : $this->redirectToRoute('config');
192 * Deletes a tagging rule and redirect to the config homepage.
194 * @param TaggingRule $rule
196 * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule")
198 * @return \Symfony\Component\HttpFoundation\RedirectResponse
200 public function deleteTaggingRule(TaggingRule
$rule)
202 if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) {
203 throw $this->createAccessDeniedException('You can not access this tagging ryle.');
206 $em = $this->getDoctrine()->getManager();
210 $this->get('session')->getFlashBag()->add(
212 'Tagging rule deleted'
215 return $this->redirect($this->generateUrl('config'));
219 * Retrieve config for the current user.
220 * If no config were found, create a new one.
222 * @return Wallabag\CoreBundle\Entity\Config
224 private function getConfig()
226 $config = $this->getDoctrine()
227 ->getRepository('WallabagCoreBundle:Config')
228 ->findOneByUser($this->getUser());
231 $config = new Config($this->getUser());