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

129 lines
3.8 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;
2015-02-22 08:30:25 +00:00
use Wallabag\CoreBundle\Entity\User;
2015-02-16 20:28:49 +00:00
use Wallabag\CoreBundle\Form\Type\ConfigType;
2015-02-17 20:03:23 +00:00
use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
use Wallabag\CoreBundle\Form\Type\UserType;
2015-02-22 08:30:25 +00:00
use Wallabag\CoreBundle\Form\Type\NewUserType;
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();
$user = $this->getUser();
2015-02-16 20:28:49 +00:00
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->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'));
}
// handle changing user information
$userForm = $this->createForm(new UserType(), $user);
$userForm->handleRequest($request);
if ($userForm->isValid()) {
$em->persist($user);
$em->flush();
$this->get('session')->getFlashBag()->add(
'notice',
'Information updated'
);
return $this->redirect($this->generateUrl('config'));
}
2015-02-22 08:30:25 +00:00
// handle adding new user
$newUser = new User();
$newUserForm = $this->createForm(new NewUserType(), $newUser);
$newUserForm->handleRequest($request);
if ($newUserForm->isValid()) {
$em->persist($newUser);
2015-02-22 09:50:27 +00:00
$config = new Config($newUser);
$config->setTheme($this->container->getParameter('theme'));
$config->setItemsPerPage($this->container->getParameter('items_on_page'));
$config->setLanguage($this->container->getParameter('language'));
$em->persist($config);
2015-02-22 08:30:25 +00:00
$em->flush();
$this->get('session')->getFlashBag()->add(
'notice',
sprintf('User "%s" added', $newUser->getUsername())
);
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(),
'userForm' => $userForm->createView(),
2015-02-22 08:30:25 +00:00
'newUserForm' => $newUserForm->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;
}
}