]>
Commit | Line | Data |
---|---|---|
4d85d7e9 J |
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; | |
e4977b8a | 9 | use Wallabag\CoreBundle\Entity\User; |
d9085c63 | 10 | use Wallabag\CoreBundle\Form\Type\ChangePasswordType; |
c0d9eba0 | 11 | use Wallabag\CoreBundle\Form\Type\UserType; |
e4977b8a | 12 | use Wallabag\CoreBundle\Form\Type\NewUserType; |
4d85d7e9 J |
13 | |
14 | class ConfigController extends Controller | |
15 | { | |
16 | /** | |
17 | * @param Request $request | |
18 | * | |
19 | * @Route("/config", name="config") | |
4d85d7e9 J |
20 | */ |
21 | public function indexAction(Request $request) | |
22 | { | |
d9085c63 | 23 | $em = $this->getDoctrine()->getManager(); |
4d85d7e9 | 24 | $config = $this->getConfig(); |
c0d9eba0 | 25 | $user = $this->getUser(); |
4d85d7e9 | 26 | |
32da2a70 J |
27 | // handle basic config detail (this form is defined as a service) |
28 | $configForm = $this->createForm('config', $config); | |
d9085c63 | 29 | $configForm->handleRequest($request); |
4d85d7e9 | 30 | |
d9085c63 | 31 | if ($configForm->isValid()) { |
4d85d7e9 J |
32 | $em->persist($config); |
33 | $em->flush(); | |
34 | ||
32da2a70 J |
35 | // switch active theme |
36 | $activeTheme = $this->get('liip_theme.active_theme'); | |
37 | $activeTheme->setName($config->getTheme()); | |
38 | ||
4d85d7e9 J |
39 | $this->get('session')->getFlashBag()->add( |
40 | 'notice', | |
41 | 'Config saved' | |
42 | ); | |
43 | ||
44 | return $this->redirect($this->generateUrl('config')); | |
45 | } | |
46 | ||
d9085c63 J |
47 | // handle changing password |
48 | $pwdForm = $this->createForm(new ChangePasswordType()); | |
49 | $pwdForm->handleRequest($request); | |
50 | ||
51 | if ($pwdForm->isValid()) { | |
d9085c63 J |
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 | ||
c0d9eba0 J |
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 | ||
e4977b8a J |
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); | |
0bd2cb1e J |
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 | ||
e4977b8a J |
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 | ||
4d85d7e9 | 105 | return $this->render('WallabagCoreBundle:Config:index.html.twig', array( |
d9085c63 J |
106 | 'configForm' => $configForm->createView(), |
107 | 'pwdForm' => $pwdForm->createView(), | |
c0d9eba0 | 108 | 'userForm' => $userForm->createView(), |
e4977b8a | 109 | 'newUserForm' => $newUserForm->createView(), |
4d85d7e9 J |
110 | )); |
111 | } | |
112 | ||
d9085c63 J |
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 | */ | |
4d85d7e9 J |
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 | } |