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 35310b049e
commit 1703082b3e
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 = self::init();
$data['article'] = Articles::getArticleToSell($id); $data['article'] = Articles::getArticleToSell($id);
dump($data);
exit;
return view('Shop.Articles.show', $data); return view('Shop.Articles.show', $data);
} }
} }

View File

@@ -24,8 +24,18 @@ class Merchandise extends Model implements HasMedia
return $this->morphMany(Article::class, 'product'); return $this->morphMany(Article::class, 'product');
} }
public function Producer()
{
return $this->belongsTo(Producer::class);
}
public function tags() public function tags()
{ {
return $this->morphToMany(Tag::class, 'taggable'); 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; namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Dyrynda\Database\Support\CascadeSoftDeletes;
class TagGroup extends Model class TagGroup extends Model
{ {
use CascadeSoftDeletes, SoftDeletes;
protected $guarded = ['id']; protected $guarded = ['id'];
protected $table = 'tag_groups'; protected $table = 'tag_groups';
protected $cascadeDeletes = ['tags'];
public function tags() public function tags()
{ {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -9,14 +9,40 @@
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="col-9 text-justify"> <div class="col-4">
<div style="max-width: 360px;"> <div style="max-width: 360px;">
@include('components.multi-images', ['images' => $article['images']]) @include('components.multi-images', ['images' => $article['images']])
</div> </div>
</div>
<div class="col-5 text-justify">
{!! $article['description']['semences'] ?? null !!} {!! $article['description']['semences'] ?? null !!}
{!! $article['description']['plants'] ?? null !!} {!! $article['description']['plants'] ?? null !!}
{!! $article['description']['variety'] ?? null !!} {!! $article['description']['variety'] ?? null !!}
{!! $article['description']['merchandise'] ?? 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>
<div class="col-3"> <div class="col-3">
@include('Shop.Articles.partials.ArticleAddBasket') @include('Shop.Articles.partials.ArticleAddBasket')

View File

@@ -7,6 +7,5 @@ Route::prefix('Tags')->name('Tags.')->group(function () {
Route::post('update', 'TagController@update')->name('update'); Route::post('update', 'TagController@update')->name('update');
Route::post('store', 'TagController@store')->name('store'); Route::post('store', 'TagController@store')->name('store');
Route::get('edit/{id}', 'TagController@edit')->name('edit'); Route::get('edit/{id}', 'TagController@edit')->name('edit');
}); });