]>
Commit | Line | Data |
---|---|---|
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\Form\Type\ConfigType; | |
10 | use Wallabag\CoreBundle\Form\Type\ChangePasswordType; | |
11 | use Wallabag\CoreBundle\Form\Type\UserType; | |
12 | ||
13 | class ConfigController extends Controller | |
14 | { | |
15 | /** | |
16 | * @param Request $request | |
17 | * | |
18 | * @Route("/config", name="config") | |
19 | */ | |
20 | public function indexAction(Request $request) | |
21 | { | |
22 | $em = $this->getDoctrine()->getManager(); | |
23 | $config = $this->getConfig(); | |
24 | $user = $this->getUser(); | |
25 | ||
26 | // handle basic config detail | |
27 | $configForm = $this->createForm(new ConfigType(), $config); | |
28 | $configForm->handleRequest($request); | |
29 | ||
30 | if ($configForm->isValid()) { | |
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 | ||
42 | // handle changing password | |
43 | $pwdForm = $this->createForm(new ChangePasswordType()); | |
44 | $pwdForm->handleRequest($request); | |
45 | ||
46 | if ($pwdForm->isValid()) { | |
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 | ||
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 | ||
75 | return $this->render('WallabagCoreBundle:Config:index.html.twig', array( | |
76 | 'configForm' => $configForm->createView(), | |
77 | 'pwdForm' => $pwdForm->createView(), | |
78 | 'userForm' => $userForm->createView(), | |
79 | )); | |
80 | } | |
81 | ||
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 | */ | |
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 | } |