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