Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

150 lines
4.9KB

  1. <?php
  2. namespace Lc\SovBundle\Field;
  3. use Doctrine\ORM\EntityRepository;
  4. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
  5. use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
  6. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  7. final class AssociationField implements FieldInterface
  8. {
  9. use FieldTrait;
  10. public const OPTION_AUTOCOMPLETE = 'autocomplete';
  11. public const OPTION_CRUD_CONTROLLER = 'crudControllerFqcn';
  12. public const OPTION_WIDGET = 'widget';
  13. public const OPTION_QUERY_BUILDER_CALLABLE = 'queryBuilderCallable';
  14. /** @internal this option is intended for internal use only */
  15. public const OPTION_RELATED_URL = 'relatedUrl';
  16. /** @internal this option is intended for internal use only */
  17. public const OPTION_DOCTRINE_ASSOCIATION_TYPE = 'associationType';
  18. public const WIDGET_AUTOCOMPLETE = 'autocomplete';
  19. public const WIDGET_NATIVE = 'native';
  20. /** @internal this option is intended for internal use only */
  21. public const PARAM_AUTOCOMPLETE_CONTEXT = 'autocompleteContext';
  22. protected $queryBuilderParameters = array();
  23. /**
  24. * @param string|false|null $label
  25. */
  26. public static function new(string $propertyName, $label = null): self
  27. {
  28. return (new self())
  29. ->setProperty($propertyName)
  30. ->setLabel($label)
  31. ->setTemplatePath('@LcSov/adminlte/crud/field/association.html.twig')
  32. ->setFormType(EntityType::class)
  33. ->addCssClass('field-association')
  34. ->setCustomOption(self::OPTION_AUTOCOMPLETE, false)
  35. ->setCustomOption(self::OPTION_CRUD_CONTROLLER, null)
  36. ->setCustomOption(self::OPTION_WIDGET, self::WIDGET_AUTOCOMPLETE)
  37. ->setCustomOption(self::OPTION_QUERY_BUILDER_CALLABLE, null)
  38. ->setCustomOption(self::OPTION_RELATED_URL, '')
  39. ->setCustomOption(self::OPTION_DOCTRINE_ASSOCIATION_TYPE, null);
  40. }
  41. public function setFilterOnDevAlias(string $devAlias): self
  42. {
  43. $this->queryBuilderParameters['devAlias'] = $devAlias;
  44. return $this;
  45. }
  46. public function setFilterIsOnline(): self
  47. {
  48. $this->queryBuilderParameters['status'] = 1;
  49. return $this;
  50. }
  51. public function setLeftJoin($entityName): self
  52. {
  53. $this->queryBuilderParameters['leftJoin'][] = $entityName;
  54. return $this;
  55. }
  56. public function addAndWhere($whereClause, $key, $value): self
  57. {
  58. $this->queryBuilderParameters['andWhere'][] = [
  59. 'whereClause' => $whereClause,
  60. 'key' => $key,
  61. 'value' => $value
  62. ];
  63. return $this;
  64. }
  65. public function addOrderBy($field, $direction = 'ASC'): self
  66. {
  67. $this->queryBuilderParameters['orderBy'][] = $field;
  68. $this->queryBuilderParameters['orderByDirection'][] = $direction;
  69. return $this;
  70. }
  71. /**
  72. * @deprecated Utiliser setFormTypeOption('choices', $choices) avec $choices issu d'un Store.
  73. */
  74. public function initQueryBuilder(): self
  75. {
  76. $param = $this->queryBuilderParameters;
  77. $this->setFormTypeOption(
  78. 'query_builder',
  79. function (EntityRepository $er) use ($param) {
  80. $qb = $er->createQueryBuilder('e');
  81. if (isset($param['status'])) {
  82. $qb->andWhere('e.status = :status')->setParameter('status', $param['status']);
  83. }
  84. if (isset($param['orderBy'])) {
  85. foreach ($param['orderBy'] as $i => $field) {
  86. $qb->addOrderBy('e.' . $param['orderBy'][$i], $param['orderByDirection'][$i]);
  87. }
  88. }
  89. if (isset($param['leftJoin'])) {
  90. foreach ($param['leftJoin'] as $i => $entityName) {
  91. $qb->leftJoin('e.' . $entityName, $entityName)->addSelect($entityName);
  92. }
  93. }
  94. if (isset($param['andWhere'])) {
  95. foreach ($param['andWhere'] as $i => $whereClause) {
  96. $qb->andWhere($whereClause['whereClause'])->setParameter($whereClause['key'], $whereClause['value']);
  97. }
  98. }
  99. return $qb;
  100. }
  101. );
  102. return $this;
  103. }
  104. public function autocomplete(): self
  105. {
  106. $this->setCustomOption(self::OPTION_AUTOCOMPLETE, true);
  107. return $this;
  108. }
  109. public function renderAsNativeWidget(bool $asNative = true): self
  110. {
  111. $this->setCustomOption(self::OPTION_WIDGET, $asNative ? self::WIDGET_NATIVE : self::WIDGET_AUTOCOMPLETE);
  112. return $this;
  113. }
  114. public function setCrudController(string $crudControllerFqcn): self
  115. {
  116. $this->setCustomOption(self::OPTION_CRUD_CONTROLLER, $crudControllerFqcn);
  117. return $this;
  118. }
  119. }