|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
-
- namespace Lc\SovBundle\Definition\Field;
-
- use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
- use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
- 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\ImageManagerField;
- use Lc\SovBundle\Field\StatusField;
- use Lc\SovBundle\Translation\TranslatorAdmin;
- 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'),
- 'metaDescription' => TextareaField::new('metaDescription')
- ->setLabel('Meta : description')
- ->setHelp('Affiché dans les résultats de recherche Google'),
- 'openGraphTitle' => TextField::new('openGraphTitle')
- ->setLabel('OpenGraph : titre'),
- 'openGraphDescription' => TextareaField::new('openGraphDescription')
- ->setLabel('OpenGraph : description'),
- 'openGraphImage' => ImageManagerField::new('openGraphImage')
- ->setLabel('OpenGraph : image'),
- '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),
- 'createdAt' => DateTimeField::new('createdAt')->setSortable(true),
- 'updatedAt' => DateTimeField::new('updatedAt')->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 configurePanelOpengraph(): array
- {
- return [
- 'openGraphTitle',
- 'openGraphDescription',
- 'openGraphImage'
- ];
- }
-
- 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 ');
- }
-
- $fieldPanel = FormField::addPanel($panel);
- $method = 'panel'.ucfirst($panel).'CustomOptions';
- if(method_exists($this, $method)) {
- foreach ($this->$method() as $customOptionKey => $customOptionValue){
- $fieldPanel->setCustomOption($customOptionKey, $customOptionValue);
- }
-
- }
-
- return array_merge(
- [
- 'panel_' . $panel => $fieldPanel
-
- ],
- $this->buildFieldArray($panelFieldArray)
- );
- }
-
- }
|