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.

ProductFamilyRepositoryQuery.php 5.7KB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Product;
  3. use Knp\Component\Pager\PaginatorInterface;
  4. use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
  5. use Lc\CaracoleBundle\Model\Product\ProductCategoryInterface;
  6. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  7. use Lc\CaracoleBundle\Repository\SectionRepositoryQueryTrait;
  8. use Lc\SovBundle\Repository\AbstractRepositoryQuery;
  9. class ProductFamilyRepositoryQuery extends AbstractRepositoryQuery
  10. {
  11. protected bool $isJoinSections = false;
  12. protected bool $isJoinProductCategories = false;
  13. protected bool $isJoinProductFamilySectionProperties = false;
  14. protected bool $isJoinProducts = false;
  15. protected bool $isJoinQualityLabels = false;
  16. public function __construct(ProductFamilyRepository $repository, PaginatorInterface $paginator)
  17. {
  18. parent::__construct($repository, 'productFamily', $paginator);
  19. }
  20. public function joinProductFamilySectionProperties(bool $addSelect = true): self
  21. {
  22. if (!$this->isJoinProductFamilySectionProperties) {
  23. $this->isJoinProductFamilySectionProperties = true;
  24. $this->leftJoin('.productFamilySectionProperties', 'productFamilySectionProperties');
  25. if ($addSelect) {
  26. $this->addSelect('productFamilySectionProperties');
  27. }
  28. }
  29. return $this;
  30. }
  31. public function joinQualityLabels(bool $addSelect = true): self
  32. {
  33. if (!$this->isJoinQualityLabels) {
  34. $this->isJoinQualityLabels = true;
  35. $this->leftJoin('.qualityLabels', 'qualityLabels');
  36. if ($addSelect) {
  37. $this->addSelect('qualityLabels');
  38. }
  39. }
  40. return $this;
  41. }
  42. public function joinSections(bool $addSelect = true): self
  43. {
  44. if (!$this->isJoinSections) {
  45. $this->isJoinSections = true;
  46. $this->leftJoin('productFamilySectionProperties.section', 'section');
  47. if ($addSelect) {
  48. $this->addSelect('section');
  49. }
  50. }
  51. return $this;
  52. }
  53. public function filterBySection(SectionInterface $section, bool $addSelectProductFamilySectionProperties = true)
  54. {
  55. $this->joinProductFamilySectionProperties($addSelectProductFamilySectionProperties);
  56. $this->andWhereSection('productFamilySectionProperties', $section);
  57. $this->andWhere('productFamilySectionProperties.status = 1');
  58. }
  59. public function filterByMerchantViaSection(MerchantInterface $merchant)
  60. {
  61. $this->joinProductFamilySectionProperties(false);
  62. $this->joinSections(false);
  63. $this->andWhereMerchant('section', $merchant);
  64. $this->andWhere('productFamilySectionProperties.status = 1');
  65. }
  66. public function selectCount(): self
  67. {
  68. return $this->select('count(productFamily.id)');
  69. }
  70. public function selectProductCategories(): self
  71. {
  72. $this->joinProductCategories();
  73. return $this->addSelect('pCategories');
  74. }
  75. public function filterLikeBehaviorStockCycle(string $behaviorStockCycle): self
  76. {
  77. return $this
  78. ->andWhere('.behaviorStockCycle LIKE :behaviorStockCycle')
  79. ->setParameter('behaviorStockCycle', $behaviorStockCycle);
  80. }
  81. public function filterByProductCategory(ProductCategoryInterface $category): self
  82. {
  83. $this->joinProductCategories();
  84. return $this
  85. ->andWhere(':category MEMBER OF .productCategories')
  86. ->setParameter('category', $category->getId());
  87. }
  88. public function filterByProductCategoryArray(Array $categoryArray): self
  89. {
  90. $this->joinProductCategories();
  91. return $this
  92. ->andWhere(':categoryArray MEMBER OF .productCategories')
  93. ->setParameter('categoryArray', $categoryArray);
  94. }
  95. public function filterByPropertyNoveltyExpirationDate($dateTime = null): self
  96. {
  97. if (is_null($dateTime)) {
  98. $dateTime = new \DateTime();
  99. }
  100. return $this->andWhere(':now <= .propertyNoveltyExpirationDate')
  101. ->setParameter('now', $dateTime);
  102. }
  103. public function filterIsOrganicLabel(): self
  104. {
  105. return $this->andWhere('.propertyOrganicLabel IS NOT NULL');
  106. }
  107. public function filterIsNovelty()
  108. {
  109. return $this->andWhere(':now <= .propertyNoveltyExpirationDate')
  110. ->setParameter('now', new \DateTime());
  111. }
  112. public function filterByTerms($terms): self
  113. {
  114. $this->andWhere('.title LIKE :terms OR productCategories.title LIKE :terms');
  115. $this->setParameter(':terms', '%' . $terms . '%');
  116. return $this;
  117. }
  118. public function filterBySupplier($supplier): self
  119. {
  120. return $this->andWhereEqual('supplier', $supplier);
  121. }
  122. public function joinProductCategories(bool $addSelect = false): self
  123. {
  124. if (!$this->isJoinProductCategories) {
  125. $this->isJoinProductCategories = true;
  126. $this->leftJoin('.productCategories', 'productCategories');
  127. if ($addSelect) {
  128. $this->addSelect('productCategories');
  129. }
  130. return $this;
  131. }
  132. return $this;
  133. }
  134. public function selectProducts(): self
  135. {
  136. return $this->joinProducts(true);
  137. }
  138. public function joinProducts(bool $addSelect = true): self
  139. {
  140. if (!$this->isJoinProducts) {
  141. $this->isJoinProducts = true;
  142. $this->innerJoin('.products', 'products');
  143. if ($addSelect) {
  144. $this->addSelect('products');
  145. }
  146. }
  147. return $this;
  148. }
  149. public function orderByPosition(): self
  150. {
  151. $this->orderBy('e.position', 'ASC');
  152. return $this;
  153. }
  154. }