-
Notifications
You must be signed in to change notification settings - Fork 1
/
Plugin.php
484 lines (417 loc) · 13 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<?php
namespace Pixel\Shop;
use App;
use Yaml;
use File;
use Event;
use Backend;
use Validator;
use Pixel\Shop\Models\Item;
use Pixel\Shop\Models\Order;
use Pixel\Shop\Models\Brand;
use Pixel\Shop\Models\Category;
use System\Models\Parameter;
use System\Classes\PluginBase;
use Illuminate\Foundation\AliasLoader;
use October\Rain\Exception\ApplicationException;
use Backend\Classes\Controller as BackendController;
class Plugin extends PluginBase
{
// DEPENDENCIES
public $require = ['RainLab.User', 'RainLab.Location', 'Responsiv.Currency'];
// PLUGIN DETAILS
public function pluginDetails()
{
return [
'name' => 'pixel.shop::lang.plugin.name',
'description' => 'pixel.shop::lang.plugin.description',
'author' => 'Pixel',
'icon' => 'icon-shopping-basket'
];
}
public function registerComponents()
{
return [
'Pixel\Shop\Components\ProductList' => 'shopProductsList',
'Pixel\Shop\Components\ProductDetails' => 'shopProductsDetails',
'Pixel\Shop\Components\CartContainer' => 'cartContainer',
'Pixel\Shop\Components\CartButton' => 'cartButton',
'Pixel\Shop\Components\UserProfile' => 'userProfile',
];
}
// REGISTER PERMISSIONS
public function registerPermissions()
{
return [
'pixel.shop.orders' => ['tab' => 'pixel.shop::lang.plugin.name', 'label' => 'pixel.shop::lang.plugin.access_orders'],
'pixel.shop.items' => ['tab' => 'pixel.shop::lang.plugin.name', 'label' => 'pixel.shop::lang.plugin.access_items'],
'pixel.shop.categories' => ['tab' => 'pixel.shop::lang.plugin.name', 'label' => 'pixel.shop::lang.plugin.access_categories'],
'pixel.shop.brands' => ['tab' => 'pixel.shop::lang.plugin.name', 'label' => 'pixel.shop::lang.plugin.access_brands'],
'pixel.shop.coupons' => ['tab' => 'pixel.shop::lang.plugin.name', 'label' => 'pixel.shop::lang.plugin.access_coupons'],
'pixel.shop.gateways_settings' => ['tab' => 'pixel.shop::lang.plugin.name', 'label' => 'pixel.shop::lang.plugin.gateways_settings'],
'pixel.shop.sales_settings' => ['tab' => 'pixel.shop::lang.plugin.name', 'label' => 'pixel.shop::lang.plugin.sales_settings'],
'pixel.shop.shipping' => ['tab' => 'pixel.shop::lang.plugin.name', 'label' => 'pixel.shop::lang.plugin.access_coupons'],
];
}
// REGISTER MENU NAVIGATION
public function registerNavigation()
{
return [
'shop' => [
'label' => 'pixel.shop::lang.menu.shop',
'url' => Backend::url('pixel/shop/index'),
'icon' => 'icon-shopping-cart',
'iconSvg' => 'plugins/pixel/shop/assets/icon.svg',
'permissions' => ['pixel.shop.*'],
'order' => 405,
'sideMenu' => [
'orders' => [
'label' => 'pixel.shop::lang.menu.orders',
'icon' => 'icon-shopping-basket',
'url' => Backend::url('pixel/shop/orders'),
'permissions' => ['pixel.shop.orders'],
'counter' => $this->ordersCounter(),
],
'items' => [
'label' => 'pixel.shop::lang.menu.items',
'icon' => 'icon-cubes',
'url' => Backend::url('pixel/shop/items'),
'permissions' => ['pixel.shop.items'],
'counter' => $this->productsStockCounter(),
],
'categories' => [
'label' => 'pixel.shop::lang.menu.categories',
'icon' => 'icon-tags',
'url' => Backend::url('pixel/shop/categories'),
'permissions' => ['pixel.shop.categories'],
],
'brands' => [
'label' => 'pixel.shop::lang.menu.brands',
'icon' => 'icon-copyright',
'url' => Backend::url('pixel/shop/brands'),
'permissions' => ['pixel.shop.brands'],
],
'coupons' => [
'label' => 'pixel.shop::lang.menu.coupons',
'icon' => 'icon-ticket',
'url' => Backend::url('pixel/shop/coupons'),
'permissions' => ['pixel.shop.coupons'],
],
]
]
];
}
// SETTINGS
public function registerSettings()
{
return [
'sales' => [
'label' => 'pixel.shop::lang.plugin.sales_settings',
'description' => 'pixel.shop::lang.plugin.sales_settings_description',
'icon' => 'icon-dollar',
'class' => 'Pixel\Shop\Models\SalesSettings',
'category' => 'pixel.shop::lang.menu.shop',
'order' => 100,
'keywords' => 'crm customer relationship management invoice estimate',
'permissions' => ['pixel.shop.sales_settings']
],
'gateways' => [
'label' => 'pixel.shop::lang.plugin.gateways_settings',
'description' => 'pixel.shop::lang.plugin.gateways_settings_description',
'icon' => 'icon-credit-card',
'class' => 'Pixel\Shop\Models\GatewaysSettings',
'category' => 'pixel.shop::lang.menu.shop',
'order' => 101,
'keywords' => 'management gateways payment method',
'permissions' => ['pixel.shop.gateways_settings']
]
];
}
// FORM WIDGETS
public function registerFormWidgets()
{
return [
'Pixel\Shop\FormWidgets\Currency' => [
'label' => 'Currency',
'code' => 'shop-currency'
],
'Pixel\Shop\FormWidgets\Slider' => [
'label' => 'Slider',
'code' => 'shop-slider'
],
'Pixel\Shop\FormWidgets\Selector' => [
'label' => 'Selector',
'code' => 'shop-selector'
],
'Pixel\Shop\FormWidgets\Variants' => [
'label' => 'Variants',
'code' => 'shop-variants'
],
'Pixel\Shop\FormWidgets\Rates' => [
'label' => 'Rates',
'code' => 'shop-rates'
],
'Pixel\Shop\FormWidgets\Zones' => [
'label' => 'Zone',
'code' => 'shop-zones'
],
'Pixel\Shop\FormWidgets\ZoneProfile' => [
'label' => 'Zone Profile',
'code' => 'shop-zone-profile'
],
];
}
// MAIL TEMPLATES
public function registerMailPartials()
{
return [
'badge' => 'pixel.shop::partials.badge',
'items' => 'pixel.shop::partials.items',
];
}
public function registerMailTemplates()
{
return [
'pixel.shop::mail.new_order',
'pixel.shop::mail.order_awaitpay',
'pixel.shop::mail.order_cancelled',
'pixel.shop::mail.order_completed',
'pixel.shop::mail.order_payed',
];
}
// RAINLAB PAGES MENU SUPPORT
private function bootMenuItem()
{
Event::listen('pages.menuitem.listTypes', function () {
return [
'pixel-shop-categories' => 'All Shop Categories',
'pixel-shop-brands' => 'All Shop Brands',
];
});
Event::listen('pages.menuitem.getTypeInfo', function ($type) {
if ($type == 'pixel-shop-categories')
return Category::getMenuTypeInfo($type);
if ($type == 'pixel-shop-brands')
return Brand::getMenuTypeInfo($type);
});
Event::listen('pages.menuitem.resolveItem', function ($type, $item, $url, $theme) {
if ($type == 'pixel-shop-categories')
return Category::resolveMenuItem($item, $url, $theme);
if ($type == 'pixel-shop-brands')
return Brand::resolveMenuItem($item, $url, $theme);
});
}
// EXTEND PLUGIN RAINLAB USER
private function bootRainlabUserExtend()
{
if (class_exists("\RainLab\User\Models\User")) {
Event::listen('backend.list.extendColumns', function ($widget) {
if (!$widget->getController() instanceof \RainLab\Location\Controllers\Locations) {
return;
}
if ($widget->model instanceof \RainLab\Location\Models\Country) {
$widget->addColumns([
'shipping_fee' => [
'label' => 'pixel.shop::lang.fields.shipping_fee',
'type' => 'currency'
]
]);
}
if ($widget->model instanceof \RainLab\Location\Models\State) {
$widget->addColumns([
'shipping_fee' => [
'label' => 'pixel.shop::lang.fields.shipping_fee',
'type' => 'currency'
]
]);
}
});
// FILLABE ATTRIBUTE
\RainLab\User\Models\User::extend(function ($model) {
$model->addFillable([
"billing_address",
"shipping_address",
"is_ship_same_bill",
]);
$model->addJsonable([
'billing_address',
'shipping_address'
]);
$model->addCasts([
'is_ship_same_bill' => 'boolean',
]);
});
// EXTEND FIELDS
\RainLab\User\Controllers\Users::extendFormFields(function ($widget) {
if (!$widget->model instanceof \RainLab\User\Models\User)
return;
$configFile = __DIR__ . '/models/user/fields.yaml';
$config = Yaml::parse(File::get($configFile));
$widget->addSecondaryTabFields($config);
});
\RainLab\Location\Controllers\Locations::extendFormFields(function ($widget) {
if ($widget->model instanceof \RainLab\Location\Models\Country) {
$widget->addFields([
'name' => [
'label' => 'rainlab.location::lang.country.name',
'span' => 'full'
],
'shipping_fee' => [
'label' => 'pixel.shop::lang.fields.shipping_fee',
'type' => 'shop-currency',
'span' => 'auto',
'comment' => 'pixel.shop::lang.messages.shipping_fee_country'
]
]);
}
if ($widget->model instanceof \RainLab\Location\Models\State) {
$widget->addFields([
'name' => [
'label' => 'rainlab.location::lang.state.name',
'span' => 'full',
'comment' => 'rainlab.location::lang.state.name_comment'
],
'code' => [
'label' => 'rainlab.location::lang.state.code',
'span' => 'auto',
'comment' => 'rainlab.location::lang.state.code_comment',
'preset' => [
'field' => 'name',
'type' => 'slug',
]
],
'shipping_fee' => [
'label' => 'pixel.shop::lang.fields.shipping_fee',
'type' => 'shop-currency',
'span' => 'auto',
'comment' => 'pixel.shop::lang.messages.shipping_fee_state'
]
]);
}
});
// EXTEND RELATIONS
\RainLab\User\Models\User::extend(function ($model) {
$model->hasMany['orders'] = [
'Pixel\Shop\Models\Order',
'order' => 'created_at desc'
];
$model->hasMany['favorites'] = [
'Pixel\Shop\Models\Favorite',
'conditions' => 'is_favorite = 1'
];
});
}
}
// EXTENDING VALIDATION TYPES
private function bootValidatorExtend()
{
Validator::extend('ccn', function ($attribute, $value, $parameters, $validator) {
$number = preg_replace('/\D/', '', $value);
$number_length = strlen($number);
$parity = $number_length % 2;
$total = 0;
for ($i = 0; $i < $number_length; $i++) {
$digit = $number[$i];
if ($i % 2 == $parity) {
$digit *= 2;
if ($digit > 9)
$digit -= 9;
}
$total += $digit;
}
if ($total % 10 != 0)
return false;
return self::evaluatePAN($value);
});
Validator::extend('cvv', function ($attribute, $value, $parameters, $validator) {
$data = $validator->getData();
if (
is_array($parameters)
&& !array_key_exists($parameters[0], $data)
&& !empty($data[$parameters[0]])
)
return;
$type = self::evaluatePAN($data[$parameters[0]]);
if (!$type)
return;
if ($type == 'amex')
return preg_match("/^[0-9]{4}$/", $value);
else
return preg_match("/^[0-9]{3}$/", $value);
});
Validator::extend('ccexp', function ($attribute, $value, $parameters, $validator) {
$part = explode('/', $value);
$new_date = $part[1] . '-' . $part[0];
$dt =date_create_from_format('Y-m', $new_date);
$format = $dt->format('Y-m');
$new_format = explode('-', $format);
$exp = intval($new_format[0] . $new_format[1]);
$now = intval(date('Ym'));
$limit = intval(date('Ym', strtotime('+20 years')));
if ($exp >= $now && $exp <= $limit)
return true;
return;
});
Validator::extend('money', function ($attribute, $value, $parameters, $validator) {
return preg_match("/^\d*(\.\d{1,2})?$/", $value);
});
Validator::extend('amount', function ($attribute, $value, $parameters, $validator) {
if ($value < 1)
return;
return preg_match("/^\d*(\.\d{1,2})?$/", $value);
});
}
// ON BOOT
public function boot()
{
// ALIAS
$alias = AliasLoader::getInstance();
$alias->alias('CurrencyShop', 'Pixel\Shop\Classes\Currency');
// BOOT ELEMENTS
$this->bootMenuItem();
$this->bootValidatorExtend();
$this->bootRainlabUserExtend();
// PLUGIN ASSETS
BackendController::extend(function ($controller) {
$controller->addCss('/plugins/pixel/shop/assets/main.css');
});
// OMNYPAY BOOT
App::register('Ignited\LaravelOmnipay\LaravelOmnipayServiceProvider');
$alias->alias('Omnipay', 'Ignited\LaravelOmnipay\Facades\OmnipayFacade');
}
private function productsStockCounter()
{
$low_stock = Item::getLowStockProducts();
return ($low_stock) ? $low_stock : null;
}
private function ordersCounter()
{
$orders = Order::where("is_paid", true)->where('status', '<>', 'completed')->count();
return ($orders) ? $orders : null;
}
public static function evaluatePAN($value)
{
$cards = array(
"visa" => "(4\d{12}(?:\d{3})?)",
"amex" => "(3[47]\d{13})",
"maestro" => "((?:5020|5038|6304|6579|6761)\d{12}(?:\d\d)?)",
"mastercard" => "(5[1-5]\d{14})",
"jcb" => "(35[2-8][89]\d\d\d{10})",
"solo" => "((?:6334|6767)\d{12}(?:\d\d)?\d?)",
"switch" => "(?:(?:(?:4903|4905|4911|4936|6333|6759)\d{12})|(?:(?:564182|633110)\d{10})(\d\d)?\d?)",
);
$names = array(
"visa",
"amex",
"maestro",
"mastercard",
"jcb",
"solo",
"switch"
);
$matches = array();
$pattern = "#^(?:" . implode("|", $cards) . ")$#";
$result = preg_match($pattern, str_replace(" ", "", $value), $matches);
return ($result > 0) ? $names[sizeof($matches) - 2] : false;
}
}