1 <?php
2
3 class CatalogFilter extends DataObject {
4 static $db = array(
5 'Title' => 'Varchar(250)',
6 'TechTitle' => 'Varchar(50)',
7 'FilterField' => 'Varchar(50)',
8 'Important' => 'Boolean',
9 'Type' => "Enum('text, bool, boolgroup, slider, list, multiselect')",
10 'Sort' => 'Int',
11 'GroupTitle' => 'Varchar(50)',
12 'ShowDefault' => 'Boolean',
13 'DefaultValue' => 'Varchar(255)',
14 'Unit' => 'Varchar',
15 );
16 static $has_one = array(
17 'ParentCatalog' => 'Catalog'
18 );
19
20 static $belongs_many_many = array(
21 'Catalogs' => 'Catalog'
22 );
23
24 static $defaults = array(
25 'FilterField' => '',
26 'Type' => 'text',
27 );
28
29 function fieldLabels($relations = true) {
30 $labels = parent::fieldLabels($relations);
31 $labels['LocalType'] = $labels['Type'];
32 $labels['LocalFilterField'] = $labels['FilterField'];
33 return $labels;
34 }
35
36
37 static $default_sort = 'Important DESC, Sort ASC, Title ASC';
38
39 static $searchable_fields = array('Title', 'FilterField', 'Important', 'Type', 'Sort');
40
41 static $summary_fields = array('Title', 'LocalFilterField', 'Important', 'LocalType', 'Sort');
42
43 static function getFilterTypes() {
44 $types = array();
45 foreach(singleton('CatalogFilter')->dbObject('Type')->enumValues() as $val) {
46 $types[$val] = _t('CatalogFilter.Type_' . $val);
47 }
48 return $types;
49 }
50
51 function getLocalType() {
52 $types = self::getFilterTypes();
53 return $types[$this->Type];
54 }
55
56 function getLocalFilterField() {
57 $fields = $this->getPossibleFilterFields();
58 if (isset($fields[$this->FilterField])) {
59 return $fields[$this->FilterField];
60 }
61 return $this->FilterField;
62 }
63
64 function populateDefaults() {
65 parent::populateDefaults();
66 $this->TechTitle = 'filter' . (DB::query("select max(ID) from CatalogFilter")->value() + 1);
67 $this->Sort = $max = DB::query("select max(Sort) from CatalogFilter")->value() + 1;
68 }
69
70 function getCMSFields() {
71 $fields = parent::getCMSFields();
72 $fields->replaceField('Type', new DropdownField('Type', $this->fieldLabel('Type'), self::getFilterTypes(), $this->Type));
73 $fields->replaceField('ParentCatalogID', new HiddenField('ParentCatalogID', $this->fieldLabel('ParentCatalog')));
74 $fields->removeByName('Catalogs');
75
76
77 $ffTitle = $this->fieldLabel('FilterField');
78 if (!$this->ID) {
79 $ffTitle .= _t('CatalogFilter.NeedSaveToViewAll');
80 }
81 $fields->replaceField('FilterField', new DropdownField('FilterField', $ffTitle, $this->getPossibleFilterFields(), $this->FilterField));
82 return $fields;
83 }
84
85 function getPossibleFilterFields() {
86 $filterFields = Product::get_possible_filter_fields();
87 if ($this->ParentCatalogID && $this->ParentCatalog()) {
88 if ($catalogParams = $this->ParentCatalog()->catalogParams()) {
89 foreach($catalogParams as $catalogParam) {
90 $filterFields[$catalogParam->TechTitle] = $catalogParam->Title;
91 }
92 }
93 } else {
94
95 if ($catalogParams = DataObject::get('ProductParam', "ParentCatalogID = 0")) {
96 foreach($catalogParams as $catalogParam) {
97 $filterFields[$catalogParam->TechTitle] = $catalogParam->Title;
98 }
99 }
100 }
101 return $filterFields;
102 }
103 }
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-