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.

180 line
5.4KB

  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 filterByPropertyNoveltyExpirationDate($dateTime = null): self
  89. {
  90. if (is_null($dateTime)) {
  91. $dateTime = new \DateTime();
  92. }
  93. return $this->andWhere(':now <= .propertyNoveltyExpirationDate')
  94. ->setParameter('now', $dateTime);
  95. }
  96. public function filterIsOrganicLabel(): self
  97. {
  98. return $this->andWhere('.propertyOrganicLabel IS NOT NULL');
  99. }
  100. public function filterIsNovelty()
  101. {
  102. return $this->andWhere(':now <= .propertyNoveltyExpirationDate')
  103. ->setParameter('now', new \DateTime());
  104. }
  105. public function filterByTerms($terms): self
  106. {
  107. $this->andWhere('.title LIKE :terms OR productCategories.title LIKE :terms');
  108. $this->setParameter(':terms', '%' . $terms . '%');
  109. return $this;
  110. }
  111. public function filterBySupplier($supplier): self
  112. {
  113. return $this->andWhereEqual('supplier', $supplier);
  114. }
  115. public function joinProductCategories(bool $addSelect = false): self
  116. {
  117. if (!$this->isJoinProductCategories) {
  118. $this->isJoinProductCategories = true;
  119. $this->leftJoin('.productCategories', 'productCategories');
  120. if ($addSelect) {
  121. $this->addSelect('productCategories');
  122. }
  123. return $this;
  124. }
  125. return $this;
  126. }
  127. public function selectProducts(): self
  128. {
  129. return $this->joinProducts(true);
  130. }
  131. public function joinProducts(bool $addSelect = true): self
  132. {
  133. if (!$this->isJoinProducts) {
  134. $this->isJoinProducts = true;
  135. $this->innerJoin('.products', 'products');
  136. if ($addSelect) {
  137. $this->addSelect('products');
  138. }
  139. }
  140. return $this;
  141. }
  142. public function orderByPosition(): self
  143. {
  144. $this->orderBy('e.position', 'ASC');
  145. return $this;
  146. }
  147. }