|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
-
- namespace Lc\SovBundle\Definition\Field;
-
- use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
- use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
- use Lc\SovBundle\Field\CollectionField;
- use Lc\SovBundle\Field\StatusField;
- use Lc\SovBundle\Translation\TranslatorAdmin;
- use PhpParser\Node\Expr\Throw_;
- use Symfony\Component\Form\Extension\Core\Type\TextType;
-
- abstract class AbstractFieldDefinition
- {
- protected TranslatorAdmin $translatorAdmin;
-
- public function __construct(TranslatorAdmin $translatorAdmin)
- {
- $this->translatorAdmin = $translatorAdmin;
- }
-
- abstract public function configureFields(): array;
-
- public function configureFieldsBase(): array
- {
- return [
- 'id' => IntegerField::new('id')->onlyOnIndex()->setSortable(true),
- 'metaTitle' => TextField::new('metaTitle')
- ->setLabel('Meta Title')
- ->setHelp('Affiché dans les résultats de recherche Google')
- ->hideOnIndex(),
- 'metaDescription' => TextareaField::new('metaDescription')
- ->setLabel('Meta description')
- ->setHelp('Affiché dans les résultats de recherche Google')
- ->hideOnIndex(),
- 'oldUrls' => CollectionField::new('oldUrls')
- ->setFormTypeOption('entry_type', TextType::class)
- ->setLabel('Anciennes urls du document')
- ->hideOnIndex(),
- 'devAlias' => TextField::new('devAlias')->hideOnIndex(),
- 'status' => StatusField::new('status')->setSortable(true),
- ];
- }
-
- public function configureDefaultFields(): array
- {
- return array_keys($this->configureFields());
- }
-
- public function configureIndex(): array
- {
- return $this->configureDefaultFields();
- }
-
- public function configureDetail(): array
- {
- return $this->configureDefaultFields();
- }
-
- public function configureForm(): array
- {
- return $this->configureDefaultFields();
- }
-
- public function configurePanels(): array
- {
- return [];
- }
-
- public function configurePanelSeo(): array
- {
- return [
- 'metaTitle',
- 'metaDescription',
- 'oldUrls'
- ];
- }
-
- public function configurePanelConf(): array
- {
- return [
- 'devAlias',
- ];
- }
-
- public function getFields(string $pageName = ''): array
- {
- if($pageName == Crud::PAGE_INDEX) {
- return $this->buildFieldArray($this->configureIndex());
- }
- elseif($pageName == Crud::PAGE_DETAIL) {
- return $this->buildFieldArray($this->configureDetail());
- }
- elseif($pageName == Crud::PAGE_EDIT || $pageName == Crud::PAGE_NEW) {
- return $this->buildFieldArrayForm();
- }
-
- return [];
- }
-
- public function getAllFields(): array
- {
- return array_merge(
- $this->configureFieldsBase(),
- $this->configureFields()
- );
- }
-
- public function getFieldsByPanel(string $panel): array
- {
- return $this->buildFieldArrayFormByPanel($panel);
- }
-
- protected function buildFieldArray(array $configureFieldArray): array
- {
- $allFieldArray = $this->getAllFields();
-
- $fieldArray = [];
- foreach($configureFieldArray as $fieldName) {
- if(isset($allFieldArray[$fieldName])) {
- $fieldArray[] = $allFieldArray[$fieldName];
- }
- else {
- throw new \ErrorException('Le field "'.$fieldName.'" n\'est pas défini dans configureFields()');
- }
- }
-
- return $fieldArray;
- }
-
- protected function buildFieldArrayForm(): array
- {
- $fieldArray = [];
- $panelArray = $this->configurePanels();
-
- if($panelArray && count($panelArray) > 0) {
- foreach($panelArray as $panel) {
- $fieldArray = array_merge(
- $fieldArray,
- $this->buildFieldArrayFormByPanel($panel)
- );
- }
- }
- else {
- $fieldArray = $this->buildFieldArray($this->configureForm());
- }
-
- return $fieldArray;
- }
-
- protected function buildFieldArrayFormByPanel(string $panel): array
- {
- $method = 'configurePanel' . ucfirst($panel);
- if(method_exists($this, $method)) {
- $panelFieldArray = $this->$method();
- }
- else{
- throw new \Exception($method . ' n\'existe pas ');
- }
-
- return array_merge(
- ['panel_' . $panel => FormField::addPanel($panel)],
- $this->buildFieldArray($panelFieldArray)
- );
- }
-
- }
|