1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Model; |
4
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Admin\FoxyAdmin; |
6
|
|
|
use SilverStripe\Forms\FieldList; |
7
|
|
|
use SilverStripe\Forms\FormAction; |
8
|
|
|
use SilverStripe\Forms\ReadonlyField; |
9
|
|
|
use SilverStripe\Forms\TextField; |
10
|
|
|
use SilverStripe\ORM\DataObject; |
11
|
|
|
use SilverStripe\ORM\DB; |
12
|
|
|
use SilverStripe\ORM\ValidationException; |
13
|
|
|
use SilverStripe\Security\Permission; |
14
|
|
|
use SilverStripe\Security\PermissionProvider; |
15
|
|
|
use SilverStripe\Security\Security; |
16
|
|
|
use SilverStripe\View\TemplateGlobalProvider; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Setting |
20
|
|
|
* @package Dynamic\Foxy\Model |
21
|
|
|
* |
22
|
|
|
* @property string $StoreKey |
23
|
|
|
* @property string $StoreTitle |
24
|
|
|
* @property string $StoreDomain |
25
|
|
|
*/ |
26
|
|
|
class Setting extends DataObject implements PermissionProvider, TemplateGlobalProvider |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private static $singular_name = 'Foxy Setting'; |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private static $plural_name = 'Foxy Settings'; |
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private static $description = 'Update the settings for your store'; |
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private static $table_name = 'FoxySetting'; |
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
private static $keyPrefix = "dYnm1c"; |
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
private static $db = [ |
|
|
|
|
57
|
|
|
'StoreKey' => 'Varchar(60)', |
58
|
|
|
'StoreTitle' => 'Varchar(255)', |
59
|
|
|
'StoreDomain' => 'Varchar(255)', |
60
|
|
|
// TODO |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Default permission to check for 'LoggedInUsers' to create or edit pages. |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
* @config |
68
|
|
|
*/ |
69
|
|
|
private static $required_permission = ['CMS_ACCESS_CMSMain', 'CMS_ACCESS_LeftAndMain']; |
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return FieldList |
73
|
|
|
*/ |
74
|
|
|
public function getCMSFields() |
75
|
|
|
{ |
76
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
77
|
|
|
// TODO |
78
|
|
|
$fields->addFieldsToTab('Root.Main', [ |
79
|
|
|
TextField::create('StoreTitle', 'Store Title') |
80
|
|
|
->setDescription('The name of your store as you\'d like it displayed to your customers'), |
81
|
|
|
TextField::create('StoreDomain', 'Store Domain') |
82
|
|
|
->setDescription('This is a unique FoxyCart subdomain for your cart, checkout, and receipt'), |
83
|
|
|
]); |
84
|
|
|
|
85
|
|
|
$fields->addFieldsToTab('Root.Advanced', [ |
86
|
|
|
ReadonlyField::create('StoreKey', 'Store Key', $this->StoreKey), |
87
|
|
|
]); |
88
|
|
|
}); |
89
|
|
|
|
90
|
|
|
return parent::getCMSFields(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return FieldList |
95
|
|
|
*/ |
96
|
|
|
public function getCMSActions() |
97
|
|
|
{ |
98
|
|
|
if (Permission::check('ADMIN') || Permission::check('EDIT_FOXY_SETTING')) { |
99
|
|
|
$actions = new FieldList( |
100
|
|
|
FormAction::create('save_foxy_setting', _t(static::class . '.SAVE', 'Save')) |
101
|
|
|
->addExtraClass('btn-primary font-icon-save') |
102
|
|
|
); |
103
|
|
|
} else { |
104
|
|
|
$actions = FieldList::create(); |
105
|
|
|
} |
106
|
|
|
$this->extend('updateCMSActions', $actions); |
107
|
|
|
return $actions; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* |
112
|
|
|
*/ |
113
|
|
|
public function requireDefaultRecords() |
114
|
|
|
{ |
115
|
|
|
parent::requireDefaultRecords(); |
116
|
|
|
if (!self::current_foxy_setting()) { |
117
|
|
|
self::make_foxy_setting(); |
118
|
|
|
DB::alteration_message('Added default FoxyStripe Setting', 'created'); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* |
124
|
|
|
*/ |
125
|
|
|
public function onBeforeWrite() |
126
|
|
|
{ |
127
|
|
|
parent::onBeforeWrite(); |
128
|
|
|
if (!$this->StoreKey) { |
129
|
|
|
$key = $this->generateStoreKey(); |
130
|
|
|
while (!ctype_alnum($key)) { |
131
|
|
|
$key = $this->generateStoreKey(); |
132
|
|
|
} |
133
|
|
|
$this->StoreKey = $key; |
134
|
|
|
DB::alteration_message('Created FoxyCart Store Key ' . $key, 'created'); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param int $count |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public function generateStoreKey($count = 0) |
143
|
|
|
{ |
144
|
|
|
$length = $this->obj('StoreKey')->getSize() - strlen($this->config()->get('keyPrefix')); |
145
|
|
|
$charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' . strtotime('now'); |
146
|
|
|
$strLength = strlen($charset); |
147
|
|
|
$str = ''; |
148
|
|
|
|
149
|
|
|
while ($count < $length) { |
150
|
|
|
$str .= $charset[mt_rand(0, $strLength - 1)]; |
151
|
|
|
$count++; |
152
|
|
|
} |
153
|
|
|
return $this->config()->get('keyPrefix') . substr(base64_encode($str), 0, $length); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function CMSEditLink() |
160
|
|
|
{ |
161
|
|
|
return FoxyAdmin::singleton()->Link(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param \SilverStripe\Security\Member|null $member |
166
|
|
|
* |
167
|
|
|
* @return bool|int|null |
168
|
|
|
*/ |
169
|
|
|
public function canEdit($member = null) |
170
|
|
|
{ |
171
|
|
|
if (!$member) { |
172
|
|
|
$member = Security::getCurrentUser(); |
173
|
|
|
} |
174
|
|
|
$extended = $this->extendedCan('canEdit', $member); |
175
|
|
|
if ($extended !== null) { |
176
|
|
|
return $extended; |
177
|
|
|
} |
178
|
|
|
return Permission::checkMember($member, 'EDIT_FOXY_SETTING'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return array |
183
|
|
|
*/ |
184
|
|
|
public function providePermissions() |
185
|
|
|
{ |
186
|
|
|
return [ |
187
|
|
|
'EDIT_FOXY_SETTING' => [ |
188
|
|
|
'name' => _t( |
189
|
|
|
static::class . '.EDIT_FOXY_SETTING', |
190
|
|
|
'Manage FoxyStripe settings' |
191
|
|
|
), |
192
|
|
|
'category' => _t( |
193
|
|
|
static::class . '.PERMISSIONS_FOXY_SETTING', |
194
|
|
|
'FoxyStripe' |
195
|
|
|
), |
196
|
|
|
'help' => _t( |
197
|
|
|
static::class . '.EDIT_PERMISSION_FOXY_SETTING', |
198
|
|
|
'Ability to edit the settings of a FoxyStripe Store.' |
199
|
|
|
), |
200
|
|
|
'sort' => 400, |
201
|
|
|
], |
202
|
|
|
]; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get the current sites {@link GlobalSiteSetting}, and creates a new one |
207
|
|
|
* through {@link make_global_config()} if none is found. |
208
|
|
|
* |
209
|
|
|
* @return self|DataObject |
210
|
|
|
*/ |
211
|
|
|
public static function current_foxy_setting() |
212
|
|
|
{ |
213
|
|
|
if ($config = self::get()->first()) { |
214
|
|
|
return $config; |
215
|
|
|
} |
216
|
|
|
return self::make_foxy_setting(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Create {@link GlobalSiteSetting} with defaults from language file. |
221
|
|
|
* |
222
|
|
|
* @return self |
223
|
|
|
*/ |
224
|
|
|
public static function make_foxy_setting() |
225
|
|
|
{ |
226
|
|
|
$config = self::create(); |
227
|
|
|
try { |
228
|
|
|
$config->write(); |
229
|
|
|
} catch (ValidationException $e) { |
|
|
|
|
230
|
|
|
} |
231
|
|
|
return $config; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Add $GlobalConfig to all SSViewers. |
236
|
|
|
* |
237
|
|
|
* @return array |
238
|
|
|
*/ |
239
|
|
|
public static function get_template_global_variables() |
240
|
|
|
{ |
241
|
|
|
return [ |
242
|
|
|
'FoxyStripe' => 'current_foxy_setting', |
243
|
|
|
]; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|