Add count function for images herited

This commit is contained in:
Ludovic CANDELLIER
2022-04-16 13:58:09 +02:00
parent ee148a27ed
commit 4d31b1682c
12 changed files with 105 additions and 47 deletions

View File

@@ -7,35 +7,14 @@ use App\Models\Shop\Category;
class untranslateShelves extends Command class untranslateShelves extends Command
{ {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'untranslateShelves'; protected $signature = 'untranslateShelves';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Migrations of shelves'; protected $description = 'Migrations of shelves';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
} }
/**
* Execute the console command.
*
* @return mixed
*/
public function handle() public function handle()
{ {
$categories = Category::all(); $categories = Category::all();

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Shop\Tag;
class untranslateTags extends Command
{
protected $signature = 'untranslateTags';
protected $description = 'Migrations of tags';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$tags = Tag::withTrashed()->get();
foreach ($tags as $tag) {
$trans = json_decode($tag->name, true);
$name = $trans['fr'];
$tag->update(['name' => $name]);
}
}
}

View File

@@ -21,7 +21,7 @@ class SpeciesDataTable extends DataTable
{ {
$datatables $datatables
->editColumn('thumb', function (Specie $specie) { ->editColumn('thumb', function (Specie $specie) {
return Species::getThumb($specie->image); return Species::getThumb($specie->image, false);
}) })
->editColumn('genre_name', function (Specie $specie) { ->editColumn('genre_name', function (Specie $specie) {
return $specie->genre ? $specie->genre->name : ''; return $specie->genre ? $specie->genre->name : '';

View File

@@ -22,7 +22,7 @@ class VarietiesDataTable extends DataTable
{ {
$datatables $datatables
->editColumn('thumb', function (Variety $variety) { ->editColumn('thumb', function (Variety $variety) {
return Varieties::getThumb($variety->image); return Varieties::getThumb($variety->image, false);
}) })
->editColumn('tags2', function (Variety $variety) { ->editColumn('tags2', function (Variety $variety) {
$html = ''; $html = '';

View File

@@ -7,6 +7,7 @@ use Yajra\DataTables\Html\Column;
use App\Datatables\ParentDataTable as DataTable; use App\Datatables\ParentDataTable as DataTable;
use App\Models\Shop\Article; use App\Models\Shop\Article;
use App\Repositories\Shop\Articles; use App\Repositories\Shop\Articles;
use App\Repositories\Shop\Tags;
class ArticlesDataTable extends DataTable class ArticlesDataTable extends DataTable
{ {
@@ -75,7 +76,7 @@ class ArticlesDataTable extends DataTable
->editColumn('tags2', function (Article $article) { ->editColumn('tags2', function (Article $article) {
$html = ''; $html = '';
foreach ($article->tags as $tag) { foreach ($article->tags as $tag) {
$html .= '<span class="btn btn-xs btn-success pb-2">' . $tag->slug . '</span> '; $html .= '<span class="btn btn-xs btn-success pb-2">' . Tags::getFullnameByTag($tag) . '</span> ';
} }
return $html; return $html;
}) })
@@ -86,16 +87,16 @@ class ArticlesDataTable extends DataTable
protected function getColumns() protected function getColumns()
{ {
return [ return [
Column::make('visible')->title('Visible')->searchable(false), Column::make('visible')->title('Visible')->searchable(false)->width(50),
Column::make('homepage')->title('Accueil')->searchable(false), Column::make('homepage')->title('Accueil')->searchable(false)->width(50),
Column::make('article_nature.name')->title('Nature'), Column::make('article_nature.name')->title('Nature'),
Column::make('thumb')->title('')->searchable(false)->orderable(false)->width(40)->class('text-center'), Column::make('thumb')->title('')->searchable(false)->orderable(false)->width(40)->class('text-center'),
Column::make('name')->title('Nom'), Column::make('name')->title('Nom'),
Column::make('tags2')->title('Tags')->searchable(false)->orderable(false), Column::make('tags2')->title('Tags')->searchable(false)->orderable(false),
Column::make('tags_count')->title('#Tag')->class('text-right')->searchable(false), Column::make('tags_count')->title('#Tag')->class('text-right')->searchable(false)->width(40),
Column::make('categories_count')->title('#Ray')->class('text-right')->searchable(false), Column::make('categories_count')->title('#Ray')->class('text-right')->searchable(false)->width(40),
Column::make('offers_count')->title('#Ofr')->class('text-right')->searchable(false), Column::make('offers_count')->title('#Ofr')->class('text-right')->searchable(false)->width(40),
Column::make('images_count')->title('#Pho')->class('text-right')->searchable(false), Column::make('images_count')->title('#Pho')->class('text-right')->searchable(false)->width(40),
$this->makeColumnButtons(), $this->makeColumnButtons(),
]; ];
} }

View File

@@ -13,7 +13,7 @@ class TagsDataTable extends DataTable
public function query(Tag $model) public function query(Tag $model)
{ {
$model = $model::with('group')->withCount(['articles', 'shelves', 'species', 'varieties']); $model = $model::with('tag_group')->withCount(['articles', 'shelves', 'species', 'varieties']);
return $this->buildQuery($model); return $this->buildQuery($model);
} }
@@ -30,7 +30,7 @@ class TagsDataTable extends DataTable
protected function getColumns() protected function getColumns()
{ {
return [ return [
Column::make('group.name')->title('Groupe')->width(200), Column::make('tag_group.name')->title('Groupe')->width(200),
Column::make('name')->title('Nom'), Column::make('name')->title('Nom'),
Column::make('articles_count')->title('#Art')->class('text-right')->searchable(false)->width(60), Column::make('articles_count')->title('#Art')->class('text-right')->searchable(false)->width(60),
Column::make('species_count')->title('#Esp')->class('text-right')->searchable(false)->width(60), Column::make('species_count')->title('#Esp')->class('text-right')->searchable(false)->width(60),

View File

@@ -10,7 +10,6 @@ class Shop
{ {
$menu->add('En vente', ['icon' => 'shopping-cart' ]) $menu->add('En vente', ['icon' => 'shopping-cart' ])
->id('shop') ->id('shop')
->activeIfRoute('shop')
->order(2); ->order(2);
$menu->addTo('shop', 'Articles', [ $menu->addTo('shop', 'Articles', [

View File

@@ -19,7 +19,7 @@ class Tag extends parentTag
'group', 'group',
'tag_group_id', 'tag_group_id',
]; ];
public $translatable = ['name']; public $translatable = [];
/* /*
public function taggable() public function taggable()
@@ -44,9 +44,9 @@ class Tag extends parentTag
return $this->morphedByMany(Category::class, 'taggable'); return $this->morphedByMany(Category::class, 'taggable');
} }
public function group() public function tag_group()
{ {
return $this->hasOne(TagGroup::class, 'id', 'tag_group_id'); return $this->belongsTo(TagGroup::class);
} }
public function shelves() public function shelves()
@@ -64,6 +64,11 @@ class Tag extends parentTag
return $this->morphedByMany(Variety::class, 'taggable'); return $this->morphedByMany(Variety::class, 'taggable');
} }
public function merchandises()
{
return $this->morphedByMany(Merchandise::class, 'taggable');
}
public function scopeByGroup($query, $id) public function scopeByGroup($query, $id)
{ {
return $query->where($this->table . '.tag_group_id', $id); return $query->where($this->table . '.tag_group_id', $id);

View File

@@ -8,7 +8,6 @@ class TagGroup extends Model
{ {
protected $guarded = ['id']; protected $guarded = ['id'];
protected $table = 'tag_groups'; protected $table = 'tag_groups';
public $timestamps = false;
public function tags() public function tags()
{ {

View File

@@ -98,12 +98,15 @@ class Articles
case 'App\Models\Botanic\Variety': case 'App\Models\Botanic\Variety':
$variety = $article->product; $variety = $article->product;
$specie = $variety->specie; $specie = $variety->specie;
$description = empty($specie->description) ? '' : $specie->description . '<br><br>'; $description = $specie->description . $variety->description;
$description .= empty($variety->description) ? '' : $variety->description . '<br><br>';
break; break;
case 'App\Models\Botanic\Specie': case 'App\Models\Botanic\Specie':
$specie = $article->product; $specie = $article->product;
$description = empty($specie->description) ? '' : $specie->description . '<br><br>'; $description = $specie->description;
break;
case 'App\Models\Shop\Merchandise':
$merchandise = $article->product;
$description = $merchandise->description;
break; break;
default: default:
$description = ''; $description = '';
@@ -210,7 +213,6 @@ class Articles
return $data; return $data;
} }
public static function getFull($id) public static function getFull($id)
{ {
$data['article'] = self::getArticleEdit($id); $data['article'] = self::getArticleEdit($id);
@@ -385,6 +387,46 @@ class Articles
return Article::find($id); return Article::find($id);
} }
public static function getFullImagesByArticleId($id)
{
$article = self::get($id);
return $article ? self::getFullImagesByArticle($article) : false;
}
public static function countFullImagesByArticleId($id)
{
$article = self::get($id);
return $article ? self::countFullImagesByArticle($article) : 0;
}
public static function countFullImagesByArticle($article)
{
return count(self::getFullImagesByArticle($article));
}
public static function getFullImagesByArticle($article)
{
switch ($article->product_type) {
case 'App\Models\Botanic\Variety':
$variety = $article->product;
$specie = $variety->specie;
$images = count($variety->images) ? $variety->images : [];
$images = count($specie->images) ? $images->push($specie->images) : $images;
case 'App\Models\Botanic\Specie':
$specie = $article->product;
$images = count($specie->images) ? $specie->images : [];
break;
case 'App\Models\Shop\Merchandise':
$merchandise = $article->product;
$images = count($merchandise->images) ? $merchandise->images : [];
break;
default:
$images = [];
}
$images = count($article->images) ? $images->push($article->images) : $images;
return $images;
}
public static function getFullImageById($id) public static function getFullImageById($id)
{ {
$article = self::get($id); $article = self::get($id);

View File

@@ -2,6 +2,7 @@
namespace App\Repositories\Shop; namespace App\Repositories\Shop;
use Illuminate\Support\Str;
use App\Models\Shop\TagGroup; use App\Models\Shop\TagGroup;
use App\Models\Shop\Tag; use App\Models\Shop\Tag;
@@ -69,6 +70,7 @@ class TagGroups
public static function create($data) public static function create($data)
{ {
$data['slug'] = Str::slug($data['name']);
return TagGroup::create($data); return TagGroup::create($data);
} }
@@ -76,6 +78,7 @@ class TagGroups
{ {
$id = $id ? $id : $data['id']; $id = $id ? $id : $data['id'];
$model = self::get($id); $model = self::get($id);
$data['slug'] = Str::slug($data['name']);
$model->update($data); $model->update($data);
return $model; return $model;
} }

View File

@@ -2,9 +2,7 @@
namespace App\Repositories\Shop; namespace App\Repositories\Shop;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use App\Models\Shop\Tag; use App\Models\Shop\Tag;
class Tags class Tags
@@ -27,9 +25,9 @@ class Tags
public static function getOptionsFullName() public static function getOptionsFullName()
{ {
$tags = Tag::with('group')->get()->toArray(); $tags = Tag::with('tag_group')->get()->toArray();
foreach ($tags as $tag) { foreach ($tags as $tag) {
$data[$tag['id']] = $tag['group']['name'] . '-' . $tag['name']; $data[$tag['id']] = $tag['tag_group']['name'] . '-' . $tag['name'];
} }
return $data; return $data;
} }
@@ -41,8 +39,13 @@ class Tags
public static function getFullname($id) public static function getFullname($id)
{ {
$tag = Tag::with('group')->find($id); $tag = Tag::with('tag_group')->find($id);
return $tag->group->name . '-' . $tag->name; return $tag ? self::getFullnameByTag($tag) : false;
}
public static function getFullnameByTag($tag)
{
return $tag->tag_group->name . '-' . $tag->name;
} }
public static function get($id) public static function get($id)