[WIP] Add thumb on offers, refactor categories, try to fix counter on relations polymorphic with eage loader, bad pattern !

This commit is contained in:
Ludovic CANDELLIER
2021-12-17 00:30:07 +01:00
parent 2be07ce72c
commit a3c6fc6ebe
26 changed files with 171 additions and 107 deletions

View File

@@ -12,20 +12,24 @@ class CategoriesDataTable extends DataTable
public function query(Category $model)
{
$model = $model::with(['tags.articles'])->withCount(['articles', 'tags']);
$model = $model::with(['tags.articles'])->withCount(['articles','tags']);
return $this->buildQuery($model);
}
public function modifier($datatables)
{
$datatables
->editColumn('name', function (Category $category) {
return $category->name;
})
->editColumn('visible', function (Category $category) {
return view("components.form.toggle", [
'name' => 'visible',
'value' => $category->visible,
'on' => __('visible'),
'off' => __('invisible'),
'meta' => 'data-id=' . $category->id,
'size' => 'sm',
'size' => 'xs',
]);
})
->editColumn('articles_tagged_count', function (Category $category) {
@@ -45,9 +49,9 @@ class CategoriesDataTable extends DataTable
return [
Column::make('visible')->title('visible')->width(60)->title(''),
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')->orderable(false)->searchable(false)->width(60),
Column::make('tags_count')->title('#Tags')->class('text-right')->searchable(false)->width(60),
Column::make('articles_tagged_count')->title('#ArtTag')->class('text-right')->searchable(false)->width(60),
Column::make('articles_tagged_count')->title('#ArtTag')->class('text-right')->searchable(false)->orderable(false)->width(60),
$this->makeColumnButtons(),
];
}

View File

@@ -5,6 +5,7 @@ namespace App\Datatables\Shop;
use Yajra\DataTables\Html\Column;
use App\Datatables\ParentDataTable as DataTable;
use App\Models\Shop\Offer;
use App\Repositories\Shop\Offers;
class OffersDataTable extends DataTable
{
@@ -34,6 +35,9 @@ class OffersDataTable extends DataTable
public function modifier($datatables)
{
$datatables
->editColumn('thumb', function (Offer $offer) {
return '<img src="' . Offers::getThumbSrc($offer) . '">';
})
->editColumn('status_id', function (Offer $offer) {
return view("components.form.toggle", [
'value' => $offer->status_id,
@@ -46,7 +50,7 @@ class OffersDataTable extends DataTable
->editColumn('stock_delayed', function (Offer $offer) {
return $offer->stock_delayed . ' - ' . $offer->delay_type;
})
->rawColumns(['active', 'action']);
->rawColumns(['active', 'thumb', 'action']);
return parent::modifier($datatables);
}
@@ -55,6 +59,7 @@ class OffersDataTable extends DataTable
return [
Column::make('status_id')->title('')->width(40),
Column::make('article.article_nature.name')->title('Nature'),
Column::make('thumb')->title('')->width(40),
Column::make('article.name')->title('Article'),
Column::make('variation.name')->title('Déclinaison'),
Column::make('tariff.name')->title('Tarif'),

View File

@@ -36,7 +36,9 @@ class CustomerController extends Controller
public function edit($id)
{
$data['customer'] = Customers::get($id)->toArray();
$data['customer'] = Customers::edit($id);
dump($data['customer']);
exit;
$data['deliveries'] = Deliveries::getOptions();
return view('Admin.Shop.Customers.edit', $data);
}

View File

@@ -7,6 +7,7 @@ use App\Http\Controllers\Controller;
use App\Repositories\Shop\Categories;
use App\Repositories\Shop\Offers;
use App\Repositories\Shop\Tags;
class CategoryController extends Controller
{
@@ -18,8 +19,11 @@ class CategoryController extends Controller
public function show($id)
{
$data = self::init();
$data['category'] = Categories::getByCategory($id)->toArray();
$data['offers'] = Offers::getByCategory($id)->toArray();
$data['category'] = Categories::getFull($id);
$data['offers'] = Offers::getByCategoryWithTags($id);
$data['tags'] = Tags::getWithCountOffers();
dump($data);
exit;
return view('Shop.shelve', $data);
}

View File

@@ -0,0 +1,16 @@
<?php
namespace App\Models\Core;
use Rinvex\Categories\Models\Category as parentCategory;
use App\Models\Shop\Article;
class Category extends parentCategory
{
public function Articles()
{
// return $this->entries(Article::class);
return $this->morphedByMany(Article::class, 'categorizable');
}
}

View File

@@ -8,18 +8,19 @@ use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\Translatable\HasTranslations;
// use Rinvex\Categories\Traits\Categorizable;
use Rinvex\Tags\Traits\Taggable;
use Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin;
use Wildside\Userstamps\Userstamps;
class Category extends Model
{
use InteractsWithMedia, SoftDeletes, Taggable, Userstamps;
use HasTranslations, InteractsWithMedia, SoftDeletes, Taggable, Userstamps;
protected $guarded = ['id'];
protected $table = 'shop_categories';
protected $table = 'categories';
public $translatable = ['name','description'];
public function Articles()
{
@@ -31,14 +32,9 @@ class Category extends Model
return $this->tags->articles;
}
public function Shelves()
public function countArticlesTagged()
{
return $this->morphedByMany(Category::class, 'categorizable');
}
public function CategoryTree()
{
return $this->belongsTo(app('rinvex.categories.category'), 'category_id');
return $this->tags()->withCount('Articles');
}
public function scopeByCategory($query, $category_id)

View File

@@ -23,9 +23,9 @@ class Offer extends Model
return $this->article->categories();
}
public function variation()
public function tags()
{
return $this->belongsTo(Variation::class);
return $this->article->tags();
}
public function tariff()
@@ -33,6 +33,11 @@ class Offer extends Model
return $this->belongsTo(Tariff::class);
}
public function variation()
{
return $this->belongsTo(Variation::class);
}
public function scopeByArticle($query, $id)
{
return $query->where('article_id', $id);
@@ -64,6 +69,20 @@ class Offer extends Model
return $query->where('status_id', $id);
}
public function scopeByTag($query, $tag_id)
{
return $query->whereHas('article.tags', function ($query) use ($tag_id) {
$query->where('tag_id', $tag_id);
});
}
public function scopeByTags($query, $tags)
{
return $query->whereHas('article.tags', function ($query) use ($tags) {
$query->whereIn('tag_id', $tags);
});
}
public function scopeByVariation($query, $id)
{
return $query->where('variation_id', $id);

View File

@@ -24,6 +24,12 @@ class Tag extends parentTag
}
*/
// TODO
public function offers()
{
return $this->articles();
}
public function articles()
{
return $this->morphedByMany(Article::class, 'taggable');

View File

@@ -1,48 +0,0 @@
<?php
namespace App\Repositories\Core;
use TheSeer\DirectoryScanner\DirectoryScanner;
class Cache
{
public static function getAutoVersion($file)
{
$filePath = public_path() . $file;
if (!file_exists($filePath)) {
return '';
}
$version = filemtime($filePath);
return '?v=' . $version;
}
public static function getFilesVersion($folder, $type)
{
$folder = str_replace('.', '/', $folder);
// dump($folder);
// dump($type);
// exit;
$scanner = new DirectoryScanner;
$scanner->addInclude('*.'.$type);
// $scanner->addExclude('*filter*');
// $scanner->addExclude('./src/*');
$path = public_path() . '/' . $folder;
$data = [];
foreach ($scanner($path) as $i) {
// dump($i);
$sub = $i->getPath();
$sub = str_replace($path, '', $sub);
$sub = str_replace('\\', '/', $sub);
// dump($sub);
$filename = '/' . $folder . $sub . '/' . $i->getFilename();
// dump($filename);
$mtime = $i->getMTime();
$data[$filename] = $mtime;
}
return $data;
}
}

View File

@@ -1,10 +1,10 @@
<?php
namespace App\Repositories\Shop;
namespace App\Repositories\Core;
use App\Repositories\Core\Arrays;
class CategoryTrees
class Categories
{
public static function getTree($withFolder = false)
{
@@ -74,9 +74,9 @@ class CategoryTrees
public static function update($data, $id = false)
{
$id = $id ? $id : $data['category_id'];
$id = $id ? $id : $data['id'];
$item = self::getNode($id);
$item->update(['name' => $data['name']]);
$item->update($data);
return $item;
}

View File

@@ -4,6 +4,7 @@ namespace App\Repositories\Shop;
use App\Models\Shop\Category;
use App\Repositories\Core\Tag;
use App\Repositories\Core\Categories as CategoryTrees;
class Categories
{
@@ -19,8 +20,10 @@ class Categories
public static function getFull($id)
{
$category = Category::with('CategoryTree')->find($id);
$category = self::get($id);
$data = $category->toArray();
$data['name'] = $category->name;
$data['description'] = $category->description;
$data['tags'] = self::getTagsByCategory($category);
return $data;
}
@@ -35,11 +38,6 @@ class Categories
return $category->tags->pluck('name', 'id')->toArray();
}
public static function getByCategory($category_id)
{
return Category::byCategory($category_id)->first();
}
public static function getTree()
{
return CategoryTrees::getTree();
@@ -47,7 +45,7 @@ class Categories
public static function getOptions()
{
return Category::orderBy('name', 'asc')->pluck('name', 'category_id')->toArray();
return Category::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
}
public static function storeFull($data)

View File

@@ -26,6 +26,14 @@ class Customers
return Customer::find($id);
}
public static function edit($id)
{
$customer = Customer::with(['addresses'])->find($id);
$data = $customer->toArray();
$data['deliveries'] = $customer->deliveries->pluck('id')->toArray();
return $data;
}
public static function storeFull($data)
{
$deliveries = $data['deliveries'];

View File

@@ -6,17 +6,42 @@ use App\Models\Shop\Offer;
class Offers
{
public static function getThumbSrcById($id)
{
return self::getThumbSrc(self::get($id));
}
public static function getThumbSrc(Offer $offer)
{
return Articles::getThumbSrc($offer->article->image);
}
public static function getLast()
{
return Offer::with(['article.image'])->orderByDesc('updated_at')->get();
}
public static function getByCategoryWithTags($category_id)
{
$category = Categories::get($category_id);
$tags = Categories::getTagsByCategory($category);
$offers1 = self::getByCategory($category_id)->toArray();
$offers2 = self::getByTags($tags)->toArray();
$data = array_merge($offers1, $offers2);
return $data;
}
public static function getByCategory($category_id)
{
return Offer::with(['article.image'])->byCategory($category_id)->get();
}
public static function getByTags($tags)
{
return Offer::with(['article.tags'])->byTags($tags)->get();
}
public static function getAll()
{
return Offer::orderBy('value', 'asc')->get();

View File

@@ -15,6 +15,16 @@ class Tags
return Tag::get()->pluck('name', 'id')->toArray();
}
public static function getWithCountOffers()
{
return Tag::withCount(['offers'])->get()->toArray();
}
public static function getWithCountArticles()
{
return Tag::withCount(['articles'])->get()->toArray();
}
public static function getOptionsFullName()
{
$tags = Tag::with('group')->get();

View File

@@ -33,7 +33,7 @@ trait Imageable
public static function getPreviewSrc($image)
{
return Medias::getPreviewSrc($image);
return $image ? Medias::getPreviewSrc($image) : null;
}
public static function deleteImage($id, $index)