obey the default channel that has been set in config/app.php

This commit is contained in:
Herbert Maschke 2020-02-03 13:34:05 +01:00
parent d6fd342341
commit 4833d9f8ec
3 changed files with 228 additions and 199 deletions

View File

@ -3,6 +3,7 @@
namespace Webkul\Core;
use Carbon\Carbon;
use Webkul\Core\Models\Channel;
use Webkul\Core\Repositories\CurrencyRepository;
use Webkul\Core\Repositories\ExchangeRateRepository;
use Webkul\Core\Repositories\CountryRepository;
@ -66,13 +67,14 @@ class Core
/**
* Create a new instance.
*
* @param Webkul\Core\Repositories\ChannelRepository $channelRepository
* @param Webkul\Core\Repositories\CurrencyRepository $currencyRepository
* @param Webkul\Core\Repositories\ExchangeRateRepository $exchangeRateRepository
* @param Webkul\Core\Repositories\CountryRepository $countryRepository
* @param Webkul\Core\Repositories\CountryStateRepository $countryStateRepository
* @param Webkul\Core\Repositories\LocaleRepository $localeRepository
* @param Webkul\Core\Repositories\CoreConfigRepository $coreConfigRepository
* @param Webkul\Core\Repositories\ChannelRepository $channelRepository
* @param Webkul\Core\Repositories\CurrencyRepository $currencyRepository
* @param Webkul\Core\Repositories\ExchangeRateRepository $exchangeRateRepository
* @param Webkul\Core\Repositories\CountryRepository $countryRepository
* @param Webkul\Core\Repositories\CountryStateRepository $countryStateRepository
* @param Webkul\Core\Repositories\LocaleRepository $localeRepository
* @param Webkul\Core\Repositories\CoreConfigRepository $coreConfigRepository
*
* @return void
*/
public function __construct(
@ -101,10 +103,10 @@ class Core
}
/**
* Returns all channels
*
* @return Collection
*/
* Returns all channels
*
* @return Collection
*/
public function getAllChannels()
{
static $channels;
@ -116,10 +118,10 @@ class Core
}
/**
* Returns currenct channel models
*
* @return mixed
*/
* Returns currenct channel models
*
* @return mixed
*/
public function getCurrentChannel()
{
static $channel;
@ -128,10 +130,10 @@ class Core
return $channel;
$channel = $this->channelRepository->findWhereIn('hostname', [
request()->getHttpHost(),
'http://' . request()->getHttpHost(),
'https://' . request()->getHttpHost()
])->first();
request()->getHttpHost(),
'http://' . request()->getHttpHost(),
'https://' . request()->getHttpHost(),
])->first();
if (! $channel)
$channel = $this->channelRepository->first();
@ -140,11 +142,11 @@ class Core
}
/**
* Returns currenct channel code
*
* @return string
*/
public function getCurrentChannelCode()
* Returns currenct channel code
*
* @return string
*/
public function getCurrentChannelCode(): string
{
static $channelCode;
@ -155,40 +157,48 @@ class Core
}
/**
* Returns default channel models
*
* @return mixed
*/
public function getDefaultChannel()
* Returns default channel models
*
* @return null or Channel
*/
public function getDefaultChannel(): ?Channel
{
static $channel;
if ($channel)
if ($channel) {
return $channel;
}
$channel = $this->channelRepository->findOneByField('code', config('app.channel'));
if ($channel) {
return $channel;
}
return $channel = $this->channelRepository->first();
}
/**
* Returns default channel code
*
* @return string
*/
public function getDefaultChannelCode()
* Returns the default channel code configured in config/app.php
*
* @return string
*/
public function getDefaultChannelCode(): string
{
static $channelCode;
if ($channelCode)
if ($channelCode) {
return $channelCode;
}
return ($channel = $this->getDefaultChannel()) ? $channelCode = $channel->code : '';
}
/**
* Returns all locales
*
* @return Collection
*/
* Returns all locales
*
* @return Collection
*/
public function getAllLocales()
{
static $locales;
@ -200,20 +210,21 @@ class Core
}
/**
* Returns current locale
*
* @return Object
*/
* Returns current locale
*
* @return Object
*/
public function getCurrentLocale()
{
static $locale;
if ($locale)
if ($locale) {
return $locale;
}
$locale = $this->localeRepository->findOneByField('code', app()->getLocale());
if(!$locale) {
if (! $locale) {
$locale = $this->localeRepository->findOneByField('code', config('app.fallback_locale'));
}
@ -221,10 +232,10 @@ class Core
}
/**
* Returns all currencies
*
* @return Collection
*/
* Returns all currencies
*
* @return Collection
*/
public function getAllCurrencies()
{
static $currencies;
@ -236,10 +247,10 @@ class Core
}
/**
* Returns base channel's currency model
*
* @return mixed
*/
* Returns base channel's currency model
*
* @return mixed
*/
public function getBaseCurrency()
{
static $currency;
@ -256,10 +267,10 @@ class Core
}
/**
* Returns base channel's currency code
*
* @return string
*/
* Returns base channel's currency code
*
* @return string
*/
public function getBaseCurrencyCode()
{
static $currencyCode;
@ -271,10 +282,10 @@ class Core
}
/**
* Returns base channel's currency model
*
* @return mixed
*/
* Returns base channel's currency model
*
* @return mixed
*/
public function getChannelBaseCurrency()
{
static $currency;
@ -288,10 +299,10 @@ class Core
}
/**
* Returns base channel's currency code
*
* @return string
*/
* Returns base channel's currency code
*
* @return string
*/
public function getChannelBaseCurrencyCode()
{
static $currencyCode;
@ -303,10 +314,10 @@ class Core
}
/**
* Returns current channel's currency model
*
* @return mixed
*/
* Returns current channel's currency model
*
* @return mixed
*/
public function getCurrentCurrency()
{
static $currency;
@ -323,10 +334,10 @@ class Core
}
/**
* Returns current channel's currency code
*
* @return string
*/
* Returns current channel's currency code
*
* @return string
*/
public function getCurrentCurrencyCode()
{
static $currencyCode;
@ -338,20 +349,21 @@ class Core
}
/**
* Converts price
*
* @param float $amount
* @param string $targetCurrencyCode
* @return string
*/
* Converts price
*
* @param float $amount
* @param string $targetCurrencyCode
*
* @return string
*/
public function convertPrice($amount, $targetCurrencyCode = null)
{
if (! isset($this->lastCurrencyCode))
$this->lastCurrencyCode = $this->getBaseCurrency()->code ;
if (! isset($this->lastCurrencyCode))
$this->lastCurrencyCode = $this->getBaseCurrency()->code;
$targetCurrency = ! $targetCurrencyCode
? $this->getCurrentCurrency()
: $this->currencyRepository->findOneByField('code', $targetCurrencyCode);
? $this->getCurrentCurrency()
: $this->currencyRepository->findOneByField('code', $targetCurrencyCode);
if (! $targetCurrency)
return $amount;
@ -363,26 +375,27 @@ class Core
if (null === $exchangeRate || ! $exchangeRate->rate)
return $amount;
$result = (float) $amount * (float) ($this->lastCurrencyCode == $targetCurrency->code ? 1.0 : $exchangeRate->rate);
$result = (float)$amount * (float)($this->lastCurrencyCode == $targetCurrency->code ? 1.0 : $exchangeRate->rate);
if ($this->lastCurrencyCode != $targetCurrency->code)
$this->lastCurrencyCode = $targetCurrency->code;
$this->lastCurrencyCode = $targetCurrency->code;
return $result;
}
/**
* Converts to base price
*
* @param float $amount
* @param string $targetCurrencyCode
* @return string
*/
* 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);
? $this->getCurrentCurrency()
: $this->currencyRepository->findOneByField('code', $targetCurrencyCode);
if (! $targetCurrency)
return $amount;
@ -394,15 +407,16 @@ class Core
if (null === $exchangeRate || ! $exchangeRate->rate)
return $amount;
return (float) $amount / $exchangeRate->rate;
return (float)$amount / $exchangeRate->rate;
}
/**
* Format and convert price with currency symbol
*
* @param float $price
* @return string
*/
* Format and convert price with currency symbol
*
* @param float $price
*
* @return string
*/
public function currency($amount = 0)
{
if (is_null($amount))
@ -412,11 +426,12 @@ class Core
}
/**
* Return currency symbol from currency code
*
* @param float $price
* @return string
*/
* Return currency symbol from currency code
*
* @param float $price
*
* @return string
*/
public function currencySymbol($code)
{
$formatter = new \NumberFormatter(app()->getLocale() . '@currency=' . $code, \NumberFormatter::CURRENCY);
@ -425,17 +440,18 @@ class Core
}
/**
* Format and convert price with currency symbol
*
* @param float $price
* @return string
*/
* Format and convert price with currency symbol
*
* @param float $price
*
* @return string
*/
public function formatPrice($price, $currencyCode)
{
if (is_null($price))
$price = 0;
$formater = new \NumberFormatter( app()->getLocale(), \NumberFormatter::CURRENCY );
$formater = new \NumberFormatter(app()->getLocale(), \NumberFormatter::CURRENCY);
if ($symbol = $this->getCurrentCurrency()->symbol) {
if ($this->currencySymbol($currencyCode) == $symbol) {
@ -451,13 +467,13 @@ class Core
}
/**
* Format and convert price with currency symbol
*
* @return array
*/
* Format and convert price with currency symbol
*
* @return array
*/
public function getAccountJsSymbols()
{
$formater = new \NumberFormatter( app()->getLocale(), \NumberFormatter::CURRENCY );
$formater = new \NumberFormatter(app()->getLocale(), \NumberFormatter::CURRENCY);
$pattern = $formater->getPattern();
@ -466,24 +482,25 @@ class Core
$pattern = str_replace("#,##0.00", "%v", $pattern);
return [
'symbol' => core()->currencySymbol(core()->getCurrentCurrencyCode()),
'symbol' => core()->currencySymbol(core()->getCurrentCurrencyCode()),
'decimal' => $formater->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL),
'format' => $pattern
'format' => $pattern,
];
}
/**
* Format price with base currency symbol
*
* @param float $price
* @return string
*/
* 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 );
$formater = new \NumberFormatter(app()->getLocale(), \NumberFormatter::CURRENCY);
if ($symbol = $this->getBaseCurrency()->symbol) {
if ($this->currencySymbol($this->getBaseCurrencyCode()) == $symbol) {
@ -502,8 +519,9 @@ class Core
* 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
* @param string|null $dateFrom
* @param string|null $dateTo
*
* @return bool
*/
public function isChannelDateInInterval($dateFrom = null, $dateTo = null)
@ -512,9 +530,9 @@ class Core
$channelTimeStamp = $this->channelTimeStamp($channel);
$fromTimeStamp = strtotime($dateFrom);
$fromTimeStamp = strtotime($dateFrom);
$toTimeStamp = strtotime($dateTo);
$toTimeStamp = strtotime($dateTo);
if ($dateTo) {
$toTimeStamp += 86400;
@ -535,7 +553,8 @@ class Core
* Get channel timestamp
* Timstamp will be builded with channel timezone settings
*
* @param mixed $channel
* @param mixed $channel
*
* @return int
*/
public function channelTimeStamp($channel)
@ -554,11 +573,12 @@ class Core
}
/**
* Check whether sql date is empty
*
* @param string $date
* @return boolean
*/
* Check whether sql date is empty
*
* @param string $date
*
* @return boolean
*/
function is_empty_date($date)
{
return preg_replace('#[ 0:-]#', '', $date) === '';
@ -567,8 +587,9 @@ class Core
/**
* Format date using current channel.
*
* @param date|null $date
* @param string $format
* @param date|null $date
* @param string $format
*
* @return string
*/
public function formatDate($date = null, $format = 'd-m-Y H:i:s')
@ -587,7 +608,7 @@ class Core
/**
* Retrieve information from payment configuration
*
* @param string $field
* @param string $field
* @param int|string|null $channelId
*
* @return mixed
@ -618,25 +639,25 @@ class Core
if (isset($fields['channel_based']) && $fields['channel_based']) {
if (isset($fields['locale_based']) && $fields['locale_based']) {
$coreConfigValue = $this->coreConfigRepository->findOneWhere([
'code' => $field,
'code' => $field,
'channel_code' => $channel,
'locale_code' => $locale
'locale_code' => $locale,
]);
} else {
$coreConfigValue = $this->coreConfigRepository->findOneWhere([
'code' => $field,
'code' => $field,
'channel_code' => $channel,
]);
}
} else {
if (isset($fields['locale_based']) && $fields['locale_based']) {
$coreConfigValue = $this->coreConfigRepository->findOneWhere([
'code' => $field,
'locale_code' => $locale
'code' => $field,
'locale_code' => $locale,
]);
} else {
$coreConfigValue = $this->coreConfigRepository->findOneWhere([
'code' => $field
'code' => $field,
]);
}
}
@ -676,6 +697,7 @@ class Core
* Returns country name by code
*
* @param string $code
*
* @return string
*/
public function country_name($code)
@ -734,7 +756,8 @@ class Core
*
* @return array
*/
public function getTimeInterval($startDate, $endDate) {
public function getTimeInterval($startDate, $endDate)
{
$timeIntervals = [];
$totalDays = $startDate->diffInDays($endDate) + 1;
@ -750,9 +773,9 @@ class Core
$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');
$end = $totalMonths - 1 == $i
? $endDate
: Carbon::createFromTimeString($date->format('Y-m-d') . ' 23:59:59');
$timeIntervals[] = ['start' => $start, 'end' => $end, 'formatedDate' => $date->format('M')];
}
@ -762,11 +785,11 @@ class Core
$date->addWeeks($i);
$start = $i == 0
? $startDate
: Carbon::createFromTimeString($this->xWeekRange($date, 0) . ' 00:00:01');
? $startDate
: Carbon::createFromTimeString($this->xWeekRange($date, 0) . ' 00:00:01');
$end = $totalWeeks - 1 == $i
? $endDate
: Carbon::createFromTimeString($this->xWeekRange($date, 1) . ' 23:59:59');
? $endDate
: Carbon::createFromTimeString($this->xWeekRange($date, 1) . ' 23:59:59');
$timeIntervals[] = ['start' => $start, 'end' => $end, 'formatedDate' => $date->format('d M')];
}
@ -788,7 +811,8 @@ class Core
/**
* @return string
*/
public function xWeekRange($date, $day) {
public function xWeekRange($date, $day)
{
$ts = strtotime($date);
if (! $day) {
@ -802,40 +826,43 @@ class Core
}
}
/**
* 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']);
}
}
/**
* 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;
}
usort($items, function ($a, $b) {
if ($a['sort'] == $b['sort']) {
return 0;
}
return ($a['sort'] < $b['sort']) ? -1 : 1;
});
return ($a['sort'] < $b['sort']) ? -1 : 1;
});
return $this->convertToAssociativeArray($items);
return $this->convertToAssociativeArray($items);
}
/**
* @param string $fieldName
*
* @return array
*/
public function getConfigField($fieldName) {
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 ) {
if ($name == $fieldName) {
return $field;
}
}
@ -849,7 +876,7 @@ class Core
unset($items[$key1]);
$items[$level1['key']] = $level1;
if (count($level1['children'])) {
if (count($level1['children'])) {
foreach ($level1['children'] as $key2 => $level2) {
$temp2 = explode('.', $level2['key']);
$finalKey2 = end($temp2);
@ -866,20 +893,20 @@ class Core
}
}
}
}
}
}
return $items;
return $items;
}
public function array_set(&$array, $key, $value)
public function array_set(&$array, $key, $value)
{
if (is_null($key)) {
return $array = $value;
}
$keys = explode('.', $key);
$count = count($keys);
$count = count($keys);
while (count($keys) > 1) {
$key = array_shift($keys);
@ -891,28 +918,28 @@ class Core
$array = &$array[$key];
}
$finalKey = array_shift($keys);
if (isset($array[$finalKey])) {
$array[$finalKey] = $this->arrayMerge($array[$finalKey], $value);
} else {
$array[$finalKey] = $value;
}
$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;
}
}
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;
return $merged;
}
public function convertEmptyStringsToNull($array)
@ -930,6 +957,7 @@ class Core
* Create singletom object through single facade
*
* @param string $className
*
* @return object
*/
public function getSingletonInstance($className)

View File

@ -27,7 +27,7 @@ class Simple extends AbstractType
'admin::catalog.products.accordians.images',
'admin::catalog.products.accordians.categories',
'admin::catalog.products.accordians.channels',
'admin::catalog.products.accordians.product-links'
'admin::catalog.products.accordians.product-links',
];
/**
@ -60,6 +60,7 @@ class Simple extends AbstractType
*/
public function haveSufficientQuantity($qty)
{
return $qty <= $this->totalQuantity() ? true : (core()->getConfigData('catalog.inventory.stock_options.backorders') ? true : false);
$backorders = core()->getConfigData('catalog.inventory.stock_options.backorders');
return $qty <= $this->totalQuantity() ? true : $backorders;
}
}