Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

204 lines
6.7KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Field;
  3. use Doctrine\ORM\EntityRepository;
  4. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
  5. use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
  6. use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
  7. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  8. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  9. final class AssociationField implements FieldInterface
  10. {
  11. use FieldTrait;
  12. public const OPTION_AUTOCOMPLETE = 'autocomplete';
  13. public const OPTION_CRUD_CONTROLLER = 'crudControllerFqcn';
  14. public const OPTION_WIDGET = 'widget';
  15. public const OPTION_QUERY_BUILDER_CALLABLE = 'queryBuilderCallable';
  16. /** @internal this option is intended for internal use only */
  17. public const OPTION_RELATED_URL = 'relatedUrl';
  18. /** @internal this option is intended for internal use only */
  19. public const OPTION_DOCTRINE_ASSOCIATION_TYPE = 'associationType';
  20. public const WIDGET_AUTOCOMPLETE = 'autocomplete';
  21. public const WIDGET_NATIVE = 'native';
  22. /** @internal this option is intended for internal use only */
  23. public const PARAM_AUTOCOMPLETE_CONTEXT = 'autocompleteContext';
  24. protected $queryBuilderParameters = array();
  25. /**
  26. * @param string|false|null $label
  27. */
  28. public static function new(string $propertyName, $label = null): self
  29. {
  30. return (new self())
  31. ->setProperty($propertyName)
  32. ->setLabel($label)
  33. ->setTemplateName('crud/field/association')
  34. ->setFormType(EntityType::class)
  35. ->addCssClass('field-association')
  36. ->setCustomOption(self::OPTION_AUTOCOMPLETE, false)
  37. ->setCustomOption(self::OPTION_CRUD_CONTROLLER, null)
  38. ->setCustomOption(self::OPTION_WIDGET, self::WIDGET_AUTOCOMPLETE)
  39. ->setCustomOption(self::OPTION_QUERY_BUILDER_CALLABLE, null)
  40. ->setCustomOption(self::OPTION_RELATED_URL, null)
  41. ->setCustomOption(self::OPTION_DOCTRINE_ASSOCIATION_TYPE, null);
  42. }
  43. public function setFilterOnSection(SectionInterface $section): self
  44. {
  45. $this->queryBuilderParameters['section'] = $section;
  46. return $this;
  47. }
  48. public function setFilterOnMerchant(MerchantInterface $merchant): self
  49. {
  50. $this->queryBuilderParameters['merchant'] = $merchant;
  51. return $this;
  52. }
  53. public function setFilterOnMerchantManyToMany(MerchantInterface $merchant): self
  54. {
  55. $this->queryBuilderParameters['merchantManyToMany'] = $merchant;
  56. return $this;
  57. }
  58. public function setFilterOnMerchantViaSection(MerchantInterface $merchant): self
  59. {
  60. $this->queryBuilderParameters['merchantViaSection'] = $merchant;
  61. return $this;
  62. }
  63. public function setFilterOnDevAlias(string $devAlias): self
  64. {
  65. $this->queryBuilderParameters['devAlias'] = $devAlias;
  66. return $this;
  67. }
  68. public function setFilterIsOnline(): self
  69. {
  70. $this->queryBuilderParameters['status'] = 1;
  71. return $this;
  72. }
  73. public function setLeftJoin($entityName): self
  74. {
  75. $this->queryBuilderParameters['leftJoin'][] = $entityName;
  76. return $this;
  77. }
  78. public function addAndWhere($whereClause, $key, $value): self
  79. {
  80. $this->queryBuilderParameters['andWhere'][] = [
  81. 'whereClause' => $whereClause,
  82. 'key' => $key,
  83. 'value' => $value
  84. ];
  85. return $this;
  86. }
  87. public function addOrderBy($field, $direction = 'ASC'): self
  88. {
  89. $this->queryBuilderParameters['orderBy'][] = $field;
  90. $this->queryBuilderParameters['orderByDirection'][] = $direction;
  91. return $this;
  92. }
  93. /**
  94. * @deprecated Utiliser setFormTypeOption('choices', $choices) avec $choices issu d'un Store.
  95. */
  96. public function initQueryBuilder(): self
  97. {
  98. $param = $this->queryBuilderParameters;
  99. $this->setFormTypeOption(
  100. 'query_builder',
  101. function (EntityRepository $er) use ($param) {
  102. $qb = $er->createQueryBuilder('e');
  103. if (isset($param['section'])) {
  104. $qb->andWhereSection('e', $param['section']);
  105. }
  106. if (isset($param['merchant'])) {
  107. $qb->andWhereMerchant('e', $param['merchant']);
  108. }
  109. if (isset($param['merchantManyToMany'])) {
  110. $qb->andWhereMerchantManyToMany('e', $param['merchantManyToMany']);
  111. }
  112. if (isset($param['merchantViaSection'])) {
  113. $qb->leftJoin('e.section', 's');
  114. $qb->andWhereMerchant('s', $param['merchantViaSection']);
  115. }
  116. if (isset($param['status'])) {
  117. $qb->andWhere('e.status = :status')->setParameter('status', $param['status']);
  118. }
  119. if (isset($param['orderBy'])) {
  120. foreach ($param['orderBy'] as $i => $field) {
  121. $qb->addOrderBy('e.' . $param['orderBy'][$i], $param['orderByDirection'][$i]);
  122. }
  123. }
  124. if (isset($param['leftJoin'])) {
  125. foreach ($param['leftJoin'] as $i => $entityName) {
  126. $qb->leftJoin('e.' . $entityName, $entityName)->addSelect($entityName);
  127. }
  128. }
  129. if (isset($param['andWhere'])) {
  130. foreach ($param['andWhere'] as $i => $whereClause) {
  131. $qb->andWhere($whereClause['whereClause'])->setParameter($whereClause['key'], $whereClause['value']);
  132. }
  133. }
  134. /*if (isset($param['devAlias'])) {
  135. $qb->andWhere('e.devAlias = :devAlias')->setParameter(
  136. 'devAlias',
  137. $param['devAlias']
  138. );
  139. }*/
  140. return $qb;
  141. }
  142. );
  143. return $this;
  144. }
  145. public function autocomplete(): self
  146. {
  147. $this->setCustomOption(self::OPTION_AUTOCOMPLETE, true);
  148. return $this;
  149. }
  150. public function renderAsNativeWidget(bool $asNative = true): self
  151. {
  152. $this->setCustomOption(self::OPTION_WIDGET, $asNative ? self::WIDGET_NATIVE : self::WIDGET_AUTOCOMPLETE);
  153. return $this;
  154. }
  155. public function setCrudController(string $crudControllerFqcn): self
  156. {
  157. $this->setCustomOption(self::OPTION_CRUD_CONTROLLER, $crudControllerFqcn);
  158. return $this;
  159. }
  160. }