Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class ProductCategory extends Page {
- private static $icon = 'mysite/images/box.png';
- private static $db = array(
- 'Summary' => 'HTMLText',
- 'InSidebar' => 'Boolean(0)',
- 'CallToAction' => 'HTMLText',
- 'ShowInHomeMenu' => 'Boolean(0)'
- );
- private static $defaults = array();
- private static $description = '';
- private static $has_one = array(
- 'Image' => 'Image'
- );
- private static $many_many = array(
- 'ProductPages' => 'ProductPage'
- );
- private static $allowed_children = array(
- 'ProductCategory',
- 'ProductPage'
- );
- public function getSettingsFields(){
- $fields = parent::getSettingsFields();
- $fields->addFieldToTab('Root.Settings', new CheckboxField('InSidebar'));
- $fields->addFieldToTab('Root.Settings', new CheckboxField('ShowInHomeMenu', 'Show Product Category In Home Dropdown Menu'));
- return $fields;
- }
- public function getCMSFields() {
- $fields = parent::getCMSFields();
- $fields->addFieldToTab('Root.Main', new UploadField('Image', 'Product Category Thumbnail'), 'Content');
- $fields->addFieldToTab('Root.Main', $summary = new HtmlEditorField('Summary', 'Summary (optional)'), 'Content');
- $summary->setRows(10);
- $summary->setRightTitle('Summary to be displayed on the relative holder page. If no summary is present then the first paragraph of the content will be used.');
- /* -----------------------------------------
- * Call To Action
- ------------------------------------------*/
- $fields->addFieldToTab('Root.CallToAction', new HtmlEditorField('CallToAction', 'Content'));
- return $fields;
- }
- /**
- * @return DataList
- */
- public function getSidebarCategoryItem(){
- return ProductCategory::get()->Filter(array('ParentID' => 0, 'InSidebar' => 1));
- }
- /**
- * @return ArrayList
- */
- public function getProductChildren(){
- $set = new ArrayList;
- foreach($this->Children() as $obj) $set->push($obj);
- foreach(ProductPage::get()->filter('ProductCategory.ID', $this->ID) as $obj) $set->push($obj);
- return $set;
- }
- }
- class ProductCategory_Controller extends Page_Controller {
- private static $allowed_actions = array('Filter');
- /**
- * @return Form
- */
- public function Filter() {
- $capacity = new DropdownField('Capacity', 'Capacity', array(
- 0 => 'Any',
- 1 => '0 - 300g',
- 2 => '301g - 1kg',
- 3 => '1.1 - 10kg',
- 4 => '10.1 - 100kg',
- 5 => '101 - 500kg',
- 6 => '501 - 1000kg',
- 7 => '1000+'
- ));
- $capacity->addExtraClass('form-control');
- $graduation = new DropdownField('Graduation', 'Graduation', array(
- 0 => 'Any',
- 1 => 'Less than 1g',
- 2 => '1 - 5g',
- 3 => '6 - 50g',
- 4 => '51g - 1kg',
- 5 => '1.1 - 5kg',
- 6 => '5.1kg'
- ));
- $graduation->addExtraClass('form-control');
- $fields = new FieldList(
- $capacity,
- $graduation
- );
- $action = new FormAction('doFilterBy', 'Filter');
- $action->addExtraClass('btn btn-secondary btn-block');
- $actions = new FieldList(
- $action
- );
- $validator = new RequiredFields(
- );
- return new Form($this, 'Filter', $fields, $actions, $validator);
- }
- /**
- * @param $data
- * @param $form
- * @return HTMLText
- */
- function doFilterBy($data, $form) {
- $c = $data['Capacity'];
- $g = $data['Graduation'];
- $filter = array();
- if($c>0){
- $filter['ProductPage.Capacity'] = $c;
- }
- if($g>0){
- $filter['ProductPage.Graduation'] = $g;
- }
- $products = ProductPage::get()->filter($filter);
- return $this->customise(array('AllChildren' => $products, 'Title' => 'Filtered Results', 'Content' => ''))->renderWith(array('ProductCategory', 'Page'));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement