Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AbstractFieldDefinition.php 6.6KB

pirms 3 gadiem
pirms 3 gadiem
pirms 3 gadiem
pirms 2 gadiem
pirms 3 gadiem
pirms 3 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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\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. }