|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\Wishlist\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Dynamic\AdditionalFormFields\Form\CancelFormAction; |
|
6
|
|
|
use Dynamic\ManageableDataObject\Interfaces\ManageableDataObjectInterface; |
|
7
|
|
|
use Dynamic\ViewableDataObject\VDOInterfaces\ViewableDataObjectInterface; |
|
8
|
|
|
use SilverStripe\Control\Controller; |
|
9
|
|
|
use SilverStripe\Forms\FieldList; |
|
10
|
|
|
use SilverStripe\Forms\FormAction; |
|
11
|
|
|
use SilverStripe\Forms\RequiredFields; |
|
12
|
|
|
use SilverStripe\ORM\DataObject; |
|
13
|
|
|
use SilverStripe\Security\Member; |
|
14
|
|
|
use SilverStripe\Security\Permission; |
|
15
|
|
|
use SilverStripe\Security\PermissionProvider; |
|
16
|
|
|
use SilverStripe\Security\Security; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class ProductWishList |
|
20
|
|
|
* |
|
21
|
|
|
* @property string $Title |
|
22
|
|
|
* @property bool $Private |
|
23
|
|
|
* |
|
24
|
|
|
* @property int $MemberID |
|
25
|
|
|
* @method Member $Member() |
|
26
|
|
|
* |
|
27
|
|
|
* @mixin \Dynamic\ViewableDataObject\Extensions\ViewableDataObject |
|
28
|
|
|
* @mixin \Dynamic\ManageableDataObject\Extensions\ManageableObjectExtension |
|
29
|
|
|
*/ |
|
30
|
|
|
class ProductWishList extends DataObject implements PermissionProvider, ViewableDataObjectInterface, ManageableDataObjectInterface |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private static $table_name = 'ProductWishList'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
private static $db = [ |
|
42
|
|
|
'Title' => 'Varchar(100)', |
|
43
|
|
|
'Private' => 'Boolean', |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
private static $has_one = [ |
|
50
|
|
|
'Member' => Member::class, |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function onBeforeWrite() |
|
57
|
|
|
{ |
|
58
|
2 |
|
parent::onBeforeWrite(); |
|
59
|
|
|
|
|
60
|
2 |
|
if (!$this->MemberID > 0) { |
|
61
|
2 |
|
if (Security::getCurrentUser()) { |
|
62
|
|
|
$this->MemberID = Security::getCurrentUser()->ID; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param array|null $params |
|
69
|
|
|
* |
|
70
|
|
|
* @return FieldList |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function getFrontEndFields($params = null) |
|
73
|
|
|
{ |
|
74
|
1 |
|
$fields = parent::getFrontEndFields($params); |
|
75
|
|
|
|
|
76
|
1 |
|
$fields->removeByName([ |
|
77
|
1 |
|
'MemberID', |
|
78
|
|
|
'MenuTitle', |
|
79
|
|
|
'URLSegment', |
|
80
|
|
|
'MetaTitle', |
|
81
|
|
|
'MetaDescription', |
|
82
|
|
|
]); |
|
83
|
|
|
|
|
84
|
1 |
|
return $fields; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param bool $showCancel |
|
89
|
|
|
* |
|
90
|
|
|
* @return FieldList |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function getFrontEndActions($showCancel = false) |
|
93
|
|
|
{ |
|
94
|
1 |
|
$processTitle = ($this->getIsEditing()) ? "Update {$this->i18n_singular_name()}" : "Create {$this->i18n_singular_name()}"; |
|
95
|
1 |
|
$actions = FieldList::create( |
|
96
|
1 |
|
FormAction::create('doSaveObject') |
|
97
|
1 |
|
->setTitle($processTitle) |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
1 |
|
if ($showCancel === true) { |
|
101
|
1 |
|
$actions->insertBefore('action_doSaveObject', CancelFormAction::create('Cancel')); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
$this->extend('updateFrontEndActions', $actions); |
|
105
|
|
|
|
|
106
|
1 |
|
return $actions; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return RequiredFields |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function getFrontEndRequiredFields() |
|
113
|
|
|
{ |
|
114
|
1 |
|
$fields = RequiredFields::create([ |
|
115
|
1 |
|
'Title', |
|
116
|
|
|
]); |
|
117
|
|
|
|
|
118
|
1 |
|
$this->extend('updateFrontEndRequiredFields', $fields); |
|
119
|
|
|
|
|
120
|
1 |
|
return $fields; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return bool |
|
125
|
|
|
*/ |
|
126
|
1 |
|
public function getIsEditing() |
|
127
|
|
|
{ |
|
128
|
1 |
|
$params = Controller::curr()->getRequest()->latestParams(); |
|
129
|
|
|
|
|
130
|
1 |
|
return isset($params['Action']) && $params['Action'] == 'edit' && isset($params['ID']); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* set ParentPage for ViewableDataobject |
|
135
|
|
|
* |
|
136
|
|
|
* @return \SilverStripe\CMS\Model\SiteTree |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getParentPage() |
|
139
|
|
|
{ |
|
140
|
|
|
$class = $this->config()->get('listing_page_class'); |
|
141
|
|
|
return $class::get()->first(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return String|bool |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getUpdateLink() |
|
148
|
|
|
{ |
|
149
|
|
|
return ($this->ID > 0) ? Controller::join_links($this->getParentPage()->Link(), 'edit', $this->ID) : false; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* set ViewAction for ViewableDataobject |
|
154
|
|
|
* |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
1 |
|
public function getViewAction() |
|
158
|
|
|
{ |
|
159
|
1 |
|
return 'view'; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return array |
|
164
|
|
|
*/ |
|
165
|
1 |
|
public function providePermissions() |
|
166
|
|
|
{ |
|
167
|
|
|
return [ |
|
168
|
1 |
|
'WishList_EDIT' => [ |
|
169
|
|
|
'name' => 'Edit a Wish List', |
|
170
|
|
|
'category' => 'Wish List Permissions', |
|
171
|
|
|
], |
|
172
|
|
|
'WishList_DELETE' => [ |
|
173
|
|
|
'name' => 'Delete a Wish List', |
|
174
|
|
|
'category' => 'Wish List Permissions', |
|
175
|
|
|
], |
|
176
|
|
|
'WishList_CREATE' => [ |
|
177
|
|
|
'name' => 'Create a Wish List', |
|
178
|
|
|
'category' => 'Wish List Permissions', |
|
179
|
|
|
], |
|
180
|
|
|
'WishList_VIEW' => [ |
|
181
|
|
|
'name' => 'View a Wish List', |
|
182
|
|
|
'category' => 'Wish List Permissions', |
|
183
|
|
|
], |
|
184
|
|
|
]; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param Member|null $member |
|
189
|
|
|
* |
|
190
|
|
|
* @return bool|int |
|
191
|
|
|
*/ |
|
192
|
1 |
|
public function canEdit($member = null) |
|
193
|
|
|
{ |
|
194
|
1 |
|
$member = ($member === null) ? Security::getCurrentUser() : $member; |
|
195
|
|
|
|
|
196
|
1 |
|
return ((Permission::check('WishList_EDIT', 'any', |
|
197
|
1 |
|
$member) && $member->ID == $this->MemberID) || Permission::check('ADMIN')); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param Member|null $member |
|
202
|
|
|
* |
|
203
|
|
|
* @return bool|int |
|
204
|
|
|
*/ |
|
205
|
1 |
|
public function canDelete($member = null) |
|
206
|
|
|
{ |
|
207
|
1 |
|
$member = ($member === null) ? Security::getCurrentUser() : $member; |
|
208
|
|
|
|
|
209
|
1 |
|
return ((Permission::check('WishList_DELETE', 'any', |
|
210
|
1 |
|
$member) && $member->ID == $this->MemberID) || Permission::check('ADMIN')); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param Member|null $member |
|
215
|
|
|
* @param array $context |
|
216
|
|
|
* |
|
217
|
|
|
* @return bool|int |
|
218
|
|
|
*/ |
|
219
|
1 |
|
public function canCreate($member = null, $context = array()) |
|
220
|
|
|
{ |
|
221
|
1 |
|
return Security::getCurrentUser() && Permission::check('WishList_CREATE', 'any', $member); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @param Member|null $member |
|
226
|
|
|
* |
|
227
|
|
|
* @return bool |
|
228
|
|
|
*/ |
|
229
|
3 |
|
public function canView($member = null) |
|
230
|
|
|
{ |
|
231
|
3 |
|
$member = ($member === null) ? Security::getCurrentUser() : $member; |
|
232
|
|
|
|
|
233
|
3 |
|
return (!$this->Private) || ((Permission::check('WishList_VIEW', 'any', |
|
234
|
3 |
|
$member) && $member->ID == $this->MemberID) || Permission::check('ADMIN')); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
} |
|
238
|
|
|
|