-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.php
More file actions
executable file
·871 lines (710 loc) · 22.7 KB
/
Core.php
File metadata and controls
executable file
·871 lines (710 loc) · 22.7 KB
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
<?php
namespace Modules\PanelCore;
use Carbon\Carbon;
use Illuminate\Support\Facades\Config;
class Core
{
/**
* Returns the default channel code configured in config/app.php
*
* @return string
*/
public function getDefaultChannelCode(): string
{
static $channelCode;
if ($channelCode) {
return $channelCode;
}
return ($channel = $this->getDefaultChannel()) ? $channelCode = $channel->code : '';
}
/**
* Returns all locales
*
* @return \Illuminate\Support\Collection
*/
public function getAllLocales()
{
static $locales;
if ($locales) {
return $locales;
}
return $locales = $this->localeRepository->all();
}
/**
* Returns current locale
*
* @return \Modules\Admin\Contracts\Locale
*/
public function getCurrentLocale()
{
static $locale;
if ($locale) {
return $locale;
}
$locale = $this->localeRepository->findOneByField('code', app()->getLocale());
if (!$locale) {
$locale = $this->localeRepository->findOneByField('code', config('app.fallback_locale'));
}
return $locale;
}
/**
* Returns all currencies
*
* @return \Illuminate\Support\Collection
*/
public function getAllCurrencies()
{
static $currencies;
if ($currencies) {
return $currencies;
}
return $currencies = $this->currencyRepository->all();
}
/**
* Returns base channel's currency model
*
* @return \Modules\Admin\Contracts\Currency
*/
public function getBaseCurrency()
{
static $currency;
if ($currency) {
return $currency;
}
$baseCurrency = $this->currencyRepository->findOneByField('code', config('app.currency'));
if (!$baseCurrency) {
$baseCurrency = $this->currencyRepository->first();
}
return $currency = $baseCurrency;
}
/**
* Returns base channel's currency code
*
* @return string
*/
public function getBaseCurrencyCode()
{
static $currencyCode;
if ($currencyCode) {
return $currencyCode;
}
return ($currency = $this->getBaseCurrency()) ? $currencyCode = $currency->code : '';
}
/**
* Returns base channel's currency model
*
* @return \Modules\Admin\Contracts\Currency
*/
public function getChannelBaseCurrency()
{
static $currency;
if ($currency) {
return $currency;
}
$currenctChannel = $this->getCurrentChannel();
return $currency = $currenctChannel->base_currency;
}
/**
* Returns base channel's currency code
*
* @return string
*/
public function getChannelBaseCurrencyCode()
{
static $currencyCode;
if ($currencyCode) {
return $currencyCode;
}
return ($currency = $this->getChannelBaseCurrency()) ? $currencyCode = $currency->code : '';
}
/**
* Returns current channel's currency model
*
* @return \Modules\Admin\Contracts\Currency
*/
public function getCurrentCurrency()
{
static $currency;
if ($currency) {
return $currency;
}
if ($currencyCode = session()->get('currency')) {
if ($currency = $this->currencyRepository->findOneByField('code', $currencyCode)) {
return $currency;
}
}
return $currency = $this->getChannelBaseCurrency();
}
/**
* Returns current channel's currency code
*
* @return string
*/
public function getCurrentCurrencyCode()
{
static $currencyCode;
if ($currencyCode) {
return $currencyCode;
}
return ($currency = $this->getCurrentCurrency()) ? $currencyCode = $currency->code : '';
}
/**
* Converts price
*
* @param float $amount
* @param string $targetCurrencyCode
* @param string $orderCurrencyCode
* @return string
*/
public function convertPrice($amount, $targetCurrencyCode = null, $orderCurrencyCode = null)
{
if (!isset($this->lastCurrencyCode)) {
$this->lastCurrencyCode = $this->getBaseCurrency()->code;
}
if ($orderCurrencyCode) {
if (!isset($this->lastOrderCode)) {
$this->lastOrderCode = $orderCurrencyCode;
}
if (($targetCurrencyCode != $this->lastOrderCode)
&& ($targetCurrencyCode != $orderCurrencyCode)
&& ($orderCurrencyCode != $this->getBaseCurrencyCode())
&& ($orderCurrencyCode != $this->lastCurrencyCode)
) {
$amount = $this->convertToBasePrice($amount, $orderCurrencyCode);
}
}
$targetCurrency = !$targetCurrencyCode
? $this->getCurrentCurrency()
: $this->currencyRepository->findOneByField('code', $targetCurrencyCode);
if (!$targetCurrency) {
return $amount;
}
$exchangeRate = $this->exchangeRateRepository->findOneWhere([
'target_currency' => $targetCurrency->id,
]);
if (null === $exchangeRate || !$exchangeRate->rate) {
return $amount;
}
$result = (float)$amount * (float)($this->lastCurrencyCode == $targetCurrency->code ? 1.0 : $exchangeRate->rate);
if ($this->lastCurrencyCode != $targetCurrency->code) {
$this->lastCurrencyCode = $targetCurrency->code;
}
return $result;
}
/**
* Converts to base price
*
* @param float $amount
* @param string $targetCurrencyCode
* @return string
*/
public function convertToBasePrice($amount, $targetCurrencyCode = null)
{
$targetCurrency = !$targetCurrencyCode
? $this->getCurrentCurrency()
: $this->currencyRepository->findOneByField('code', $targetCurrencyCode);
if (!$targetCurrency) {
return $amount;
}
$exchangeRate = $this->exchangeRateRepository->findOneWhere([
'target_currency' => $targetCurrency->id,
]);
if (null === $exchangeRate || !$exchangeRate->rate) {
return $amount;
}
return (float)$amount / $exchangeRate->rate;
}
/**
* Format and convert price with currency symbol
*
* @param float $price
* @return string
*/
public function currency($amount = 0)
{
if (is_null($amount)) {
$amount = 0;
}
return $this->formatPrice($this->convertPrice($amount), $this->getCurrentCurrency()->code);
}
/**
* Return currency symbol from currency code
*
* @param float $price
* @return string
*/
public function currencySymbol($code)
{
$formatter = new \NumberFormatter(app()->getLocale() . '@currency=' . $code, \NumberFormatter::CURRENCY);
return $formatter->getSymbol(\NumberFormatter::CURRENCY_SYMBOL);
}
/**
* Format and convert price with currency symbol
*
* @param float $price
* @param string $currencyCode
* @return string
*/
public function formatPrice($price, $currencyCode)
{
$code = $this->getCurrentCurrency()->code;
if (is_null($price)) {
$price = 0;
} else {
if ($code != $currencyCode) {
$price = $this->convertPrice($price, $code, $currencyCode);
}
}
$formater = new \NumberFormatter(app()->getLocale(), \NumberFormatter::CURRENCY);
$symbol = $this->getCurrentCurrency()->symbol;
if ($symbol) {
if ($this->currencySymbol($currencyCode) == $symbol) {
return $formater->formatCurrency($price, $currencyCode);
} else {
$formater->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $symbol);
return $formater->format($price); // $this->convertPrice($price, $code)
}
} else {
return $formater->formatCurrency($price, $currencyCode);
}
}
/**
* Format and convert price with currency symbol
*
* @return array
*/
public function getAccountJsSymbols()
{
$formater = new \NumberFormatter(app()->getLocale(), \NumberFormatter::CURRENCY);
$pattern = $formater->getPattern();
$pattern = str_replace("¤", "%s", $pattern);
$pattern = str_replace("#,##0.00", "%v", $pattern);
return [
'symbol' => core()->currencySymbol(core()->getCurrentCurrencyCode()),
'decimal' => $formater->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL),
'format' => $pattern,
];
}
/**
* Format price with base currency symbol
*
* @param float $price
* @return string
*/
public function formatBasePrice($price)
{
if (is_null($price)) {
$price = 0;
}
$formater = new \NumberFormatter(app()->getLocale(), \NumberFormatter::CURRENCY);
if ($symbol = $this->getBaseCurrency()->symbol) {
if ($this->currencySymbol($this->getBaseCurrencyCode()) == $symbol) {
return $formater->formatCurrency($price, $this->getBaseCurrencyCode());
} else {
$formater->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $symbol);
return $formater->format($this->convertPrice($price));
}
} else {
return $formater->formatCurrency($price, $this->getBaseCurrencyCode());
}
}
/**
* Checks if current date of the given channel (in the channel timezone) is within the range
*
* @param int|string|\Modules\Admin\Contracts\Channel $channel
* @param string|null $dateFrom
* @param string|null $dateTo
* @return bool
*/
public function isChannelDateInInterval($dateFrom = null, $dateTo = null)
{
$channel = $this->getCurrentChannel();
$channelTimeStamp = $this->channelTimeStamp($channel);
$fromTimeStamp = strtotime($dateFrom);
$toTimeStamp = strtotime($dateTo);
if ($dateTo) {
$toTimeStamp += 86400;
}
if (!$this->is_empty_date($dateFrom) && $channelTimeStamp < $fromTimeStamp) {
$result = false;
} elseif (!$this->is_empty_date($dateTo) && $channelTimeStamp > $toTimeStamp) {
$result = false;
} else {
$result = true;
}
return $result;
}
/**
* Get channel timestamp, timstamp will be builded with channel timezone settings
*
* @param \Modules\Admin\Contracts\Channel $channel
* @return int
*/
public function channelTimeStamp($channel)
{
$timezone = $channel->timezone;
$currentTimezone = @date_default_timezone_get();
@date_default_timezone_set($timezone);
$date = date('Y-m-d H:i:s');
@date_default_timezone_set($currentTimezone);
return strtotime($date);
}
/**
* Check whether sql date is empty
*
* @param string $date
* @return bool
*/
function is_empty_date($date)
{
return preg_replace('#[ 0:-]#', '', $date) === '';
}
/**
* Format date using current channel.
*
* @param \Illuminate\Support\Carbon|null $date
* @param string $format
*
* @return string
*/
public function formatDate($date = null, $format = 'd-m-Y H:i:s')
{
$channel = $this->getCurrentChannel();
if (is_null($date)) {
$date = Carbon::now();
}
$date->setTimezone($channel->timezone);
return $date->format($format);
}
/**
* Retrieve information from payment configuration
*
* @param string $field
* @param string|null $locale
*
* @return mixed
*/
public function getConfigData($field)
{
$fields = $this->getConfigField($field);
$coreConfigValue = $this->coreConfigRepository->findOneWhere([
'code' => $field,
]);
if (!$coreConfigValue) {
$fields = explode(".", $field);
array_shift($fields);
$field = implode(".", $fields);
return Config::get($field);
}
return $coreConfigValue->value;
}
/**
* Retrieve a group of information from the core config table
*
* @param mixed $criteria
* @return mixed
*/
public function retrieveGroupConfig($criteria)
{
return $criteria;
}
/**
* Retrieve all countries
*
* @return \Illuminate\Support\Collection
*/
public function countries()
{
return $this->countryRepository->all();
}
/**
* Returns country name by code
*
* @param string $code
* @return string
*/
public function country_name($code)
{
$country = $this->countryRepository->findOneByField('code', $code);
return $country ? $country->name : '';
}
/**
* Retrieve all country states
*
* @param string $countryCode
* @return \Illuminate\Support\Collection
*/
public function states($countryCode)
{
return $this->countryStateRepository->findByField('country_code', $countryCode);
}
/**
* Retrieve all grouped states by country code
*
* @return \Illuminate\Support\Collection
*/
public function groupedStatesByCountries()
{
$collection = [];
foreach ($this->countryStateRepository->all() as $state) {
$collection[$state->country_code][] = $state->toArray();
}
return $collection;
}
/**
* Retrieve all grouped states by country code
*
* @return \Illuminate\Support\Collection
*/
public function findStateByCountryCode($countryCode = null, $stateCode = null)
{
$collection = array();
$collection = $this->countryStateRepository->findByField(['country_code' => $countryCode, 'code' => $stateCode]);
if (count($collection)) {
return $collection->first();
} else {
return false;
}
}
/**
* Returns time intervals
*
* @param \Illuminate\Support\Carbon $startDate
* @param \Illuminate\Support\Carbon $endDate
* @return array
*/
public function getTimeInterval($startDate, $endDate)
{
$timeIntervals = [];
$totalDays = $startDate->diffInDays($endDate) + 1;
$totalMonths = $startDate->diffInMonths($endDate) + 1;
$startWeekDay = Carbon::createFromTimeString($this->xWeekRange($startDate, 0) . ' 00:00:01');
$endWeekDay = Carbon::createFromTimeString($this->xWeekRange($endDate, 1) . ' 23:59:59');
$totalWeeks = $startWeekDay->diffInWeeks($endWeekDay);
if ($totalMonths > 5) {
for ($i = 0; $i < $totalMonths; $i++) {
$date = clone $startDate;
$date->addMonths($i);
$start = Carbon::createFromTimeString($date->format('Y-m-d') . ' 00:00:01');
$end = $totalMonths - 1 == $i
? $endDate
: Carbon::createFromTimeString($date->format('Y-m-d') . ' 23:59:59');
$timeIntervals[] = ['start' => $start, 'end' => $end, 'formatedDate' => $date->format('M')];
}
} elseif ($totalWeeks > 6) {
for ($i = 0; $i < $totalWeeks; $i++) {
$date = clone $startDate;
$date->addWeeks($i);
$start = $i == 0
? $startDate
: Carbon::createFromTimeString($this->xWeekRange($date, 0) . ' 00:00:01');
$end = $totalWeeks - 1 == $i
? $endDate
: Carbon::createFromTimeString($this->xWeekRange($date, 1) . ' 23:59:59');
$timeIntervals[] = ['start' => $start, 'end' => $end, 'formatedDate' => $date->format('d M')];
}
} else {
for ($i = 0; $i < $totalDays; $i++) {
$date = clone $startDate;
$date->addDays($i);
$start = Carbon::createFromTimeString($date->format('Y-m-d') . ' 00:00:01');
$end = Carbon::createFromTimeString($date->format('Y-m-d') . ' 23:59:59');
$timeIntervals[] = ['start' => $start, 'end' => $end, 'formatedDate' => $date->format('d M')];
}
}
return $timeIntervals;
}
/**
*
* @param string $date
* @param int $day
* @return string
*/
public function xWeekRange($date, $day)
{
$ts = strtotime($date);
if (!$day) {
$start = (date('D', $ts) == 'Sun') ? $ts : strtotime('last sunday', $ts);
return date('Y-m-d', $start);
} else {
$end = (date('D', $ts) == 'Sat') ? $ts : strtotime('next saturday', $ts);
return date('Y-m-d', $end);
}
}
/**
* Method to sort through the acl items and put them in order
*
* @param array $items
* @return array
*/
public function sortItems($items)
{
foreach ($items as &$item) {
if (count($item['children'])) {
$item['children'] = $this->sortItems($item['children']);
}
}
usort($items, function ($a, $b) {
if ($a['sort'] == $b['sort']) {
return 0;
}
return ($a['sort'] < $b['sort']) ? -1 : 1;
});
return $this->convertToAssociativeArray($items);
}
/**
* @param string $fieldName
* @return array
*/
public function getConfigField($fieldName)
{
foreach (config('core') as $coreData) {
if (isset($coreData['fields'])) {
foreach ($coreData['fields'] as $field) {
$name = $coreData['key'] . '.' . $field['name'];
if ($name == $fieldName) {
return $field;
}
}
}
}
}
/**
* @param array $items
* @return array
*/
public function convertToAssociativeArray($items)
{
foreach ($items as $key1 => $level1) {
unset($items[$key1]);
$items[$level1['key']] = $level1;
if (count($level1['children'])) {
foreach ($level1['children'] as $key2 => $level2) {
$temp2 = explode('.', $level2['key']);
$finalKey2 = end($temp2);
unset($items[$level1['key']]['children'][$key2]);
$items[$level1['key']]['children'][$finalKey2] = $level2;
if (count($level2['children'])) {
foreach ($level2['children'] as $key3 => $level3) {
$temp3 = explode('.', $level3['key']);
$finalKey3 = end($temp3);
unset($items[$level1['key']]['children'][$finalKey2]['children'][$key3]);
$items[$level1['key']]['children'][$finalKey2]['children'][$finalKey3] = $level3;
}
}
}
}
}
return $items;
}
/**
* @param array $items
* @param string $key
* @param string|int|float $value
* @return array
*/
public function array_set(&$array, $key, $value)
{
if (is_null($key)) {
return $array = $value;
}
$keys = explode('.', $key);
$count = count($keys);
while (count($keys) > 1) {
$key = array_shift($keys);
if (!isset($array[$key]) || !is_array($array[$key])) {
$array[$key] = [];
}
$array = &$array[$key];
}
$finalKey = array_shift($keys);
if (isset($array[$finalKey])) {
$array[$finalKey] = $this->arrayMerge($array[$finalKey], $value);
} else {
$array[$finalKey] = $value;
}
return $array;
}
/**
* @param array $array1
* @param array $array2
* @return array
*/
protected function arrayMerge(array &$array1, array &$array2)
{
$merged = $array1;
foreach ($array2 as $key => &$value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = $this->arrayMerge($merged[$key], $value);
} else {
$merged[$key] = $value;
}
}
return $merged;
}
/**
* @param array $array1
* @return array
*/
public function convertEmptyStringsToNull($array)
{
foreach ($array as $key => $value) {
if ($value == "" || $value == "null") {
$array[$key] = null;
}
}
return $array;
}
/**
* Create singletom object through single facade
*
* @param string $className
* @return object
*/
public function getSingletonInstance($className)
{
static $instance = [];
if (array_key_exists($className, $instance)) {
return $instance[$className];
}
return $instance[$className] = app($className);
}
/**
* Returns a string as selector part for identifying elements in views
*
* @param float $taxRate
* @return string
*/
public static function taxRateAsIdentifier(float $taxRate): string
{
return str_replace('.', '_', (string)$taxRate);
}
/**
* Get Shop email sender details
*
* @return array
*/
public function getSenderEmailDetails()
{
$sender_name = core()->getConfigData('general.general.email_settings.sender_name') ? core()->getConfigData('general.general.email_settings.sender_name') : config('mail.from.name');
$sender_email = core()->getConfigData('general.general.email_settings.shop_email_from') ? core()->getConfigData('general.general.email_settings.shop_email_from') : config('mail.from.address');
return [
'name' => $sender_name,
'email' => $sender_email,
];
}
/**
* Get Admin email details
*
* @return array
*/
public function getAdminEmailDetails()
{
$admin_name = core()->getConfigData('general.general.email_settings.admin_name') ? core()->getConfigData('general.general.email_settings.admin_name') : config('mail.admin.name');
$admin_email = core()->getConfigData('general.general.email_settings.admin_email') ? core()->getConfigData('general.general.email_settings.admin_email') : config('mail.admin.address');
return [
'name' => $admin_name,
'email' => $admin_email,
];
}
}