Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

163 lines
4.7KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Reduction;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Knp\Component\Pager\PaginatorInterface;
  5. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  6. use Lc\CaracoleBundle\Repository\MerchantRepositoryQueryTrait;
  7. use Lc\SovBundle\Model\User\UserInterface;
  8. use Lc\SovBundle\Repository\AbstractRepositoryQuery;
  9. class ReductionCatalogRepositoryQuery extends AbstractRepositoryQuery
  10. {
  11. protected bool $isJoinProductFamilies = false;
  12. protected bool $isJoinProductFamily = false;
  13. protected bool $isJoinProductCategories = false;
  14. protected bool $isJoinUsers = false;
  15. protected bool $isJoinGroupUsers = false;
  16. use MerchantRepositoryQueryTrait;
  17. public function __construct(ReductionCatalogRepository $repository, PaginatorInterface $paginator)
  18. {
  19. parent::__construct($repository, 'r', $paginator);
  20. }
  21. public function joinUsers()
  22. {
  23. if (!$this->isJoinUsers) {
  24. $this->isJoinUsers = true;
  25. return $this
  26. ->leftJoin('.users', 'pf_users')
  27. ->addSelect('pf_users');
  28. }
  29. return $this;
  30. }
  31. public function joinGroupUsers()
  32. {
  33. if (!$this->isJoinGroupUsers) {
  34. $this->isJoinGroupUsers = true;
  35. return $this
  36. ->leftJoin('.groupUsers', 'groupUsers')
  37. ->addSelect('groupUsers');
  38. }
  39. return $this;
  40. }
  41. public function joinProductFamilies()
  42. {
  43. if (!$this->isJoinProductFamilies) {
  44. $this->isJoinProductFamilies = true;
  45. return $this
  46. ->leftJoin('.productFamilies', 'productFamilies')
  47. ->addSelect('productFamilies');
  48. }
  49. return $this;
  50. }
  51. public function joinProductFamily()
  52. {
  53. if (!$this->isJoinProductFamily) {
  54. $this->isJoinProductFamily = true;
  55. return $this
  56. ->leftJoin('.productFamily', 'productFamily')
  57. ->addSelect('productFamily');
  58. }
  59. return $this;
  60. }
  61. public function joinProductCategories()
  62. {
  63. if (!$this->isJoinProductCategories) {
  64. $this->isJoinProductCategories = true;
  65. return $this
  66. ->leftJoin('.productCategories', 'productCategories')
  67. ->addSelect('productCategories');
  68. }
  69. return $this;
  70. }
  71. public function filterProductFamily(ProductFamilyInterface $productFamily)
  72. {
  73. return $this
  74. ->andWhere('.productFamily = :productFamily')
  75. ->setParameter(':productFamily', $productFamily);
  76. }
  77. public function filterConditionDate()
  78. {
  79. return $this
  80. ->andWhere('.permanent = 1 OR ( .dateStart <= :now AND .dateEnd >= :now)')
  81. ->setParameter(':now', new \DateTime());
  82. }
  83. public function filterConditionUser(UserInterface $user = null)
  84. {
  85. $this->joinUsers();
  86. if ($user) {
  87. return $this
  88. ->andWhere(':user MEMBER OF .users OR .users is empty')
  89. ->setParameter('user', $user);
  90. } else {
  91. return $this
  92. ->andWhere('.users is empty');
  93. }
  94. }
  95. public function filterConditionGroupUser(UserInterface $user = null)
  96. {
  97. $this->joinGroupUsers();
  98. if ($user) {
  99. return $this
  100. ->andWhere(':groupUser MEMBER OF .groupUsers OR .groupUsers is empty')
  101. ->setParameter('groupUser', $user->getGroupUsers());
  102. } else {
  103. return $this
  104. ->andWhere('.groupUsers is empty');
  105. }
  106. }
  107. public function filterConditionProductFamilies(array $productFamiliesIds, array $productFamilies)
  108. {
  109. $this->joinProductFamilies();
  110. $this->joinProductFamily();
  111. return $this
  112. ->andWhere(':productFamiliesIds MEMBER OF .productFamilies OR .productFamilies is empty')
  113. ->andWhere('.productFamily IN (:productFamilies) OR .productFamily is null')
  114. ->setParameter('productFamiliesIds', $productFamiliesIds)
  115. ->setParameter('productFamilies', $productFamilies);
  116. }
  117. public function filterConditionProductFamily(ProductFamilyInterface $productFamily)
  118. {
  119. $this->joinProductFamily();
  120. return $this
  121. ->andWhere(':productFamily MEMBER OF .productFamily OR .productFamily is empty')
  122. ->setParameter('productFamily', $productFamily);
  123. }
  124. public function filterConditionProductCategories(array $productCategories)
  125. {
  126. $this->joinProductCategories();
  127. return $this
  128. ->andWhere(':productCategory MEMBER OF .productCategories OR .productCategories is empty')
  129. ->setParameter('productCategory', $productCategories);
  130. }
  131. }