]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/ConfigController.php
Updating logged in user (email, name, etc ..)
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / ConfigController.php
CommitLineData
4d85d7e9
J
1<?php
2
3namespace Wallabag\CoreBundle\Controller;
4
5use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7use Symfony\Component\HttpFoundation\Request;
8use Wallabag\CoreBundle\Entity\Config;
9use Wallabag\CoreBundle\Form\Type\ConfigType;
d9085c63 10use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
c0d9eba0 11use Wallabag\CoreBundle\Form\Type\UserType;
4d85d7e9
J
12
13class ConfigController extends Controller
14{
15 /**
16 * @param Request $request
17 *
18 * @Route("/config", name="config")
4d85d7e9
J
19 */
20 public function indexAction(Request $request)
21 {
d9085c63 22 $em = $this->getDoctrine()->getManager();
4d85d7e9 23 $config = $this->getConfig();
c0d9eba0 24 $user = $this->getUser();
4d85d7e9 25
d9085c63
J
26 // handle basic config detail
27 $configForm = $this->createForm(new ConfigType(), $config);
28 $configForm->handleRequest($request);
4d85d7e9 29
d9085c63 30 if ($configForm->isValid()) {
4d85d7e9
J
31 $em->persist($config);
32 $em->flush();
33
34 $this->get('session')->getFlashBag()->add(
35 'notice',
36 'Config saved'
37 );
38
39 return $this->redirect($this->generateUrl('config'));
40 }
41
d9085c63
J
42 // handle changing password
43 $pwdForm = $this->createForm(new ChangePasswordType());
44 $pwdForm->handleRequest($request);
45
46 if ($pwdForm->isValid()) {
d9085c63
J
47 $user->setPassword($pwdForm->get('new_password')->getData());
48 $em->persist($user);
49 $em->flush();
50
51 $this->get('session')->getFlashBag()->add(
52 'notice',
53 'Password updated'
54 );
55
56 return $this->redirect($this->generateUrl('config'));
57 }
58
c0d9eba0
J
59 // handle changing user information
60 $userForm = $this->createForm(new UserType(), $user);
61 $userForm->handleRequest($request);
62
63 if ($userForm->isValid()) {
64 $em->persist($user);
65 $em->flush();
66
67 $this->get('session')->getFlashBag()->add(
68 'notice',
69 'Information updated'
70 );
71
72 return $this->redirect($this->generateUrl('config'));
73 }
74
4d85d7e9 75 return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
d9085c63
J
76 'configForm' => $configForm->createView(),
77 'pwdForm' => $pwdForm->createView(),
c0d9eba0 78 'userForm' => $userForm->createView(),
4d85d7e9
J
79 ));
80 }
81
d9085c63
J
82 /**
83 * Retrieve config for the current user.
84 * If no config were found, create a new one.
85 *
86 * @return Wallabag\CoreBundle\Entity\Config
87 */
4d85d7e9
J
88 private function getConfig()
89 {
90 $config = $this->getDoctrine()
91 ->getRepository('WallabagCoreBundle:Config')
92 ->findOneByUser($this->getUser());
93
94 if (!$config) {
95 $config = new Config($this->getUser());
96 }
97
98 return $config;
99 }
100}