src/Admin/UserAdmin.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Admin;
  3. use App\Application\Sonata\UserBundle\Entity\User;
  4. use App\Entity\SonataUserUser;
  5. use App\Util\ClientUtil;
  6. use App\Util\PropertyUtil;
  7. use Sonata\AdminBundle\Datagrid\ListMapper;
  8. use Sonata\AdminBundle\Form\FormMapper;
  9. use Sonata\AdminBundle\Form\Type\ModelType;
  10. use Sonata\Form\Type\DatePickerType;
  11. use Sonata\Form\Validator\ErrorElement;
  12. use Sonata\UserBundle\Form\Type\SecurityRolesType;
  13. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  14. use Symfony\Component\Form\Extension\Core\Type\LocaleType;
  15. use Symfony\Component\Form\Extension\Core\Type\TextType;
  16. use Symfony\Component\Form\Extension\Core\Type\TimezoneType;
  17. use Symfony\Component\Form\Extension\Core\Type\UrlType;
  18. use Symfony\Component\Form\FormTypeInterface;
  19. use Symfony\Component\Validator\Constraints\Regex;
  20. class UserAdmin extends \Sonata\UserBundle\Admin\Model\UserAdmin
  21. {
  22.     protected function configureListFields(ListMapper $listMapper): void
  23.     {
  24.         $listMapper
  25.             ->addIdentifier('username')
  26.             ->add('email')
  27.             ->add('groups')
  28.             ->add('enabled'null, ['editable' => true])
  29.             ->add('createdAt');
  30.         if ($this->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
  31.             $listMapper
  32.                 ->add('impersonating''string', ['template' => '@SonataUser/Admin/Field/impersonating.html.twig'])
  33.             ;
  34.         }
  35.         $listMapper->add('_action'null, [
  36.             'actions' => [
  37.                 'addresses' => ['template' => 'admin/user/address.html.twig'],
  38.                 'cancelations' => ['template' => 'admin/user/cancel.html.twig'],
  39.                 'delete' => [],
  40.             ],
  41.         ]);
  42.     }
  43.     protected function configureFormFields(FormMapper $formMapper): void
  44.     {
  45.         /** @var User $user */
  46.         $user $this->getSubject();
  47. // define group zoning
  48.         $formMapper
  49.             ->tab('User')
  50.             ->with('Profile', ['class' => 'col-md-6'])->end()
  51.             ->with('General', ['class' => 'col-md-6'])->end()
  52.             ->with('Social', ['class' => 'col-md-6 d-none'])->end()
  53.             ->end()
  54.             ->tab('Security')
  55.             ->with('Status', ['class' => 'col-md-4'])->end()
  56.             ->with('Groups', ['class' => 'col-md-4'])->end()
  57.             ->with('Keys', ['class' => 'col-md-4'])->end()
  58.             ->with('Roles', ['class' => 'col-md-12 d-none'])->end()
  59.             ->end();
  60.         $now = new \DateTime();
  61.         $genderOptions = [
  62.             'choices' => \call_user_func([$this->getUserManager()->getClass(), 'getGenderList']),
  63.             'required' => false,
  64.             'translation_domain' => $this->getTranslationDomain(),
  65.         ];
  66.         // NEXT_MAJOR: Remove this when dropping support for SF 2.8
  67.         if (method_exists(FormTypeInterface::class, 'setDefaultOptions')) {
  68.             $genderOptions['choices_as_values'] = true;
  69.         }
  70.         $formMapper
  71.             ->tab('User')
  72.             ->with('General')
  73.             ->add('username')
  74.             ->add('accountType')
  75.             ->add('accountState')
  76.             ->add('accountRole')
  77.             ->add('email')
  78.             ->add('plainPassword'TextType::class, [
  79.                 'required' => (!$this->getSubject() || null === $this->getSubject()->getId()),
  80.             ])
  81.             ->end()
  82.             ->with('Profile')
  83.             ->add('firstname'null, ['required' => true])
  84.             ->add('lastname'null, ['required' => true])
  85.             ->add('website'UrlType::class, ['required' => false])
  86. //            ->add('biography', TextType::class, ['required' => false])
  87.             ->add('locale'LocaleType::class, ['required' => false'data' => (!empty($user->getId()) ? $user->getLocale() : 'cs')])
  88.             ->add('timezone'TimezoneType::class, ['required' => false'data' => (!empty($user->getId()) ? $user->getTimezone() : 'Europe/Prague')])
  89.             ->add('phone'null, ['required' => false'constraints' => [
  90.                 new Regex('/([+]?\d{1,3}[. \s]?)?(\d{9}?)/')
  91.             ]])
  92.             ->add('dateOfBirth'DatePickerType::class, [
  93.                 'years' => range(1900$now->format('Y')),
  94.                 'dp_min_date' => '1-1-1900',
  95.                 'dp_max_date' => $now->format('c'),
  96.                 'required' => false,
  97.             ])
  98.             ->add('gender'ChoiceType::class, $genderOptions)
  99.             ->end()
  100.             ->with('Social')
  101.             ->add('facebookUid'null, ['required' => false])
  102.             ->add('facebookName'null, ['required' => false])
  103.             ->add('twitterUid'null, ['required' => false])
  104.             ->add('twitterName'null, ['required' => false])
  105.             ->add('gplusUid'null, ['required' => false])
  106.             ->add('gplusName'null, ['required' => false])
  107.             ->end()
  108.             ->end()
  109.             ->tab('Security')
  110.             ->with('Status')
  111.             ->add('enabled'null, ['required' => false])
  112.             ->end()
  113.             ->with('Groups')
  114.             ->add('groups'ModelType::class, [
  115.                 'required' => false,
  116.                 'expanded' => true,
  117.                 'multiple' => true,
  118.             ])
  119.             ->end()
  120.             ->with('Roles')
  121.             ->add('realRoles'SecurityRolesType::class, [
  122.                 'label' => 'Role',
  123.                 'expanded' => true,
  124.                 'multiple' => true,
  125.                 'required' => false,
  126.             ])
  127.             ->end()
  128.             ->with('Keys')
  129.             ->add('token'null, ['required' => false])
  130.             ->add('twoStepVerificationCode'null, ['required' => false])
  131.             ->end()
  132.             ->end();
  133.     }
  134.     public function getFormBuilder()
  135.     {
  136.         $this->formOptions['data_class'] = $this->getClass();
  137.         $formBuilder $this->getFormContractor()->getFormBuilder(
  138.             $this->getUniqid(),
  139.             $this->formOptions
  140.         );
  141.         $this->defineFormBuilder($formBuilder);
  142.         return $formBuilder;
  143.     }
  144.     /**
  145.      * @param ErrorElement $errorElement
  146.      * @param $object
  147.      */
  148.     public function validate(ErrorElement $errorElement$object)
  149.     {
  150.         if (!empty($object->getPhone()) && empty(preg_match('/^[0-9\-\(\)\/\+\s]*$/'$object->getPhone()))) {
  151.             $error 'Telefon je v nesprávném tvaru';
  152.             $errorElement->with('phone')->addViolation($error)->end();
  153.         }
  154.     }
  155.     /**
  156.      * @param SonataUserUser $object
  157.      */
  158.     public function postUpdate($object)
  159.     {
  160.         if ($this->hasRequest() && null !== $object->getLocale()) {
  161.             $this->getRequest()->getSession()->set('_locale'$object->getLocale());
  162.         }
  163.     }
  164. }