From a3c6fc6ebe6b1f7775de424473986b20ceca61a7 Mon Sep 17 00:00:00 2001 From: Ludovic CANDELLIER Date: Fri, 17 Dec 2021 00:30:07 +0100 Subject: [PATCH] [WIP] Add thumb on offers, refactor categories, try to fix counter on relations polymorphic with eage loader, bad pattern ! --- app/Datatables/Shop/CategoriesDataTable.php | 12 +++-- app/Datatables/Shop/OffersDataTable.php | 7 ++- .../Admin/Shop/CustomerController.php | 4 +- .../Controllers/Shop/CategoryController.php | 8 +++- app/Models/Core/Category.php | 16 +++++++ app/Models/Shop/Category.php | 16 +++---- app/Models/Shop/Offer.php | 23 ++++++++- app/Models/Shop/Tag.php | 6 +++ app/Repositories/Core/Cache.php | 48 ------------------- .../CategoryTrees.php => Core/Categories.php} | 8 ++-- app/Repositories/Shop/Categories.php | 12 ++--- app/Repositories/Shop/Customers.php | 8 ++++ app/Repositories/Shop/Offers.php | 27 ++++++++++- app/Repositories/Shop/Tags.php | 10 ++++ app/Traits/Repository/Imageable.php | 2 +- composer.json | 3 +- config/rinvex.categories.php | 2 +- package.json | 2 +- .../views/Admin/Shop/Customers/form.blade.php | 2 +- .../Shop/layout/partials/article.blade.php | 2 +- .../partials/category_articles.blade.php | 2 +- .../partials/category_filters.blade.php | 3 ++ resources/views/Shop/shelve.blade.php | 22 +++++---- resources/views/components/address.blade.php | 12 ++--- .../views/components/form/toggle.blade.php | 18 +++++-- resources/views/load/form/toggle.blade.php | 3 +- 26 files changed, 171 insertions(+), 107 deletions(-) create mode 100644 app/Models/Core/Category.php delete mode 100644 app/Repositories/Core/Cache.php rename app/Repositories/{Shop/CategoryTrees.php => Core/Categories.php} (94%) create mode 100644 resources/views/Shop/layout/partials/category_filters.blade.php diff --git a/app/Datatables/Shop/CategoriesDataTable.php b/app/Datatables/Shop/CategoriesDataTable.php index 5301fd10..7d843a62 100644 --- a/app/Datatables/Shop/CategoriesDataTable.php +++ b/app/Datatables/Shop/CategoriesDataTable.php @@ -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(), ]; } diff --git a/app/Datatables/Shop/OffersDataTable.php b/app/Datatables/Shop/OffersDataTable.php index 43898477..3a9f5af9 100644 --- a/app/Datatables/Shop/OffersDataTable.php +++ b/app/Datatables/Shop/OffersDataTable.php @@ -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 ''; + }) ->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'), diff --git a/app/Http/Controllers/Admin/Shop/CustomerController.php b/app/Http/Controllers/Admin/Shop/CustomerController.php index d0c077a6..8106d5d7 100644 --- a/app/Http/Controllers/Admin/Shop/CustomerController.php +++ b/app/Http/Controllers/Admin/Shop/CustomerController.php @@ -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); } diff --git a/app/Http/Controllers/Shop/CategoryController.php b/app/Http/Controllers/Shop/CategoryController.php index a26578d6..fd1a5be0 100644 --- a/app/Http/Controllers/Shop/CategoryController.php +++ b/app/Http/Controllers/Shop/CategoryController.php @@ -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); } diff --git a/app/Models/Core/Category.php b/app/Models/Core/Category.php new file mode 100644 index 00000000..7e9a7b8a --- /dev/null +++ b/app/Models/Core/Category.php @@ -0,0 +1,16 @@ +entries(Article::class); + return $this->morphedByMany(Article::class, 'categorizable'); + } +} diff --git a/app/Models/Shop/Category.php b/app/Models/Shop/Category.php index b0c535c9..b30f2840 100644 --- a/app/Models/Shop/Category.php +++ b/app/Models/Shop/Category.php @@ -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) diff --git a/app/Models/Shop/Offer.php b/app/Models/Shop/Offer.php index 2298a579..bc32de1a 100644 --- a/app/Models/Shop/Offer.php +++ b/app/Models/Shop/Offer.php @@ -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); diff --git a/app/Models/Shop/Tag.php b/app/Models/Shop/Tag.php index 9581ba19..7ae397b2 100644 --- a/app/Models/Shop/Tag.php +++ b/app/Models/Shop/Tag.php @@ -24,6 +24,12 @@ class Tag extends parentTag } */ + // TODO + public function offers() + { + return $this->articles(); + } + public function articles() { return $this->morphedByMany(Article::class, 'taggable'); diff --git a/app/Repositories/Core/Cache.php b/app/Repositories/Core/Cache.php deleted file mode 100644 index 3155096f..00000000 --- a/app/Repositories/Core/Cache.php +++ /dev/null @@ -1,48 +0,0 @@ -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; - } -} diff --git a/app/Repositories/Shop/CategoryTrees.php b/app/Repositories/Core/Categories.php similarity index 94% rename from app/Repositories/Shop/CategoryTrees.php rename to app/Repositories/Core/Categories.php index 3184c257..2bfacbf7 100644 --- a/app/Repositories/Shop/CategoryTrees.php +++ b/app/Repositories/Core/Categories.php @@ -1,10 +1,10 @@ update(['name' => $data['name']]); + $item->update($data); return $item; } diff --git a/app/Repositories/Shop/Categories.php b/app/Repositories/Shop/Categories.php index 1ddfa263..b06b1a57 100644 --- a/app/Repositories/Shop/Categories.php +++ b/app/Repositories/Shop/Categories.php @@ -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) diff --git a/app/Repositories/Shop/Customers.php b/app/Repositories/Shop/Customers.php index 065d4131..8a0a84df 100644 --- a/app/Repositories/Shop/Customers.php +++ b/app/Repositories/Shop/Customers.php @@ -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']; diff --git a/app/Repositories/Shop/Offers.php b/app/Repositories/Shop/Offers.php index 4ad367c5..b11e9c60 100644 --- a/app/Repositories/Shop/Offers.php +++ b/app/Repositories/Shop/Offers.php @@ -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(); diff --git a/app/Repositories/Shop/Tags.php b/app/Repositories/Shop/Tags.php index ffc93c23..dfa9178d 100644 --- a/app/Repositories/Shop/Tags.php +++ b/app/Repositories/Shop/Tags.php @@ -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(); diff --git a/app/Traits/Repository/Imageable.php b/app/Traits/Repository/Imageable.php index 472049e4..39cb47e4 100644 --- a/app/Traits/Repository/Imageable.php +++ b/app/Traits/Repository/Imageable.php @@ -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) diff --git a/composer.json b/composer.json index 30809f45..6b0cc000 100644 --- a/composer.json +++ b/composer.json @@ -8,12 +8,13 @@ ], "license": "proprietary", "require": { - "php": "^7.4", + "php": "^7.4|^8.0", "alexisgeneau/mailvalidate": "dev-master", "arcanedev/log-viewer": "^8.1", "arrilot/laravel-widgets": "^3.13", "barryvdh/laravel-dompdf": "^0.9", "barryvdh/laravel-snappy": "^0.4.7", + "bencoderus/min-auth": "^1.0", "box/spout": "^3.3", "browner12/helpers": "^3.0", "cesargb/laravel-cascade-delete": "^1.2", diff --git a/config/rinvex.categories.php b/config/rinvex.categories.php index 516162cf..02d5ae4a 100644 --- a/config/rinvex.categories.php +++ b/config/rinvex.categories.php @@ -17,7 +17,7 @@ return [ // Categories Models 'models' => [ - 'category' => \Rinvex\Categories\Models\Category::class, + 'category' => App\Models\Core\Category::class, ], ]; diff --git a/package.json b/package.json index 728daa8f..ac6810f0 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "FGDigital", + "name": "OpenSEM", "version": "2.0.0", "private": true, "scripts": { diff --git a/resources/views/Admin/Shop/Customers/form.blade.php b/resources/views/Admin/Shop/Customers/form.blade.php index d4562d76..1bf0c975 100644 --- a/resources/views/Admin/Shop/Customers/form.blade.php +++ b/resources/views/Admin/Shop/Customers/form.blade.php @@ -20,7 +20,7 @@ @include('components.form.select', ['name' => 'deliveries[]', 'list' => $deliveries ?? [], 'values' => $customer['deliveries'] ?? null, 'with_empty' => '', 'class' => 'select2', 'multiple' => true]) - @include('components.address', ['with_country' => false, 'prefix' => 'addresses[0]', 'with_tab' => true]) + @include('components.address', ['with_country' => false, 'prefix' => 'addresses[0]', 'with_tab' => true, 'item' => $customer['addresses'][0]]) diff --git a/resources/views/Shop/layout/partials/article.blade.php b/resources/views/Shop/layout/partials/article.blade.php index a720d904..d3ea313a 100644 --- a/resources/views/Shop/layout/partials/article.blade.php +++ b/resources/views/Shop/layout/partials/article.blade.php @@ -1,5 +1,5 @@
- ... + ...
{{ $offer['article']['name'] }} diff --git a/resources/views/Shop/layout/partials/category_articles.blade.php b/resources/views/Shop/layout/partials/category_articles.blade.php index b9500434..f7327820 100644 --- a/resources/views/Shop/layout/partials/category_articles.blade.php +++ b/resources/views/Shop/layout/partials/category_articles.blade.php @@ -1,6 +1,6 @@
@foreach ($offers as $offer) -
+
@include('Shop.layout.partials.article')
@endforeach diff --git a/resources/views/Shop/layout/partials/category_filters.blade.php b/resources/views/Shop/layout/partials/category_filters.blade.php new file mode 100644 index 00000000..7c899c9a --- /dev/null +++ b/resources/views/Shop/layout/partials/category_filters.blade.php @@ -0,0 +1,3 @@ +
+Filtres +
\ No newline at end of file diff --git a/resources/views/Shop/shelve.blade.php b/resources/views/Shop/shelve.blade.php index 05e7a2a7..3e9602b4 100644 --- a/resources/views/Shop/shelve.blade.php +++ b/resources/views/Shop/shelve.blade.php @@ -4,17 +4,19 @@ @section('content')
-
-

{{ $category['name'] }}

-

{!! $category['description'] !!}

+
+ @include('Shop.layout.partials.category_filters')
-
- @include('Shop.layout.partials.category_add') -
-
- -
-
+
+
+
+

{{ $category['name'] }}

+

{!! $category['description'] !!}

+
+
+ @include('Shop.layout.partials.category_add') +
+
@include('Shop.layout.partials.category_articles')
diff --git a/resources/views/components/address.blade.php b/resources/views/components/address.blade.php index fa218894..5ea4110f 100644 --- a/resources/views/components/address.blade.php +++ b/resources/views/components/address.blade.php @@ -25,32 +25,32 @@

- @include('components.form.input', ['name' => ($prefix ?? null) . (($with_tab ?? false) ? '[address]' : 'address'), 'value' => $item[($prefix ?? null) . 'address'] ?? null, 'disabled' => $disabled ?? false ]) + @include('components.form.input', ['name' => ($prefix ?? null) . (($with_tab ?? false) ? '[address]' : 'address'), 'value' => ($with_tab ?? false) ? $item['address'] : ($item[($prefix ?? null) . 'address'] ?? null), 'disabled' => $disabled ?? false ])

- @include('components.form.input', ['name' => ($prefix ?? null) . (($with_tab ?? false) ? '[address2]' : 'address2'), 'value' => $item[($prefix ?? null) . 'address2'] ?? null, 'disabled' => $disabled ?? false ]) + @include('components.form.input', ['name' => ($prefix ?? null) . (($with_tab ?? false) ? '[address2]' : 'address2'), 'value' => ($with_tab ?? false) ? $item['address2'] : ($item[($prefix ?? null) . 'address2'] ?? null), 'disabled' => $disabled ?? false ])

- @include('components.form.input', ['name' => ($prefix ?? null) . (($with_tab ?? false) ? '[zipcode]' : 'zipcode'), 'value' => $item[($prefix ?? null) . 'zipcode'] ?? null, 'disabled' => $disabled ?? false ]) + @include('components.form.input', ['name' => ($prefix ?? null) . (($with_tab ?? false) ? '[zipcode]' : 'zipcode'), 'value' => ($with_tab ?? false) ? $item['zipcode'] : ($item[($prefix ?? null) . 'zipcode'] ?? null), 'disabled' => $disabled ?? false ])

- @include('components.form.input', ['name' => ($prefix ?? null) . (($with_tab ?? false) ? '[city]' : 'city'), 'value' => $item[($prefix ?? null) . 'city'] ?? null, 'disabled' => $disabled ?? false ]) + @include('components.form.input', ['name' => ($prefix ?? null) . (($with_tab ?? false) ? '[city]' : 'city'), 'value' => ($with_tab ?? false) ? $item['city'] : ($item[($prefix ?? null) . 'city'] ?? null), 'disabled' => $disabled ?? false ])
@if ($with_country ?? true)

- @include('components.form.select', ['name' => ($prefix ?? null) . (($with_tab ?? false) ? '[country_id]' : 'country_id'), 'list' => $countries ?? null, 'value' => $item[($prefix ?? null) . 'country_id'] ?? null, 'with_empty' => '', 'required' => true, 'disabled' => $disabled ?? false ]) + @include('components.form.select', ['name' => ($prefix ?? null) . (($with_tab ?? false) ? '[country_id]' : 'country_id'), 'list' => $countries ?? null, 'value' => ($with_tab ?? false) ? $item['country_id'] : ($item[($prefix ?? null) . 'country_id'] ?? null), 'with_empty' => '', 'required' => true, 'disabled' => $disabled ?? false ])

- @include('components.form.input', ['name' => ($prefix ?? null) . (($with_tab ?? false) ? '[state]' : 'state'), 'value' => $item[($prefix ?? null) . 'state'] ?? null, 'disabled' => $disabled ?? false ]) + @include('components.form.input', ['name' => ($prefix ?? null) . (($with_tab ?? false) ? '[state]' : 'state'), 'value' => ($with_tab ?? false) ? $item['state'] : ($item[($prefix ?? null) . 'state'] ?? null), 'disabled' => $disabled ?? false ])
@endif diff --git a/resources/views/components/form/toggle.blade.php b/resources/views/components/form/toggle.blade.php index f4b517c2..0a5624c5 100644 --- a/resources/views/components/form/toggle.blade.php +++ b/resources/views/components/form/toggle.blade.php @@ -1,4 +1,16 @@ - - + -@include('load.form.toggle') \ No newline at end of file +@include('load.form.toggle') diff --git a/resources/views/load/form/toggle.blade.php b/resources/views/load/form/toggle.blade.php index 220125d8..8d20a6fc 100644 --- a/resources/views/load/form/toggle.blade.php +++ b/resources/views/load/form/toggle.blade.php @@ -12,7 +12,8 @@ $('input' + selector).change(function() { data['id'] = $(this).data('id'); - data['active'] = $(this).is(':checked'); + var name = (typeof($(this).data('name')) == 'undefined') ? 'active' : $(this).data('name'); + data[name] = $(this).is(':checked'); if (data['id'] && (typeof(url) != 'undefined') && (url != '')) { var dataJson = Object.assign({}, data); $.post(url, dataJson);