Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AssociationField.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 setFilterOnDevAlias(string $devAlias): self
  54. {
  55. $this->queryBuilderParameters['devAlias'] = $devAlias;
  56. return $this;
  57. }
  58. public function setFilterIsOnline(): self
  59. {
  60. $this->queryBuilderParameters['status'] = 1;
  61. return $this;
  62. }
  63. public function addOrderBy($field, $direction ='ASC') : self
  64. {
  65. $this->queryBuilderParameters['orderBy'][] = $field;
  66. $this->queryBuilderParameters['orderByDirection'][] = $direction;
  67. return $this;
  68. }
  69. public function initQueryBuilder(): self
  70. {
  71. $param = $this->queryBuilderParameters;
  72. $this->setFormTypeOption(
  73. 'query_builder',
  74. function (EntityRepository $er) use ($param) {
  75. $qb = $er->createQueryBuilder('e');
  76. if (isset($param['section'])) {
  77. $qb->andWhereSection('e',$param['section']);
  78. }
  79. if (isset($param['merchant'])) {
  80. $qb->andWhereMerchant('e',$param['merchant']);
  81. }
  82. if (isset($param['status'])) {
  83. $qb->andWhere('e.status = :status')->setParameter('status', $param['status']);
  84. }
  85. if (isset($param['orderBy'])) {
  86. foreach ($param['orderBy'] as $i=>$field) {
  87. $qb->addOrderBy('e.'.$param['orderBy'][$i], $param['orderByDirection'][$i]);
  88. }
  89. }
  90. /*if (isset($param['devAlias'])) {
  91. $qb->andWhere('e.devAlias = :devAlias')->setParameter(
  92. 'devAlias',
  93. $param['devAlias']
  94. );
  95. }*/
  96. return $qb;
  97. }
  98. );
  99. return $this;
  100. }
  101. public function autocomplete(): self
  102. {
  103. $this->setCustomOption(self::OPTION_AUTOCOMPLETE, true);
  104. return $this;
  105. }
  106. public function renderAsNativeWidget(bool $asNative = true): self
  107. {
  108. $this->setCustomOption(self::OPTION_WIDGET, $asNative ? self::WIDGET_NATIVE : self::WIDGET_AUTOCOMPLETE);
  109. return $this;
  110. }
  111. public function setCrudController(string $crudControllerFqcn): self
  112. {
  113. $this->setCustomOption(self::OPTION_CRUD_CONTROLLER, $crudControllerFqcn);
  114. return $this;
  115. }
  116. }