276 lines
8.7 KiB
PHP
276 lines
8.7 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Shop;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use App\Repositories\Core\Tag;
|
|
use App\Repositories\Core\Medias;
|
|
use App\Repositories\Core\Comments;
|
|
use App\Repositories\Botanic\Species;
|
|
use App\Repositories\Botanic\Varieties;
|
|
use App\Models\Shop\Article;
|
|
|
|
use App\Traits\Repository\Imageable;
|
|
class Articles
|
|
{
|
|
use Imageable;
|
|
|
|
public static function autocomplete($str)
|
|
{
|
|
$data = Article::byAutocomplete($str)->orderBy('name')->limit(30)->get()->pluck('name', 'id');
|
|
$export = [];
|
|
foreach ($data as $key => $name) {
|
|
$export[] = ['value' => $key, 'text' => $name];
|
|
}
|
|
return $export;
|
|
}
|
|
|
|
public static function getOptions()
|
|
{
|
|
return Article::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
}
|
|
|
|
public static function getOptionsWithNature()
|
|
{
|
|
$articles = Article::with(['article_nature'])->get();
|
|
foreach ($articles as $article) {
|
|
$data[$article->id] = ($article->article_nature->name ?? null) . ' - ' . $article->name;
|
|
}
|
|
asort($data, SORT_NATURAL);
|
|
return $data;
|
|
}
|
|
|
|
public static function getAll()
|
|
{
|
|
return Article::orderBy('name', 'asc')->get();
|
|
}
|
|
|
|
public static function getArticle($id)
|
|
{
|
|
$article = self::get($id);
|
|
$data = $article->toArray();
|
|
$data['inherited'] = self::getInherited($id);
|
|
$data['categories'] = self::getCategoriesNameByArticle($article);
|
|
$data['tags'] = self::getTagsNameByArticle($article);
|
|
$data['comments'] = Comments::getByModel($article);
|
|
return $data;
|
|
}
|
|
|
|
public static function getFull($id)
|
|
{
|
|
$data['article'] = self::getArticleEdit($id);
|
|
self::getMeta($data);
|
|
return $data;
|
|
}
|
|
|
|
public static function getArticleEdit($id)
|
|
{
|
|
$article = self::get($id);
|
|
$data = $article->toArray();
|
|
$data['inherited'] = self::getInherited($id);
|
|
$data['categories'] = self::getCategoriesByArticle($article);
|
|
$data['tags'] = self::getTagsByArticle($article);
|
|
$data['comments'] = Comments::getByModel($article);
|
|
return $data;
|
|
}
|
|
|
|
public static function getInherited($id)
|
|
{
|
|
$article = Article::with('product.tags.group')->findOrFail($id);
|
|
$product_type = $article->product_type;
|
|
switch ($product_type) {
|
|
case 'App\Models\Botanic\Variety':
|
|
$data[] = [
|
|
'name' => 'Espèces',
|
|
'description' => Species::getDescription($article->product->specie_id),
|
|
'tags' => Species::getTags($article->product->specie_id),
|
|
];
|
|
$data[] = [
|
|
'name' => 'Variétés',
|
|
'description' => $article->product->description,
|
|
'tags' => $article->product->tags->toArray()
|
|
];
|
|
break;
|
|
case 'App\Models\Botanic\Specie':
|
|
$data[] = [
|
|
'name' => 'Espèces',
|
|
'description' => $article->product->description,
|
|
'tags' => $article->product->tags->toArray()
|
|
];
|
|
break;
|
|
case 'App\Models\Shop\Merchandise':
|
|
$data[] = [
|
|
'name' => 'Marchandise',
|
|
'description' => $article->product->description,
|
|
'tags' => $article->product->tags->toArray(),
|
|
];
|
|
break;
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public static function getInheritedByProduct($product_id, $product_type)
|
|
{
|
|
switch ($product_type) {
|
|
case 'App\Models\Botanic\Variety':
|
|
$product = Varieties::get($product_id);
|
|
$data[] = [
|
|
'name' => 'Espèces',
|
|
'description' => Species::getDescription($product->specie_id),
|
|
'tags' => Species::getTags($product->specie_id),
|
|
];
|
|
$data[] = [
|
|
'name' => 'Variétés',
|
|
'description' => $product->description,
|
|
'tags' => $product->tags->toArray(),
|
|
];
|
|
break;
|
|
case 'App\Models\Botanic\Specie':
|
|
$product = Species::get($product_id);
|
|
$data[] = [
|
|
'name' => 'Espèces',
|
|
'description' => $product->description,
|
|
'tags' => $product->tags->toArray(),
|
|
];
|
|
break;
|
|
case 'App\Models\Shop\Merchandise':
|
|
$product = Merchandises::get($product_id);
|
|
$data[] = [
|
|
'name' => 'Marchandise',
|
|
'description' => $product->description,
|
|
'tags' => $product->tags->toArray()
|
|
];
|
|
break;
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public static function getInheritedImagesByProduct($product_id, $product_type)
|
|
{
|
|
switch ($product_type) {
|
|
case 'App\Models\Botanic\Variety':
|
|
$data['images'] = Varieties::getImages($product_id);
|
|
break;
|
|
case 'App\Models\Botanic\Specie':
|
|
$data['images'] = Species::getImages($product_id);
|
|
break;
|
|
case 'App\Models\Shop\Merchandise':
|
|
$data['images'] = Merchandises::getImages($product_id);
|
|
break;
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public static function getMeta(&$data = [])
|
|
{
|
|
$data['products'] = (($data['article']['product_type'] ?? false) == 'App\Models\Botanic\Variety') ? Varieties::getOptionsWithSpecie() : Species::getOptions();
|
|
$data['categories_options'] = Categories::getOptions();
|
|
$data['natures_options'] = ArticleNatures::getOptions();
|
|
$data['packages'] = ($data['article']['article_family_id'] ?? false) ? Packages::getSelectByFamily($data['article']['article_family_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::byCategory($category_id)->with(['prices', 'product', 'image'])->get();
|
|
}
|
|
|
|
public static function getCategoriesByArticle($article)
|
|
{
|
|
return $article->categories->pluck('id')->toArray();
|
|
}
|
|
|
|
public static function getCategoriesNameByArticle($article)
|
|
{
|
|
return $article->categories->pluck('name', 'id')->toArray();
|
|
}
|
|
|
|
public static function getTagsByArticle($article)
|
|
{
|
|
return $article->tags->pluck('id')->toArray();
|
|
}
|
|
|
|
public static function getTagsNameByArticle($article)
|
|
{
|
|
return $article->tags->pluck('name', '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;
|
|
unset($data['images']);
|
|
|
|
$categories = isset($data['categories']) ? $data['categories'] : false;
|
|
unset($data['categories']);
|
|
|
|
$tags = isset($data['tags']) ? $data['tags'] : false;
|
|
unset($data['tags']);
|
|
|
|
$prices = isset($data['prices']) ? $data['prices'] : false;
|
|
unset($data['prices']);
|
|
|
|
$article = self::store($data);
|
|
self::storeImages($article, $images);
|
|
self::storeCategories($article, $categories);
|
|
self::storeTags($article, $tags);
|
|
return $article->id;
|
|
}
|
|
|
|
public static function store($data)
|
|
{
|
|
$id = isset($data['id']) ? $data['id'] : false;
|
|
return $id ? self::update($data, $id) : 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);
|
|
}
|
|
|
|
}
|