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 @@