]>
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; | |
9 | use Wallabag\CoreBundle\Form\Type\ConfigType; | |
d9085c63 | 10 | use Wallabag\CoreBundle\Form\Type\ChangePasswordType; |
4d85d7e9 J |
11 | |
12 | class ConfigController extends Controller | |
13 | { | |
14 | /** | |
15 | * @param Request $request | |
16 | * | |
17 | * @Route("/config", name="config") | |
4d85d7e9 J |
18 | */ |
19 | public function indexAction(Request $request) | |
20 | { | |
d9085c63 | 21 | $em = $this->getDoctrine()->getManager(); |
4d85d7e9 J |
22 | $config = $this->getConfig(); |
23 | ||
d9085c63 J |
24 | // handle basic config detail |
25 | $configForm = $this->createForm(new ConfigType(), $config); | |
26 | $configForm->handleRequest($request); | |
4d85d7e9 | 27 | |
d9085c63 | 28 | if ($configForm->isValid()) { |
4d85d7e9 | 29 | |
4d85d7e9 J |
30 | $em->persist($config); |
31 | $em->flush(); | |
32 | ||
33 | $this->get('session')->getFlashBag()->add( | |
34 | 'notice', | |
35 | 'Config saved' | |
36 | ); | |
37 | ||
38 | return $this->redirect($this->generateUrl('config')); | |
39 | } | |
40 | ||
d9085c63 J |
41 | // handle changing password |
42 | $pwdForm = $this->createForm(new ChangePasswordType()); | |
43 | $pwdForm->handleRequest($request); | |
44 | ||
45 | if ($pwdForm->isValid()) { | |
46 | $user = $this->getUser(); | |
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 | ||
4d85d7e9 | 59 | return $this->render('WallabagCoreBundle:Config:index.html.twig', array( |
d9085c63 J |
60 | 'configForm' => $configForm->createView(), |
61 | 'pwdForm' => $pwdForm->createView(), | |
4d85d7e9 J |
62 | )); |
63 | } | |
64 | ||
d9085c63 J |
65 | /** |
66 | * Retrieve config for the current user. | |
67 | * If no config were found, create a new one. | |
68 | * | |
69 | * @return Wallabag\CoreBundle\Entity\Config | |
70 | */ | |
4d85d7e9 J |
71 | private function getConfig() |
72 | { | |
73 | $config = $this->getDoctrine() | |
74 | ->getRepository('WallabagCoreBundle:Config') | |
75 | ->findOneByUser($this->getUser()); | |
76 | ||
77 | if (!$config) { | |
78 | $config = new Config($this->getUser()); | |
79 | } | |
80 | ||
81 | return $config; | |
82 | } | |
83 | } |