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\GridField\GridField; |
||
10 | use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; |
||
11 | use SilverStripe\Forms\LiteralField; |
||
12 | use SilverStripe\Forms\ReadonlyField; |
||
13 | use SilverStripe\Forms\TextField; |
||
14 | use SilverStripe\ORM\DataObject; |
||
15 | use SilverStripe\ORM\DB; |
||
16 | use SilverStripe\ORM\ValidationException; |
||
17 | use SilverStripe\Security\Permission; |
||
18 | use SilverStripe\Security\PermissionProvider; |
||
19 | use SilverStripe\Security\Security; |
||
20 | use SilverStripe\View\TemplateGlobalProvider; |
||
21 | use Symbiote\GridFieldExtensions\GridFieldOrderableRows; |
||
22 | |||
23 | /** |
||
24 | * Class Setting |
||
25 | * @package Dynamic\Foxy\Model |
||
26 | * |
||
27 | * @property string StoreKey |
||
28 | * @property bool EnableSidecart |
||
29 | * @property string StoreTitle |
||
30 | * @property string StoreDomain |
||
31 | */ |
||
32 | class Setting extends DataObject implements PermissionProvider, TemplateGlobalProvider |
||
33 | { |
||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private static $singular_name = 'Foxy Setting'; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private static $plural_name = 'Foxy Settings'; |
||
0 ignored issues
–
show
|
|||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private static $description = 'Update the settings for your store'; |
||
0 ignored issues
–
show
|
|||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | private static $table_name = 'FoxySetting'; |
||
0 ignored issues
–
show
|
|||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | private static $keyPrefix = "dYnm1c"; |
||
0 ignored issues
–
show
|
|||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | private static $db = [ |
||
0 ignored issues
–
show
|
|||
63 | 'StoreKey' => 'Varchar(60)', |
||
64 | 'EnableSidecart' => 'Boolean', |
||
65 | ]; |
||
66 | |||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | private static $defaults = [ |
||
0 ignored issues
–
show
|
|||
71 | 'EnableSidecart' => 1, |
||
72 | ]; |
||
73 | |||
74 | /** |
||
75 | * Default permission to check for 'LoggedInUsers' to create or edit pages. |
||
76 | * |
||
77 | * @var array |
||
78 | * @config |
||
79 | */ |
||
80 | private static $required_permission = ['CMS_ACCESS_CMSMain', 'CMS_ACCESS_LeftAndMain']; |
||
0 ignored issues
–
show
|
|||
81 | |||
82 | /** |
||
83 | * @return FieldList |
||
84 | */ |
||
85 | public function getCMSFields() |
||
86 | { |
||
87 | $this->beforeUpdateCMSFields(function (FieldList $fields) { |
||
88 | $fields->removeByName([ |
||
89 | 'StoreKey', |
||
90 | ]); |
||
91 | |||
92 | $fields->addFieldsToTab('Root.Main', [ |
||
93 | ReadonlyField::create('StoreDomain', 'Store Domain', FoxyHelper::config()->get('cart_url')) |
||
94 | ->setDescription('This is a unique FoxyCart subdomain for your cart, checkout, and receipt'), |
||
95 | CheckboxField::create('CustomSSL', 'Use custom SSL', FoxyHelper::config()->get('custom_ssl')) |
||
96 | ->performReadonlyTransformation(), |
||
97 | ]); |
||
98 | |||
99 | if (FoxyHelper::config()->get('secret') != null) { |
||
100 | $key = FoxyHelper::config()->get('secret'); |
||
101 | $description = 'Your secret key as set in config.yml'; |
||
102 | } else { |
||
103 | $key = $this->StoreKey; |
||
104 | $description = 'Recommended secret key for your Foxy store. Add to your config.yml to implement'; |
||
105 | } |
||
106 | |||
107 | $fields->addFieldToTab( |
||
108 | 'Root.Main', |
||
109 | ReadonlyField::create('Key', 'Store Key', $key) |
||
110 | ->setDescription($description) |
||
111 | ); |
||
112 | |||
113 | if (self::store_name_warning() !== null) { |
||
114 | $fields->addFieldToTab('Root.Main', LiteralField::create('StoreSubDomainHeaderWarning', _t( |
||
115 | 'ProductPage.StoreSubDomainHeaderWarning', |
||
116 | '<p class="message error">Store domain must be entered in the |
||
117 | <a href="/admin/foxy/">Foxy settings</a></p>' |
||
118 | )), 'StoreDomain'); |
||
119 | } |
||
120 | |||
121 | $fields->addFieldsToTab( |
||
122 | 'Root.Options.Settings', |
||
123 | [ |
||
124 | CheckboxField::create('EnableSidecart', 'Enable Sidecart') |
||
125 | ->setDescription('Cart slides in from right side when product added to cart'), |
||
126 | ] |
||
127 | ); |
||
128 | |||
129 | $fields->addFieldsToTab( |
||
130 | 'Root.Options.VariationTypes', |
||
131 | [ |
||
132 | LiteralField::create('VariationDescrip', _t( |
||
133 | __CLASS__ . '.VariationDescrip', |
||
134 | '<p>Product Variation Types allow you to group a set of product variants by type.</p>' |
||
135 | )), |
||
136 | GridField::create( |
||
137 | 'VariationType', |
||
138 | _t(__CLASS__ . '.VariationTypeLabel', 'Variation Types'), |
||
139 | VariationType::get(), |
||
140 | GridFieldConfig_RecordEditor::create() |
||
141 | ->addComponents([ |
||
142 | new GridFieldOrderableRows('SortOrder') |
||
143 | ]) |
||
144 | ), |
||
145 | ] |
||
146 | ); |
||
147 | |||
148 | $fields->addFieldsToTab('Root.Categories', [ |
||
149 | LiteralField::create('CategoryDescrip', _t( |
||
150 | __CLASS__ . '.CategoryDescrip', |
||
151 | '<p>FoxyCart Categories offer a way to give products additional behaviors that cannot be |
||
152 | accomplished by product options alone, including category specific coupon codes, |
||
153 | shipping and handling fees, and email receipts. |
||
154 | <a href="https://wiki.foxycart.com/v/2.0/categories" target="_blank"> |
||
155 | Learn More |
||
156 | </a></p> |
||
157 | <p>Categories you\'ve created in FoxyStripe must also be created in your |
||
158 | <a href="https://admin.foxycart.com/admin.php?ThisAction=ManageProductCategories" |
||
159 | target="_blank">FoxyCart Categories</a> admin panel.</p>' |
||
160 | )), |
||
161 | GridField::create( |
||
162 | 'FoxyCategory', |
||
163 | _t(__CLASS__ . '.FoxyCategory', 'FoxyCart Categories'), |
||
164 | FoxyCategory::get(), |
||
165 | GridFieldConfig_RecordEditor::create() |
||
166 | ), |
||
167 | ]); |
||
168 | }); |
||
169 | |||
170 | return parent::getCMSFields(); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @return FieldList |
||
175 | */ |
||
176 | public function getCMSActions() |
||
177 | { |
||
178 | if (Permission::check('ADMIN') || Permission::check('EDIT_FOXY_SETTING')) { |
||
179 | $actions = new FieldList( |
||
180 | FormAction::create('save_foxy_setting', _t(static::class . '.SAVE', 'Save')) |
||
181 | ->addExtraClass('btn-primary font-icon-save') |
||
182 | ); |
||
183 | } else { |
||
184 | $actions = FieldList::create(); |
||
185 | } |
||
186 | $this->extend('updateCMSActions', $actions); |
||
187 | return $actions; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @return null|string |
||
192 | * @throws \SilverStripe\ORM\ValidationException |
||
193 | */ |
||
194 | public static function store_name_warning() |
||
195 | { |
||
196 | $warning = null; |
||
197 | $helper = FoxyHelper::create(); |
||
198 | if (!$helper->getStoreCartURL()) { |
||
199 | $warning = 'Must define FoxyCart Store Domain in your config'; |
||
200 | } |
||
201 | return $warning; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * |
||
206 | */ |
||
207 | public function requireDefaultRecords() |
||
208 | { |
||
209 | parent::requireDefaultRecords(); |
||
210 | if (!self::current_foxy_setting()) { |
||
211 | self::make_foxy_setting(); |
||
212 | DB::alteration_message('Added default FoxyStripe Setting', 'created'); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * |
||
218 | */ |
||
219 | public function onBeforeWrite() |
||
220 | { |
||
221 | parent::onBeforeWrite(); |
||
222 | if (!$this->StoreKey) { |
||
223 | $key = $this->generateStoreKey(); |
||
224 | while (!ctype_alnum($key)) { |
||
225 | $key = $this->generateStoreKey(); |
||
226 | } |
||
227 | $this->StoreKey = $key; |
||
228 | DB::alteration_message('Created FoxyCart Store Key ' . $key, 'created'); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param int $count |
||
234 | * @return string |
||
235 | */ |
||
236 | public function generateStoreKey($count = 0) |
||
237 | { |
||
238 | $length = $this->obj('StoreKey')->getSize() - strlen($this->config()->get('keyPrefix')); |
||
239 | $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' . strtotime('now'); |
||
240 | $strLength = strlen($charset); |
||
241 | $str = ''; |
||
242 | |||
243 | while ($count < $length) { |
||
244 | $str .= $charset[mt_rand(0, $strLength - 1)]; |
||
245 | $count++; |
||
246 | } |
||
247 | return $this->config()->get('keyPrefix') . substr(base64_encode($str), 0, $length); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @return string |
||
252 | */ |
||
253 | public function CMSEditLink() |
||
254 | { |
||
255 | return FoxyAdmin::singleton()->Link(); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @param \SilverStripe\Security\Member|null $member |
||
260 | * |
||
261 | * @return bool|int|null |
||
262 | */ |
||
263 | public function canEdit($member = null) |
||
264 | { |
||
265 | if (!$member) { |
||
266 | $member = Security::getCurrentUser(); |
||
267 | } |
||
268 | $extended = $this->extendedCan('canEdit', $member); |
||
269 | if ($extended !== null) { |
||
270 | return $extended; |
||
271 | } |
||
272 | return Permission::checkMember($member, 'EDIT_FOXY_SETTING'); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * @return array |
||
277 | */ |
||
278 | public function providePermissions() |
||
279 | { |
||
280 | return [ |
||
281 | 'EDIT_FOXY_SETTING' => [ |
||
282 | 'name' => _t( |
||
283 | __CLASS__ . '.PERMISSION_MANAGE_FOXY_DESCRIPTION', |
||
284 | 'Manage Foxy settings' |
||
285 | ), |
||
286 | 'category' => _t( |
||
287 | __CLASS__ . '.PERMISSIONS_CATEGORY', |
||
288 | 'Foxy' |
||
289 | ), |
||
290 | 'help' => _t( |
||
291 | __CLASS__ . '.PERMISSION_MANAGE_FOXY_HELP', |
||
292 | 'Ability to edit the settings of a Foxy Store' |
||
293 | ), |
||
294 | 'sort' => 400, |
||
295 | ], |
||
296 | ]; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Get the current sites {@link GlobalSiteSetting}, and creates a new one |
||
301 | * through {@link make_global_config()} if none is found. |
||
302 | * |
||
303 | * @return self|DataObject |
||
304 | */ |
||
305 | public static function current_foxy_setting() |
||
306 | { |
||
307 | if ($config = self::get()->first()) { |
||
308 | return $config; |
||
309 | } |
||
310 | return self::make_foxy_setting(); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Create {@link GlobalSiteSetting} with defaults from language file. |
||
315 | * |
||
316 | * @return self |
||
317 | */ |
||
318 | public static function make_foxy_setting() |
||
319 | { |
||
320 | $config = self::create(); |
||
321 | try { |
||
322 | $config->write(); |
||
323 | } catch (ValidationException $e) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
|
|||
324 | } |
||
325 | return $config; |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Add $GlobalConfig to all SSViewers. |
||
330 | * |
||
331 | * @return array |
||
332 | */ |
||
333 | public static function get_template_global_variables() |
||
334 | { |
||
335 | return [ |
||
336 | 'FoxyStripe' => 'current_foxy_setting', |
||
337 | ]; |
||
338 | } |
||
339 | } |
||
340 |