query = $query; $this->priceSolver = $priceSolver; $this->cache = $cache; } public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface { $query->orderBy('position'); return $query; } public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface { $query->filterIsOnlineAndOffline(); $this->addFilterBySectionOptionnal($query); $this->addFilterByMerchantViaSectionOptionnal($query); return $query; } public function relationsDefault($query): RepositoryQueryInterface { $query->joinProductCategories(); $query->joinProducts(); $query->joinQualityLabels(); return $query; } public function getByParentCategory( ProductCategoryInterface $parentCategory, $user = null, $query = null ) { $productFamiliesArray = []; foreach ($parentCategory->getChildrens() as $i => $category) { $productFamiliesCategory = $this->getByCategory($category); foreach ($productFamiliesCategory as $productFamily) { $productFamiliesArray[$productFamily->getId()] = $productFamily; } uasort($productFamiliesArray, array($this, 'compMethodSortProductFamiliesByPosition')); } return $this->getWithReductions($productFamiliesArray, $user, true); } private function compMethodSortProductFamiliesByPosition($a, $b) { return $a->getPosition() > $b->getPosition(); } // getProductFamiliesByCategory public function getByCategory( ProductCategoryInterface $productCategory, $query = null ) { $query = $this->createDefaultQuery($query); $query ->filterIsOnline() ->filterByProductCategory($productCategory); return $query->find(); } //TODO ajouter status en donné de contexte pour éviter ce truc public function getByCategoryOnlineAndOffline( ProductCategoryInterface $productCategory, $query = null ) { $query = $this->createDefaultQuery($query); $query ->filterByProductCategory($productCategory); return $query->find(); } // getProductFamiliesNovelties public function getNovelty($user = null, $organizeByParentCategory = true, $query = null) { $query = $this->createDefaultQuery($query); $query ->filterByPropertyNoveltyExpirationDate() ->filterIsOnline(); $results = $query->find(); return $this->getWithReductions($results, $user, false, $organizeByParentCategory); } public function getCachedNovelty($user = null, $organizeByParentCategory = true, $query = null): array { $productFamilyStore = $this; return $this->cache->get($this->getCacheKeyNovelty(), function (ItemInterface $item) use ($productFamilyStore, $user, $organizeByParentCategory, $query) { $item->expiresAfter(3600); return $productFamilyStore->getNovelty($user, $organizeByParentCategory, $query); }); } public function deleteCacheNovelty(): void { $this->cache->delete($this->getCacheKeyNovelty()); } public function getCacheKeyNovelty() { return 'products_novelty_'.$this->section->getId(); } // getProductFamiliesOrganics public function getOrganic($user = null, $organizeByParentCategory = true, $query = null) { $query = $this->createDefaultQuery($query); $query ->filterIsOrganicLabel() ->filterIsOnline(); $results = $query->find(); return $this->getWithReductions($results, $user, false, $organizeByParentCategory); } // getProductFamiliesOnDiscount public function getDiscount($user = null, $organizeByParentCategory = true, $query = null) { return $this->getWithReductions($this->getOnline($query), $user, false, $organizeByParentCategory, true); } public function getCachedDiscount($user = null, $organizeByParentCategory = true, $query = null): array { $productFamilyStore = $this; return $this->cache->get($this->getCacheKeyDiscount(), function (ItemInterface $item) use ($productFamilyStore, $user, $organizeByParentCategory, $query) { $item->expiresAfter(3600); return $productFamilyStore->getDiscount($user, $organizeByParentCategory, $query); }); } public function deleteCacheDiscount(): void { $this->cache->delete($this->getCacheKeyDiscount()); } public function getCacheKeyDiscount() { return 'products_discount_'.$this->section->getId(); } public function loadAllDatas(ProductFamilyInterface $productFamily) { $productFamily->getProductFamilySectionProperties()->toArray(); } // getProductFamiliesFavorites public function getFavorite($user = null, $organizeByParentCategory = true, $query = null) { if ($user) { return $this->getWithReductions( $user->getFavoriteProductFamilies(), $user, false, $organizeByParentCategory ); } return []; } // findByTerms public function getByTerms( $terms, $maxResults = false, $organizeByParentCategory = false, $user = null, $query = null ) { $query = $this->createDefaultQuery($query); $query->filterIsOnline(); $query->filterByTerms($terms); $query->groupBy('id'); if ($maxResults) { $query->limit($maxResults); } $results = $query->find(); return $this->getWithReductions($results, $user, false, $organizeByParentCategory); } public function getBestReductionCatalog( ProductFamilyInterface $productFamily, ReductionCatalogInterface $reductionCatalog1, ReductionCatalogInterface $reductionCatalog2 ) { $price1 = $this->priceSolver->getPriceWithTaxByReduction( $productFamily, $reductionCatalog1 ); $price2 = $this->priceSolver->getPriceWithTaxByReduction( $productFamily, $reductionCatalog2 ); if ($price1 > $price2) { return $reductionCatalog2; } else { return $reductionCatalog1; } } // setReductionForProductFamilies public function getWithReductions( $productFamilies, UserInterface $user = null, $organizeByCategory = false, $organizeByParentCategory = false, $onlyOnDiscount = false ) { $conditions = [ 'productFamiliesIds' => [], 'categories' => [], 'productFamilies' => [], ]; foreach ($productFamilies as $productFamily) { $conditions['productFamiliesIds'][] = $productFamily->getId(); $conditions['productFamilies'][] = $productFamily; $conditions['categories'] = array_merge( $conditions['categories'], $productFamily->getProductCategories()->toArray() ); } if ($productFamilies) { $reductionCatalogs = $this->reductionCatalogStore->getByProductFamiliesConditions( $conditions, $user ); } $productFamiliesToReturn = array(); foreach ($productFamilies as $productFamily) { // Cache : chargement de toutes les données manquantes (fetch eager ne fonctionne pas) $this->loadAllDatas($productFamily); foreach ($reductionCatalogs as $reductionCatalog) { $conditionProductFamilies = $conditionProductFamily = $conditionProductCategory = false; if ($reductionCatalog->getProductFamilies()->contains( $productFamily ) || $reductionCatalog->getProductFamilies()->isEmpty()) { $conditionProductFamilies = true; } if ($reductionCatalog->getProductFamily() == $productFamily || $reductionCatalog->getProductFamily( ) === null) { $conditionProductFamily = true; } foreach ($productFamily->getProductCategories() as $productCategory) { if ($reductionCatalog->getProductCategories()->contains( $productCategory ) || $reductionCatalog->getProductCategories()->isEmpty()) { $conditionProductCategory = true; } } if ($conditionProductFamilies && $conditionProductFamily && $conditionProductCategory) { if ($productFamily->getReductionCatalog()) { $productFamily->setReductionCatalog( $this->getBestReductionCatalog( $productFamily, $reductionCatalog, $productFamily->getReductionCatalog() ) ); } else { $productFamily->setReductionCatalog($reductionCatalog); } } } if (($onlyOnDiscount && $productFamily->getReductionCatalog()) || !$onlyOnDiscount) { if ($organizeByParentCategory) { $productCategories = $productFamily->getProductCategories(); if ($productCategories && count($productCategories) > 0) { $parentCategory = $productCategories[0]->getParentCategory(); if ($this->productCategorySolver->isDisplay($parentCategory, $user)) { if (!isset($productFamiliesToReturn[$parentCategory->getId()])) { $productFamiliesToReturn[$parentCategory->getId()] = [ 'category' => $parentCategory, 'products' => [] ]; } $productFamiliesToReturn[$parentCategory->getId()]['products'][] = $productFamily; } } } elseif ($organizeByCategory) { foreach ($productFamily->getProductCategories() as $productCategory) { if ($this->isDisplay($productFamily)) { $productFamiliesToReturn[$productCategory->getId()][] = $productFamily; } } } else { if ($this->isDisplay($productFamily)) { $productFamiliesToReturn[] = $productFamily; } } } } if ($organizeByParentCategory) { uasort($productFamiliesToReturn, array($this, 'compMethodSortProductFamiliesByParentCategory')); } return $productFamiliesToReturn; } }