Add relations in tables, add saving states for datatables, minor fixes

This commit is contained in:
Ludovic CANDELLIER
2021-09-14 23:14:03 +02:00
parent be3b6bc0a8
commit 8107078ea7
16 changed files with 255 additions and 47 deletions

View File

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

View File

@@ -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(),
];
}

View File

@@ -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(),
];
}

View File

@@ -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');

View File

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

View File

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

View File

@@ -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()
{

View File

@@ -0,0 +1,17 @@
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
class Taggable extends Model
{
protected $guarded = ['id'];
public function taggable()
{
return $this->morphTo();
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Repositories\Core;
class Arrays
{
public static function changeKeyName($array, $newkey, $oldkey)
{
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key] = self::changeKeyName($value, $newkey, $oldkey);
} else {
$array[$newkey] = $array[$oldkey];
}
}
unset($array[$oldkey]);
return $array;
}
}

View File

@@ -0,0 +1,78 @@
<?php
namespace App\Repositories\Core;
class Trees
{
public static function getTree($model)
{
$tree = $model->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);
}
}

View File

@@ -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');
}
}

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,29 @@
@if (count($generic['prices'] ?? []))
<table class="table table-bordered table-hover table-striped w-100 mb-0 dataTable">
<input type="hidden" name="price_generics[0]" value="{{ $generic['id'] }}">
<thead>
<th>
<button type="button" class="btn btn-xs btn-danger delete-generic-price-btn mt-2" data-card-widget="collapse" data-toggle="tooltip" title="supprimer" data-id="{{ $generic['id'] }}">
<i class="fas fa-trash"></i>
</button>
{{ $generic['category']['name'] ?? null }}
</th>
@foreach ($generic['prices'] as $price)
<th>
{{ $price['quantity'] ?? null }} {{ $price['unity_id'] ?? null }}
</th>
@endforeach
</thead>
<tr>
<td>
{{ $generic['name'] ?? null }}
</td>
@foreach ($generic['prices'] as $price)
<td>
{{ $price['price_taxed'] ?? null }}
</td>
@endforeach
</tr>
</table>
@endif

View File

@@ -11,7 +11,7 @@
</div>
<div class="col-4">
{{ 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' => ''])
</div>
</div>
</div>