Merge pull request #2373 from Haendlerbund/locale-improvements

add improvements to locale determination on product creation
This commit is contained in:
Jitendra Singh 2020-02-18 15:10:14 +05:30 committed by GitHub
commit 7d161e31ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 152 additions and 67 deletions

View File

@ -6,7 +6,6 @@
@section('content')
<div class="content">
<?php $locale = request()->get('locale') ?: app()->getLocale(); ?>
<form method="POST" action="" @submit.prevent="onSubmit" enctype="multipart/form-data">

View File

@ -6,9 +6,6 @@
@section('content')
<div class="content">
<?php $locale = request()->get('locale') ?: app()->getLocale(); ?>
<?php $channel = request()->get('channel') ?: core()->getDefaultChannelCode(); ?>
{!! view_render_event('bagisto.admin.catalog.product.edit.before', ['product' => $product]) !!}
<form method="POST" action="" @submit.prevent="onSubmit" enctype="multipart/form-data">
@ -28,7 +25,7 @@
@foreach (core()->getAllChannels() as $channelModel)
<option
value="{{ $channelModel->code }}" {{ ($channelModel->code) == $channel ? 'selected' : '' }}>
value="{{ $channelModel->code }}" {{ ($channelModel->code) == $channelCode ? 'selected' : '' }}>
{{ $channelModel->name }}
</option>
@ -41,7 +38,7 @@
@foreach (core()->getAllLocales() as $localeModel)
<option
value="{{ $localeModel->code }}" {{ ($localeModel->code) == $locale ? 'selected' : '' }}>
value="{{ $localeModel->code }}" {{ ($localeModel->code) == $localeCode ? 'selected' : '' }}>
{{ $localeModel->name }}
</option>
@ -114,11 +111,11 @@
$channel_locale = [];
if ($attribute->value_per_channel) {
array_push($channel_locale, $channel);
array_push($channel_locale, $channelCode);
}
if ($attribute->value_per_locale) {
array_push($channel_locale, $locale);
array_push($channel_locale, $localeCode);
}
?>

View File

@ -39,8 +39,9 @@ class CategoryController extends Controller
/**
* Create a new controller instance.
*
* @param \Webkul\Category\Repositories\CategoryRepository $categoryRepository
* @param \Webkul\Attribute\Repositories\AttributeRepository $attributeRepository
* @param \Webkul\Category\Repositories\CategoryRepository $categoryRepository
* @param \Webkul\Attribute\Repositories\AttributeRepository $attributeRepository
*
* @return void
*/
public function __construct(
@ -74,7 +75,7 @@ class CategoryController extends Controller
{
$categories = $this->categoryRepository->getCategoryTree(null, ['id']);
$attributes = $this->attributeRepository->findWhere(['is_filterable' => 1]);
$attributes = $this->attributeRepository->findWhere(['is_filterable' => 1]);
return view($this->_config['view'], compact('categories', 'attributes'));
}
@ -87,10 +88,10 @@ class CategoryController extends Controller
public function store()
{
$this->validate(request(), [
'slug' => ['required', 'unique:category_translations,slug', new \Webkul\Core\Contracts\Validations\Slug],
'name' => 'required',
'image.*' => 'mimes:jpeg,jpg,bmp,png',
'description' => 'required_if:display_mode,==,description_only,products_and_description'
'slug' => ['required', 'unique:category_translations,slug', new \Webkul\Core\Contracts\Validations\Slug],
'name' => 'required',
'image.*' => 'mimes:jpeg,jpg,bmp,png',
'description' => 'required_if:display_mode,==,description_only,products_and_description',
]);
if (strtolower(request()->input('name')) == 'root') {
@ -98,14 +99,14 @@ class CategoryController extends Controller
$result = $categoryTransalation->where('name', request()->input('name'))->get();
if(count($result) > 0) {
if (count($result) > 0) {
session()->flash('error', trans('admin::app.response.create-root-failure'));
return redirect()->back();
}
}
$category = $this->categoryRepository->create(request()->all());
$this->categoryRepository->create(request()->all());
session()->flash('success', trans('admin::app.response.create-success', ['name' => 'Category']));
@ -116,6 +117,7 @@ class CategoryController extends Controller
* Show the form for editing the specified resource.
*
* @param int $id
*
* @return \Illuminate\View\View
*/
public function edit($id)
@ -124,15 +126,25 @@ class CategoryController extends Controller
$categories = $this->categoryRepository->getCategoryTreeWithoutDescendant($id);
$attributes = $this->attributeRepository->findWhere(['is_filterable' => 1]);
$attributes = $this->attributeRepository->findWhere(['is_filterable' => 1]);
return view($this->_config['view'], compact('category', 'categories', 'attributes'));
$locale = request()->get('locale')
?: core()->getDefaultChannel()->default_locale->code
?: app()->getLocale();
return view($this->_config['view'], compact(
'category',
'categories',
'attributes',
'locale'
));
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function update($id)
@ -146,7 +158,7 @@ class CategoryController extends Controller
}
}],
$locale . '.name' => 'required',
'image.*' => 'mimes:jpeg,jpg,bmp,png'
'image.*' => 'mimes:jpeg,jpg,bmp,png',
]);
$this->categoryRepository->update(request()->all(), $id);
@ -159,14 +171,15 @@ class CategoryController extends Controller
/**
* Remove the specified resource from storage.
*
* @param int $id
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$category = $this->categoryRepository->findOrFail($id);
if(strtolower($category->name) == "root") {
if (strtolower($category->name) == "root") {
session()->flash('warning', trans('admin::app.response.delete-category-root', ['name' => 'Category']));
} else {
try {
@ -179,7 +192,7 @@ class CategoryController extends Controller
session()->flash('success', trans('admin::app.response.delete-success', ['name' => 'Category']));
return response()->json(['message' => true], 200);
} catch(\Exception $e) {
} catch (\Exception $e) {
session()->flash('error', trans('admin::app.response.delete-failed', ['name' => 'Category']));
}
}
@ -192,7 +205,8 @@ class CategoryController extends Controller
*
* @return response \Illuminate\Http\Response
*/
public function massDestroy() {
public function massDestroy()
{
$suppressFlash = false;
if (request()->isMethod('delete') || request()->isMethod('post')) {
@ -205,7 +219,7 @@ class CategoryController extends Controller
$this->categoryRepository->delete($value);
Event::dispatch('catalog.category.delete.after', $value);
} catch(\Exception $e) {
} catch (\Exception $e) {
$suppressFlash = true;
continue;

View File

@ -135,6 +135,9 @@ class Core
'https://' . request()->getHttpHost(),
])->first();
if (! $channel)
$channel = $this->channelRepository->findOneByField('code', config('app.channel'));
if (! $channel)
$channel = $this->channelRepository->first();

View File

@ -10,7 +10,19 @@ use Webkul\Core\Contracts\Channel as ChannelContract;
class Channel extends Model implements ChannelContract
{
protected $fillable = ['code', 'name', 'description', 'theme', 'home_page_content', 'footer_content', 'hostname', 'default_locale_id', 'base_currency_id', 'root_category_id', 'home_seo'];
protected $fillable = [
'code',
'name',
'description',
'theme',
'home_page_content',
'footer_content',
'hostname',
'default_locale_id',
'base_currency_id',
'root_category_id',
'home_seo',
];
/**
* Get the channel locales.

View File

@ -73,12 +73,13 @@ class ProductController extends Controller
/**
* Create a new controller instance.
*
* @param \Webkul\Category\Repositories\CategoryRepository $categoryRepository
* @param \Webkul\Product\Repositories\ProductRepository $productRepository
* @param \Webkul\Product\Repositories\ProductDownloadableLinkRepository $productDownloadableLinkRepository
* @param \Webkul\Product\Repositories\ProductDownloadableSampleRepository $productDownloadableSampleRepository
* @param \Webkul\Attribute\Repositories\AttributeFamilyRepository $attributeFamilyRepository
* @param \Webkul\Inventory\Repositories\InventorySourceRepository $inventorySource
* @param \Webkul\Category\Repositories\CategoryRepository $categoryRepository
* @param \Webkul\Product\Repositories\ProductRepository $productRepository
* @param \Webkul\Product\Repositories\ProductDownloadableLinkRepository $productDownloadableLinkRepository
* @param \Webkul\Product\Repositories\ProductDownloadableSampleRepository $productDownloadableSampleRepository
* @param \Webkul\Attribute\Repositories\AttributeFamilyRepository $attributeFamilyRepository
* @param \Webkul\Inventory\Repositories\InventorySourceRepository $inventorySource
*
* @return void
*/
public function __construct(
@ -149,7 +150,7 @@ class ProductController extends Controller
if (ProductType::hasVariants(request()->input('type'))
&& (! request()->has('super_attributes')
|| ! count(request()->get('super_attributes')))) {
|| ! count(request()->get('super_attributes')))) {
session()->flash('error', trans('admin::app.catalog.products.configurable-error'));
@ -157,9 +158,9 @@ class ProductController extends Controller
}
$this->validate(request(), [
'type' => 'required',
'type' => 'required',
'attribute_family_id' => 'required',
'sku' => ['required', 'unique:products,sku', new \Webkul\Core\Contracts\Validations\Slug]
'sku' => ['required', 'unique:products,sku', new \Webkul\Core\Contracts\Validations\Slug],
]);
$product = $this->productRepository->create(request()->all());
@ -172,7 +173,8 @@ class ProductController extends Controller
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @param int $id
*
* @return \Illuminate\View\View
*/
public function edit($id)
@ -183,14 +185,25 @@ class ProductController extends Controller
$inventorySources = $this->inventorySourceRepository->all();
return view($this->_config['view'], compact('product', 'categories', 'inventorySources'));
$channel = request()->get('channel') ?: core()->getDefaultChannel();
$channelCode = $channel->code;
$localeCode = request()->get('localeCode') ?: $channel->default_locale->code ?: app()->getLocale();
return view($this->_config['view'], compact('product',
'categories',
'inventorySources',
'channel',
'channelCode',
'localeCode'
));
}
/**
* Update the specified resource in storage.
*
* @param \Webkul\Product\Http\Requests\ProductForm $request
* @param int $id
* @param \Webkul\Product\Http\Requests\ProductForm $request
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function update(ProductForm $request, $id)
@ -205,7 +218,8 @@ class ProductController extends Controller
/**
* Uploads downloadable file
*
* @param int $id
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function uploadLink($id)
@ -218,7 +232,8 @@ class ProductController extends Controller
/**
* Uploads downloadable sample file
*
* @param int $id
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function uploadSample($id)
@ -231,7 +246,8 @@ class ProductController extends Controller
/**
* Remove the specified resource from storage.
*
* @param int $id
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function destroy($id)
@ -287,7 +303,7 @@ class ProductController extends Controller
return redirect()->back();
}
if (!$data['massaction-type'] == 'update') {
if (! $data['massaction-type'] == 'update') {
return redirect()->back();
}
@ -296,8 +312,8 @@ class ProductController extends Controller
foreach ($productIds as $productId) {
$this->productRepository->update([
'channel' => null,
'locale' => null,
'status' => $data['update-options']
'locale' => null,
'status' => $data['update-options'],
], $productId);
}
@ -328,10 +344,10 @@ class ProductController extends Controller
foreach ($this->productRepository->searchProductByAttribute(request()->input('query')) as $row) {
$results[] = [
'id' => $row->product_id,
'sku' => $row->sku,
'name' => $row->name,
];
'id' => $row->product_id,
'sku' => $row->sku,
'name' => $row->name,
];
}
return response()->json($results);
@ -340,17 +356,18 @@ class ProductController extends Controller
}
}
/**
/**
* Download image or file
*
* @param int $productId, $attributeId
* @param int $productId , $attributeId
*
* @return \Illuminate\Http\Response
*/
public function download($productId, $attributeId)
{
$productAttribute = $this->productAttributeValue->findOneWhere([
'product_id' => $productId,
'attribute_id' => $attributeId
'attribute_id' => $attributeId,
]);
return Storage::download($productAttribute['text_value']);

View File

@ -2,6 +2,7 @@
namespace Webkul\Product\Models;
use Webkul\Core\Models\Channel;
use Illuminate\Database\Eloquent\Model;
use Webkul\Attribute\Models\AttributeFamilyProxy;
use Webkul\Category\Models\CategoryProxy;
@ -181,8 +182,8 @@ class Product extends Model implements ProductContract
public function inventory_source_qty($inventorySourceId)
{
return $this->inventories()
->where('inventory_source_id', $inventorySourceId)
->sum('qty');
->where('inventory_source_id', $inventorySourceId)
->sum('qty');
}
/**
@ -243,6 +244,7 @@ class Product extends Model implements ProductContract
*
* @param Group $group
* @param bool $skipSuperAttribute
*
* @return Collection
*/
public function getEditableAttributes($group = null, $skipSuperAttribute = true)
@ -253,7 +255,8 @@ class Product extends Model implements ProductContract
/**
* Get an attribute from the model.
*
* @param string $key
* @param string $key
*
* @return mixed
*/
public function getAttribute($key)
@ -262,8 +265,9 @@ class Product extends Model implements ProductContract
if (isset($this->id)) {
$this->attributes[$key] = '';
$attribute = core()->getSingletonInstance(\Webkul\Attribute\Repositories\AttributeRepository::class)
->getAttributeByCode($key);
$attribute = core()
->getSingletonInstance(\Webkul\Attribute\Repositories\AttributeRepository::class)
->getAttributeByCode($key);
$this->attributes[$key] = $this->getCustomAttributeValue($attribute);
@ -284,8 +288,9 @@ class Product extends Model implements ProductContract
$hiddenAttributes = $this->getHidden();
if (isset($this->id)) {
$familyAttributes = core()->getSingletonInstance(\Webkul\Attribute\Repositories\AttributeRepository::class)
->getFamilyAttributes($this->attribute_family);
$familyAttributes = core()
->getSingletonInstance(\Webkul\Attribute\Repositories\AttributeRepository::class)
->getFamilyAttributes($this->attribute_family);
foreach ($familyAttributes as $attribute) {
if (in_array($attribute->code, $hiddenAttributes)) {
@ -306,24 +311,41 @@ class Product extends Model implements ProductContract
*/
public function getCustomAttributeValue($attribute)
{
if (! $attribute)
if (! $attribute) {
return;
}
$channel = request()->get('channel') ?: (core()->getCurrentChannelCode() ?: core()->getDefaultChannelCode());
$locale = request()->get('locale') ?: app()->getLocale();
$locale = $this->determineLocale($channel);
if ($attribute->value_per_channel) {
if ($attribute->value_per_locale) {
$attributeValue = $this->attribute_values()->where('channel', $channel)->where('locale', $locale)->where('attribute_id', $attribute->id)->first();
$attributeValue = $this
->attribute_values()
->where('channel', $channel)
->where('locale', $locale)
->where('attribute_id', $attribute->id)
->first();
} else {
$attributeValue = $this->attribute_values()->where('channel', $channel)->where('attribute_id', $attribute->id)->first();
$attributeValue = $this
->attribute_values()
->where('channel', $channel)
->where('attribute_id', $attribute->id)
->first();
}
} else {
if ($attribute->value_per_locale) {
$attributeValue = $this->attribute_values()->where('locale', $locale)->where('attribute_id', $attribute->id)->first();
$attributeValue = $this
->attribute_values()
->where('locale', $locale)
->where('attribute_id', $attribute->id)
->first();
} else {
$attributeValue = $this->attribute_values()->where('attribute_id', $attribute->id)->first();
$attributeValue = $this
->attribute_values()
->where('attribute_id', $attribute->id)
->first();
}
}
@ -333,7 +355,8 @@ class Product extends Model implements ProductContract
/**
* Overrides the default Eloquent query builder
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function newEloquentBuilder($query)
@ -356,4 +379,24 @@ class Product extends Model implements ProductContract
{
return $this;
}
/**
* Determine if the given channel has a default locale configured.
* If not, return the channel given in the request.
* If none given, return the fallback channel given in the application configuration
*
* @param string $channel
*
* @return string
*/
private function determineLocale(string $channel): string
{
$channelModel = Channel::where(['code' => $channel])->first();
if ($channelModel) {
return $channelModel->default_locale->code;
}
return request()->get('locale') ?: app()->getLocale();
}
}