Advertisement
Guest User

Untitled

a guest
May 18th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.19 KB | None | 0 0
  1. <?php
  2.  
  3. class ProductCategory extends Page {
  4.  
  5.     private static $icon = 'mysite/images/box.png';
  6.  
  7.     private static $db = array(
  8.         'Summary' => 'HTMLText',
  9.         'InSidebar' => 'Boolean(0)',
  10.         'CallToAction' => 'HTMLText',
  11.         'ShowInHomeMenu' => 'Boolean(0)'
  12.     );
  13.  
  14.     private static $defaults = array();
  15.  
  16.     private static $description = '';
  17.  
  18.     private static $has_one = array(
  19.         'Image' => 'Image'
  20.     );
  21.  
  22.     private static $many_many = array(
  23.         'ProductPages' => 'ProductPage'
  24.     );
  25.  
  26.     private static $allowed_children = array(
  27.         'ProductCategory',
  28.         'ProductPage'
  29.     );
  30.  
  31.     public function getSettingsFields(){
  32.         $fields = parent::getSettingsFields();
  33.         $fields->addFieldToTab('Root.Settings', new CheckboxField('InSidebar'));
  34.         $fields->addFieldToTab('Root.Settings', new CheckboxField('ShowInHomeMenu', 'Show Product Category In Home Dropdown Menu'));
  35.         return $fields;
  36.     }
  37.  
  38.     public function getCMSFields() {
  39.  
  40.         $fields = parent::getCMSFields();
  41.  
  42.         $fields->addFieldToTab('Root.Main', new UploadField('Image', 'Product Category Thumbnail'), 'Content');
  43.         $fields->addFieldToTab('Root.Main', $summary = new HtmlEditorField('Summary', 'Summary (optional)'), 'Content');
  44.         $summary->setRows(10);
  45.         $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.');
  46.  
  47.         /* -----------------------------------------
  48.          * Call To Action
  49.         ------------------------------------------*/
  50.  
  51.         $fields->addFieldToTab('Root.CallToAction', new HtmlEditorField('CallToAction', 'Content'));
  52.  
  53.         return $fields;
  54.  
  55.     }
  56.  
  57.     /**
  58.      * @return DataList
  59.      */
  60.     public function getSidebarCategoryItem(){
  61.         return ProductCategory::get()->Filter(array('ParentID' => 0, 'InSidebar' => 1));
  62.     }
  63.  
  64.     /**
  65.      * @return ArrayList
  66.      */
  67.     public function getProductChildren(){
  68.         $set = new ArrayList;
  69.         foreach($this->Children() as $obj) $set->push($obj);
  70.         foreach(ProductPage::get()->filter('ProductCategory.ID', $this->ID) as $obj) $set->push($obj);
  71.         return $set;
  72.     }
  73.  
  74. }
  75.  
  76. class ProductCategory_Controller extends Page_Controller {
  77.  
  78.     private static $allowed_actions = array('Filter');
  79.  
  80.     /**
  81.      * @return Form
  82.      */
  83.     public function Filter() {
  84.  
  85.         $capacity = new DropdownField('Capacity', 'Capacity', array(
  86.             0 => 'Any',
  87.             1 => '0 - 300g',
  88.             2 => '301g - 1kg',
  89.             3 => '1.1 - 10kg',
  90.             4 => '10.1 - 100kg',
  91.             5 => '101 - 500kg',
  92.             6 => '501 - 1000kg',
  93.             7 => '1000+'
  94.         ));
  95.         $capacity->addExtraClass('form-control');
  96.  
  97.         $graduation = new DropdownField('Graduation', 'Graduation', array(
  98.             0 => 'Any',
  99.             1 => 'Less than 1g',
  100.             2 => '1 - 5g',
  101.             3 => '6 - 50g',
  102.             4 => '51g - 1kg',
  103.             5 => '1.1 - 5kg',
  104.             6 => '5.1kg'
  105.         ));
  106.         $graduation->addExtraClass('form-control');
  107.  
  108.         $fields = new FieldList(
  109.             $capacity,
  110.             $graduation
  111.         );
  112.  
  113.         $action = new FormAction('doFilterBy', 'Filter');
  114.         $action->addExtraClass('btn btn-secondary btn-block');
  115.  
  116.         $actions = new FieldList(
  117.             $action
  118.         );
  119.  
  120.         $validator = new RequiredFields(
  121.         );
  122.  
  123.         return new Form($this, 'Filter', $fields, $actions, $validator);
  124.  
  125.     }
  126.  
  127.     /**
  128.      * @param $data
  129.      * @param $form
  130.      * @return HTMLText
  131.      */
  132.     function doFilterBy($data, $form) {
  133.  
  134.         $c = $data['Capacity'];
  135.         $g = $data['Graduation'];
  136.         $filter = array();
  137.  
  138.         if($c>0){
  139.             $filter['ProductPage.Capacity'] = $c;
  140.         }
  141.         if($g>0){
  142.             $filter['ProductPage.Graduation'] = $g;
  143.         }
  144.  
  145.         $products = ProductPage::get()->filter($filter);
  146.  
  147.         return $this->customise(array('AllChildren' => $products, 'Title' => 'Filtered Results', 'Content' => ''))->renderWith(array('ProductCategory', 'Page'));
  148.  
  149.     }
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement