Refactor article getter for descriptions & tags, minor fixes on tags

This commit is contained in:
Ludovic CANDELLIER
2022-06-22 22:28:18 +02:00
parent 6b1cc0f045
commit e9ab7173f8
12 changed files with 148 additions and 37 deletions

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Shop\Tag;
use App\Repositories\Shop\TagGroups;
class addTagGroup extends Command
{
protected $signature = 'addTagGroup';
protected $description = 'Migrations of tags';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$tags = Tag::withTrashed()->get();
foreach ($tags as $tag) {
$tag->update(['group' => TagGroups::getName($tag->tag_group_id)]);
}
}
}

View File

@@ -14,8 +14,6 @@ class ArticleController extends Controller
{
$data = self::init();
$data['article'] = Articles::getArticleToSell($id);
dump($data);
exit;
return view('Shop.Articles.show', $data);
}
}

View File

@@ -24,8 +24,18 @@ class Merchandise extends Model implements HasMedia
return $this->morphMany(Article::class, 'product');
}
public function Producer()
{
return $this->belongsTo(Producer::class);
}
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
public function scopeByProducer($query, $producer_id)
{
return $query->where('producer_id', $producer_id);
}
}

View File

@@ -3,11 +3,17 @@
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Dyrynda\Database\Support\CascadeSoftDeletes;
class TagGroup extends Model
{
use CascadeSoftDeletes, SoftDeletes;
protected $guarded = ['id'];
protected $table = 'tag_groups';
protected $cascadeDeletes = ['tags'];
public function tags()
{

View File

@@ -30,6 +30,7 @@ class Articles
public static function getOffersGroupedByNature($id, $sale_channel_id = false)
{
$article_ids = self::getSiblingsIds($id);
$article_ids[] = $id;
$offers = Offers::getOffersByArticles($article_ids, $sale_channel_id);
foreach ($offers as $offer) {
$data[strtolower($offer->article_nature->name)][] = [
@@ -92,10 +93,16 @@ class Articles
}
public static function getArticleToSell($id, $sale_channel_id = false)
{
$data = self::getArticle($id);
$data['offers'] = self::getOffersGroupedByNature($id, $sale_channel_id);
return $data;
}
public static function getArticle($id)
{
$article = self::get($id);
$data = $article->toArray();
// $parents = self::getInheritedByProduct($article->product_id, $article->product_type);
$data['description'] = self::getFullDescriptionByArticle($article);
$images = self::getFullImagesByArticle($article);
$data['image'] = self::getPreviewSrc($images[0] ?? false);
@@ -103,8 +110,8 @@ class Articles
$data['image_big'] = self::getImageSrc($images[0] ?? false);
$data['inherited'] = self::getInherited($id);
$data['categories'] = self::getCategoriesNameByArticle($article);
$data['tags'] = self::getTagsSlugByArticle($article);
$data['offers'] = self::getOffersGroupedByNature($id, $sale_channel_id);
$data['tags'] = self::getFullTagsSlugByArticle($article);
$data['comments'] = Comments::getByModel($article);
return $data;
}
@@ -113,6 +120,7 @@ class Articles
switch ($article->product_type) {
case 'App\Models\Botanic\Variety':
$data['variety'] = $article->product->description;
$data['plus'] = $article->product->plus;
if ($article->product->specie->description ?? false) {
$data['specie'] = $article->product->specie->description;
}
@@ -122,6 +130,7 @@ class Articles
break;
case 'App\Models\Shop\Merchandise':
$data['merchandise'] = $article->product->description;
$data['producer'] = $article->product->producer->description;
break;
default:
}
@@ -132,20 +141,14 @@ class Articles
if ($siblings) {
array_push($data, $siblings);
}
return $data;
}
public static function getArticle($id)
{
$article = self::get($id);
$data = $article->toArray();
$data['description'] = (!empty($article->description)) ? $article->description : $article->product->description;
$data['image'] = Articles::getPreview($article->image);
$data['image_big'] = Articles::getImage($article->image);
$data['inherited'] = self::getInherited($id);
$data['categories'] = self::getCategoriesNameByArticle($article);
$data['tags'] = self::getTagsSlugByArticle($article);
$data['comments'] = Comments::getByModel($article);
/*
$data['resume'] = ($data['semences'] ?? null) .
($data['plants'] ?? null) .
($data['variety'] ?? null) .
($data['merchandise'] ?? null) .
($data['plus'] ?? null);
*/
$data['description'] = $article->description;
return $data;
}
@@ -421,6 +424,36 @@ class Articles
return $article->tags->pluck('slug', 'id')->toArray();
}
public static function getFullTagsSlugByArticle($article)
{
$data = [];
switch ($article->product_type) {
case 'App\Models\Botanic\Variety':
$data += $article->product->tags->toArray();
if ($article->product->specie ?? false) {
$data += $article->product->specie->tags->toArray();
}
break;
case 'App\Models\Botanic\Specie':
$data += $article->product->tags->toArray();
break;
case 'App\Models\Shop\Merchandise':
$data += $article->product->tags->toArray();
$data += $article->product->producer->tags->toArray();
break;
default:
}
$data += $article->tags->toArray();
foreach ($data as $tag) {
if (!isset($tags[$tag['group']][$tag['name']])) {
$tags[$tag['group']][] = $tag['name'];
}
}
return $tags;
}
public static function getPricesByArticle($article)
{
return Prices::getByArticle($article->id);
@@ -472,8 +505,7 @@ class Articles
public static function getFullImageById($id)
{
$article = self::get($id);
return self::getFullImageByImage($article);
return self::getFullImageByArticle(self::get($id));
}
public static function getFullImageByArticle($article)

View File

@@ -3,6 +3,7 @@
namespace App\Repositories\Shop;
use Illuminate\Support\Str;
use App\Models\Shop\TagGroup;
use App\Models\Shop\Tag;
@@ -55,7 +56,12 @@ class TagGroups
public static function getSlug($id)
{
return self::get($id)->slug;
return self::get($id)->slug ?? '';
}
public static function getName($id)
{
return self::get($id)->name ?? '';
}
public static function get($id)
@@ -84,6 +90,7 @@ class TagGroups
$data['slug'] = Str::slug($data['name']);
}
$model->update($data);
Tags::updateGroup($id, $data['name']);
return $model;
}

View File

@@ -66,8 +66,8 @@ class Tags
public static function create($data)
{
$data['name'] = ['fr' => $data['name']];
$data['slug'] = self::buildSlug($data);
$data['group'] = TagGroups::getName($data['tag_group_id']);
$data['sort_order'] = self::getNewOrder($data['tag_group_id']);
$tag = Tag::create($data);
return $tag;
@@ -78,15 +78,20 @@ class Tags
{
$id = $id ? $id : $data['id'];
$tag = self::get($id);
$data['name'] = ['fr' => $data['name']];
$data['slug'] = self::buildSlug($data);
$data['group'] = TagGroups::getName($data['tag_group_id']);
$tag->update($data);
return $tag;
}
public static function updateGroup($tag_group_id, $group)
{
return Tag::byGroup($tag_group_id)->update(['group' => $group]);
}
public static function buildSlug($data)
{
return TagGroups::getSlug($data['tag_group_id']) . '-' . Str::slug($data['name']['fr']);
return Str::slug($data['name']);
}
public static function destroy($id)

View File

@@ -11,7 +11,7 @@
{{ $article['name'] ?? null }}
</div>
<div class="col-3" class="text-right">
{!! $article['image'] !!}
<img src="{{ $article['image'] }}" class="img-fluid">
</div>
</div>
@@ -19,7 +19,7 @@
<div class="col-12">
{{ Form::label('categories', __('shop.shelves.title')) }}<br>
@foreach (($article['categories'] ?? null) as $category)
{{ $category }}
<span class="btn btn-xs btn-success pb-2">{{ $category }}</span>
@endforeach
</div>
</div>
@@ -42,7 +42,7 @@
<div class="row mb-3">
<div class="col-12">
{{ Form::label('description', 'Description') }}
{!! $article['description'] ?? null !!}
{!! $article['description']['description'] ?? null !!}
</div>
</div>

View File

@@ -3,6 +3,7 @@
'data' => $article['offers']['semences'],
'title' => 'Semence',
'model' => 'semences',
'bgClass' => 'bg-yellow',
])
@endif
@@ -11,6 +12,7 @@
'data' => $article['offers']['plants'],
'title' => 'Plant',
'model' => 'plants',
'bgClass' => 'bg-green',
])
@endif

View File

@@ -1,7 +1,7 @@
@component('components.card', [
'id_card' => $model . '-basket',
'title' => $title,
'class' => 'mb-3',
'class' => 'mb-3 ' . ($bgClass ?? null),
])
@include('components.form.select', [

View File

@@ -9,14 +9,40 @@
</div>
</div>
<div class="row">
<div class="col-9 text-justify">
<div class="col-4">
<div style="max-width: 360px;">
@include('components.multi-images', ['images' => $article['images']])
</div>
</div>
<div class="col-5 text-justify">
{!! $article['description']['semences'] ?? null !!}
{!! $article['description']['plants'] ?? null !!}
{!! $article['description']['variety'] ?? null !!}
{!! $article['description']['merchandise'] ?? null !!}
@if ($article['description']['plus'] ?? false)
<h3>Spécificités</h3>
{!! $article['description']['plus'] !!}
@endif
@if (count($article['tags'] ?? []))
<h3>Caractéristiques</h3>
@foreach ($article['tags'] as $tag_group => $items)
<div>
{{ $tag_group }} :
@foreach ($items as $tag)
<span class="btn btn-xs pt-0 pb-0">{{ $tag }}</span>
@endforeach
</div>
@endforeach
@endif
@if ($article['description']['specie'] ?? false)
<h3 class="mt-3">Complément</h3>
{!! $article['description']['specie'] ?? null !!}
{!! $article['description']['producer'] ?? null !!}
@endif
</div>
<div class="col-3">
@include('Shop.Articles.partials.ArticleAddBasket')

View File

@@ -1,12 +1,11 @@
<?php
Route::prefix('Tags')->name('Tags.')->group(function () {
Route::get('', 'TagController@index')->name('index');
Route::get('create', 'TagController@create')->name('create');
Route::delete('destroy/{id?}', 'TagController@destroy')->name('destroy');
Route::post('update', 'TagController@update')->name('update');
Route::post('store', 'TagController@store')->name('store');
Route::get('edit/{id}', 'TagController@edit')->name('edit');
Route::get('', 'TagController@index')->name('index');
Route::get('create', 'TagController@create')->name('create');
Route::delete('destroy/{id?}', 'TagController@destroy')->name('destroy');
Route::post('update', 'TagController@update')->name('update');
Route::post('store', 'TagController@store')->name('store');
Route::get('edit/{id}', 'TagController@edit')->name('edit');
});