src/Controller/UTools/UserController.php line 30

Open in your IDE?
  1. <?php
  2. namespace FMS\eLibreria\CmsBundle\Controller\UTools;
  3. use FMS\eLibreria\CmsBundle\Controller\AbstractCrudEntity;
  4. use FMS\eLibreria\CmsBundle\Controller\CmsController;
  5. use FMS\eLibreria\CmsBundle\Exception\RedirectFormException;
  6. use FMS\eLibreria\CmsBundle\Form\Types\ATools\Employee\EmployeePasswordType;
  7. use FMS\eLibreria\CmsBundle\Form\Types\ATools\Employee\EmployeeType;
  8. use FMS\eLibreria\CmsBundle\Utils\BreadcrumbItem;
  9. use FMS\eLibreria\CmsBundle\Utils\EntityForm;
  10. use FMS\eLibreria\CoreBundle\Form\Types\AbstractCoreType;
  11. use FMS\eLibreria\CoreBundle\Form\Types\AbstractTypeEntity;
  12. use FMS\eLibreria\CoreBundle\Model\Entity\Employee;
  13. use Symfony\Component\Form\FormInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. class UserController extends CmsController {
  16. const LAST_USERNAME = "last_username";
  17. const ERROR = "error";
  18. public function login(Request $request){
  19. $authenticationUtils = $this->getAuthenticationUtils();
  20. $options = array();
  21. $options[self::LAST_USERNAME] = $authenticationUtils->getLastUsername();
  22. $options[self::ERROR] = $authenticationUtils->getLastAuthenticationError();
  23. return $this->render("user/login.html.twig", $options);
  24. }
  25. protected function handleForm($editMode, $entity, FormInterface $form, $returnAction = null){
  26. if($form->isSubmitted() && $form->isValid()){
  27. $em = $this->getEntityManager();
  28. $em->persist($entity);
  29. $em->flush();
  30. $this->addFlash(self::FLASH_SUCCESS, $editMode ? self::EDIT_MSG_SUCCESS : self::CREATE_MSG_SUCCESS);
  31. throw new RedirectFormException($this->generateUrl("cms_utools_user_edit_profile"));
  32. }
  33. }
  34. /**
  35. * @param $editMode
  36. * @param $entity Employee
  37. * @param FormInterface $form
  38. */
  39. protected function handleChangePasswordForm($editMode, $entity, FormInterface $form){
  40. if($form->isSubmitted() && $form->isValid()){
  41. $em = $this->getEntityManager();
  42. if(!is_null($entity->getNewPassword())){
  43. $passwordEncoder = $this->getUserPasswordEncoder();
  44. $entity->setPassword($passwordEncoder->encodePassword($entity, $entity->getNewPassword()));
  45. $em->persist($entity);
  46. $em->flush();
  47. $this->addFlash(self::FLASH_SUCCESS, $editMode ? self::EDIT_MSG_SUCCESS : self::CREATE_MSG_SUCCESS);
  48. throw new RedirectFormException($this->generateUrl("cms_utools_user_edit_profile"));
  49. }
  50. }
  51. }
  52. public function editProfile(Request $request){
  53. $options = $this->getBasicOptions();
  54. $entity = $this->getUser();
  55. $rootBr = $this->getBreadcrumbRootItem($options);
  56. $rootBr->addChildren(new BreadcrumbItem("breadcrumb.profile"));
  57. $rootBr->addChildren(new BreadcrumbItem($entity->__toString()));
  58. $forms = array();
  59. $entityforms = array();
  60. $optionsChangePasswordForm = array(
  61. AbstractCoreType::OPTION_LABEL => "employees.header.change_password",
  62. AbstractCoreType::OPTION_COLLAPSABLE => true,
  63. AbstractCoreType::OPTION_INIT_COLLAPSED => true,
  64. EmployeePasswordType::EDITING_PROFILE => true,
  65. );
  66. $entityforms[] =
  67. new EntityForm(
  68. EmployeePasswordType::class,
  69. $optionsChangePasswordForm,
  70. array($this, "handleChangePasswordForm"));
  71. $entityforms[] = new EntityForm(EmployeeType::class, array(AbstractCoreType::OPTION_LABEL => "employees.header.edit"));
  72. $editMode = true;
  73. /** @var EntityForm $entityForm */
  74. foreach($entityforms as $entityForm){
  75. $optionsForm = $entityForm->getOptionsForm();
  76. $optionsForm[AbstractTypeEntity::OPTION_EDIT_MODE] = $editMode;
  77. $form = $this->createForm($entityForm->getFormClass(), $entity, $optionsForm);
  78. $form->handleRequest($request);
  79. try {
  80. $callable = $entityForm->getCallable();
  81. if(is_null($callable)) {
  82. $this->handleForm($editMode, $entity, $form);
  83. } else {
  84. call_user_func($callable, $editMode, $entity, $form);
  85. }
  86. } catch(RedirectFormException $redirectFormException){
  87. return $this->redirect($redirectFormException->getToUrl());
  88. }
  89. $forms[] = $form->createView();
  90. }
  91. $options[AbstractCrudEntity::TWIG_OPTIONS_FORMS] = $forms;
  92. return $this->render("crud/" . AbstractCrudEntity::FORM_TEMPLATE_VERTICAL_ONE_PAGE, $options);
  93. }
  94. }