Source of file FinalProductList.php
Size: 9,803 Bytes - Last Modified: 2021-12-23T10:39:35+00:00
/var/www/docs.ssmods.com/process/src/src/ProductsAndGroups/Builders/FinalProductList.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 | <?php namespace Sunnysideup\Ecommerce\ProductsAndGroups\Builders; use SilverStripe\ORM\DataList; use SilverStripe\ORM\DB; use Sunnysideup\Ecommerce\Api\ArrayMethods; use Sunnysideup\Ecommerce\Api\ClassHelpers; use Sunnysideup\Ecommerce\Pages\ProductGroup; use Sunnysideup\Ecommerce\Pages\ProductGroupController; use Sunnysideup\Vardump\Vardump; /** * A wrapper for a paginated of products which can be filtered and sorted. * * This list is linked to a controller and can be changed (the base group list is usually the same) * * @author: Nicolaas [at] Sunny Side Up .co.nz * @package: ecommerce * @subpackage: Builders */ class FinalProductList extends AbstractProductsAndGroupsList { /** * @var BaseProductList */ protected $baseProductList; /** * @var ProductGroupController */ protected $rootGroupController; /** * @var int[] */ protected $filterForCandidateCategoryIdsFiltered = []; /** * @var int[] */ protected $alsoShowParentIdsFiltered = []; /** * @var int[] */ protected $alsoShowProductsIdsFiltered = []; /** * singleton_cache. * * @var self */ protected static $singleton_cache; protected $rawCountCachedCache; private static $group_filter_candidates_sort = [ 'ClassName' => 'DESC', 'Title' => 'ASC', ]; /** * @param ProductGroupController $rootGroupController * @param ProductGroup $rootGroup */ public function __construct($rootGroupController, $rootGroup) { if (false === self::$singleton_cache) { user_error('Use ::inst to create me!'); } $this->setRootGroupController($rootGroupController); $this->setRootGroup($rootGroup); $this->baseProductList = $rootGroup->getBaseProductList(); ClassHelpers::check_for_instance_of($this->baseProductList, BaseProductList::class, true); $this->products = $this->baseProductList->getProducts(); } /** * create instances. * * @param ProductGroupController $rootGroupController * @param ProductGroup $rootGroup * * @return FinalProductList */ public static function inst($rootGroupController, $rootGroup) { if (! isset(self::$singleton_cache)) { self::$singleton_cache = new FinalProductList($rootGroupController, $rootGroup); } return self::$singleton_cache; } //################################################# // SETTERS / GETTERS //################################################# /** * Set the root {@link ProductGroup} to display the products from. * * @param ProductGroup $rootGroupController */ public function setRootGroupController($rootGroupController): self { $this->rootGroupController = $rootGroupController; ClassHelpers::check_for_instance_of($rootGroupController, ProductGroupController::class, true); return $this; } public function getBuyableClassName(): string { return $this->getBaseProductList()->getBuyableClassName(); } /** * @param array|string $filter */ public function setExtraFilter($filter): self { if ($filter) { $this->products = $this->products->filter($filter); } return $this; } /** * @param array|string $sort */ public function setAlternativeSort($sort): self { if ($sort) { $this->products = $this->products->sort($sort); } return $this; } /** * returns the associated BaseProductList. * * @return BaseProductList */ public function getBaseProductList() { return $this->baseProductList; } //################################################# // APPLYERS //################################################# public function apply(string $classNameOrType, string $key, $params = null): self { $applyer = $this->getApplyer($classNameOrType); //Vardump::now(get_class($obj)); $this->products = $applyer ->apply($key, $params) ->getProducts() ; //Vardump::now($this->products); return $this; } /** * @param array|string $params optional additional filter */ public function applySearchFilter(string $key, $params = null): self { return $this->apply('SEARCHFILTER', $key, $params); } /** * @param array|string $params optional additional filter */ public function applyGroupFilter(string $key, $params = null): self { return $this->apply('GROUPFILTER', $key, $params); } /** * @param array|string $params optional additional filter */ public function applyFilter(string $key, $params = null): self { return $this->apply('FILTER', $key, $params); } /** * @param array|string $params optional additional filter */ public function applySorter(string $key, $params = null): self { return $this->apply('SORT', $key, $params); } /** * @param array|string $params optional additional filter */ public function applyDisplayer(string $key, $params = null): self { return $this->apply('DISPLAY', $key, $params); } //######################################### // PRODUCTS: Also show //######################################### public function getAlsoShowProductsIds(): array { return ArrayMethods::filter_array($this->alsoShowProductsIdsFiltered); } public function getAlsoShowProductsIdsFiltered(): array { return ArrayMethods::filter_array($this->alsoShowProductsIdsFiltered); } public function getAlsoShowProducts(): DataList { return $this->baseProductList->getAlsoShowProducts(); } public function getAlsoShowProductsFiltered(): DataList { return $this->products->filter(['ID' => $this->getAlsoShowProductsIdsFiltered()]); } //######################################### // GROUPS - smart //######################################### public function getFilterForCandidateCategoryIds(): array { return $this->baseProductList->getFilterForCandidateCategoryIds(); } public function getFilterForCandidateCategoryIdsFiltered(): array { return ArrayMethods::filter_array($this->filterForCandidateCategoryIdsFiltered); } public function getFilterForCandidateCategories(): DataList { return $this->baseProductList->getFilterForCandidateCategories(); } /** * @todo use EcommerceCache */ public function getFilterForCandidateCategoriesFiltered() { if (empty($this->filterForCandidateCategoryIdsFiltered)) { $ids1 = $this->getAlsoShowParentsFiltered()->columnUnique(); $ids2 = $this->getAlsoShowProductsProductGroupInclusiveFiltered()->columnUnique(); $ids3 = $this->getParentGroupsBasedOnProductsFiltered()->columnUnique(); $this->filterForCandidateCategoryIdsFiltered = array_merge($ids1, $ids2, $ids3); } $list = $this->turnIdListIntoProductGroups($this->getFilterForCandidateCategoryIdsFiltered(), true); return $list->exclude(['ID' => $this->getParentGroupIds()]) ->Sort($this->Config()->get('group_filter_candidates_sort')) ; } /** * @todo use EcommerceCache */ public function getAlsoShowParentIdsFiltered(): array { if (! count($this->alsoShowParentIdsFiltered)) { $rows = DB::query(' SELECT "ProductGroupID" FROM "Product_ProductGroups" WHERE "ProductID" IN (' . implode(', ', $this->getProductIds()) . ');')->column(); $this->alsoShowParentIdsFiltered = ArrayMethods::filter_array($rows); } return $this->alsoShowParentIdsFiltered; } public function getAlsoShowProductsProductGroupInclusiveFiltered(): DataList { return $this->turnIdListIntoProductGroups($this->getAlsoShowProductsFiltered()->columnUnique('ParentID')); } public function getParentGroupsBasedOnProductsFiltered(): DataList { return $this->turnIdListIntoProductGroups($this->getProducts()->columnUnique('ParentID')); } //################################################# // GROUPS: Parents from natural hierachy //################################################# /** * required for SubGroups. */ public function getParentGroupIds(): array { return $this->baseProductList->getParentGroupIds(); } public function getParentGroups(): DataList { return $this->baseProductList->getParentGroups(); } //################################################# // GROUPS: Also Show Products, based on Products included through AlsoShow Show //################################################# public function getAlsoShowParentIds(): array { return $this->baseProductList->getAlsoShowParentIds(); } public function getAlsoShowParents(): DataList { return $this->baseProductList->getAlsoShowParents(); } public function getAlsoShowParentsFiltered(): DataList { $list = ProductGroup::get()->filter(['ID' => $this->getAlsoShowParentIdsFiltered()]); return RelatedProductGroups::apply_default_filter_to_groups($list); } public function getRawCountCached(): int { if (null === $this->rawCountCachedCache) { $this->rawCountCachedCache = $this->getRawCount(); } return $this->rawCountCachedCache; } } |