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