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