161 lines
4.2 KiB
PHP
161 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Shop;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use App\Repositories\Core\Tag;
|
|
use App\Repositories\Core\Media;
|
|
use App\Repositories\Botanic\Species;
|
|
use App\Repositories\Botanic\Varieties;
|
|
use App\Models\Shop\Article;
|
|
|
|
class Articles
|
|
{
|
|
|
|
public static function getAll()
|
|
{
|
|
return Article::orderBy('name','asc')->get();
|
|
}
|
|
|
|
public static function getFull($id)
|
|
{
|
|
$article = Article::with('product.tags')->findOrFail($id);
|
|
$data['article'] = $article->toArray();
|
|
$data['article']['categories'] = self::getCategoriesByArticle($article);
|
|
$data['article']['tags'] = self::getTagsByArticle($article);
|
|
$data['article']['prices'] = self::getPricesByArticle($article);
|
|
self::getMeta($data);
|
|
return $data;
|
|
}
|
|
|
|
public static function getMeta(&$data = [])
|
|
{
|
|
$data['products'] = ( ($data['product_type'] ?? false) == 'App\Models\Botanic\Variety') ? Varieties::getOptionsWithSpecie() : Species::getOptions();
|
|
$data['categories_options'] = Categories::getOptions();
|
|
$data['price_generics'] = PriceGenericCategories::getOptionsWithChildrens();
|
|
$data['families_options'] = ArticleFamilies::getOptions();
|
|
$data['taxes_options'] = Taxes::getOptions();
|
|
$data['packages'] = ($data['article_family_id'] ?? false) ? Packages::getSelectByFamily($data['article_family_id']) : [];
|
|
$data['unities'] = ($data['packages']['id'] ?? false) ? Unities::getSelectByPackage($data['packages']['id']) : [];
|
|
$data['tags_list'] = TagGroups::getTreeTags();
|
|
$data['models_options'] = ['App\Models\Botanic\Specie' => 'Espèces', 'App\Models\Botanic\Variety' => 'Variétés'];
|
|
return $data;
|
|
}
|
|
|
|
public static function getByCategory($category_id)
|
|
{
|
|
return Article::with(['prices','product','image'])->get();
|
|
}
|
|
|
|
public static function getCategoriesByArticle($article)
|
|
{
|
|
return $article->categories->pluck('id')->toArray();
|
|
}
|
|
|
|
public static function getTagsByArticle($article)
|
|
{
|
|
return $article->tags->pluck('id')->toArray();
|
|
}
|
|
|
|
public static function getPricesByArticle($article)
|
|
{
|
|
return Prices::getByArticle($article->id);
|
|
}
|
|
|
|
public static function get($id)
|
|
{
|
|
return Article::find($id);
|
|
}
|
|
|
|
public static function storeFull($data)
|
|
{
|
|
$images = isset($data['images']) ? $data['images'] : false;
|
|
$categories = isset($data['categories']) ? $data['categories'] : false;
|
|
$tags = isset($data['tags']) ? $data['tags'] : false;
|
|
$prices = isset($data['prices']) ? $data['prices'] : false;
|
|
unset($data['images']);
|
|
unset($data['categories']);
|
|
unset($data['tags']);
|
|
unset($data['prices']);
|
|
$article = self::store($data);
|
|
self::storeImages($article, $images);
|
|
self::storeCategories($article, $categories);
|
|
self::storeTags($article, $tags);
|
|
self::storePrices($article, $prices);
|
|
return $article->id;
|
|
}
|
|
|
|
public static function store($data)
|
|
{
|
|
$id = isset($data['id']) ? $data['id'] : false;
|
|
return $id ? self::update($data) : self::create($data);
|
|
}
|
|
|
|
public static function create($data)
|
|
{
|
|
return Article::create($data);
|
|
}
|
|
|
|
public static function update($data, $id = false)
|
|
{
|
|
$id = $id ? $id : $data['id'];
|
|
$article = self::get($id);
|
|
$ret = $article->update($data);
|
|
return $article;
|
|
}
|
|
|
|
public static function destroy($id)
|
|
{
|
|
return Article::destroy($id);
|
|
}
|
|
|
|
public static function storeCategories($article, $categories)
|
|
{
|
|
if ($categories) {
|
|
$categories = collect($categories)->transform(function ($item, $key) {
|
|
return (int) $item;
|
|
})->toArray();
|
|
return $article->syncCategories($categories, true);
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function storeTags($article, $tags)
|
|
{
|
|
return Tag::storeTags($article, $tags);
|
|
}
|
|
|
|
public static function storePrices($article, $prices)
|
|
{
|
|
return ArticlePrices::storePrices($article->id, $prices);
|
|
}
|
|
|
|
public static function storeImages($article, $files)
|
|
{
|
|
return Media::storeImages($article, $files);
|
|
}
|
|
|
|
public static function storeImage($article, $file)
|
|
{
|
|
return Media::storeImage($article, $file);
|
|
}
|
|
|
|
public static function getImages($id)
|
|
{
|
|
return Media::getImages(self::get($id));
|
|
}
|
|
|
|
public static function getThumbSrc($image)
|
|
{
|
|
return Media::getThumbSrc($image);
|
|
}
|
|
|
|
public static function deleteImage($id, $index)
|
|
{
|
|
return Media::deleteImage(self::get($id), $index);
|
|
}
|
|
|
|
}
|