選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

199 行
6.0KB

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