Source of file FacetedCategory.php
Size: 4,881 Bytes - Last Modified: 2021-12-23T10:03:27+00:00
/var/www/docs.ssmods.com/process/src/code/helpers/FacetedCategory.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 | <?php /** * This extension can be applied to ProductCategory * to allow categories to have facets as well. * * NOTE: You could apply this to either ProductCategory * or ProductCategory_Controller. I tend to use the model b/c * that will also cover some other cases like where you * might list subcategory products on the parent category page. * In such a case those products would be filtered as well. * * @author Mark Guinn <mark@adaircreative.com> * @date 10.22.2013 * @package shop_search * @subpackage helpers */ class FacetedCategory extends SiteTreeExtension { private static $db = array( 'DisabledFacets' => 'Text', // This will be a comma-delimited list of facets that aren't used for a given category ); /** @var array - facet definition - see ShopSearch and/or docs/en/Facets.md for format */ private static $facets = array(); /** @var bool - if true there will be a tab in the cms to disable some or all defined facets */ private static $show_disabled_facets_tab = true; /** @var string - which method should we use to get the child products for FilteredProducts */ private static $products_method = 'ProductsShowable'; /** @var bool - automatically create facets for static attributes */ private static $auto_facet_attributes = false; /** * @param FieldList $fields */ public function updateCMSFields(FieldList $fields) { if (Config::inst()->get('FacetedCategory', 'show_disabled_facets_tab')) { $spec = FacetHelper::inst()->expandFacetSpec($this->getFacetSpec()); $facets = array(); foreach ($spec as $f => $v) { $facets[$f] = $v['Label']; } $fields->addFieldToTab('Root.Facets', CheckboxSetField::create('DisabledFacets', "Don't show the following facets for this category:", $facets)); } } /** * @return Controller */ protected function getController() { return ($this->owner instanceof Controller) ? $this->owner : Controller::curr(); } /** * @return array */ protected function getFacetSpec() { $spec = Config::inst()->get('FacetedCategory', 'facets'); return (empty($spec) || !is_array($spec)) ? array() : $spec; } /** * @return array */ protected function getFilters() { $qs_f = Config::inst()->get('ShopSearch', 'qs_filters'); if (!$qs_f) { return array(); } $request = $this->getController()->getRequest(); $filters = $request->requestVar($qs_f); if (empty($filters) || !is_array($filters)) { return array(); } return FacetHelper::inst()->scrubFilters($filters); } /** * @param bool $recursive * @return mixed */ public function FilteredProducts($recursive=true) { if (!isset($this->_filteredProducts)) { $fn = Config::inst()->get('FacetedCategory', 'products_method'); if (empty($fn)) { $fn = 'ProductsShowable'; } $this->_filteredProducts = $this->owner->$fn($recursive); $this->_filteredProducts = FacetHelper::inst()->addFiltersToDataList($this->_filteredProducts, $this->getFilters()); } return $this->_filteredProducts; } protected $_filteredProducts; /** * @return array */ public function getDisabledFacetsArray() { if (empty($this->owner->DisabledFacets)) { return array(); } return explode(',', $this->owner->DisabledFacets); } /** * @return ArrayList */ public function Facets() { $spec = $this->getFacetSpec(); if (empty($spec)) { return new ArrayList; } // remove any disabled facets foreach ($this->getDisabledFacetsArray() as $disabled) { if (isset($spec[$disabled])) { unset($spec[$disabled]); } } $request = $this->getController()->getRequest(); $baseLink = $request->getURL(false); $filters = $this->getFilters(); $baseParams = array_merge($request->requestVars(), array()); unset($baseParams['url']); $products = $this->owner->hasMethod('ProductsForFaceting') ? $this->owner->ProductsForFaceting() : $this->FilteredProducts(); $facets = FacetHelper::inst()->buildFacets($products, $spec, (bool)Config::inst()->get('FacetedCategory', 'auto_facet_attributes')); $facets = FacetHelper::inst()->transformHierarchies($facets); $facets = FacetHelper::inst()->updateFacetState($facets, $filters); $facets = FacetHelper::inst()->insertFacetLinks($facets, $baseParams, $baseLink); return $facets; } } |