sarga/packages/Webkul/Core/src/Core.php

766 lines
19 KiB
PHP
Raw Normal View History

2018-07-24 11:11:32 +00:00
<?php
namespace Webkul\Core;
2018-08-30 13:22:15 +00:00
use Carbon\Carbon;
2018-10-04 06:42:06 +00:00
use Webkul\Core\Repositories\CurrencyRepository;
use Webkul\Core\Repositories\ExchangeRateRepository;
2018-10-12 12:54:10 +00:00
use Webkul\Core\Repositories\CountryRepository;
use Webkul\Core\Repositories\CountryStateRepository;
2018-10-17 07:21:47 +00:00
use Webkul\Core\Repositories\ChannelRepository;
use Webkul\Core\Repositories\LocaleRepository;
use Webkul\Core\Repositories\CoreConfigRepository;
2018-09-27 07:53:26 +00:00
use Illuminate\Support\Facades\Config;
2018-07-24 11:11:32 +00:00
class Core
{
2018-10-04 06:42:06 +00:00
/**
2018-10-17 07:21:47 +00:00
* ChannelRepository class
*
* @var mixed
*/
protected $channelRepository;
/**
* CurrencyRepository class
2018-10-04 06:42:06 +00:00
*
* @var mixed
*/
protected $currencyRepository;
/**
2018-10-17 07:21:47 +00:00
* ExchangeRateRepository class
2018-10-04 06:42:06 +00:00
*
* @var mixed
*/
protected $exchangeRateRepository;
2018-10-12 12:54:10 +00:00
/**
2018-10-17 07:21:47 +00:00
* CountryRepository class
2018-10-12 12:54:10 +00:00
*
* @var mixed
*/
protected $countryRepository;
/**
2018-10-17 07:21:47 +00:00
* CountryStateRepository class
2018-10-12 12:54:10 +00:00
*
* @var mixed
*/
protected $countryStateRepository;
2018-10-17 07:21:47 +00:00
/**
* LocaleRepository class
*
* @var mixed
*/
protected $localeRepository;
/**
* CoreConfigRepository class
*
* @var mixed
*/
protected $coreConfigRepository;
2018-10-04 06:42:06 +00:00
/**
* Create a new instance.
*
2018-10-17 07:21:47 +00:00
* @param Webkul\Core\Repositories\ChannelRepository $channelRepository
2018-10-04 06:42:06 +00:00
* @param Webkul\Core\Repositories\CurrencyRepository $currencyRepository
* @param Webkul\Core\Repositories\ExchangeRateRepository $exchangeRateRepository
2018-10-12 12:54:10 +00:00
* @param Webkul\Core\Repositories\CountryRepository $countryRepository
* @param Webkul\Core\Repositories\CountryStateRepository $countryStateRepository
2018-10-17 07:21:47 +00:00
* @param Webkul\Core\Repositories\LocaleRepository $localeRepository
* @param Webkul\Core\Repositories\CoreConfigRepository $coreConfigRepository
2018-10-04 06:42:06 +00:00
* @return void
*/
2018-10-12 08:22:06 +00:00
public function __construct(
2018-10-17 07:21:47 +00:00
ChannelRepository $channelRepository,
2018-10-12 08:22:06 +00:00
CurrencyRepository $currencyRepository,
2018-10-12 12:54:10 +00:00
ExchangeRateRepository $exchangeRateRepository,
CountryRepository $countryRepository,
2018-10-17 07:21:47 +00:00
CountryStateRepository $countryStateRepository,
LocaleRepository $localeRepository,
CoreConfigRepository $coreConfigRepository
2018-10-12 08:22:06 +00:00
)
2018-10-04 06:42:06 +00:00
{
2018-10-17 07:21:47 +00:00
$this->channelRepository = $channelRepository;
2018-10-04 06:42:06 +00:00
$this->currencyRepository = $currencyRepository;
$this->exchangeRateRepository = $exchangeRateRepository;
2018-10-12 12:54:10 +00:00
$this->countryRepository = $countryRepository;
$this->countryStateRepository = $countryStateRepository;
2018-10-17 07:21:47 +00:00
$this->localeRepository = $localeRepository;
$this->coreConfigRepository = $coreConfigRepository;
2018-10-04 06:42:06 +00:00
}
2018-08-21 10:58:55 +00:00
/**
* Returns all channels
*
* @return Collection
*/
2018-10-17 13:57:51 +00:00
public function getAllChannels()
{
2018-08-30 13:22:15 +00:00
static $channels;
if($channels)
return $channels;
2018-10-17 07:21:47 +00:00
return $channels = $this->channelRepository->all();
2018-08-21 10:58:55 +00:00
}
/**
* Returns currenct channel models
*
* @return mixed
*/
2018-10-17 13:57:51 +00:00
public function getCurrentChannel()
{
2018-08-30 13:22:15 +00:00
static $channel;
if($channel)
return $channel;
2018-10-17 13:57:51 +00:00
$channel = $this->channelRepository->findWhereIn('hostname', [
request()->getHttpHost(),
'http://' . request()->getHttpHost(),
'https://' . request()->getHttpHost()
])->first();
2018-10-25 16:15:28 +00:00
2018-10-17 13:57:51 +00:00
if(!$channel)
$channel = $this->channelRepository->first();
return $channel;
2018-08-21 10:58:55 +00:00
}
/**
* Returns currenct channel code
*
* @return string
*/
2018-10-17 13:57:51 +00:00
public function getCurrentChannelCode()
{
2018-08-30 13:22:15 +00:00
static $channelCode;
if($channelCode)
return $channelCode;
return ($channel = $this->getCurrentChannel()) ? $channelCode = $channel->code : '';
2018-08-21 10:58:55 +00:00
}
2018-10-10 05:50:47 +00:00
/**
* Returns default channel models
*
* @return mixed
*/
2018-10-17 13:57:51 +00:00
public function getDefaultChannel()
{
2018-10-10 05:50:47 +00:00
static $channel;
if($channel)
return $channel;
2018-10-17 07:21:47 +00:00
return $channel = $this->channelRepository->first();
2018-10-10 05:50:47 +00:00
}
/**
* Returns default channel code
*
* @return string
*/
2018-10-17 13:57:51 +00:00
public function getDefaultChannelCode()
{
2018-10-10 05:50:47 +00:00
static $channelCode;
if($channelCode)
return $channelCode;
return ($channel = $this->getDefaultChannel()) ? $channelCode = $channel->code : '';
}
2018-08-21 10:58:55 +00:00
/**
* Returns all locales
*
* @return Collection
*/
2018-10-17 13:57:51 +00:00
public function getAllLocales()
{
2018-08-30 13:22:15 +00:00
static $locales;
if($locales)
return $locales;
2018-10-17 07:21:47 +00:00
return $locales = $this->localeRepository->all();
2018-07-24 11:11:32 +00:00
}
2018-08-21 10:58:55 +00:00
/**
* Returns all currencies
*
* @return Collection
*/
public function getAllCurrencies()
{
2018-08-30 13:22:15 +00:00
static $currencies;
if($currencies)
return $currencies;
2018-10-17 07:21:47 +00:00
return $currencies = $this->currencyRepository->all();
2018-07-24 11:11:32 +00:00
}
2018-08-21 10:58:55 +00:00
2018-10-04 06:42:06 +00:00
/**
* Returns base channel's currency model
*
* @return mixed
*/
public function getBaseCurrency()
{
static $currency;
if($currency)
return $currency;
2018-10-17 07:21:47 +00:00
$baseCurrency = $this->currencyRepository->findOneByField('code', config('app.currency'));
2018-10-04 06:42:06 +00:00
2018-10-17 07:21:47 +00:00
if(!$baseCurrency)
$baseCurrency = $this->currencyRepository->first();
return $currency = $baseCurrency;
2018-10-04 06:42:06 +00:00
}
/**
* Returns base channel's currency code
*
* @return string
*/
public function getBaseCurrencyCode()
{
static $currencyCode;
if($currencyCode)
return $currencyCode;
return ($currency = $this->getBaseCurrency()) ? $currencyCode = $currency->code : '';
}
2018-10-17 07:21:47 +00:00
/**
* Returns base channel's currency model
*
* @return mixed
*/
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 : '';
}
2018-08-21 10:58:55 +00:00
/**
* Returns current channel's currency model
*
* @return mixed
*/
public function getCurrentCurrency()
{
2018-08-30 13:22:15 +00:00
static $currency;
2018-08-21 10:58:55 +00:00
2018-08-30 13:22:15 +00:00
if($currency)
return $currency;
2018-08-21 10:58:55 +00:00
if($currencyCode = session()->get('currency')) {
if($currency = $this->currencyRepository->findOneByField('code', $currencyCode))
return $currency;
}
return $currency = $this->getChannelBaseCurrency();
2018-08-21 10:58:55 +00:00
}
/**
* Returns current channel's currency code
*
* @return string
*/
public function getCurrentCurrencyCode()
{
2018-08-30 13:22:15 +00:00
static $currencyCode;
if($currencyCode)
return $currencyCode;
return ($currency = $this->getCurrentCurrency()) ? $currencyCode = $currency->code : '';
2018-08-21 10:58:55 +00:00
}
2018-09-26 10:19:18 +00:00
/**
2018-10-17 07:21:47 +00:00
* Converts price
2018-09-26 10:19:18 +00:00
*
2018-10-17 07:21:47 +00:00
* @param float $price
* @param string $targetCurrencyCode
* @return string
2018-09-26 10:19:18 +00:00
*/
2018-10-17 07:21:47 +00:00
public function convertPrice($amount, $targetCurrencyCode = null)
2018-09-26 10:19:18 +00:00
{
2018-10-04 06:42:06 +00:00
$targetCurrency = !$targetCurrencyCode
? $this->getCurrentCurrency()
: $this->currencyRepository->findByField('code', $targetCurrencyCode);
2018-10-17 07:21:47 +00:00
if(!$targetCurrency)
2018-10-04 06:42:06 +00:00
return $amount;
$exchangeRate = $this->exchangeRateRepository->findOneWhere([
'target_currency' => $targetCurrency->id,
]);
if (null === $exchangeRate)
return $amount;
2018-10-17 07:21:47 +00:00
return (float) round($amount * $exchangeRate->rate);
2018-09-26 10:19:18 +00:00
}
2018-08-21 10:58:55 +00:00
/**
* Format and convert price with currency symbol
*
* @param float $price
* @return string
*/
2018-10-04 06:42:06 +00:00
public function currency($amount = 0)
2018-08-21 10:58:55 +00:00
{
if(is_null($amount))
2018-10-12 08:22:06 +00:00
$amount = 0;
2018-10-04 06:42:06 +00:00
$currencyCode = $this->getCurrentCurrency()->code;
2018-10-04 06:42:06 +00:00
return currency($this->convertPrice($amount), $currencyCode);
2018-08-21 10:58:55 +00:00
}
2018-09-26 10:19:18 +00:00
/**
* Format and convert price with currency symbol
*
* @param float $price
* @return string
*/
public function formatPrice($price, $currencyCode)
{
if(is_null($price))
$price = 0;
2018-09-26 10:19:18 +00:00
return currency($price, $currencyCode);
}
/**
* Format price with base currency symbol
*
* @param float $price
* @return string
*/
public function formatBasePrice($price)
{
if(is_null($price))
$price = 0;
2018-10-25 16:15:28 +00:00
return currency($price, $this->getBaseCurrencyCode());
}
2018-08-21 10:58:55 +00:00
/**
* Checks if current date of the given channel (in the channel timezone) is within the range
*
* @param int|string|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;
}
$result = false;
if (!$this->is_empty_date($dateFrom) && $channelTimeStamp < $fromTimeStamp) {
} elseif (!$this->is_empty_date($dateTo) && $channelTimeStamp > $toTimeStamp) {
} else {
$result = true;
}
return $result;
}
/**
* Get channel timestamp
* Timstamp will be builded with channel timezone settings
*
* @param mixed $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 boolean
*/
function is_empty_date($date)
{
return preg_replace('#[ 0:-]#', '', $date) === '';
}
2018-08-30 13:22:15 +00:00
/**
* Format date using current channel.
*
* @param date|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);
}
2018-09-27 07:53:26 +00:00
/**
* Retrieve information from payment configuration
*
* @param string $field
* @param int|string|null $channelId
*
* @return mixed
*/
public function getConfigData($field, $channel = null, $locale = null)
2018-09-27 07:53:26 +00:00
{
if (null === $channel) {
2018-12-07 07:08:29 +00:00
$channel = request()->get('channel') ?: ($this->getCurrentChannelCode() ?: $this->getDefaultChannelCode());
}
if (null === $locale) {
2018-12-07 07:08:29 +00:00
$locale = request()->get('locale') ?: app()->getLocale();
2018-09-27 07:53:26 +00:00
}
2018-12-14 06:26:52 +00:00
$fields = $this->getConfigField($field);
$channel_based = false;
$locale_based = false;
if (isset($fields['channel_based']) && $fields['channel_based']) {
$channel_based = true;
}
if (isset($fields['locale_based']) && $fields['locale_based']) {
$locale_based = true;
}
if (isset($fields['channel_based']) && $fields['channel_based']) {
if (isset($fields['locale_based']) && $fields['locale_based']) {
$coreConfigValue = $this->coreConfigRepository->findOneWhere([
'code' => $field,
'channel_code' => $channel,
'locale_code' => $locale
]);
} else {
$coreConfigValue = $this->coreConfigRepository->findOneWhere([
'code' => $field,
'channel_code' => $channel,
]);
}
} else {
if (isset($fields['locale_based']) && $fields['locale_based']) {
$coreConfigValue = $this->coreConfigRepository->findOneWhere([
'code' => $field,
'locale_code' => $locale
]);
}
else {
$coreConfigValue = $this->coreConfigRepository->findOneWhere([
'code' => $field,
]);
}
}
2018-12-07 07:08:29 +00:00
if(!$coreConfigValue)
return Config::get($field);
return $coreConfigValue->value;
2018-09-27 07:53:26 +00:00
}
2018-10-12 08:22:06 +00:00
/**
* Retrieve all countries
*
* @return Collection
*/
public function countries()
{
2018-10-12 12:54:10 +00:00
return $this->countryRepository->all();
2018-10-12 08:22:06 +00:00
}
/**
* Retrieve all country states
*
* @return Collection
*/
public function states($countryCode)
{
2018-10-12 12:54:10 +00:00
return $this->countryStateRepository->findByField('country_code', $countryCode);
2018-10-12 08:22:06 +00:00
}
/**
* Retrieve all grouped states by country code
*
* @return Collection
*/
public function groupedStatesByCountries()
{
$collection = [];
2018-10-12 12:54:10 +00:00
foreach ($this->countries() as $country) {
$collection[$country->code] = $this->states($country->code)->toArray();
2018-10-12 08:22:06 +00:00
}
return $collection;
}
2018-10-23 11:45:44 +00:00
/**
* Returns time intervals
*
* @return array
*/
public function getTimeInterval($startDate, $endDate) {
$timeIntervals = [];
2018-10-26 04:20:43 +00:00
$totalDays = $startDate->diffInDays($endDate) + 1;
$totalMonths = $startDate->diffInMonths($endDate) + 1;
2018-10-25 16:15:28 +00:00
2018-10-23 11:45:44 +00:00
$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) {
2018-10-25 16:15:28 +00:00
for ($i = 0; $i < $totalMonths; $i++) {
2018-10-23 11:45:44 +00:00
$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) {
2018-10-25 16:15:28 +00:00
for ($i = 0; $i < $totalWeeks; $i++) {
2018-10-23 11:45:44 +00:00
$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 {
2018-10-25 16:15:28 +00:00
for ($i = 0; $i < $totalDays; $i++) {
2018-10-23 11:45:44 +00:00
$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;
}
/**
* @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);
}
}
2018-12-14 06:26:52 +00:00
/**
* Method to sort through the acl items and put them in order
*
* @return void
*/
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);
2018-12-14 06:26:52 +00:00
}
/**
* @param string $fieldName
* @return array
*/
public function getConfigField($fieldName) {
foreach (config('core') as $coreData) {
if (isset($coreData['fields'])) {
foreach ($coreData['fields'] as $field) {
$key = $coreData['key'];
$key = explode(".", $key);
array_shift($key);
$key = implode(".", $key);
$name = $key . '.' . $field['name'];
if ($name == $fieldName ) {
return $field;
}
}
}
}
}
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;
}
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;
}
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;
}
2018-07-24 11:11:32 +00:00
}