Add count function for images herited

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

View File

@@ -7,35 +7,14 @@ use App\Models\Shop\Category;
class untranslateShelves extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'untranslateShelves';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Migrations of shelves';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$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
->editColumn('thumb', function (Specie $specie) {
return Species::getThumb($specie->image);
return Species::getThumb($specie->image, false);
})
->editColumn('genre_name', function (Specie $specie) {
return $specie->genre ? $specie->genre->name : '';

View File

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

View File

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

View File

@@ -13,7 +13,7 @@ class TagsDataTable extends DataTable
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);
}
@@ -30,7 +30,7 @@ class TagsDataTable extends DataTable
protected function getColumns()
{
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('articles_count')->title('#Art')->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' ])
->id('shop')
->activeIfRoute('shop')
->order(2);
$menu->addTo('shop', 'Articles', [

View File

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

View File

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

View File

@@ -98,12 +98,15 @@ class Articles
case 'App\Models\Botanic\Variety':
$variety = $article->product;
$specie = $variety->specie;
$description = empty($specie->description) ? '' : $specie->description . '<br><br>';
$description .= empty($variety->description) ? '' : $variety->description . '<br><br>';
$description = $specie->description . $variety->description;
break;
case 'App\Models\Botanic\Specie':
$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;
default:
$description = '';
@@ -210,7 +213,6 @@ class Articles
return $data;
}
public static function getFull($id)
{
$data['article'] = self::getArticleEdit($id);
@@ -385,6 +387,46 @@ class Articles
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)
{
$article = self::get($id);

View File

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

View File

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