1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\FieldList; |
6
|
|
|
use SilverStripe\Forms\ReadonlyField; |
7
|
|
|
use SilverStripe\ORM\DataObject; |
8
|
|
|
use SilverStripe\Security\Permission; |
9
|
|
|
use SilverStripe\Security\Security; |
10
|
|
|
|
11
|
|
|
class FoxyCategory extends DataObject |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private static $db = [ |
|
|
|
|
17
|
|
|
'Title' => 'Varchar(255)', |
18
|
|
|
'Code' => 'Varchar(50)', |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private static $summary_fields = [ |
|
|
|
|
25
|
|
|
'Title' => 'Name', |
26
|
|
|
'Code' => 'Code', |
27
|
|
|
]; |
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private static $indexes = [ |
|
|
|
|
32
|
|
|
'Code' => [ |
33
|
|
|
'type' => 'unique', |
34
|
|
|
'columns' => ['Code'], |
35
|
|
|
], |
36
|
|
|
]; |
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private static $table_name = 'FoxyCategory'; |
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param bool $includerelations |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function fieldLabels($includerelations = true) |
47
|
|
|
{ |
48
|
|
|
$labels = parent::fieldLabels($includerelations); |
49
|
|
|
|
50
|
|
|
$labels['Title'] = _t(__CLASS__ . '.TitleLabel', 'Title'); |
51
|
|
|
$labels['Code'] = _t(__CLASS__ . '.CodeLabel', 'Code'); |
52
|
|
|
|
53
|
|
|
return $labels; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return FieldList|void |
58
|
|
|
*/ |
59
|
|
|
public function getCMSFields() |
60
|
|
|
{ |
61
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
62
|
|
|
if ($this->ID) { |
63
|
|
|
if ($this->Code == 'DEFAULT') { |
64
|
|
|
$fields->replaceField( |
65
|
|
|
'Title', |
66
|
|
|
ReadonlyField::create('Title') |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$fields->replaceField( |
70
|
|
|
'Code', |
71
|
|
|
ReadonlyField::create('Code') |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
return parent::getCMSFields(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return \SilverStripe\ORM\ValidationResult |
82
|
|
|
*/ |
83
|
|
|
public function validate() |
84
|
|
|
{ |
85
|
|
|
$result = parent::validate(); |
86
|
|
|
|
87
|
|
|
if (!$this->Code) { |
88
|
|
|
$result->addError( |
89
|
|
|
_t(__CLASS__ . '.CodeRequired', 'You must set a product price in the Foxy tab') |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (FoxyCategory::get()->filter('Code', $this->Code)->exclude('ID', $this->ID)->first()) { |
94
|
|
|
$result->addError( |
95
|
|
|
_t(__CLASS__ . '.CodeUnique', 'Code must be unique for each category.') |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $result; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
104
|
|
|
*/ |
105
|
|
|
public function requireDefaultRecords() |
106
|
|
|
{ |
107
|
|
|
parent::requireDefaultRecords(); |
108
|
|
|
|
109
|
|
|
$allCats = self::get(); |
110
|
|
|
if (!$allCats->count()) { |
111
|
|
|
$cat = new self(); |
112
|
|
|
$cat->Title = 'Default'; |
113
|
|
|
$cat->Code = 'DEFAULT'; |
|
|
|
|
114
|
|
|
$cat->write(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $member |
120
|
|
|
* @return bool|int|void |
121
|
|
|
*/ |
122
|
|
|
public function canCreate($member = null, $context = []) |
123
|
|
|
{ |
124
|
|
|
if (!$member) { |
125
|
|
|
$member = Security::getCurrentUser(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param $member |
133
|
|
|
* @return bool|int|void|null |
134
|
|
|
*/ |
135
|
|
|
public function canEdit($member = null, $context = []) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
if (!$member) { |
138
|
|
|
$member = Security::getCurrentUser(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param $member |
146
|
|
|
* @return bool|int|void |
147
|
|
|
*/ |
148
|
|
|
public function canDelete($member = null, $context = []) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
if (!$member) { |
151
|
|
|
$member = Security::getCurrentUser(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS'); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|