Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

170 lines
4.9KB

  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\StatusField;
  10. use Lc\SovBundle\Translation\TranslatorAdmin;
  11. use PhpParser\Node\Expr\Throw_;
  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. ->hideOnIndex(),
  29. 'metaDescription' => TextareaField::new('metaDescription')
  30. ->setLabel('Meta description')
  31. ->setHelp('Affiché dans les résultats de recherche Google')
  32. ->hideOnIndex(),
  33. 'oldUrls' => CollectionField::new('oldUrls')
  34. ->setFormTypeOption('entry_type', TextType::class)
  35. ->setLabel('Anciennes urls du document')
  36. ->hideOnIndex(),
  37. 'devAlias' => TextField::new('devAlias')->hideOnIndex(),
  38. 'status' => StatusField::new('status')->setSortable(true),
  39. ];
  40. }
  41. public function configureDefaultFields(): array
  42. {
  43. return array_keys($this->configureFields());
  44. }
  45. public function configureIndex(): array
  46. {
  47. return $this->configureDefaultFields();
  48. }
  49. public function configureDetail(): array
  50. {
  51. return $this->configureDefaultFields();
  52. }
  53. public function configureForm(): array
  54. {
  55. return $this->configureDefaultFields();
  56. }
  57. public function configurePanels(): array
  58. {
  59. return [];
  60. }
  61. public function configurePanelSeo(): array
  62. {
  63. return [
  64. 'metaTitle',
  65. 'metaDescription',
  66. 'oldUrls'
  67. ];
  68. }
  69. public function configurePanelConf(): array
  70. {
  71. return [
  72. 'devAlias',
  73. ];
  74. }
  75. public function getFields(string $pageName = ''): array
  76. {
  77. if($pageName == Crud::PAGE_INDEX) {
  78. return $this->buildFieldArray($this->configureIndex());
  79. }
  80. elseif($pageName == Crud::PAGE_DETAIL) {
  81. return $this->buildFieldArray($this->configureDetail());
  82. }
  83. elseif($pageName == Crud::PAGE_EDIT || $pageName == Crud::PAGE_NEW) {
  84. return $this->buildFieldArrayForm();
  85. }
  86. return [];
  87. }
  88. public function getAllFields(): array
  89. {
  90. return array_merge(
  91. $this->configureFieldsBase(),
  92. $this->configureFields()
  93. );
  94. }
  95. public function getFieldsByPanel(string $panel): array
  96. {
  97. return $this->buildFieldArrayFormByPanel($panel);
  98. }
  99. protected function buildFieldArray(array $configureFieldArray): array
  100. {
  101. $allFieldArray = $this->getAllFields();
  102. $fieldArray = [];
  103. foreach($configureFieldArray as $fieldName) {
  104. if(isset($allFieldArray[$fieldName])) {
  105. $fieldArray[] = $allFieldArray[$fieldName];
  106. }
  107. else {
  108. throw new \ErrorException('Le field "'.$fieldName.'" n\'est pas défini dans configureFields()');
  109. }
  110. }
  111. return $fieldArray;
  112. }
  113. protected function buildFieldArrayForm(): array
  114. {
  115. $fieldArray = [];
  116. $panelArray = $this->configurePanels();
  117. if($panelArray && count($panelArray) > 0) {
  118. foreach($panelArray as $panel) {
  119. $fieldArray = array_merge(
  120. $fieldArray,
  121. $this->buildFieldArrayFormByPanel($panel)
  122. );
  123. }
  124. }
  125. else {
  126. $fieldArray = $this->buildFieldArray($this->configureForm());
  127. }
  128. return $fieldArray;
  129. }
  130. protected function buildFieldArrayFormByPanel(string $panel): array
  131. {
  132. $method = 'configurePanel' . ucfirst($panel);
  133. if(method_exists($this, $method)) {
  134. $panelFieldArray = $this->$method();
  135. }
  136. else{
  137. throw new \Exception($method . ' n\'existe pas ');
  138. }
  139. return array_merge(
  140. ['panel_' . $panel => FormField::addPanel($panel)],
  141. $this->buildFieldArray($panelFieldArray)
  142. );
  143. }
  144. }