From 144532acbf3629731e54df859208b1b5c627a2c6 Mon Sep 17 00:00:00 2001 From: Ludovic CANDELLIER Date: Mon, 30 Aug 2021 22:59:50 +0200 Subject: [PATCH] Add refreshing for inherited data --- app/Datatables/Shop/ArticlesDataTable.php | 8 ++++--- .../Admin/Shop/ArticleController.php | 20 ++++++++++++++++ .../Admin/Shop/CategoryController.php | 2 +- app/Models/Shop/Article.php | 3 ++- app/Repositories/Shop/Articles.php | 24 +++++++++++++++++-- app/Repositories/Shop/Categories.php | 5 ++-- app/Repositories/Shop/CategoryTrees.php | 4 ++-- .../partials/characteristics.blade.php | 14 +++++++++-- .../partials/product/description.blade.php | 4 ++-- routes/Admin/Shop/Articles.php | 4 ++++ 10 files changed, 73 insertions(+), 15 deletions(-) diff --git a/app/Datatables/Shop/ArticlesDataTable.php b/app/Datatables/Shop/ArticlesDataTable.php index 82fdff43..9a56945c 100644 --- a/app/Datatables/Shop/ArticlesDataTable.php +++ b/app/Datatables/Shop/ArticlesDataTable.php @@ -3,18 +3,20 @@ namespace App\Datatables\Shop; use Yajra\DataTables\Html\Column; + use App\Datatables\ParentDataTable as DataTable; use App\Models\Shop\Article; class ArticlesDataTable extends DataTable { public $model_name = 'articles'; + public $sortedColumn = 1; public function query(Article $model) { // $model = $model::with('Family')->select('shop_articles.*','family.name as family_name')->join('shop_article_families as family', 'family.id', '=', 'shop_articles.article_family_id')->groupBy('shop_articles.id'); - $model = $model::with('article_nature')->select('shop_articles.*'); - // $model = $model::joinRelations('Family')->select('shop_articles.*','shop_article_families.name as family_name'); + // $model = $model::with('article_nature')->select('shop_articles.*'); + $model = $model::with('article_nature')->joinRelationship('article_nature')->select('shop_articles.*','shop_article_natures.name as nature_name'); $model = self::filterByArticleNature($model); return self::buildQuery($model); } @@ -28,7 +30,7 @@ class ArticlesDataTable extends DataTable protected function getColumns() { return [ - Column::make('article_nature.name')->title('Nature')->orderable(false), + Column::make('article_nature.name')->data('nature_name')->title('Nature'), Column::make('name')->title('Nom'), self::makeColumnButtons(), ]; diff --git a/app/Http/Controllers/Admin/Shop/ArticleController.php b/app/Http/Controllers/Admin/Shop/ArticleController.php index 1307a49f..54dbee3f 100644 --- a/app/Http/Controllers/Admin/Shop/ArticleController.php +++ b/app/Http/Controllers/Admin/Shop/ArticleController.php @@ -45,6 +45,8 @@ class ArticleController extends Controller public function edit($id) { $data = Articles::getFull($id); + // dump($data); + // exit; return view('Admin.Shop.Articles.edit', $data); } @@ -53,6 +55,24 @@ class ArticleController extends Controller return Articles::destroy($id); } + public function getProductDescription($product_id, $model) + { + $data['article']['inherited'] = Articles::getInheritedByProduct($product_id, base64_decode($model)); + return view('Admin.Shop.Articles.partials.product.description', $data); + } + + public function getProductTags($product_id, $model) + { + $data = Articles::getInheritedByProduct($product_id, base64_decode($model)); + return view('Admin.Shop.Articles.partials.product.tags', $data); + } + + public function getProductImages($product_id, $model) + { + $data = Articles::getInheritedByProduct($product_id, base64_decode($model)); + return view('Admin.Shop.Articles.partials.product.images', $data); + } + public function getImages(Request $request, $id = false) { $id = $id ? $id : $request->input('id'); diff --git a/app/Http/Controllers/Admin/Shop/CategoryController.php b/app/Http/Controllers/Admin/Shop/CategoryController.php index 9e47c18c..3d44c1cb 100644 --- a/app/Http/Controllers/Admin/Shop/CategoryController.php +++ b/app/Http/Controllers/Admin/Shop/CategoryController.php @@ -31,7 +31,7 @@ class CategoryController extends Controller public function store(Request $request) { - $ret = Categories::store($request->all()); + $ret = Categories::storeFull($request->all()); return redirect()->route('Admin.Shop.Categories.index'); } diff --git a/app/Models/Shop/Article.php b/app/Models/Shop/Article.php index 29082561..43900dfd 100644 --- a/app/Models/Shop/Article.php +++ b/app/Models/Shop/Article.php @@ -14,12 +14,13 @@ use Spatie\MediaLibrary\MediaCollections\Models\Media; use BeyondCode\Comments\Traits\HasComments; use Rinvex\Categories\Traits\Categorizable; use Rinvex\Tags\Traits\Taggable; +use Kirschbaum\PowerJoins\PowerJoins; use Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin; use Wildside\Userstamps\Userstamps; class Article extends Model implements HasMedia { - use Categorizable, EloquentJoin, HasComments, InteractsWithMedia, Taggable, SoftDeletes, UserStamps; + use Categorizable, EloquentJoin, HasComments, InteractsWithMedia, Powerjoins, Taggable, SoftDeletes, UserStamps; protected $guarded = ['id']; protected $table = 'shop_articles'; diff --git a/app/Repositories/Shop/Articles.php b/app/Repositories/Shop/Articles.php index d20452f5..e34ba989 100644 --- a/app/Repositories/Shop/Articles.php +++ b/app/Repositories/Shop/Articles.php @@ -35,7 +35,7 @@ class Articles public static function getArticle($id) { - $article = Article::with('product.tags')->findOrFail($id); + $article = self::get($id); $data = $article->toArray(); $data['inherited'] = self::getInherited($id); $data['categories'] = self::getCategoriesNameByArticle($article); @@ -53,7 +53,7 @@ class Articles public static function getArticleEdit($id) { - $article = Article::with('product.tags')->findOrFail($id); + $article = self::get($id); $data = $article->toArray(); $data['inherited'] = self::getInherited($id); $data['categories'] = self::getCategoriesByArticle($article); @@ -81,6 +81,26 @@ class Articles return $data; } + public static function getInheritedByProduct($product_id, $product_type) + { + switch ($product_type) { + case 'App\Models\Botanic\Variety': + $product = Varieties::get($product_id); + $data[] = ['name' => 'Espèces', 'description' => Species::getDescription($product->specie_id), 'tags' => Species::getTags($product->specie_id)]; + $data[] = ['name' => 'Variétés', 'description' => $product->description, 'tags' => $product->tags->toArray()]; + break; + case 'App\Models\Botanic\Specie': + $product = Species::get($product_id); + $data[] = ['name' => 'Espèces', 'description' => $product->description, 'tags' => $product->tags->toArray()]; + break; + case 'App\Models\Shop\Merchandise': + $product = Merchandises::get($product_id); + $data[] = ['name' => 'Marchandise', 'description' => $product->description, 'tags' => $product->tags->toArray()]; + break; + } + return $data; + } + public static function getMeta(&$data = []) { $data['products'] = (($data['article']['product_type'] ?? false) == 'App\Models\Botanic\Variety') ? Varieties::getOptionsWithSpecie() : Species::getOptions(); diff --git a/app/Repositories/Shop/Categories.php b/app/Repositories/Shop/Categories.php index b7be28f0..ef5484f9 100644 --- a/app/Repositories/Shop/Categories.php +++ b/app/Repositories/Shop/Categories.php @@ -28,10 +28,10 @@ class Categories public static function storeFull($data) { - $images = isset($data['images']) ? $data['images'] : false; + $images = $data['images'] ?? false; unset($data['images']); - $tags = isset($data['tags']) ? $data['tags'] : false; + $tags = $data['tags'] ?? false; unset($data['tags']); $category = self::store($data); @@ -54,6 +54,7 @@ class Categories return (int) $item; } )->toArray(); + return $category->syncTags($tags, true); } else { return false; diff --git a/app/Repositories/Shop/CategoryTrees.php b/app/Repositories/Shop/CategoryTrees.php index d31ad4e2..55d6c2b8 100644 --- a/app/Repositories/Shop/CategoryTrees.php +++ b/app/Repositories/Shop/CategoryTrees.php @@ -33,11 +33,11 @@ class CategoryTrees switch ($type) { case 'after': - dump("$node_id After $target_id"); + // dump("$node_id After $target_id"); $category->afterNode($category_target); break; case 'inside': - dump("$node_id inside $target_id"); + // dump("$node_id inside $target_id"); $category_target->appendNode($category); break; } diff --git a/resources/views/Admin/Shop/Articles/partials/characteristics.blade.php b/resources/views/Admin/Shop/Articles/partials/characteristics.blade.php index 89c4c362..9a08a717 100644 --- a/resources/views/Admin/Shop/Articles/partials/characteristics.blade.php +++ b/resources/views/Admin/Shop/Articles/partials/characteristics.blade.php @@ -23,7 +23,7 @@
{{ Form::label('article_nature_id', __('shop.article_natures.name')) }}
- @include('components.select', ['name' => 'article_nature_id', 'list' => $natures_options, 'value' => $article['article_nature_id'] ?? null, 'class' => 'select2', 'with_empty' => '']) + @include('components.select', ['name' => 'article_nature_id', 'list' => $natures_options, 'value' => $article['article_nature_id'] ?? null, 'class' => 'select2', 'with_empty' => '', 'required' => true])
@@ -42,7 +42,7 @@
-
+
@include('Admin.Shop.Articles.partials.product.description')
@@ -69,6 +69,16 @@ var product = $('#product_id').select2('data'); var name = product[0]['text']; $('input[name="name"]').val(name); + console.log(product); + + var product_type = $('#product_type').select2('data'); + var name = product_type[0]['id']; + console.log(product_type); + console.log(name); + + var url = "{{ route('Admin.Shop.Articles.getProductDescription') }}/" + product[0]['id'] + '/' + btoa(name); + console.log(url); + $('#product_description').load(url); }); $('#product_type').change( function() { diff --git a/resources/views/Admin/Shop/Articles/partials/product/description.blade.php b/resources/views/Admin/Shop/Articles/partials/product/description.blade.php index dd756741..255ac0b0 100644 --- a/resources/views/Admin/Shop/Articles/partials/product/description.blade.php +++ b/resources/views/Admin/Shop/Articles/partials/product/description.blade.php @@ -1,5 +1,5 @@ -@if (!empty($article['product']['description'])) - @component('components.layout.box-collapse', ['id' => 'product_description', 'title' => 'Informations héritées']) +@if (count($article['inherited'] ?? [])) + @component('components.layout.box-collapse', ['id' => 'product_description_box', 'title' => 'Informations héritées']) @foreach ($article['inherited'] as $inherited) @component('components.card', ['title' => $inherited['name'], 'class' => 'mb-3']) {!! $inherited['description'] !!} diff --git a/routes/Admin/Shop/Articles.php b/routes/Admin/Shop/Articles.php index 143398b4..c9a01885 100644 --- a/routes/Admin/Shop/Articles.php +++ b/routes/Admin/Shop/Articles.php @@ -12,5 +12,9 @@ Route::prefix('Articles')->name('Articles.')->group(function () { Route::any('autocomplete/{q?}', 'ArticleController@autocomplete')->name('autocomplete'); + Route::get('getProductDescription/{product_id?}/{model?}', 'ArticleController@getProductDescription')->name('getProductDescription'); + Route::get('getProductTags/{product_id?}/{model?}', 'ArticleController@getProductTags')->name('getProductTags'); + Route::get('getProductImages/{product_id?}/{model?}', 'ArticleController@getProductImages')->name('getProductImages'); }); +