190 lines
6.0 KiB
PHP
190 lines
6.0 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 autocomplete($str)
|
|
{
|
|
$data = Article::where('name', 'LIKE', "%${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 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']['inherited'] = self::getInherited($id);
|
|
// dump($data);
|
|
// exit;
|
|
$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 getInherited($id)
|
|
{
|
|
$article = Article::with('product.tags')->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 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 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, $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);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|