<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Application\Sonata\UserBundle\Admin;
use App\Application\Sonata\UserBundle\Entity\User;
use App\Enums\UFEnum;
use App\Services\ImageCachingManager;
use FOS\UserBundle\Model\UserManagerInterface;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\Type\ModelType;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\Form\Type\CollectionType;
use Sonata\Form\Type\DatePickerType;
use Sonata\UserBundle\Form\Type\SecurityRolesType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Security\Core\Security;
class UserAdmin extends AbstractAdmin
{
/**
* @var Security
*/
private $security;
/**
* @var UserManagerInterface
*/
protected $userManager;
public function __construct($code, $class, $baseControllerName, Security $security)
{
parent::__construct($code, $class, $baseControllerName);
$this->security = $security;
}
/**
* {@inheritdoc}
*/
public function getFormBuilder()
{
$this->formOptions['data_class'] = $this->getClass();
$options = $this->formOptions;
$options['validation_groups'] = (!$this->getSubject() || null === $this->getSubject()->getId()) ? 'Registration' : 'Profile';
$formBuilder = $this->getFormContractor()->getFormBuilder($this->getUniqid(), $options);
$this->defineFormBuilder($formBuilder);
return $formBuilder;
}
/**
* {@inheritdoc}
*/
public function getExportFields()
{
// avoid security field to be exported
return array_filter(parent::getExportFields(), function ($v) {
return !in_array($v, ['password', 'salt']);
});
}
/**
* {@inheritdoc}
*/
public function preUpdate($user): void
{
$this->getUserManager()->updateCanonicalFields($user);
$this->getUserManager()->updatePassword($user);
}
/**
* @return UserManagerInterface
*/
public function getUserManager()
{
return $this->userManager;
}
/**
* @param UserManagerInterface $userManager
*/
public function setUserManager(UserManagerInterface $userManager): void
{
$this->userManager = $userManager;
}
/**
* {@inheritdoc}
*/
protected function configureListFields(ListMapper $listMapper): void
{
$listMapper
->addIdentifier('username')
->add('email')
->add('groups')
->add('enabled', null, ['editable' => true])
->add('createdAt')
;
if ($this->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
$listMapper
->add('impersonating', 'string', ['template' => '@SonataUser/Admin/Field/impersonating.html.twig'])
;
}
}
/**
* {@inheritdoc}
*/
protected function configureDatagridFilters(DatagridMapper $filterMapper): void
{
$filterMapper
->add('id')
->add('username')
->add('firstname')
->add('lastname')
->add('email')
->add('gender')
->add('dateOfBirth')
->add('groups')
;
}
/**
* {@inheritdoc}
*/
protected function configureShowFields(ShowMapper $showMapper): void
{
$showMapper
->with('General')
->add('username')
->add('email')
->add('cpfCnpj')
->end()
->with('Groups')
->add('groups')
->end()
->with('Profile')
->add('firstname')
->add('lastname')
->add('dateOfBirth')
->add('gender')
->add('phone')
->add('uf')
->end()
;
}
/**
* {@inheritdoc}
*/
protected function configureFormFields(FormMapper $formMapper): void
{
/** @var User $user */
$user = $this->getSubject();
$avatarThumb = '<img src="' . $user->getAvatar() . '" class="admin-preview img-thumbnail img-circle" style="max-width:150px;max-height:150px;"/>';
if ($this->isGranted('ROLE_SUPER_ADMIN')) {
$formMapper
->tab('User')
->with('Profile', ['class' => 'col-md-6'])->end()
->with('General', ['class' => 'col-md-6'])->end()
->end()
->tab('Security')
->with('Status', ['class' => 'col-md-4'])->end()
->with('Groups', ['class' => 'col-md-4'])->end()
->end();
} else {
$formMapper
->tab('User')
->with('Profile', ['class' => 'col-md-6'])->end()
->with('General', ['class' => 'col-md-6'])->end()
->end();
}
// define group zoning
// $formMapper
// ->tab('User')
// ->with('Profile', ['class' => 'col-md-6'])->end()
// ->with('General', ['class' => 'col-md-6'])->end()
// ->end()
// ->tab('Security')
// ->with('Status', ['class' => 'col-md-4'])->end()
// ->with('Groups', ['class' => 'col-md-4'])->end()
// ->end();
// if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
// $formMapper
// ->tab('Security')
// ->with('Roles', ['class' => 'col-md-12'])->end()
// ->end()
// ;
// }
$now = new \DateTime();
$genderOptions = [
'choices' => call_user_func([$this->getUserManager()->getClass(), 'getGenderList']),
'required' => true,
'expanded' => true,
'multiple' => false,
'translation_domain' => $this->getTranslationDomain(),
];
if ($this->isGranted('ROLE_SUPER_ADMIN')) {
$formMapper
->tab('User')
->with('General')
->add('franquia', ModelType::class, [
'label' => 'Franquia/Empresa',
'required' => true,
'expanded' => false,
'multiple' => false,
])
->add('username')
->add('email')
->add('cpfCnpj', null, ['required' => false, 'label' => 'CPF'])
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
'required' => (!$this->getSubject() || is_null($this->getSubject()->getId())),
))
->end()
->with('Profile')
// ->add('avatarFile', FileType::class, array_merge([
// 'data_class' => null,
// 'required' => false,
// 'label' => "Avatar",
// ], $user->getAvatar() != '' ? ['help' => $avatarThumb] : []))
->add('firstname', null, ['required' => true])
->add('lastname', null, ['required' => false])
->add('gender', ChoiceType::class, $genderOptions)
->add('dateOfBirth', DatePickerType::class , [
'years' => range(1900, $now->format('Y')),
'dp_min_date' => '1-1-1900',
'dp_max_date' => $now->format('c'),
'required' => false,
])
->add('phone', TelType::class, ['required' => false])
->add('uf', ChoiceType::class, [
'label' => 'UF',
'choices' => UFEnum::getAssociatedValues(),
])
->end()
->end();
} else {
$formMapper
->tab('User')
->with('General')
->add('cpfCnpj', null, ['required' => false, 'label' => 'CPF'])
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
'required' => (!$this->getSubject() || is_null($this->getSubject()->getId())),
))
->end()
->with('Profile')
// ->add('avatarFile', FileType::class, array_merge([
// 'data_class' => null,
// 'required' => false,
// 'label' => "Avatar",
// ], $user->getAvatar() != '' ? ['help' => $avatarThumb] : []))
->add('firstname', null, ['required' => true])
->add('lastname', null, ['required' => false])
->add('gender', ChoiceType::class, $genderOptions)
->add('dateOfBirth', DatePickerType::class , [
'years' => range(1900, $now->format('Y')),
'dp_min_date' => '1-1-1900',
'dp_max_date' => $now->format('c'),
'required' => false,
])
->add('phone', TelType::class, ['required' => false])
->add('uf', ChoiceType::class, [
'label' => 'UF',
'choices' => UFEnum::getAssociatedValues(),
])
->end()
->end();
}
if($this->security->isGranted('ROLE_SUPER_ADMIN')){
$formMapper
->tab('Security')
->with('Status')
->add('enabled', null, ['required' => false])
->end()
->with('Groups')
->add('groups', ModelType::class, [
'required' => false,
'expanded' => true,
'multiple' => true,
])
->end()
->end();
}
if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
$formMapper
->tab('Security')
->with('Roles',['class' => 'col-md-12'])
->add('realRoles', SecurityRolesType::class, [
'label' => false,
'expanded' => true,
'multiple' => true,
'required' => false,
])
->end()
->end()
;
}
}
public function postPersist($object)
{
//return $this->manageFilesUpload($object);
}
public function postUpdate($object)
{
//return $this->manageFilesUpload($object);
}
/**
* @param User $obj
* @return User
*/
private function manageFilesUpload($obj)
{
$file = $obj->getAvatarFile();
$container = $this->getConfigurationPool()->getContainer();
// Upload File and create thumbnail
if ($file instanceof UploadedFile) {
$obj = $container->get(ImageCachingManager::class)
->processUploadAndFilter($obj, 'avatarOriginal', 'avatar', $file, 'thumb_400px');
}
return $obj;
}
}