1 <?php
2
3 class ProductParam extends DataObject {
4 static $db = array(
5 'Title' => 'Varchar(250)',
6 'TechTitle' => 'Varchar(50)',
7 'Type' => "Enum('text, number, bool')",
8 'ShowInList' => 'Boolean',
9 'ShowInView' => 'Boolean',
10 'Sort' => 'Int',
11 'ShowDefault' => 'Boolean',
12
13 'PossibleValues' => 'Text',
14 'MultiValues' => 'Boolean',
15 'ParamForVariation' => 'Boolean',
16 );
17
18 static $has_one = array(
19 'ParentCatalog' => 'Catalog'
20 );
21
22 static $belongs_many_many = array(
23 'Catalogs' => 'Catalog'
24 );
25
26 static $defaults = array(
27 'Type' => 'text',
28 'ShowInView' => 1,
29 );
30
31 function fieldLabels($relations = true) {
32 $labels = parent::fieldLabels($relations);
33 $labels['LocalType'] = $labels['Type'];
34 return $labels;
35 }
36
37 static $default_sort = 'Sort ASC, Title ASC';
38
39 static $searchable_fields = array('Title', 'TechTitle', 'Type', 'ShowDefault', 'Sort');
40
41 static $summary_fields = array('Title', 'TechTitle', 'LocalType', 'ShowDefault', 'Sort');
42
43 static function getParamTypes() {
44 $types = array();
45 foreach(singleton('ProductParam')->dbObject('Type')->enumValues() as $val) {
46 $types[$val] = _t('ProductParam.Type_' . $val);
47 }
48 return $types;
49 }
50
51
52 static function group_values_list($list) {
53 if ($list) {
54 $rs = array();
55 foreach($list as $value) {
56 if (isset($rs[$value->ProductParamID])) {
57 $rs[$value->ProductParamID]->Value .= ", {$value->Value}";
58 } else {
59 $rs[$value->ProductParamID] = $value;
60 }
61 }
62 return new DataObjectSet($rs);
63 }
64 return false;
65 }
66
67 68 69 70 71 72
73
74 function getUnit() {
75 return 'м';
76 }
77
78 function getLocalType() {
79 $types = self::getParamTypes();
80 return $types[$this->Type];
81 }
82
83 function populateDefaults() {
84 parent::populateDefaults();
85 $this->TechTitle = 'param' . (DB::query("select max(ID) from ProductParam")->value() + 1);
86 $this->Sort = $max = DB::query("select max(Sort) from ProductParam")->value() + 1;
87 }
88
89 function getCMSFields() {
90 $fields = parent::getCMSFields();
91 $fields->replaceField('Type', new DropdownField('Type', $this->fieldLabel('Type'), self::getParamTypes(), $this->Type));
92 $fields->replaceField('ParentCatalogID', new HiddenField('ParentCatalogID', $this->fieldLabel('ParentCatalog')));
93 $fields->removeByName('Catalogs');
94 if (!Catalog::$use_variations) {
95 $fields->removeByName('ParamForVariation');
96 }
97 return $fields;
98 }
99
100 function onBeforeDelete() {
101 parent::onBeforeDelete();
102
103 if ($oldParamValues = DataObject::get('ProductParamValue', "ProductParamID = {$this->ID}")) {
104 foreach($oldParamValues as $oldParamValue)
105 $oldParamValue->delete();
106 }
107 }
108
109 function PossibleValuesList() {
110 if (trim($this->PossibleValues)) {
111 $values = array();
112 foreach(explode("\n", $this->PossibleValues) as $value) {
113 if ($val = trim($value)) {
114 $values[$val] = $val;
115 }
116 }
117 return $values;
118 }
119 return false;
120 }
121 }
[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.
-