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.

159 lines
4.5KB

  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 $isJoinProductCategories = false;
  12. protected bool $isJoinProductFamilySectionProperties = false;
  13. protected bool $isJoinProducts = false;
  14. protected bool $isJoinQualityLabels = false;
  15. public function __construct(ProductFamilyRepository $repository, PaginatorInterface $paginator)
  16. {
  17. parent::__construct($repository, 'pf', $paginator);
  18. }
  19. public function joinProductFamilySectionProperties(bool $addSelect = true): self
  20. {
  21. if (!$this->isJoinProductFamilySectionProperties) {
  22. $this->isJoinProductFamilySectionProperties = true;
  23. $this->leftJoin('.productFamilySectionProperties', 'pfsp');
  24. if ($addSelect) {
  25. $this->addSelect('pfsp');
  26. }
  27. }
  28. return $this;
  29. }
  30. public function joinQualityLabels(bool $addSelect = true): self
  31. {
  32. if (!$this->isJoinQualityLabels) {
  33. $this->isJoinQualityLabels = true;
  34. $this->leftJoin('.qualityLabels', 'pfql');
  35. if ($addSelect) {
  36. $this->addSelect('pfql');
  37. }
  38. }
  39. return $this;
  40. }
  41. public function filterBySection(SectionInterface $section,bool $addSelectProductFamilySectionProperties = true)
  42. {
  43. $this->joinProductFamilySectionProperties($addSelectProductFamilySectionProperties);
  44. $this->andWhereSection('pfsp', $section);
  45. $this->andWhere('pfsp.status = 1');
  46. }
  47. public function selectCount(): self
  48. {
  49. return $this->select('count(pf.id)');
  50. }
  51. public function selectProductCategories(): self
  52. {
  53. $this->joinProductCategories();
  54. return $this->addSelect('pCategories');
  55. }
  56. public function filterLikeBehaviorStockCycle(string $behaviorStockCycle): self
  57. {
  58. return $this
  59. ->andWhere('.behaviorStockCycle LIKE :behaviorStockCycle')
  60. ->setParameter('behaviorStockCycle', $behaviorStockCycle);
  61. }
  62. public function filterByProductCategory(ProductCategoryInterface $category): self
  63. {
  64. $this->joinProductCategories();
  65. return $this
  66. ->andWhere(':category MEMBER OF .productCategories')
  67. ->setParameter('category', $category->getId());
  68. }
  69. public function filterByPropertyNoveltyExpirationDate($dateTime = null): self
  70. {
  71. if (is_null($dateTime)) {
  72. $dateTime = new \DateTime();
  73. }
  74. return $this->andWhere(':now <= .propertyNoveltyExpirationDate')
  75. ->setParameter('now', $dateTime);
  76. }
  77. public function filterIsOrganicLabel(): self
  78. {
  79. return $this->andWhere('.propertyOrganicLabel IS NOT NULL');
  80. }
  81. public function filterIsNovelty()
  82. {
  83. return $this->andWhere(':now <= .propertyNoveltyExpirationDate')
  84. ->setParameter('now', new \DateTime()) ;
  85. }
  86. public function filterByTerms($terms): self
  87. {
  88. $this->andWhere('.title LIKE :terms OR cat.title LIKE :terms');
  89. $this->setParameter(':terms', '%'.$terms.'%') ;
  90. return $this;
  91. }
  92. public function filterBySupplier($supplier): self
  93. {
  94. return $this->andWhereEqual('supplier', $supplier);
  95. }
  96. public function joinProductCategories(bool $addSelect = false): self
  97. {
  98. if (!$this->isJoinProductCategories) {
  99. $this->isJoinProductCategories = true;
  100. return $this
  101. ->leftJoin('.productCategories', 'cat')
  102. ->addSelect('cat')
  103. ;
  104. }
  105. return $this;
  106. }
  107. public function selectProducts(): self
  108. {
  109. $this->joinProducts();
  110. return $this->addSelect('products');
  111. }
  112. public function joinProducts(): self
  113. {
  114. if (!$this->isJoinProducts) {
  115. $this->isJoinProducts = true;
  116. return $this
  117. ->innerJoin('.products', 'pfp')
  118. ->addSelect('pfp')
  119. ;
  120. }
  121. return $this;
  122. }
  123. public function orderByPosition(): self
  124. {
  125. $this->orderBy('e.position', 'ASC');
  126. return $this;
  127. }
  128. }