From 71eb0cce704a7f125bb9f5c7c811c48c70f598c9 Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Tue, 8 Jun 2021 15:24:51 +0530 Subject: [PATCH] Some Refactor And Optimization Done --- .../views/catalog/products/edit.blade.php | 10 +-- .../views/configuration/index.blade.php | 10 +-- packages/Webkul/Core/src/Core.php | 63 ++++++++++++++----- .../Webkul/Product/src/Models/Product.php | 8 +-- .../Admin/ConfigurationController.php | 8 +-- .../views/admin/meta-info/meta-data.blade.php | 10 +-- 6 files changed, 62 insertions(+), 47 deletions(-) diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/products/edit.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/products/edit.blade.php index 617bd736a..ccc2f3e28 100755 --- a/packages/Webkul/Admin/src/Resources/views/catalog/products/edit.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/catalog/products/edit.blade.php @@ -7,13 +7,9 @@ @section('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]) !!} diff --git a/packages/Webkul/Admin/src/Resources/views/configuration/index.blade.php b/packages/Webkul/Admin/src/Resources/views/configuration/index.blade.php index 375a9ae56..c2641fe70 100755 --- a/packages/Webkul/Admin/src/Resources/views/configuration/index.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/configuration/index.blade.php @@ -7,13 +7,9 @@ @section('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
diff --git a/packages/Webkul/Core/src/Core.php b/packages/Webkul/Core/src/Core.php index 33bd7236e..17df0a603 100755 --- a/packages/Webkul/Core/src/Core.php +++ b/packages/Webkul/Core/src/Core.php @@ -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; } /** diff --git a/packages/Webkul/Product/src/Models/Product.php b/packages/Webkul/Product/src/Models/Product.php index ca14e497c..9141deb60 100755 --- a/packages/Webkul/Product/src/Models/Product.php +++ b/packages/Webkul/Product/src/Models/Product.php @@ -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) { diff --git a/packages/Webkul/Velocity/src/Http/Controllers/Admin/ConfigurationController.php b/packages/Webkul/Velocity/src/Http/Controllers/Admin/ConfigurationController.php index 1f9dc5de2..00b00015a 100644 --- a/packages/Webkul/Velocity/src/Http/Controllers/Admin/ConfigurationController.php +++ b/packages/Webkul/Velocity/src/Http/Controllers/Admin/ConfigurationController.php @@ -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(); } /** diff --git a/packages/Webkul/Velocity/src/Resources/views/admin/meta-info/meta-data.blade.php b/packages/Webkul/Velocity/src/Resources/views/admin/meta-info/meta-data.blade.php index 7c4f8daf1..0330fb0e7 100644 --- a/packages/Webkul/Velocity/src/Resources/views/admin/meta-info/meta-data.blade.php +++ b/packages/Webkul/Velocity/src/Resources/views/admin/meta-info/meta-data.blade.php @@ -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')