From ffb9f81353da7dbc375e4a03d8007fd5c4588a70 Mon Sep 17 00:00:00 2001 From: Ludovic CANDELLIER Date: Tue, 14 Sep 2021 23:14:03 +0200 Subject: [PATCH] Add relations in tables, add saving states for datatables, minor fixes --- app/Datatables/ParentDataTable.php | 2 +- app/Datatables/Shop/ArticlesDataTable.php | 8 +- app/Datatables/Shop/TagsDataTable.php | 5 +- app/Models/Botanic/Specie.php | 5 ++ app/Models/Botanic/Variety.php | 5 ++ app/Models/Shop/Article.php | 10 +-- app/Models/Shop/Tag.php | 24 +++++- app/Models/Shop/Taggable.php | 17 ++++ app/Repositories/Core/Arrays.php | 20 +++++ app/Repositories/Core/Trees.php | 78 +++++++++++++++++++ app/Repositories/Shop/CategoryTrees.php | 59 ++++++++++---- app/Repositories/Shop/TagGroups.php | 5 ++ app/Repositories/Shop/Tags.php | 31 ++++---- composer.json | 2 +- .../partials/table-prices.blade_old.php | 29 +++++++ .../Admin/Shop/Variations/form.blade.php | 2 +- 16 files changed, 255 insertions(+), 47 deletions(-) create mode 100644 app/Models/Shop/Taggable.php create mode 100644 app/Repositories/Core/Arrays.php create mode 100644 app/Repositories/Core/Trees.php create mode 100644 resources/views/Admin/Shop/PriceLists/partials/table-prices.blade_old.php diff --git a/app/Datatables/ParentDataTable.php b/app/Datatables/ParentDataTable.php index 4461f675..629bdd87 100644 --- a/app/Datatables/ParentDataTable.php +++ b/app/Datatables/ParentDataTable.php @@ -20,7 +20,7 @@ class ParentDataTable extends DataTable public $scrollX = false; public $sortedColumn = 0; public $sortedOrder = 'asc'; - public $stateSave = false; + public $stateSave = true; /** * Build DataTable class. diff --git a/app/Datatables/Shop/ArticlesDataTable.php b/app/Datatables/Shop/ArticlesDataTable.php index 9a56945c..4c94e23a 100644 --- a/app/Datatables/Shop/ArticlesDataTable.php +++ b/app/Datatables/Shop/ArticlesDataTable.php @@ -14,9 +14,7 @@ class ArticlesDataTable extends DataTable 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::with('article_nature')->joinRelationship('article_nature')->select('shop_articles.*','shop_article_natures.name as nature_name'); + $model = $model::with('article_nature')->withCount(['categories', 'tags'])->joinRelationship('article_nature'); $model = self::filterByArticleNature($model); return self::buildQuery($model); } @@ -30,8 +28,10 @@ class ArticlesDataTable extends DataTable protected function getColumns() { return [ - Column::make('article_nature.name')->data('nature_name')->title('Nature'), + Column::make('article_nature.name')->title('Nature'), Column::make('name')->title('Nom'), + Column::make('tags_count')->title('Tags')->class('text-right'), + Column::make('categories_count')->title('Rayons')->class('text-right'), self::makeColumnButtons(), ]; } diff --git a/app/Datatables/Shop/TagsDataTable.php b/app/Datatables/Shop/TagsDataTable.php index 9976c1ba..ed5330e4 100644 --- a/app/Datatables/Shop/TagsDataTable.php +++ b/app/Datatables/Shop/TagsDataTable.php @@ -13,7 +13,7 @@ class TagsDataTable extends DataTable public function query(Tag $model) { - $model = $model::with('group')->select(['tags.*']); + $model = $model::with('group')->withCount(['articles','species','varieties']); return self::buildQuery($model); } @@ -22,6 +22,9 @@ class TagsDataTable extends DataTable return [ Column::make('group.name')->title('Groupe'), Column::make('name')->title('Nom'), + Column::make('articles_count')->title('Articles')->class('text-right'), + Column::make('species_count')->title('Espèces')->class('text-right'), + Column::make('varieties_count')->title('Variétés')->class('text-right'), self::makeColumnButtons(), ]; } diff --git a/app/Models/Botanic/Specie.php b/app/Models/Botanic/Specie.php index 49e5e2a2..6929bf40 100644 --- a/app/Models/Botanic/Specie.php +++ b/app/Models/Botanic/Specie.php @@ -15,6 +15,11 @@ class Specie extends Model protected $guarded = ['id']; protected $table = 'botanic_species'; + public function tags() + { + return $this->morphToMany('App\Models\Shop\Tag', 'taggable'); + } + public function Genre() { return $this->belongsTo('App\Models\Botanic\Genre'); diff --git a/app/Models/Botanic/Variety.php b/app/Models/Botanic/Variety.php index f5c81fee..c9e43319 100644 --- a/app/Models/Botanic/Variety.php +++ b/app/Models/Botanic/Variety.php @@ -31,6 +31,11 @@ class Variety extends Model implements HasMedia return $this->morphMany('App\Models\Shop\Article', 'product'); } + public function tags() + { + return $this->morphToMany('App\Models\Shop\Tag', 'taggable'); + } + public function registerMediaConversions(Media $media = null) : void { $this->addMediaConversion('thumb')->fit(Manipulations::FIT_CROP, 32, 32); diff --git a/app/Models/Shop/Article.php b/app/Models/Shop/Article.php index 43900dfd..b7382758 100644 --- a/app/Models/Shop/Article.php +++ b/app/Models/Shop/Article.php @@ -40,11 +40,6 @@ class Article extends Model implements HasMedia return $this->hasOne('App\Models\Core\Media', 'model_id')->where('model_type', 'App\Models\Shop\Article'); } - public function inventories() - { - return $this->hasMany('App\Models\Shop\Inventory'); - } - public function invoiceItems() { return $this->hasMany('App\Models\Shop\InvoiceItem'); @@ -60,6 +55,11 @@ class Article extends Model implements HasMedia return $this->morphTo(); } + public function tags() + { + return $this->morphToMany('App\Models\Shop\Tag', 'taggable'); + } + public function scopeByArticle($query, $id) { return $query->where($this->table . '.id', $id); diff --git a/app/Models/Shop/Tag.php b/app/Models/Shop/Tag.php index 093ea090..9d4aec11 100644 --- a/app/Models/Shop/Tag.php +++ b/app/Models/Shop/Tag.php @@ -8,7 +8,29 @@ class Tag extends Model { protected $guarded = ['id']; - public $translatable = ['name']; + public $translatable = ['name']; + + /* + public function taggable() + { + return $this->hasMany('App\Models\Shop\Taggable'); + } + */ + + public function articles() + { + return $this->morphedByMany('App\Models\Shop\Article','taggable'); + } + + public function varieties() + { + return $this->morphedByMany('App\Models\Botanic\Variety','taggable'); + } + + public function species() + { + return $this->morphedByMany('App\Models\Botanic\Specie','taggable'); + } public function group() { diff --git a/app/Models/Shop/Taggable.php b/app/Models/Shop/Taggable.php new file mode 100644 index 00000000..3c1aee12 --- /dev/null +++ b/app/Models/Shop/Taggable.php @@ -0,0 +1,17 @@ +morphTo(); + } + +} diff --git a/app/Repositories/Core/Arrays.php b/app/Repositories/Core/Arrays.php new file mode 100644 index 00000000..21254edc --- /dev/null +++ b/app/Repositories/Core/Arrays.php @@ -0,0 +1,20 @@ + $value) { + if (is_array($value)) { + $array[$key] = self::changeKeyName($value, $newkey, $oldkey); + } else { + $array[$newkey] = $array[$oldkey]; + } + } + unset($array[$oldkey]); + return $array; + } + +} diff --git a/app/Repositories/Core/Trees.php b/app/Repositories/Core/Trees.php new file mode 100644 index 00000000..c166ddd1 --- /dev/null +++ b/app/Repositories/Core/Trees.php @@ -0,0 +1,78 @@ +orderBy('_lft', 'asc')->get()->toTree()->toArray(); + return self::getChildren($tree[0]['children']); + } + + public static function getChildren($data) + { + $tree = []; + foreach ($data as $item) { + $leaf = []; + $leaf['name'] = $item['name']; + $leaf['id'] = $item['id']; + $children = (isset($item['children'])) ? self::getChildren($item['children']) : false; + if ($children) { + $leaf['children'] = $children; + } + $tree[] = $leaf; + } + return $tree; + } + + public static function moveTree($node_id, $target_id, $type) + { + $item = self::getNode($node_id); + $item_target = self::getNode($target_id); + + switch ($type) { + case 'after': + // dump("$node_id After $target_id"); + $item->afterNode($item_target); + break; + case 'inside': + // dump("$node_id inside $target_id"); + $item_target->appendNode($item); + break; + } + $item->save(); + return "1"; + } + + + public static function create($data, $model) + { + $parent = (isset($data['parent_id']) && $data['parent_id']) ? self::getNode($data['parent_id']) : self::getRoot(); + $tree = $model->create(['name' => $data['name']]); + $tree->appendToNode($parent)->save(); + return $tree; + } + + public static function update($data, $id = false) + { + $id = $id ? $id : $data['id']; + $item = self::get($id); + return $item->update(['name' => $data['name']]); + } + + public static function destroy($id) + { + // return Category::destroy($id); + } + + public static function getRoot() + { + return self::getNode(1); + } + + public static function getNode($id, $model) + { + return $model->find($id); + } +} diff --git a/app/Repositories/Shop/CategoryTrees.php b/app/Repositories/Shop/CategoryTrees.php index 55d6c2b8..3184c257 100644 --- a/app/Repositories/Shop/CategoryTrees.php +++ b/app/Repositories/Shop/CategoryTrees.php @@ -2,24 +2,42 @@ namespace App\Repositories\Shop; +use App\Repositories\Core\Arrays; + class CategoryTrees { - public static function getTree() + public static function getTree($withFolder = false) { - $categories = app('rinvex.categories.category')->orderBy('_lft', 'asc')->get()->toTree()->toArray(); - return self::getChildren($categories[0]['children']); + $categories = self::getCategoryTree()->toArray(); + return self::getChildren($categories[0]['children'], $withFolder); } - public static function getChildren($data) + public static function getFancyTree() + { + $categories = self::getTree(true); + $categories = Arrays::changeKeyName($categories, 'title', 'name'); + $categories = Arrays::changeKeyName($categories, 'key', 'id'); + return $categories; + } + + public static function getCategoryTree() + { + return self::getModel()->defaultOrder()->get()->toTree(); + } + + public static function getChildren($data, $withFolder = false) { $tree = []; foreach ($data as $item) { $leaf = []; $leaf['name'] = $item['name']; $leaf['id'] = $item['id']; - $children = (isset($item['children'])) ? self::getChildren($item['children']) : false; + $children = ($item['children'] ?? false) ? self::getChildren($item['children']) : false; if ($children) { $leaf['children'] = $children; + if ($withFolder) { + $leaf['folder'] = true; + } } $tree[] = $leaf; } @@ -32,14 +50,14 @@ class CategoryTrees $category_target = self::getNode($target_id); switch ($type) { - case 'after': - // dump("$node_id After $target_id"); - $category->afterNode($category_target); - break; - case 'inside': - // dump("$node_id inside $target_id"); - $category_target->appendNode($category); - break; + case 'after': + // dump("$node_id After $target_id"); + $category->afterNode($category_target); + break; + case 'inside': + // dump("$node_id inside $target_id"); + $category_target->appendNode($category); + break; } $category->save(); return "1"; @@ -48,8 +66,8 @@ class CategoryTrees public static function create($data) { - $parent = (isset($data['parent_id']) && $data['parent_id']) ? self::getNode($data['parent_id']) : self::getRoot(); - $category = app('rinvex.categories.category')->create(['name' => $data['name']]); + $parent = ($data['parent_id'] ?? false) ? self::getNode($data['parent_id']) : self::getRoot(); + $category = self::getModel()->create(['name' => $data['name']]); $category->appendToNode($parent)->save(); return $category; } @@ -57,7 +75,9 @@ class CategoryTrees public static function update($data, $id = false) { $id = $id ? $id : $data['category_id']; - return self::getNode($id)->update(['name' => $data['name']]); + $item = self::getNode($id); + $item->update(['name' => $data['name']]); + return $item; } public static function destroy($id) @@ -72,6 +92,11 @@ class CategoryTrees public static function getNode($id) { - return app('rinvex.categories.category')->find($id); + return self::getModel()->find($id); + } + + public static function getModel() + { + return app('rinvex.categories.category'); } } diff --git a/app/Repositories/Shop/TagGroups.php b/app/Repositories/Shop/TagGroups.php index a3343438..b232e7ef 100644 --- a/app/Repositories/Shop/TagGroups.php +++ b/app/Repositories/Shop/TagGroups.php @@ -41,6 +41,11 @@ class TagGroups return TagGroup::orderBy('name', 'asc')->get(); } + public static function getSlug($id) + { + return self::get($id)->slug; + } + public static function get($id) { return TagGroup::find($id); diff --git a/app/Repositories/Shop/Tags.php b/app/Repositories/Shop/Tags.php index b990d055..0b18e52a 100644 --- a/app/Repositories/Shop/Tags.php +++ b/app/Repositories/Shop/Tags.php @@ -3,20 +3,12 @@ namespace App\Repositories\Shop; use Illuminate\Support\Facades\Storage; -use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; -use Yajra\DataTables\DataTables; - use App\Models\Shop\Tag; class Tags { - public static function getDatatable() - { - $model = Tag::orderBy('name'); - return Datatables::of($model)->make(true); - } public static function getOptions() { @@ -42,25 +34,32 @@ class Tags public static function create($data) { - $tag = app('rinvex.tags.tag')->create(['name' => ['fr' => $data['name']]]); - $tag2 = Tag::find($tag->id); - $tag2->tag_group_id = $data['tag_group_id']; - $tag2->sort_order = self::getNewOrder($data['tag_group_id']); - $tag2->save(); - // return app('rinvex.tags.tag')->createByName($data['name']); - // return Tag::create($data); + $slug = self::buildSlug($data); + $tag = app('rinvex.tags.tag')->create([ + 'name' => ['fr' => $data['name']], + 'slug' => $slug, + 'tag_group_id' => $data['tag_group_id'], + 'sort_order' => self::getNewOrder($data['tag_group_id']) + ]); return $tag; } + public static function update($data, $id = false) { $id = $id ? $id : $data['id']; - $tag = Tag::find($id); + $tag = self::get($id); $data['name'] = ['fr' => $data['name']]; + $data['slug'] = self::buildSlug($data); $tag->update($data); return $tag; } + public static function buildSlug($data) + { + return TagGroups::getSlug($data['tag_group_id']) . '-' . Str::slug($data['name']['fr']); + } + public static function destroy($id) { return Tag::destroy($id); diff --git a/composer.json b/composer.json index 94162a78..b44bf069 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,7 @@ "intervention/imagecache": "^2.4", "jasonlewis/expressive-date": "^1.0", "jenssegers/date": "^4.0", - "kalnoy/nestedset": "^5.0", + "kalnoy/nestedset": "^6.0", "kirschbaum-development/eloquent-power-joins": "^2.3", "knplabs/knp-snappy": "^1.2", "laracasts/utilities": "^3.0", diff --git a/resources/views/Admin/Shop/PriceLists/partials/table-prices.blade_old.php b/resources/views/Admin/Shop/PriceLists/partials/table-prices.blade_old.php new file mode 100644 index 00000000..f82c7cf7 --- /dev/null +++ b/resources/views/Admin/Shop/PriceLists/partials/table-prices.blade_old.php @@ -0,0 +1,29 @@ +@if (count($generic['prices'] ?? [])) + + + + + @foreach ($generic['prices'] as $price) + + @endforeach + + + + @foreach ($generic['prices'] as $price) + + @endforeach + +
+ + + {{ $generic['category']['name'] ?? null }} + + {{ $price['quantity'] ?? null }} {{ $price['unity_id'] ?? null }} +
+ {{ $generic['name'] ?? null }} + + {{ $price['price_taxed'] ?? null }} +
+@endif diff --git a/resources/views/Admin/Shop/Variations/form.blade.php b/resources/views/Admin/Shop/Variations/form.blade.php index 73b9b01d..55f5da6b 100644 --- a/resources/views/Admin/Shop/Variations/form.blade.php +++ b/resources/views/Admin/Shop/Variations/form.blade.php @@ -11,7 +11,7 @@
{{ Form::label('unity_id', 'Unité') }} - @include('components.select', ['name' => 'unity_id', 'list' => $unities ?? [], 'value' => $variation['unity_id'] ?? false, 'required' => true, 'with_empty' => '']) + @include('components.select', ['name' => 'unity_id', 'list' => $unities ?? [], 'value' => $variation['unity_id'] ?? false, 'required' => false, 'with_empty' => ''])