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.

208 líneas
6.6KB

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