1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Discount\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\FieldList; |
6
|
|
|
use SilverStripe\ORM\DataObject; |
7
|
|
|
use SilverStripe\Security\Permission; |
8
|
|
|
|
9
|
|
|
class Discount extends DataObject |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private static $singular_name = 'Discount'; |
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private static $plural_name = 'Discounts'; |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private static $db = array( |
|
|
|
|
25
|
|
|
'Title' => 'Varchar(255)', |
26
|
|
|
'Quantity' => 'Int', |
27
|
|
|
'Percentage' => 'Int' |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private static $summary_fields = array( |
|
|
|
|
34
|
|
|
'Title', |
35
|
|
|
'Quantity', |
36
|
|
|
'DiscountPercentage' => [ |
37
|
|
|
'title' => 'Discount', |
38
|
|
|
], |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private static $table_name = 'FoxyDiscount'; |
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return FieldList |
48
|
|
|
*/ |
49
|
|
|
public function getCMSFields() |
50
|
|
|
{ |
51
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
52
|
|
|
$quantity = $fields->dataFieldByName('Quantity'); |
53
|
|
|
$quantity->setTitle('Quantity to trigger discount'); |
54
|
|
|
|
55
|
|
|
$percentage = $fields->dataFieldByName('Percentage'); |
56
|
|
|
$percentage->setTitle('Percent discount'); |
57
|
|
|
}); |
58
|
|
|
|
59
|
|
|
return parent::getCMSFields(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getDiscountPercentage() |
66
|
|
|
{ |
67
|
|
|
return "{$this->Percentage}%"; |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Basic permissions, defaults to page perms where possible. |
72
|
|
|
* |
73
|
|
|
* @param \SilverStripe\Security\Member|null $member |
74
|
|
|
* @return boolean |
75
|
|
|
*/ |
76
|
|
|
public function canView($member = null) |
77
|
|
|
{ |
78
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
79
|
|
|
if ($extended !== null) { |
80
|
|
|
return $extended; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return Permission::check('CMS_ACCESS', 'any', $member); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Basic permissions, defaults to page perms where possible. |
88
|
|
|
* |
89
|
|
|
* @param \SilverStripe\Security\Member|null $member |
90
|
|
|
* |
91
|
|
|
* @return boolean |
92
|
|
|
*/ |
93
|
|
|
public function canEdit($member = null) |
94
|
|
|
{ |
95
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
96
|
|
|
if ($extended !== null) { |
97
|
|
|
return $extended; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return Permission::check('CMS_ACCESS', 'any', $member); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Basic permissions, defaults to page perms where possible. |
105
|
|
|
* |
106
|
|
|
* Uses archive not delete so that current stage is respected i.e if a |
107
|
|
|
* element is not published, then it can be deleted by someone who doesn't |
108
|
|
|
* have publishing permissions. |
109
|
|
|
* |
110
|
|
|
* @param \SilverStripe\Security\Member|null $member |
111
|
|
|
* |
112
|
|
|
* @return boolean |
113
|
|
|
*/ |
114
|
|
|
public function canDelete($member = null) |
115
|
|
|
{ |
116
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
117
|
|
|
if ($extended !== null) { |
118
|
|
|
return $extended; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return Permission::check('CMS_ACCESS', 'any', $member); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Basic permissions, defaults to page perms where possible. |
126
|
|
|
* |
127
|
|
|
* @param \SilverStripe\Security\Member|null $member |
128
|
|
|
* @param array $context |
129
|
|
|
* |
130
|
|
|
* @return boolean |
131
|
|
|
*/ |
132
|
|
|
public function canCreate($member = null, $context = array()) |
133
|
|
|
{ |
134
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
135
|
|
|
if ($extended !== null) { |
136
|
|
|
return $extended; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return Permission::check('CMS_ACCESS', 'any', $member); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|