<?php
namespace FMS\eLibreria\CmsBundle\Controller\UTools;
use FMS\eLibreria\CmsBundle\Controller\AbstractCrudEntity;
use FMS\eLibreria\CmsBundle\Controller\CmsController;
use FMS\eLibreria\CmsBundle\Exception\RedirectFormException;
use FMS\eLibreria\CmsBundle\Form\Types\ATools\Employee\EmployeePasswordType;
use FMS\eLibreria\CmsBundle\Form\Types\ATools\Employee\EmployeeType;
use FMS\eLibreria\CmsBundle\Utils\BreadcrumbItem;
use FMS\eLibreria\CmsBundle\Utils\EntityForm;
use FMS\eLibreria\CoreBundle\Form\Types\AbstractCoreType;
use FMS\eLibreria\CoreBundle\Form\Types\AbstractTypeEntity;
use FMS\eLibreria\CoreBundle\Model\Entity\Employee;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
class UserController extends CmsController {
const LAST_USERNAME = "last_username";
const ERROR = "error";
public function login(Request $request){
$authenticationUtils = $this->getAuthenticationUtils();
$options = array();
$options[self::LAST_USERNAME] = $authenticationUtils->getLastUsername();
$options[self::ERROR] = $authenticationUtils->getLastAuthenticationError();
return $this->render("user/login.html.twig", $options);
}
protected function handleForm($editMode, $entity, FormInterface $form, $returnAction = null){
if($form->isSubmitted() && $form->isValid()){
$em = $this->getEntityManager();
$em->persist($entity);
$em->flush();
$this->addFlash(self::FLASH_SUCCESS, $editMode ? self::EDIT_MSG_SUCCESS : self::CREATE_MSG_SUCCESS);
throw new RedirectFormException($this->generateUrl("cms_utools_user_edit_profile"));
}
}
/**
* @param $editMode
* @param $entity Employee
* @param FormInterface $form
*/
protected function handleChangePasswordForm($editMode, $entity, FormInterface $form){
if($form->isSubmitted() && $form->isValid()){
$em = $this->getEntityManager();
if(!is_null($entity->getNewPassword())){
$passwordEncoder = $this->getUserPasswordEncoder();
$entity->setPassword($passwordEncoder->encodePassword($entity, $entity->getNewPassword()));
$em->persist($entity);
$em->flush();
$this->addFlash(self::FLASH_SUCCESS, $editMode ? self::EDIT_MSG_SUCCESS : self::CREATE_MSG_SUCCESS);
throw new RedirectFormException($this->generateUrl("cms_utools_user_edit_profile"));
}
}
}
public function editProfile(Request $request){
$options = $this->getBasicOptions();
$entity = $this->getUser();
$rootBr = $this->getBreadcrumbRootItem($options);
$rootBr->addChildren(new BreadcrumbItem("breadcrumb.profile"));
$rootBr->addChildren(new BreadcrumbItem($entity->__toString()));
$forms = array();
$entityforms = array();
$optionsChangePasswordForm = array(
AbstractCoreType::OPTION_LABEL => "employees.header.change_password",
AbstractCoreType::OPTION_COLLAPSABLE => true,
AbstractCoreType::OPTION_INIT_COLLAPSED => true,
EmployeePasswordType::EDITING_PROFILE => true,
);
$entityforms[] =
new EntityForm(
EmployeePasswordType::class,
$optionsChangePasswordForm,
array($this, "handleChangePasswordForm"));
$entityforms[] = new EntityForm(EmployeeType::class, array(AbstractCoreType::OPTION_LABEL => "employees.header.edit"));
$editMode = true;
/** @var EntityForm $entityForm */
foreach($entityforms as $entityForm){
$optionsForm = $entityForm->getOptionsForm();
$optionsForm[AbstractTypeEntity::OPTION_EDIT_MODE] = $editMode;
$form = $this->createForm($entityForm->getFormClass(), $entity, $optionsForm);
$form->handleRequest($request);
try {
$callable = $entityForm->getCallable();
if(is_null($callable)) {
$this->handleForm($editMode, $entity, $form);
} else {
call_user_func($callable, $editMode, $entity, $form);
}
} catch(RedirectFormException $redirectFormException){
return $this->redirect($redirectFormException->getToUrl());
}
$forms[] = $form->createView();
}
$options[AbstractCrudEntity::TWIG_OPTIONS_FORMS] = $forms;
return $this->render("crud/" . AbstractCrudEntity::FORM_TEMPLATE_VERTICAL_ONE_PAGE, $options);
}
}