You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.8KB

  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\TextareaField;
  6. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  7. use Lc\SovBundle\Field\CollectionField;
  8. use Lc\SovBundle\Translation\TranslatorAdmin;
  9. use PhpParser\Node\Expr\Throw_;
  10. use Symfony\Component\Form\Extension\Core\Type\TextType;
  11. abstract class AbstractFieldDefinition
  12. {
  13. protected array $panels;
  14. protected array $fieldsByPanel;
  15. protected array $fieldList;
  16. protected bool $isPopulate = false;
  17. protected TranslatorAdmin $translatorAdmin;
  18. abstract public function configureFieldsIndex(): array;
  19. abstract public function configurePanels(): array;
  20. abstract public function configureFields(): array;
  21. public function __construct(TranslatorAdmin $translatorAdmin)
  22. {
  23. $this->translatorAdmin = $translatorAdmin;
  24. }
  25. protected function populate(): void
  26. {
  27. if ($this->isPopulate === false) {
  28. $this->fieldList = [] ;
  29. $allFieldArray = $this->configureFields();;
  30. $this->panels = $this->configurePanels();
  31. foreach ($this->panels as $panel) {
  32. $method = 'configureFieldsPanel' . ucfirst($panel);
  33. if (method_exists($this, $method)) {
  34. $fieldArray = $this->$method();
  35. foreach($fieldArray as $key => $field) {
  36. if(is_string($field)) {
  37. unset($fieldArray[$key]);
  38. $fieldArray[$field] = $allFieldArray[$field];
  39. }
  40. }
  41. $this->fieldsByPanel[$panel] = array_merge(
  42. ['panel_' . $panel => FormField::addPanel($panel)],
  43. $fieldArray
  44. );
  45. $this->fieldList = array_merge($this->fieldList, $this->fieldsByPanel[$panel]);
  46. }else{
  47. throw new \Exception($method . ' n\'existe pas ');
  48. }
  49. }
  50. $this->fieldList = array_merge($this->fieldList, $allFieldArray);
  51. $this->isPopulate = false;
  52. }
  53. }
  54. public function getFieldList(string $pageName = '')
  55. {
  56. $this->populate();
  57. if($pageName == Crud::PAGE_INDEX) {
  58. $fieldArray = [];
  59. foreach($this->configureFieldsIndex() as $fieldName) {
  60. $fieldArray[] = $this->fieldList[$fieldName];
  61. }
  62. return $fieldArray;
  63. }
  64. else {
  65. return $this->fieldList;
  66. }
  67. }
  68. public function getFieldListByPanel($panel)
  69. {
  70. $this->populate();
  71. return $this->fieldsByPanel[$panel];
  72. }
  73. public function getPanelList()
  74. {
  75. $this->populate();
  76. return $this->fieldsByPanel;
  77. }
  78. public function configureFieldsPanelSeo(): array
  79. {
  80. return array(
  81. 'metaTitle' => TextField::new('metaTitle')
  82. ->setLabel('Meta Title')
  83. ->setHelp('Affiché dans les résultats de recherche Google')
  84. ->hideOnIndex(),
  85. 'metaDescription' => TextareaField::new('metaDescription')
  86. ->setLabel('Meta description')
  87. ->setHelp('Affiché dans les résultats de recherche Google')
  88. ->hideOnIndex(),
  89. 'oldUrls' => CollectionField::new('oldUrls')
  90. ->setFormTypeOption('entry_type', TextType::class)
  91. ->setLabel('Anciennes urls du document')
  92. ->hideOnIndex(),
  93. );
  94. }
  95. public function configureFieldsPanelConf(): array
  96. {
  97. return [
  98. //FormField::addPanel('configuration')->setTemplateName('crud/field/generic'),
  99. 'devAlias' => TextField::new('devAlias')->hideOnIndex(),
  100. ];
  101. }
  102. }