wallabag/src/Wallabag/CoreBundle/Controller/ConfigController.php

84 lines
2.3 KiB
PHP
Raw Normal View History

2015-02-16 20:28:49 +00:00
<?php
namespace Wallabag\CoreBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Wallabag\CoreBundle\Entity\Config;
use Wallabag\CoreBundle\Form\Type\ConfigType;
2015-02-17 20:03:23 +00:00
use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
2015-02-16 20:28:49 +00:00
class ConfigController extends Controller
{
/**
* @param Request $request
*
* @Route("/config", name="config")
*/
public function indexAction(Request $request)
{
2015-02-17 20:03:23 +00:00
$em = $this->getDoctrine()->getManager();
2015-02-16 20:28:49 +00:00
$config = $this->getConfig();
2015-02-17 20:03:23 +00:00
// handle basic config detail
$configForm = $this->createForm(new ConfigType(), $config);
$configForm->handleRequest($request);
2015-02-16 20:28:49 +00:00
2015-02-17 20:03:23 +00:00
if ($configForm->isValid()) {
2015-02-16 20:28:49 +00:00
$em->persist($config);
$em->flush();
$this->get('session')->getFlashBag()->add(
'notice',
'Config saved'
);
return $this->redirect($this->generateUrl('config'));
}
2015-02-17 20:03:23 +00:00
// handle changing password
$pwdForm = $this->createForm(new ChangePasswordType());
$pwdForm->handleRequest($request);
if ($pwdForm->isValid()) {
$user = $this->getUser();
$user->setPassword($pwdForm->get('new_password')->getData());
$em->persist($user);
$em->flush();
$this->get('session')->getFlashBag()->add(
'notice',
'Password updated'
);
return $this->redirect($this->generateUrl('config'));
}
2015-02-16 20:28:49 +00:00
return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
2015-02-17 20:03:23 +00:00
'configForm' => $configForm->createView(),
'pwdForm' => $pwdForm->createView(),
2015-02-16 20:28:49 +00:00
));
}
2015-02-17 20:03:23 +00:00
/**
* Retrieve config for the current user.
* If no config were found, create a new one.
*
* @return Wallabag\CoreBundle\Entity\Config
*/
2015-02-16 20:28:49 +00:00
private function getConfig()
{
$config = $this->getDoctrine()
->getRepository('WallabagCoreBundle:Config')
->findOneByUser($this->getUser());
if (!$config) {
$config = new Config($this->getUser());
}
return $config;
}
}