Source of file ProductCategory.php
Size: 6,823 Bytes - Last Modified: 2021-12-23T10:48:01+00:00
/var/www/docs.ssmods.com/process/src/code/ProductCategory.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 | <?php /** * Represents a Product category, Products can be added to many categories and they * can have a ProductCategory as a parent in the site tree. */ class ProductCategory extends Page { private static $singular_name = 'Product Category'; private static $plural_name = 'Product Categories'; /** * Many many relations for a ProductCategory * * @var Array */ private static $many_many = array( 'Products' => 'Product' ); private static $many_many_extraFields = array( 'Products' => array( 'ProductOrder' => 'Int' ) ); /** * Summary fields for viewing categories in the CMS * * @var Array */ private static $summary_fields = array( 'Title' => 'Name', 'MenuTitle' => 'Menu Title' ); public function isSection() { $current = Controller::curr(); $request = $current->getRequest(); $url = $request->getURL(); if (stristr($url, 'product/')) { $params = $request->allParams(); $productID = $params['ID']; $product = Product::get() ->where("\"URLSegment\" = '{$productID}'") ->first(); if ($product && $product->exists()) { return $this->isCurrent() || in_array($product->ID, $this->Products()->column('ID')); } } return parent::isSection(); } public function ListboxCrumb($maxDepth = 20, $unlinked = false, $stopAtPageType = false, $showHidden = false) { $page = $this; $pages = array(); $crumb = ''; while( $page && (!$maxDepth || count($pages) < $maxDepth) && (!$stopAtPageType || $page->ClassName != $stopAtPageType) ) { if($showHidden || $page->ShowInMenus || ($page->ID == $this->ID)) { $pages[] = $page; } $page = $page->Parent; } $i = 1; foreach ($pages as $page) { $crumb .= $page->getMenuTitle(); if ($i++ < count($pages)) { $crumb .= ' > '; } } return $crumb; } } /** * Controller to display a ProductCategory and retrieve its Products. */ class ProductCategory_Controller extends Page_Controller { /** * Set number of products per page displayed in ProductCategory pages * * @var Int */ public static $products_per_page = 12; /** * Set how the products are ordered on ProductCategory pages * * @see ProductCategory_Controller::Products() * @var String Suitable for inserting in ORDER BY clause */ // public static $products_ordered_by = "\"ProductCategory_Products\".\"ProductOrder\" DESC"; public static $products_ordered_by = "\"SiteTree\".\"ParentID\" ASC, \"SiteTree\".\"Sort\" ASC"; /** * Include some CSS. * * @see Page_Controller::init() */ function init() { parent::init(); Requirements::css('swipestripe/css/Shop.css'); } /** * Get Products that have this ProductCategory set or have this ProductCategory as a parent in site tree. * Supports pagination. * * @see Page_Controller::Products() * @return FieldList */ public function Products() { $limit = self::$products_per_page; $orderBy = self::$products_ordered_by; $cats = array($this->ID); foreach ($this->Children() as $child) { if ($child instanceof ProductCategory) { $cats[] = $child->ID; } } $in = "('" . implode("','", $cats) . "')"; $products = Product::get() ->where("\"ProductCategory_Products\".\"ProductCategoryID\" IN $in OR \"ParentID\" IN $in") ->sort($orderBy) ->leftJoin('ProductCategory_Products', "\"ProductCategory_Products\".\"ProductID\" = \"SiteTree\".\"ID\""); $this->extend('updateCategoryProducts', $products); $list = PaginatedList::create($products, $this->request) ->setPageLength($limit); return $list; } } class ProductCategory_Products extends DataObject { private static $db = array( 'ProductOrder' => 'Int' ); private static $has_one = array( 'ProductCategory' => 'ProductCategory', 'Product' => 'Product' ); } class ProductCategory_Extension extends DataExtension { /** * Belongs many many relations for Product * * @var Array */ private static $belongs_many_many = array( 'ProductCategories' => 'ProductCategory' ); private static $searchable_fields = array( 'Category' => array( 'field' => 'TextField', 'filter' => 'ProductCategory_SearchFilter', 'title' => 'Category' ) ); private static $casting = array( 'Category' => 'Varchar', ); public function onBeforeWrite() { //If the ParentID is set to a ProductCategory, select that category for this Product $parent = $this->owner->getParent(); if ($parent && $parent instanceof ProductCategory) { $productCategories = $this->owner->ProductCategories(); if ($this->owner->isInDB() && !in_array($parent->ID, array_keys($productCategories->map()->toArray()))) { $productCategories->add($parent); } } } public function updateCMSFields(FieldList $fields) { $categories = ProductCategory::get()->map('ID', 'ListboxCrumb')->toArray(); arsort($categories); $fields->addFieldToTab( 'Root.Main', ListboxField::create('ProductCategories', 'Categories') ->setMultiple(true) ->setSource($categories) ->setAttribute('data-placeholder', 'Add categories'), 'Content' ); return $fields; } } class ProductCategory_CMSExtension extends Extension { function updateSearchForm($form) { $fields = $form->Fields(); $cats = ProductCategory::get()->map()->toArray(); $fields->push(DropdownField::create('q[Category]', 'Category', $cats) ->setHasEmptyDefault(true) ); $form->loadDataFrom($this->owner->request->getVars()); $form->setFields($fields); } } /** * Search filter for {@link Product} categories, filtering search results for * certain {@link ProductCategory}s in the CMS. */ class ProductCategory_SearchFilter extends SearchFilter { /** * Apply filter query SQL to a search query * * @see SearchFilter::apply() * @return SQLQuery */ public function apply(DataQuery $query) { $this->model = $query->applyRelation($this->relation); $value = $this->getValue(); if ($value) { $query->innerJoin( 'ProductCategory_Products', "\"ProductCategory_Products\".\"ProductID\" = \"SiteTree\".\"ID\"" ); $query->innerJoin( 'SiteTree_Live', "\"SiteTree_Live\".\"ID\" = \"ProductCategory_Products\".\"ProductCategoryID\"" ); $query->where("\"SiteTree_Live\".\"ID\" LIKE '%" . Convert::raw2sql($value) . "%'"); } return $query; } /** * Determine whether the filter should be applied, depending on the * value of the field being passed * * @see SearchFilter::isEmpty() * @return Boolean */ public function isEmpty() { return $this->getValue() == null || $this->getValue() == ''; } protected function applyOne(DataQuery $query) { SS_Log::log(new Exception(print_r($this->getValue(), true)), SS_Log::NOTICE); return; } protected function excludeOne(DataQuery $query) { return; } } |