|
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
|
|
|
|
|
9
|
|
|
class FoxyCategory extends DataObject |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
private static $db = [ |
|
|
|
|
|
|
15
|
|
|
'Title' => 'Varchar(255)', |
|
16
|
|
|
'Code' => 'Varchar(50)', |
|
17
|
|
|
]; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private static $summary_fields = [ |
|
|
|
|
|
|
23
|
|
|
'Title' => 'Name', |
|
24
|
|
|
'Code' => 'Code', |
|
25
|
|
|
]; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private static $indexes = [ |
|
|
|
|
|
|
30
|
|
|
'Code' => [ |
|
31
|
|
|
'type' => 'unique', |
|
32
|
|
|
'columns' => ['Code'], |
|
33
|
|
|
], |
|
34
|
|
|
]; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private static $table_name = 'FoxyCategory'; |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param bool $includerelations |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
|
|
public function fieldLabels($includerelations = true) |
|
45
|
|
|
{ |
|
46
|
|
|
$labels = parent::fieldLabels($includerelations); |
|
47
|
|
|
|
|
48
|
|
|
$labels['Title'] = _t(__CLASS__ . '.TitleLabel', 'Title'); |
|
49
|
|
|
$labels['Code'] = _t(__CLASS__ . '.CodeLabel', 'Code'); |
|
50
|
|
|
|
|
51
|
|
|
return $labels; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return FieldList|void |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getCMSFields() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
60
|
|
|
if ($this->ID) { |
|
61
|
|
|
if ($this->Code == 'DEFAULT') { |
|
62
|
|
|
$fields->replaceField( |
|
63
|
|
|
'Title', |
|
64
|
|
|
ReadonlyField::create('Title') |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
$fields->replaceField( |
|
68
|
|
|
'Code', |
|
69
|
|
|
ReadonlyField::create('Code') |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
}); |
|
74
|
|
|
|
|
75
|
|
|
return parent::getCMSFields(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return \SilverStripe\ORM\ValidationResult |
|
80
|
|
|
*/ |
|
81
|
|
|
public function validate() |
|
82
|
|
|
{ |
|
83
|
|
|
$result = parent::validate(); |
|
84
|
|
|
|
|
85
|
|
|
if (FoxyCategory::get()->filter('Code', $this->Code)->exclude('ID', $this->ID)->first()) { |
|
86
|
|
|
$result->addError('Code must be unique for each category.'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $result; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
|
94
|
|
|
*/ |
|
95
|
|
|
public function requireDefaultRecords() |
|
96
|
|
|
{ |
|
97
|
|
|
parent::requireDefaultRecords(); |
|
98
|
|
|
|
|
99
|
|
|
$allCats = self::get(); |
|
100
|
|
|
if (!$allCats->count()) { |
|
101
|
|
|
$cat = new self(); |
|
102
|
|
|
$cat->Title = 'Default'; |
|
103
|
|
|
$cat->Code = 'DEFAULT'; |
|
|
|
|
|
|
104
|
|
|
$cat->write(); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|