Source of file ProductGroupSchema.php
Size: 8,375 Bytes - Last Modified: 2021-12-23T10:39:35+00:00
/var/www/docs.ssmods.com/process/src/src/ProductsAndGroups/ProductGroupSchema.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 | <?php namespace Sunnysideup\Ecommerce\ProductsAndGroups; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Injector\Injectable; use Sunnysideup\Ecommerce\Api\ClassHelpers; use Sunnysideup\Ecommerce\Pages\ProductGroup; use Sunnysideup\Ecommerce\Pages\ProductGroupController; use Sunnysideup\Ecommerce\ProductsAndGroups\Applyers\BaseApplyer; use Sunnysideup\Ecommerce\ProductsAndGroups\Applyers\ProductDisplayer; use Sunnysideup\Ecommerce\ProductsAndGroups\Applyers\ProductFilter; use Sunnysideup\Ecommerce\ProductsAndGroups\Applyers\ProductGroupFilter; use Sunnysideup\Ecommerce\ProductsAndGroups\Applyers\ProductSearchFilter; use Sunnysideup\Ecommerce\ProductsAndGroups\Applyers\ProductSorter; use Sunnysideup\Ecommerce\ProductsAndGroups\Builders\BaseProductList; use Sunnysideup\Ecommerce\ProductsAndGroups\Builders\FinalProductList; use Sunnysideup\Ecommerce\ProductsAndGroups\Builders\RelatedProductGroups; use Sunnysideup\Ecommerce\ProductsAndGroups\Settings\UserPreference; use Sunnysideup\Vardump\DebugTrait; /** * In terms of ProductAndGroupsLists, this class knows all about * the classes being used and the settings associated with it. * It is linked to the ProductGroup and so it does not know about user preferences * and other settings that are set in run-time. */ class ProductGroupSchema { use Configurable; use Injectable; use DebugTrait; /** * list of sort / filter / display variables. * * @var array */ protected const SORT_DISPLAY_NAMES = [ 'SEARCHFILTER' => [ 'getVariable' => 'searchfilter', 'dbFieldName' => '', 'translationCode' => 'SEARCH_RESULTS', 'defaultApplyer' => ProductSearchFilter::class, ], 'GROUPFILTER' => [ 'getVariable' => 'groupfilter', 'dbFieldName' => '', 'translationCode' => 'GROUPFILTER_BY', 'defaultApplyer' => ProductGroupFilter::class, ], 'FILTER' => [ 'getVariable' => 'filter', 'dbFieldName' => 'DefaultFilter', 'translationCode' => 'FILTER_BY', 'defaultApplyer' => ProductFilter::class, ], 'SORT' => [ 'getVariable' => 'sort', 'dbFieldName' => 'DefaultSortOrder', 'translationCode' => 'SORT_BY', 'defaultApplyer' => ProductSorter::class, ], 'DISPLAY' => [ 'getVariable' => 'display', 'dbFieldName' => 'DisplayStyle', 'translationCode' => 'DISPLAY_STYLE', 'defaultApplyer' => ProductDisplayer::class, ], ]; /** * @var array<null|string, mixed>|mixed */ public $sortFilterDisplayNames; protected static $applyerCache = []; /** * @var string */ private static $product_group_list_class_name = RelatedProductGroups::class; /** * @var string */ private static $base_product_list_class_name = BaseProductList::class; /** * @var string */ private static $final_product_list_class_name = FinalProductList::class; /** * @var string */ private static $user_preferences_class_name = UserPreference::class; /** * @var string */ private static $debug_provider_class_name = Debug::class; public function getData() { return self::SORT_DISPLAY_NAMES; } public function getBaseProductListClassName(): string { return Config::inst()->get(static::class, 'base_product_list_class_name'); } public function getFinalProductListClassName(): string { return Config::inst()->get(static::class, 'final_product_list_class_name'); } public function getProductGroupListClassName(): string { return Config::inst()->get(static::class, 'product_group_list_class_name'); } public function getUserPreferencesClassName(): string { return Config::inst()->get(static::class, 'user_preferences_class_name'); } public function getDebugProvider(): string { return Config::inst()->get(static::class, 'debug_provider_class_name'); } /** * @param ProductGroupController $rootGroupController * @param ProductGroup $rootGroup */ public function getDebugProviderAsObject($rootGroupController, $rootGroup): Debug { $className = $this->getDebugProvider(); return new $className($rootGroupController, $rootGroup); } /** * Returns the full sortFilterDisplayNames set, a subset, or one value * by either type (e.g. FILER) or variable (e.g dbFieldName) * or both. * * @param string $typeOrVariable FILTER | SORT | DISPLAY OR variable * * @return array|string */ public function getSortFilterDisplayValues(?string $typeOrVariable = '', ?string $variable = '') { $data = $this->getData(); if ($variable) { return $data[$typeOrVariable][$variable] ?? 'error'; } $newData = []; if (isset($this->sortFilterDisplayNames[$typeOrVariable])) { $newData = $data[$typeOrVariable]; } elseif ($typeOrVariable) { foreach ($this->sortFilterDisplayNames as $group) { $newData[] = $group[$typeOrVariable] ?? 'error'; } } else { $newData = $data; } return $newData; } /** * @param string $type FILTER|SORT|DISPLAY * @param bool $showError optional */ public function IsSortFilterDisplayNamesType(string $type, ?bool $showError = true): bool { $data = $this->getData(); if (isset($data[$type])) { return true; } if ($showError) { user_error('Invalid type supplied: ' . $type . 'Please use: SORT / FILTER / DISPLAY / SEARCHFILTER'); } return false; } public function getOptions(string $classNameOrType): array { $obj = $this->getApplyer($classNameOrType); return $obj->getOptions(); } /** * returns a dropdown like list of options for a searches. */ public function getSearchFilterOptionsMap(): array { return $this->getOptionsMap('SEARCHFILTER'); } /** * returns a dropdown like list of options for a filters. */ public function getGroupFilterOptionsMap(): array { return $this->getOptionsMap('GROUPFILTER'); } /** * returns a dropdown like list of options for a filters. */ public function getFilterOptionsMap(): array { return $this->getOptionsMap('FILTER'); } /** * returns a dropdown like list of options for a sorters. */ public function getSortOptionsMap(): array { return $this->getOptionsMap('SORT'); } /** * returns a dropdown like list of options for a display styles. */ public function getDisplayOptionsMap(): array { return $this->getOptionsMap('DISPLAY'); } /** * todo: CHECK! */ public function getApplyerClassName(string $type): string { if ($this->IsSortFilterDisplayNamesType($type)) { return $this->getSortFilterDisplayValues($type, 'defaultApplyer'); } return ''; } /** * you can provide type or class name. * * @param null|mixed $finalProductList * * @return BaseApplyer */ public function getApplyer(string $classNameOrType, $finalProductList = null) { $className = $classNameOrType; $betterClassName = $this->getApplyerClassName($classNameOrType); if ($betterClassName) { $className = $betterClassName; } if (empty(self::$applyerCache[$className])) { self::$applyerCache[$className] = new $className($finalProductList); } ClassHelpers::check_for_instance_of(self::$applyerCache[$className], BaseApplyer::class); return self::$applyerCache[$className]; } /** * returns a dropdown like list of options for a BaseClass class name. */ protected function getOptionsMap(string $classNameOrType): array { $obj = $this->getApplyer($classNameOrType); return $obj->getOptionsMap(); } } |