No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

AbstractFieldDefinition.php 6.5KB

hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. 'oldUrls' => CollectionField::new('oldUrls')
  37. ->setFormTypeOption('entry_type', TextType::class)
  38. ->setLabel('Anciennes urls du document')
  39. ->hideOnIndex(),
  40. 'openGraphTitle' => TextField::new('openGraphTitle')
  41. ->setLabel('OpenGraph : titre')
  42. ->setHelp('Utilisé par les réseaux sociaux pour récupérer le titre de la page'),
  43. 'openGraphDescription' => TextareaField::new('openGraphDescription')
  44. ->setLabel('OpenGraph : description')
  45. ->setHelp('Utilisé par les réseaux sociaux pour récupérer la description de la page'),
  46. 'openGraphImage' => ImageManagerField::new('openGraphImage')
  47. ->setLabel('OpenGraph : image'),
  48. 'devAlias' => TextField::new('devAlias')->hideOnIndex(),
  49. 'status' => StatusField::new('status')->setSortable(true),
  50. 'createdAt' => DateTimeField::new('createdAt')->setSortable(true),
  51. 'updatedAt' => DateTimeField::new('updatedAt')->setSortable(true),
  52. ];
  53. }
  54. public function configureDefaultFields(): array
  55. {
  56. return array_keys($this->configureFields());
  57. }
  58. public function configureIndex(): array
  59. {
  60. return $this->configureDefaultFields();
  61. }
  62. public function configureDetail(): array
  63. {
  64. return $this->configureDefaultFields();
  65. }
  66. public function configureForm(): array
  67. {
  68. return $this->configureDefaultFields();
  69. }
  70. public function configurePanels(): array
  71. {
  72. return [];
  73. }
  74. public function configurePanelSeo(): array
  75. {
  76. return [
  77. 'metaTitle',
  78. 'metaDescription',
  79. 'oldUrls'
  80. ];
  81. }
  82. public function configurePanelOpengraph(): array
  83. {
  84. return [
  85. 'openGraphTitle',
  86. 'openGraphDescription',
  87. 'openGraphImage'
  88. ];
  89. }
  90. public function configurePanelConf(): array
  91. {
  92. return [
  93. 'devAlias',
  94. ];
  95. }
  96. public function getFields(string $pageName = ''): array
  97. {
  98. if($pageName == Crud::PAGE_INDEX) {
  99. return $this->buildFieldArray($this->configureIndex());
  100. }
  101. elseif($pageName == Crud::PAGE_DETAIL) {
  102. return $this->buildFieldArray($this->configureDetail());
  103. }
  104. elseif($pageName == Crud::PAGE_EDIT || $pageName == Crud::PAGE_NEW) {
  105. return $this->buildFieldArrayForm();
  106. }
  107. return [];
  108. }
  109. public function getAllFields(): array
  110. {
  111. return array_merge(
  112. $this->configureFieldsBase(),
  113. $this->configureFields(),
  114. );
  115. }
  116. public function getFieldsByPanel(string $panel): array
  117. {
  118. return $this->buildFieldArrayFormByPanel($panel);
  119. }
  120. protected function buildFieldArray(array $configureFieldArray): array
  121. {
  122. $allFieldArray = $this->getAllFields();
  123. $fieldArray = [];
  124. foreach($configureFieldArray as $fieldName) {
  125. if(isset($allFieldArray[$fieldName])) {
  126. $fieldArray[] = $allFieldArray[$fieldName];
  127. }
  128. else {
  129. throw new \ErrorException('Le field "'.$fieldName.'" n\'est pas défini dans configureFields()');
  130. }
  131. }
  132. return $fieldArray;
  133. }
  134. protected function buildFieldArrayForm(): array
  135. {
  136. $fieldArray = [];
  137. $panelArray = $this->configurePanels();
  138. if($panelArray && count($panelArray) > 0) {
  139. foreach($panelArray as $panel) {
  140. $fieldArray = array_merge(
  141. $fieldArray,
  142. $this->buildFieldArrayFormByPanel($panel)
  143. );
  144. }
  145. }
  146. else {
  147. $fieldArray = $this->buildFieldArray($this->configureForm());
  148. }
  149. return $fieldArray;
  150. }
  151. protected function buildFieldArrayFormByPanel(string $panel): array
  152. {
  153. $method = 'configurePanel' . ucfirst($panel);
  154. if(method_exists($this, $method)) {
  155. $panelFieldArray = $this->$method();
  156. }
  157. else{
  158. throw new \Exception($method . ' n\'existe pas ');
  159. }
  160. $fieldPanel = FormField::addPanel($panel);
  161. $method = 'panel'.ucfirst($panel).'CustomOptions';
  162. if(method_exists($this, $method)) {
  163. foreach ($this->$method() as $customOptionKey => $customOptionValue){
  164. $fieldPanel->setCustomOption($customOptionKey, $customOptionValue);
  165. }
  166. }
  167. return array_merge(
  168. [
  169. 'panel_' . $panel => $fieldPanel
  170. ],
  171. $this->buildFieldArray($panelFieldArray)
  172. );
  173. }
  174. }