You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
6.2KB

  1. <?php
  2. namespace Lc\SovBundle\Definition\Field;
  3. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  4. use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
  5. use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
  6. use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
  7. use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
  8. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  9. use Lc\SovBundle\Field\CKEditorField;
  10. use Lc\SovBundle\Field\CollectionField;
  11. use Lc\SovBundle\Field\ImageManagerField;
  12. use Lc\SovBundle\Field\StatusField;
  13. use Lc\SovBundle\Translation\TranslatorAdmin;
  14. use Symfony\Component\Form\Extension\Core\Type\TextType;
  15. abstract class AbstractFieldDefinition
  16. {
  17. protected TranslatorAdmin $translatorAdmin;
  18. public function __construct(TranslatorAdmin $translatorAdmin)
  19. {
  20. $this->translatorAdmin = $translatorAdmin;
  21. }
  22. abstract public function configureFields(): array;
  23. public function configureFieldsBase(): array
  24. {
  25. return [
  26. 'id' => IntegerField::new('id')->onlyOnIndex()->setSortable(true),
  27. 'title' => TextField::new('title'),
  28. 'description' => CKEditorField::new('description'),
  29. 'image' => ImageManagerField::new('image'),
  30. 'metaTitle' => TextField::new('metaTitle')
  31. ->setLabel('Meta : title')
  32. ->setHelp('Affiché dans les résultats de recherche Google'),
  33. 'metaDescription' => TextareaField::new('metaDescription')
  34. ->setLabel('Meta : description')
  35. ->setHelp('Affiché dans les résultats de recherche Google'),
  36. 'openGraphTitle' => TextField::new('openGraphTitle')
  37. ->setLabel('OpenGraph : titre'),
  38. 'openGraphDescription' => TextareaField::new('openGraphDescription')
  39. ->setLabel('OpenGraph : description'),
  40. 'openGraphImage' => ImageManagerField::new('openGraphImage')
  41. ->setLabel('OpenGraph : image'),
  42. 'oldUrls' => CollectionField::new('oldUrls')
  43. ->setFormTypeOption('entry_type', TextType::class)
  44. ->setLabel('Anciennes urls du document')
  45. ->hideOnIndex(),
  46. 'devAlias' => TextField::new('devAlias')->hideOnIndex(),
  47. 'status' => StatusField::new('status')->setSortable(true),
  48. 'createdAt' => DateTimeField::new('createdAt')->setSortable(true),
  49. 'updatedAt' => DateTimeField::new('updatedAt')->setSortable(true),
  50. ];
  51. }
  52. public function configureDefaultFields(): array
  53. {
  54. return array_keys($this->configureFields());
  55. }
  56. public function configureIndex(): array
  57. {
  58. return $this->configureDefaultFields();
  59. }
  60. public function configureDetail(): array
  61. {
  62. return $this->configureDefaultFields();
  63. }
  64. public function configureForm(): array
  65. {
  66. return $this->configureDefaultFields();
  67. }
  68. public function configurePanels(): array
  69. {
  70. return [];
  71. }
  72. public function configurePanelSeo(): array
  73. {
  74. return [
  75. 'metaTitle',
  76. 'metaDescription',
  77. 'oldUrls'
  78. ];
  79. }
  80. public function configurePanelOpengraph(): array
  81. {
  82. return [
  83. 'openGraphTitle',
  84. 'openGraphDescription',
  85. 'openGraphImage'
  86. ];
  87. }
  88. public function configurePanelConf(): array
  89. {
  90. return [
  91. 'devAlias',
  92. ];
  93. }
  94. public function getFields(string $pageName = ''): array
  95. {
  96. if($pageName == Crud::PAGE_INDEX) {
  97. return $this->buildFieldArray($this->configureIndex());
  98. }
  99. elseif($pageName == Crud::PAGE_DETAIL) {
  100. return $this->buildFieldArray($this->configureDetail());
  101. }
  102. elseif($pageName == Crud::PAGE_EDIT || $pageName == Crud::PAGE_NEW) {
  103. return $this->buildFieldArrayForm();
  104. }
  105. return [];
  106. }
  107. public function getAllFields(): array
  108. {
  109. return array_merge(
  110. $this->configureFieldsBase(),
  111. $this->configureFields(),
  112. );
  113. }
  114. public function getFieldsByPanel(string $panel): array
  115. {
  116. return $this->buildFieldArrayFormByPanel($panel);
  117. }
  118. protected function buildFieldArray(array $configureFieldArray): array
  119. {
  120. $allFieldArray = $this->getAllFields();
  121. $fieldArray = [];
  122. foreach($configureFieldArray as $fieldName) {
  123. if(isset($allFieldArray[$fieldName])) {
  124. $fieldArray[] = $allFieldArray[$fieldName];
  125. }
  126. else {
  127. throw new \ErrorException('Le field "'.$fieldName.'" n\'est pas défini dans configureFields()');
  128. }
  129. }
  130. return $fieldArray;
  131. }
  132. protected function buildFieldArrayForm(): array
  133. {
  134. $fieldArray = [];
  135. $panelArray = $this->configurePanels();
  136. if($panelArray && count($panelArray) > 0) {
  137. foreach($panelArray as $panel) {
  138. $fieldArray = array_merge(
  139. $fieldArray,
  140. $this->buildFieldArrayFormByPanel($panel)
  141. );
  142. }
  143. }
  144. else {
  145. $fieldArray = $this->buildFieldArray($this->configureForm());
  146. }
  147. return $fieldArray;
  148. }
  149. protected function buildFieldArrayFormByPanel(string $panel): array
  150. {
  151. $method = 'configurePanel' . ucfirst($panel);
  152. if(method_exists($this, $method)) {
  153. $panelFieldArray = $this->$method();
  154. }
  155. else{
  156. throw new \Exception($method . ' n\'existe pas ');
  157. }
  158. $fieldPanel = FormField::addPanel($panel);
  159. $method = 'panel'.ucfirst($panel).'CustomOptions';
  160. if(method_exists($this, $method)) {
  161. foreach ($this->$method() as $customOptionKey => $customOptionValue){
  162. $fieldPanel->setCustomOption($customOptionKey, $customOptionValue);
  163. }
  164. }
  165. return array_merge(
  166. [
  167. 'panel_' . $panel => $fieldPanel
  168. ],
  169. $this->buildFieldArray($panelFieldArray)
  170. );
  171. }
  172. }