Files
opensem/app/Repositories/Shop/Articles.php
Ludovic CANDELLIER f4aecc9130 fixes on tags with slug
2022-01-22 22:05:18 +01:00

345 lines
11 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)->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['description'] = (!empty($article->description)) ? $article->description : $article->product->description;
$data['image'] = Articles::getPreview($article->image);
$data['image_big'] = Articles::getImage($article->image);
$data['inherited'] = self::getInherited($id);
$data['categories'] = self::getCategoriesNameByArticle($article);
$data['tags'] = self::getTagsSlugByArticle($article);
$data['comments'] = Comments::getByModel($article);
return $data;
}
public static function getArticlesToSell()
{
$articles = self::getArticlesWithOffers();
foreach ($articles as $article) {
$price_lists = $article->offers[0]->tariff->price_lists->toArray();
// dump($price_lists);
if (count($price_lists)) {
$data[$article->name] = [
'description' => (!empty($article->description)) ? $article->description : $article->product->description,
'image' => $article->image,
'product_type' => $article->product_type,
'product_id' => $article->product_id,
'product_name' => $article->product->name,
'parent_name' => trim(str_replace($article->product->name, '', $article->name)),
];
$prices = $price_lists[0]['price_list_values'][0];
$article_nature_name = strtolower($article->article_nature->name);
// dump($prices);
$data[$article->name][$article_nature_name] = [
'article_id' => $article->id,
'offer_id' => $article->offers[0]->id,
'quantity' => $prices['quantity'],
'price' => $prices['price_taxed'],
'variation' => $article->offers[0]->variation->name,
];
}
}
// dump($data);
// exit;
return $data;
}
public static function getArticlesWithOffers()
{
return Article::visible()->withAvailableOffers()->with([
'image',
'product',
'article_nature',
'offers.variation.package',
'offers.tariff.price_lists.price_list_values',
])->get();
}
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 = [])
{
switch ($data['article']['product_type'] ?? false) {
case 'App\Models\Botanic\Variety':
$data['products'] = Varieties::getOptionsWithSpecie();
break;
case 'App\Models\Botanic\Variety':
$data['products'] = Species::getOptions();
break;
case 'App\Models\Shop\Merchandise':
$data['products'] = Merchandises::getOptions();
break;
default:
$data['products'] = [];
}
$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',
'App\Models\Shop\Merchandise' => 'Marchandise',
'App\Models\Shop\Merchandise' => 'Autres',
];
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 getTagsSlugByArticle($article)
{
return $article->tags->pluck('slug', '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);
}
}