-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from smartbooster/better_smart_parameter_entit…
…y_type Better Smart Parameter type management and value validation
- Loading branch information
Showing
18 changed files
with
560 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,48 @@ | |
|
||
namespace Smart\SonataBundle\Admin; | ||
|
||
use Doctrine\ORM\UnitOfWork; | ||
use Smart\CoreBundle\Validator\Constraints\EmailChain; | ||
use Smart\SonataBundle\Entity\Log\HistorizableInterface; | ||
use Smart\SonataBundle\Entity\ParameterInterface; | ||
use Smart\SonataBundle\Enum\ParameterTypeEnum; | ||
use Smart\SonataBundle\Logger\HistoryLogger; | ||
use Sonata\AdminBundle\Datagrid\DatagridMapper; | ||
use Sonata\AdminBundle\Datagrid\ListMapper; | ||
use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface; | ||
use Sonata\AdminBundle\Form\FormMapper; | ||
use Sonata\AdminBundle\Route\RouteCollectionInterface; | ||
use Sonata\AdminBundle\Show\ShowMapper; | ||
use Sonata\DoctrineORMAdminBundle\Filter\ChoiceFilter; | ||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; | ||
use Symfony\Component\Form\Extension\Core\Type\NumberType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextareaType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\Validator\Constraints\Email; | ||
use Yokai\EnumBundle\Form\Type\EnumType; | ||
|
||
/** | ||
* @author Mathieu Ducrot <[email protected]> | ||
* @method ParameterInterface getSubject() | ||
*/ | ||
class ParameterAdmin extends AbstractAdmin | ||
{ | ||
private ParameterTypeEnum $typeEnum; | ||
private HistoryLogger $historyLogger; | ||
private array $updateInitialData = []; | ||
|
||
public function __construct( | ||
string $code, | ||
?string $class, | ||
string $baseControllerName, | ||
ParameterTypeEnum $typeEnum, | ||
HistoryLogger $historyLogger, | ||
) { | ||
parent::__construct($code, $class, $baseControllerName); | ||
$this->typeEnum = $typeEnum; | ||
$this->historyLogger = $historyLogger; | ||
} | ||
|
||
protected function configureRoutes(RouteCollectionInterface $collection): void | ||
{ | ||
$collection | ||
|
@@ -27,8 +58,16 @@ protected function configureListFields(ListMapper $list): void | |
$list | ||
->addIdentifier('id', null, ['label' => 'field.label_id']) | ||
->addIdentifier('code', null, ['label' => 'field.label_code']) | ||
->add('type', FieldDescriptionInterface::TYPE_CHOICE, [ | ||
'label' => 'field.label_type', | ||
'choices' => array_flip($this->typeEnum->getChoices()), | ||
'catalog' => false, // enum is self translated | ||
]) | ||
->add('help', null, ['label' => 'field.label_help']) | ||
->add('value', null, ['label' => 'field.label_value']) | ||
->add('value', null, [ | ||
'label' => 'field.label_value', | ||
'template' => 'admin/parameter_admin/list_value.html.twig' | ||
]) | ||
; | ||
} | ||
|
||
|
@@ -39,36 +78,108 @@ protected function configureDatagridFilters(DatagridMapper $filter): void | |
'show_filter' => true, | ||
'label' => 'field.label_code' | ||
]) | ||
->add('type', ChoiceFilter::class, [ | ||
'field_type' => EnumType::class, | ||
'field_options' => ['enum' => ParameterTypeEnum::class], | ||
'show_filter' => true, | ||
'label' => 'field.label_type', | ||
]) | ||
; | ||
} | ||
|
||
protected function configureShowFields(ShowMapper $show): void | ||
{ | ||
$parameter = $this->getSubject(); | ||
$show | ||
->with('fieldset.label_general', ['label' => 'fieldset.label_general']) | ||
->add('id', null, ['label' => 'field.label_id']) | ||
->add('code', null, ['label' => 'field.label_code']) | ||
->add('help', null, ['label' => 'field.label_help']) | ||
->add('value', null, ['label' => 'field.label_value']) | ||
->add('type', FieldDescriptionInterface::TYPE_CHOICE, [ | ||
'label' => 'field.label_type', | ||
'choices' => array_flip($this->typeEnum->getChoices()), | ||
'catalog' => false, // enum is self translated | ||
]) | ||
; | ||
if ($parameter->getHelp()) { | ||
$show->add('help', null, ['label' => 'field.label_help']); | ||
} | ||
if ($parameter->getRegex() !== null) { | ||
$show->add('regex', null, ['label' => 'field.label_regex']); | ||
} | ||
$valueType = null; | ||
if ($parameter->isBooleanType()) { | ||
$valueType = FieldDescriptionInterface::TYPE_BOOLEAN; | ||
} elseif ($parameter->isTextareaType()) { | ||
$valueType = FieldDescriptionInterface::TYPE_HTML; | ||
} | ||
$show | ||
->add('value', $valueType, ['label' => 'field.label_value']) | ||
->end() | ||
->with('fieldset.label_history', ['class' => 'col-md-12', 'label' => 'fieldset.label_history']) | ||
->add('history', null, ['template' => 'admin/parameter_admin/timeline_history_field.html.twig']) | ||
->end() | ||
; | ||
} | ||
|
||
protected function configureFormFields(FormMapper $form): void | ||
{ | ||
$parameter = $this->getSubject(); | ||
$form | ||
->with('fieldset.label_general', ['label' => 'fieldset.label_general']) | ||
->add('code', null, [ | ||
'label' => 'field.label_code', | ||
'disabled' => true | ||
]) | ||
->add('type', EnumType::class, [ | ||
'label' => 'field.label_type', | ||
'enum' => ParameterTypeEnum::class, | ||
'disabled' => true, | ||
]) | ||
; | ||
|
||
if ($parameter->getHelp()) { | ||
$form | ||
->add('help', null, [ | ||
'label' => 'field.label_help', | ||
'disabled' => true | ||
]) | ||
->add('value', null, ['label' => 'field.label_value']) | ||
->end() | ||
; | ||
; | ||
} | ||
if ($parameter->getRegex() !== null) { | ||
$form->add('regex', null, [ | ||
'label' => 'field.label_regex', | ||
'disabled' => true, | ||
]); | ||
} | ||
|
||
$valueType = TextType::class; | ||
$valueOptions = []; | ||
if ($parameter->isBooleanType()) { | ||
$valueType = CheckboxType::class; | ||
$valueOptions = ['required' => false]; | ||
} elseif ($parameter->isEmailChainType() || $parameter->isListType() || $parameter->isTextareaType()) { | ||
$valueType = TextareaType::class; | ||
$valueOptions = ['attr' => ['rows' => 10]]; | ||
if (!$parameter->isTextareaType()) { | ||
$valueOptions['help'] = 'field.help_parameter_array_values'; | ||
} | ||
if ($parameter->isEmailChainType()) { | ||
$valueOptions['constraints'] = [new EmailChain(separator: PHP_EOL)]; | ||
} | ||
} elseif ($parameter->isFloatType() || $parameter->isIntegerType()) { | ||
$valueType = NumberType::class; | ||
$valueOptions = ['html5' => true]; | ||
if ($parameter->isIntegerType()) { | ||
$valueOptions['scale'] = 0; | ||
} | ||
} elseif ($parameter->isEmailType()) { | ||
// MDT Should be converted with Assert\When for when we drop support for SF 5.4 | ||
$valueOptions['constraints'] = [new Email()]; | ||
} | ||
$form->add('value', $valueType, [ | ||
'label' => 'field.label_value', | ||
...$valueOptions, | ||
])->end(); | ||
} | ||
|
||
public function getExportFormats(): array | ||
|
@@ -84,4 +195,21 @@ protected function configureExportFields(): array | |
$this->trans('field.label_help') => 'help', | ||
]; | ||
} | ||
|
||
protected function preUpdate(object $object): void | ||
{ | ||
/** @var UnitOfWork $uow @phpstan-ignore-next-line */ | ||
$uow = $this->getModelManager()->getEntityManager($this->getClass())->getUnitOfWork(); | ||
$this->updateInitialData = [ | ||
'value' => $uow->getOriginalEntityData($object)['value'], | ||
]; | ||
} | ||
|
||
protected function postUpdate(object $object): void | ||
{ | ||
/** @var HistorizableInterface $object */ | ||
$this->historyLogger->logDiff($object, $this->updateInitialData, [ | ||
'author' => $this->getUser()->getFullName(), // @phpstan-ignore-line | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.