Source of file ProductSorter.php
Size: 3,095 Bytes - Last Modified: 2021-12-23T10:39:35+00:00
/var/www/docs.ssmods.com/process/src/src/ProductsAndGroups/Applyers/ProductSorter.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 | <?php namespace Sunnysideup\Ecommerce\ProductsAndGroups\Applyers; use Sunnysideup\Ecommerce\Api\ArrayMethods; use Sunnysideup\Ecommerce\Pages\Product; /** * provides data on the user. */ class ProductSorter extends BaseApplyer { protected static $defaultSortOrderFromFilter = []; /** * @var array */ private static $options = [ BaseApplyer::DEFAULT_NAME => [ 'Title' => 'Default Order', 'SQL' => '"Sort" ASC, "Title" ASC', 'UsesParamData' => false, 'IsShowFullList' => false, ], 'new' => [ 'Title' => 'Latest Arrivals', 'SQL' => '"Created" DESC, "Price" DESC', 'UsesParamData' => false, 'IsShowFullList' => false, ], 'lowprice' => [ 'Title' => 'Lowest Price', 'SQL' => '"Price" ASC, "Sort" ASC, "Title" ASC', 'UsesParamData' => false, 'IsShowFullList' => false, ], 'highprice' => [ 'Title' => 'Highest Price', 'SQL' => '"Price" DESC, "Sort" ASC, "Title" ASC', 'UsesParamData' => false, 'IsShowFullList' => false, ], 'name' => [ 'Title' => 'Name', 'SQL' => '"Title" ASC, "Sort" ASC', 'UsesParamData' => false, 'IsShowFullList' => false, ], ]; public static function setDefaultSortOrderFromFilter(array $array) { self::$defaultSortOrderFromFilter = $array; } /** * @param string $key optional key * @param array|string $params optional params to go with key */ public function apply(?string $key = null, $params = null): self { if (! $this->applyStart($key, $params)) { $sort = $this->getSql($key, $params); if (is_array($sort) && count($sort)) { $this->products = $this->products->sort($sort); } elseif ($sort) { $this->products = $this->products->sort($sort); } // @todo $this->applyEnd($key, $params); } return $this; } /** * if the key is default and you provide a param of IDs then it sort by params. * * @param string $key * @param array|string $params additional param for sql * * @return array|string */ public function getSql(?string $key = null, $params = null) { // if (BaseApplyer::DEFAULT_NAME === $key && self::$defaultSortOrderFromFilter) { // return self::$defaultSortOrderFromFilter; // } // @todo: make smarter... if (is_array($params) && count($params)) { return ArrayMethods::create_sort_statement_from_id_array($params, Product::class); } return parent::getSql($key, $params); } /** * you can add an extra sort (or two), based on filters (or other stuff.). */ public function getOptions(): array { return self::$defaultSortOrderFromFilter + parent::getOptions(); } } |