[WIP] Add thumb on offers, refactor categories, try to fix counter on relations polymorphic with eage loader, bad pattern !
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Shop;
|
||||
|
||||
use App\Repositories\Core\Arrays;
|
||||
|
||||
class CategoryTrees
|
||||
{
|
||||
public static function getTree($withFolder = false)
|
||||
{
|
||||
$categories = self::getCategoryTree()->toArray();
|
||||
return self::getChildren($categories[0]['children'], $withFolder);
|
||||
}
|
||||
|
||||
public static function getFancyTree()
|
||||
{
|
||||
$categories = self::getTree(true);
|
||||
$categories = Arrays::changeKeyName($categories, 'title', 'name');
|
||||
$categories = Arrays::changeKeyName($categories, 'key', 'id');
|
||||
return $categories;
|
||||
}
|
||||
|
||||
public static function getCategoryTree()
|
||||
{
|
||||
return self::getModel()->defaultOrder()->get()->toTree();
|
||||
}
|
||||
|
||||
public static function getChildren($data, $withFolder = false)
|
||||
{
|
||||
$tree = [];
|
||||
foreach ($data as $item) {
|
||||
$leaf = [];
|
||||
$leaf['name'] = $item['name'];
|
||||
$leaf['id'] = $item['id'];
|
||||
$children = ($item['children'] ?? false) ? self::getChildren($item['children']) : false;
|
||||
if ($children) {
|
||||
$leaf['children'] = $children;
|
||||
if ($withFolder) {
|
||||
$leaf['folder'] = true;
|
||||
}
|
||||
}
|
||||
$tree[] = $leaf;
|
||||
}
|
||||
return $tree;
|
||||
}
|
||||
|
||||
public static function moveTree($node_id, $target_id, $type)
|
||||
{
|
||||
$category = self::getNode($node_id);
|
||||
$category_target = self::getNode($target_id);
|
||||
|
||||
switch ($type) {
|
||||
case 'after':
|
||||
// dump("$node_id After $target_id");
|
||||
$category->afterNode($category_target);
|
||||
break;
|
||||
case 'inside':
|
||||
// dump("$node_id inside $target_id");
|
||||
$category_target->appendNode($category);
|
||||
break;
|
||||
}
|
||||
$category->save();
|
||||
return "1";
|
||||
}
|
||||
|
||||
|
||||
public static function create($data)
|
||||
{
|
||||
$parent = ($data['parent_id'] ?? false) ? self::getNode($data['parent_id']) : self::getRoot();
|
||||
$category = self::getModel()->create(['name' => $data['name']]);
|
||||
$category->appendToNode($parent)->save();
|
||||
return $category;
|
||||
}
|
||||
|
||||
public static function update($data, $id = false)
|
||||
{
|
||||
$id = $id ? $id : $data['category_id'];
|
||||
$item = self::getNode($id);
|
||||
$item->update(['name' => $data['name']]);
|
||||
return $item;
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
// return Category::destroy($id);
|
||||
}
|
||||
|
||||
public static function getRoot()
|
||||
{
|
||||
return self::getNode(1);
|
||||
}
|
||||
|
||||
public static function getNode($id)
|
||||
{
|
||||
return self::getModel()->find($id);
|
||||
}
|
||||
|
||||
public static function getModel()
|
||||
{
|
||||
return app('rinvex.categories.category');
|
||||
}
|
||||
}
|
||||
@@ -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'];
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user