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

183 行
5.4KB

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