]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/ConfigController.php
68e034fa7a9588def73786d3ddbb0a274fcbf93e
[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\ConfigType;
11 use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
12 use Wallabag\CoreBundle\Form\Type\UserType;
13 use Wallabag\CoreBundle\Form\Type\NewUserType;
14
15 class ConfigController extends Controller
16 {
17 /**
18 * @param Request $request
19 *
20 * @Route("/config", name="config")
21 */
22 public function indexAction(Request $request)
23 {
24 $em = $this->getDoctrine()->getManager();
25 $config = $this->getConfig();
26 $user = $this->getUser();
27
28 // handle basic config detail
29 $configForm = $this->createForm(new ConfigType(), $config);
30 $configForm->handleRequest($request);
31
32 if ($configForm->isValid()) {
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
44 // handle changing password
45 $pwdForm = $this->createForm(new ChangePasswordType());
46 $pwdForm->handleRequest($request);
47
48 if ($pwdForm->isValid()) {
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
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
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);
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
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
102 return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
103 'configForm' => $configForm->createView(),
104 'pwdForm' => $pwdForm->createView(),
105 'userForm' => $userForm->createView(),
106 'newUserForm' => $newUserForm->createView(),
107 ));
108 }
109
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 */
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 }