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.

160 line
4.6KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Reduction;
  3. use Knp\Component\Pager\PaginatorInterface;
  4. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  5. use Lc\CaracoleBundle\Repository\MerchantRepositoryQueryTrait;
  6. use Lc\CaracoleBundle\Repository\SectionRepositoryQueryTrait;
  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 SectionRepositoryQueryTrait;
  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 $productFamilies)
  108. {
  109. $this->joinProductFamilies();
  110. return $this
  111. ->andWhere(':productFamilies MEMBER OF .productFamilies OR .productFamilies is empty')
  112. ->setParameter('productFamilies', $productFamilies);
  113. }
  114. public function filterConditionProductFamily(ProductFamilyInterface $productFamily)
  115. {
  116. $this->joinProductFamily();
  117. return $this
  118. ->andWhere(':productFamily MEMBER OF .productFamily OR .productFamily is empty')
  119. ->setParameter('productFamily', $productFamily);
  120. }
  121. public function filterConditionProductCategories(array $productCategories)
  122. {
  123. $this->joinProductCategories();
  124. return $this
  125. ->andWhere(':productCategory MEMBER OF .productCategories OR .productCategories is empty')
  126. ->setParameter('productCategory', $productCategories);
  127. }
  128. }