From b719efb58c5831ad5324dfd8ebdd0467c851c735 Mon Sep 17 00:00:00 2001 From: Deon George Date: Tue, 18 Oct 2022 23:23:45 +1100 Subject: [PATCH] Rework on product name/description and translate --- app/Http/Controllers/ProductController.php | 11 ++- app/Http/Requests/ProductAddEdit.php | 4 +- app/Models/Product.php | 69 +++++++++++-------- app/Models/ProductTranslate.php | 7 +- app/Models/User.php | 2 +- ...8_223248_optimise_product_descriptions.php | 43 ++++++++++++ .../adminlte/product/widget/detail.blade.php | 56 ++++++++++++--- .../backend/adminlte/service/home.blade.php | 2 +- .../adminlte/service/widget/active.blade.php | 2 +- .../supplier/widget/offerings.blade.php | 4 +- .../order/widget/info/broadband.blade.php | 2 +- .../order/widget/info/domain.blade.php | 2 +- .../order/widget/info/email.blade.php | 2 +- .../metronic/order/widget/info/host.blade.php | 2 +- .../order/widget/info/phone.blade.php | 2 +- 15 files changed, 148 insertions(+), 62 deletions(-) create mode 100644 database/migrations/2022_10_18_223248_optimise_product_descriptions.php diff --git a/app/Http/Controllers/ProductController.php b/app/Http/Controllers/ProductController.php index 0c8d212..3a4b18d 100644 --- a/app/Http/Controllers/ProductController.php +++ b/app/Http/Controllers/ProductController.php @@ -84,7 +84,7 @@ class ProductController extends Controller public function details_addedit(ProductAddEdit $request,Product $o) { - foreach ($request->except(['_token','submit','description']) as $key => $item) + foreach ($request->except(['_token','submit','translate']) as $key => $item) $o->{$key} = $item; $o->active = (bool)$request->active; @@ -95,13 +95,12 @@ class ProductController extends Controller return redirect()->back()->withErrors($e->getMessage())->withInput(); } - $o->load(['description']); - $oo = $o->description ?: new ProductTranslate; - - foreach ($request->get('description',[]) as $key => $item) + $o->load(['translate']); + $oo = $o->translate ?: new ProductTranslate; + foreach ($request->get('translate',[]) as $key => $item) $oo->{$key} = $item; - $o->description()->save($oo); + $o->translate()->save($oo); return redirect()->back() ->with('success','Product saved'); diff --git a/app/Http/Requests/ProductAddEdit.php b/app/Http/Requests/ProductAddEdit.php index 71c6996..c5ba9cd 100644 --- a/app/Http/Requests/ProductAddEdit.php +++ b/app/Http/Requests/ProductAddEdit.php @@ -28,7 +28,9 @@ class ProductAddEdit extends FormRequest public function rules() { return [ - 'description.name' => 'required|string|min:2|max:100', + 'translate.name_short' => 'required|string|min:2|max:100', + 'translate.name_detail' => 'required|string|min:2|max:100', + 'translate.description' => 'required|string|min:2|max:255', 'active' => 'sometimes|accepted', 'model' => 'sometimes|string', // @todo Check that it is a valid model type 'model_id' => 'sometimes|int', // @todo Check that it is a valid model type diff --git a/app/Models/Product.php b/app/Models/Product.php index e1d1c97..c9aa3f4 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -31,9 +31,10 @@ use App\Traits\{ProductDetails,SiteID}; * + category_name : Type of product supplied (Friendly Name for display, not for internal logic) * + supplied : Supplier product provided for this offering * + supplier : Supplier for this offering - * + name : Brief Name for our product // @todo we should change this to be consistent with service - * + name_short : Product ID for our Product - * + name_long : Long Name for our product + * + name : Brief Name for our product with name_detail + * + name_short : Product ID for our Product (description.name => name_short) + * + name_detail : Details of our product (description.description_short => name_detail) + * + description : Product description (description.description_full => description_full) * + billing_interval : Default Billing Interval * + billing_interval_string: Default Billing Interval in human-readable form * + setup_charge : Charge to setup this product @@ -73,21 +74,10 @@ class Product extends Model implements IDs 'pricing'=>'collection', ]; - protected $with = ['description']; + protected $with = ['translate']; /* RELATIONS */ - /** - * Get the product name in the users language, and if the user isnt logged in, the sites language - * - * @return \Illuminate\Database\Eloquent\Relations\HasOne - */ - public function description() - { - return $this->hasOne(ProductTranslate::class) - ->where('language_id',(Auth::user() && Auth::user()->language_id) ? Auth::user()->language_id : config('site')->language_id); - } - /** * Which services are configured with this product * @@ -98,6 +88,17 @@ class Product extends Model implements IDs return $this->hasMany(Service::class); } + /** + * Get the product name in the users language, and if the user isnt logged in, the sites language + * + * @return \Illuminate\Database\Eloquent\Relations\HasOne + */ + public function translate() + { + return $this->hasOne(ProductTranslate::class) + ->where('language_id',(Auth::user() && Auth::user()->language_id) ? Auth::user()->language_id : config('site')->language_id); + } + /** * Return a child model with details of the service * This will return a product/* model. @@ -211,6 +212,16 @@ class Product extends Model implements IDs return $this->type->getContractTermAttribute(); } + /** + * This product full description + * + * @return string + */ + public function getDescriptionAttribute(): string + { + return (($x=$this->translate) && $x->description) ? $x->description : 'No Description'; + } + /** * Get the minimum cost of this product * @@ -243,7 +254,17 @@ class Product extends Model implements IDs */ public function getNameAttribute(): string { - return $this->description ? $this->description->description_short : 'Unknown PRODUCT'; + return $this->getNameShortAttribute().(($x=$this->getNameDetailAttribute()) ? ': '.$x : ''); + } + + /** + * Our products Long Name + * + * @return string + */ + public function getNameDetailAttribute(): string + { + return $this->translate ? $this->translate->name_detail : 'Unknown Name'; } /** @@ -253,23 +274,13 @@ class Product extends Model implements IDs */ public function getNameShortAttribute(): string { - return $this->description ? $this->description->name : 'Unknown PID'; - } - - /** - * This product full description - * - * @return string - */ - public function getNameLongAttribute(): string - { - return $this->description->description_full; + return $this->translate ? $this->translate->name_short : 'Unknown PID'; } /** * Suppliers * - * @return Model + * @return Model|null */ public function getSupplierAttribute(): ?Model { @@ -279,7 +290,7 @@ class Product extends Model implements IDs /** * Suppliers product * - * @return Model + * @return Model|null */ public function getSuppliedAttribute(): ?Model { diff --git a/app/Models/ProductTranslate.php b/app/Models/ProductTranslate.php index 83da067..6e073fc 100644 --- a/app/Models/ProductTranslate.php +++ b/app/Models/ProductTranslate.php @@ -6,12 +6,7 @@ use Illuminate\Database\Eloquent\Model; class ProductTranslate extends Model { - protected $table = 'ab_product_translate'; + protected $table = 'product_translate'; public $timestamps = FALSE; - - public function getDescriptionFullAttribute($value) - { - return unserialize($value); - } } \ No newline at end of file diff --git a/app/Models/User.php b/app/Models/User.php index bb7626e..722a0ca 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -328,7 +328,7 @@ class User extends Authenticatable implements IDs } $result->load([ - 'product.description', + 'product.translate', 'service.type', ]); diff --git a/database/migrations/2022_10_18_223248_optimise_product_descriptions.php b/database/migrations/2022_10_18_223248_optimise_product_descriptions.php new file mode 100644 index 0000000..91bbe51 --- /dev/null +++ b/database/migrations/2022_10_18_223248_optimise_product_descriptions.php @@ -0,0 +1,43 @@ +dropForeign('ab_product_translate_site_id_foreign'); + $table->dropIndex('ab_product_translate_id_site_id_index'); + $table->dropIndex('ab_product_translate_site_id_foreign'); + + $table->foreign(['language_id'])->references(['id'])->on('languages'); + $table->foreign(['product_id','site_id'])->references(['id','site_id'])->on('products'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + abort(500,'Cant go back'); + } +}; diff --git a/resources/views/theme/backend/adminlte/product/widget/detail.blade.php b/resources/views/theme/backend/adminlte/product/widget/detail.blade.php index 3c63a93..0961012 100644 --- a/resources/views/theme/backend/adminlte/product/widget/detail.blade.php +++ b/resources/views/theme/backend/adminlte/product/widget/detail.blade.php @@ -3,24 +3,56 @@

Product Details @include('adminlte::widget.success_button')


- - +@dump($errors)
@csrf
- -
+ +
@include('adminlte::widget.form_text',[ - 'label'=>'Product Name', + 'label'=>'Product ID', 'icon'=>'fas fa-atom', - 'id'=>'description.name', - 'old'=>'description.name', - 'name'=>'description[name]', - 'value'=>$o->name ?? '', + 'id'=>'translate.name_short', + 'old'=>'translate.name_short', + 'name'=>'translate[name_short]', + 'value'=>$o->name_short ?? '', ])
+ +
+ @include('adminlte::widget.form_text',[ + 'label'=>'Product Name', + 'icon'=>'fas fa-atom', + 'id'=>'translate.name_detail', + 'old'=>'translate.name_detail', + 'name'=>'translate[name_detail]', + 'value'=>$o->name_detail ?? '', + ]) +
+
+ +
+
+
+ + +
+
+ + + @error('description') + {{ $message }} + @enderror + +
+
+
+
+
+ +
@include('adminlte::widget.form_toggle',[ @@ -43,7 +75,7 @@ 'old'=>'model', 'name'=>'model', 'options'=>$o->availableTypes()->transform(function($item) { return ['id'=>$item,'value'=>$item]; }), - 'value'=>get_class($o->type) ?? '', + 'value'=>($o->type && $x=get_class($o->type)) ? $x : '', ])
@@ -97,7 +129,9 @@ @section('page-scripts') @css(select2) + @css(simplemde) @js(select2,autofocus) + @js(simplemde)