Some Refactor And Optimization Done

This commit is contained in:
devansh bawari 2021-06-08 15:24:51 +05:30
parent 572fb12095
commit 71eb0cce70
6 changed files with 62 additions and 47 deletions

View File

@ -7,13 +7,9 @@
@section('content')
<div class="content">
@php
/* getting channel and locales */
$mainConfigurations = core()->getChannelCodeAndLocaleCode();
/* assigning */
$locale = $mainConfigurations['localeCode'];
$channel = $mainConfigurations['channelCode'];
$channelLocales = $mainConfigurations['channelLocales'];
$locale = core()->checkRequestedLocaleInChannel();
$channel = core()->getRequestedChannelCode();
$channelLocales = core()->getAllLocalesByRequestedChannelCode()['locales'];
@endphp
{!! view_render_event('bagisto.admin.catalog.product.edit.before', ['product' => $product]) !!}

View File

@ -7,13 +7,9 @@
@section('content')
<div class="content">
@php
/* getting channel and locales */
$mainConfigurations = core()->getChannelCodeAndLocaleCode();
/* assigning */
$locale = $mainConfigurations['localeCode'];
$channel = $mainConfigurations['channelCode'];
$channelLocales = $mainConfigurations['channelLocales'];
$locale = core()->checkRequestedLocaleInChannel();
$channel = core()->getRequestedChannelCode();
$channelLocales = core()->getAllLocalesByRequestedChannelCode()['locales'];
@endphp
<form method="POST" action="" @submit.prevent="onSubmit" enctype="multipart/form-data">

View File

@ -232,6 +232,16 @@ class Core
return ($channel = $this->getDefaultChannel()) ? $channelCode = $channel->code : '';
}
/**
* Get channel code from request.
*
* @return string
*/
public function getRequestedChannelCode(): string
{
return request()->get('channel') ?: ($this->getCurrentChannelCode() ?: $this->getDefaultChannelCode());
}
/**
* Returns the channel name.
*
@ -245,7 +255,7 @@ class Core
}
/**
* Returns all locales
* Return all locales.
*
* @return \Illuminate\Support\Collection
*/
@ -260,6 +270,27 @@ class Core
return $locales = $this->localeRepository->all();
}
/**
* Return all locales which are present in requested channel.
*
* @return array
*/
public function getAllLocalesByRequestedChannelCode()
{
static $data = [];
if (! empty($data)) {
return $data;
}
$channel = $this->channelRepository->findOneByField('code', $this->getRequestedChannelCode());
return $data = [
'channel' => $channel,
'locales' => $channel->locales
];
}
/**
* Returns current locale.
*
@ -283,25 +314,29 @@ class Core
}
/**
* Get channel code and locale code based on the request.
* Get locale code from request.
*
* @return array
* @return string
*/
public function getChannelCodeAndLocaleCode()
public function getRequestedLocaleCode(): string
{
$both = [
'channelCode' => request()->get('channel') ?: (core()->getCurrentChannelCode() ?: core()->getDefaultChannelCode()),
'localeCode' => request()->get('locale') ?: app()->getLocale()
];
return request()->get('locale') ?: app()->getLocale();
}
$channel = $this->channelRepository->findOneByField('code', $both['channelCode']);
$both['channelLocales'] = $channel->locales;
/**
* Check requested locale in channel. If not found then set channel default locale.
*
* @return string
*/
public function checkRequestedLocaleInChannel()
{
$localeCode = $this->getRequestedLocaleCode();
if (! $both['channelLocales']->contains('code', $both['localeCode'])) {
$both['localeCode'] = $channel->default_locale->code;
}
$channelLocales = $this->getAllLocalesByRequestedChannelCode();
return $both;
return ! $channelLocales['locales']->contains('code', $localeCode)
? $channelLocales['channel']->default_locale->code
: $localeCode;
}
/**

View File

@ -391,12 +391,8 @@ class Product extends Model implements ProductContract
return;
}
/* getting channel and locales */
$mainConfigurations = core()->getChannelCodeAndLocaleCode();
/* assigning */
$locale = $mainConfigurations['localeCode'];
$channel = $mainConfigurations['channelCode'];
$locale = core()->checkRequestedLocaleInChannel();
$channel = core()->getRequestedChannelCode();
if ($attribute->value_per_channel) {
if ($attribute->value_per_locale) {

View File

@ -41,12 +41,8 @@ class ConfigurationController extends Controller
$this->velocityHelper = app('Webkul\Velocity\Helpers\Helper');
$this->velocityMetaDataRepository = $velocityMetadataRepository;
/* getting channel and locales */
$mainConfigurations = core()->getChannelCodeAndLocaleCode();
/* assigning */
$this->locale = $mainConfigurations['localeCode'];
$this->channel = $mainConfigurations['channelCode'];
$this->locale = core()->checkRequestedLocaleInChannel();
$this->channel = core()->getRequestedChannelCode();
}
/**

View File

@ -5,13 +5,9 @@
@stop
@php
/* getting channel and locales */
$mainConfigurations = core()->getChannelCodeAndLocaleCode();
/* assigning */
$locale = $mainConfigurations['localeCode'];
$channel = $mainConfigurations['channelCode'];
$channelLocales = $mainConfigurations['channelLocales'];
$locale = core()->checkRequestedLocaleInChannel();
$channel = core()->getRequestedChannelCode();
$channelLocales = core()->getAllLocalesByRequestedChannelCode()['locales'];
@endphp
@section('content')