]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/ConfigController.php
Merge pull request #1132 from wallabag/v2-theme
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / ConfigController.php
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 Wallabag\CoreBundle\Entity\Config;
9 use Wallabag\CoreBundle\Entity\User;
10 use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
11 use Wallabag\CoreBundle\Form\Type\UserType;
12 use Wallabag\CoreBundle\Form\Type\NewUserType;
13
14 class ConfigController extends Controller
15 {
16 /**
17 * @param Request $request
18 *
19 * @Route("/config", name="config")
20 */
21 public function indexAction(Request $request)
22 {
23 $em = $this->getDoctrine()->getManager();
24 $config = $this->getConfig();
25 $user = $this->getUser();
26
27 // handle basic config detail (this form is defined as a service)
28 $configForm = $this->createForm('config', $config);
29 $configForm->handleRequest($request);
30
31 if ($configForm->isValid()) {
32 $em->persist($config);
33 $em->flush();
34
35 // switch active theme
36 $activeTheme = $this->get('liip_theme.active_theme');
37 $activeTheme->setName($config->getTheme());
38
39 $this->get('session')->getFlashBag()->add(
40 'notice',
41 'Config saved'
42 );
43
44 return $this->redirect($this->generateUrl('config'));
45 }
46
47 // handle changing password
48 $pwdForm = $this->createForm(new ChangePasswordType());
49 $pwdForm->handleRequest($request);
50
51 if ($pwdForm->isValid()) {
52 $user->setPassword($pwdForm->get('new_password')->getData());
53 $em->persist($user);
54 $em->flush();
55
56 $this->get('session')->getFlashBag()->add(
57 'notice',
58 'Password updated'
59 );
60
61 return $this->redirect($this->generateUrl('config'));
62 }
63
64 // handle changing user information
65 $userForm = $this->createForm(new UserType(), $user);
66 $userForm->handleRequest($request);
67
68 if ($userForm->isValid()) {
69 $em->persist($user);
70 $em->flush();
71
72 $this->get('session')->getFlashBag()->add(
73 'notice',
74 'Information updated'
75 );
76
77 return $this->redirect($this->generateUrl('config'));
78 }
79
80 // handle adding new user
81 $newUser = new User();
82 $newUserForm = $this->createForm(new NewUserType(), $newUser);
83 $newUserForm->handleRequest($request);
84
85 if ($newUserForm->isValid()) {
86 $em->persist($newUser);
87
88 $config = new Config($newUser);
89 $config->setTheme($this->container->getParameter('theme'));
90 $config->setItemsPerPage($this->container->getParameter('items_on_page'));
91 $config->setLanguage($this->container->getParameter('language'));
92
93 $em->persist($config);
94
95 $em->flush();
96
97 $this->get('session')->getFlashBag()->add(
98 'notice',
99 sprintf('User "%s" added', $newUser->getUsername())
100 );
101
102 return $this->redirect($this->generateUrl('config'));
103 }
104
105 return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
106 'configForm' => $configForm->createView(),
107 'pwdForm' => $pwdForm->createView(),
108 'userForm' => $userForm->createView(),
109 'newUserForm' => $newUserForm->createView(),
110 ));
111 }
112
113 /**
114 * Retrieve config for the current user.
115 * If no config were found, create a new one.
116 *
117 * @return Wallabag\CoreBundle\Entity\Config
118 */
119 private function getConfig()
120 {
121 $config = $this->getDoctrine()
122 ->getRepository('WallabagCoreBundle:Config')
123 ->findOneByUser($this->getUser());
124
125 if (!$config) {
126 $config = new Config($this->getUser());
127 }
128
129 return $config;
130 }
131 }