refactoring on Articles, minor fixes
This commit is contained in:
@@ -29,7 +29,7 @@ class Articles
|
||||
|
||||
public static function getOffersGroupedByNature($id, $saleChannelId = false)
|
||||
{
|
||||
$articleIds = self::getSiblingsIds($id);
|
||||
$articleIds = ArticleSiblings::getSiblingsIds($id);
|
||||
$articleIds[] = $id;
|
||||
$offers = Offers::getOffersByArticles($articleIds, $saleChannelId);
|
||||
foreach ($offers as $offer) {
|
||||
@@ -43,33 +43,6 @@ class Articles
|
||||
return $data ?? false;
|
||||
}
|
||||
|
||||
public static function getSiblingsIDs($id)
|
||||
{
|
||||
return self::getSiblings($id)->pluck('id')->toArray();
|
||||
}
|
||||
|
||||
public static function getSiblingsDescriptions($id)
|
||||
{
|
||||
$data = [];
|
||||
$siblings = self::getSiblings($id);
|
||||
foreach ($siblings as $sibling) {
|
||||
if ($sibling->description && ($sibling->article_nature->name ?? false)) {
|
||||
$data[strtolower($sibling->article_nature->name)] = $sibling->description;
|
||||
}
|
||||
}
|
||||
|
||||
return $data ?? false;
|
||||
}
|
||||
|
||||
public static function getSiblings($id)
|
||||
{
|
||||
return Article::with([
|
||||
'siblings' => function ($query) use ($id) {
|
||||
$query->where('id', '!=', $id);
|
||||
},
|
||||
])->find($id)->siblings;
|
||||
}
|
||||
|
||||
public static function getOffersById($id)
|
||||
{
|
||||
return Offers::getOffersByArticle($id);
|
||||
@@ -105,13 +78,13 @@ class Articles
|
||||
$article = self::get($id);
|
||||
$data = $article->toArray();
|
||||
$data['description'] = self::getFullDescriptionByArticle($article);
|
||||
$images = self::getFullImagesByArticle($article);
|
||||
$images = ArticleImages::getFullImagesByArticle($article);
|
||||
$data['image'] = self::getPreviewSrc($images[0] ?? false);
|
||||
$data['images'] = count($images) ? $images : false;
|
||||
$data['image_big'] = self::getImageSrc($images[0] ?? false);
|
||||
$data['inherited'] = self::getInherited($id);
|
||||
$data['categories'] = self::getCategoriesNameByArticle($article);
|
||||
$data['tags'] = self::getFullTagsSlugByArticle($article);
|
||||
$data['inherited'] = ArticleInherited::getInherited($id);
|
||||
$data['categories'] = ArticleCategories::getCategoriesNameByArticle($article);
|
||||
$data['tags'] = ArticleTags::getFullTagsSlugByArticle($article);
|
||||
$data['comments'] = Comments::getByModel($article);
|
||||
|
||||
return $data;
|
||||
@@ -140,7 +113,7 @@ class Articles
|
||||
if ($article->description) {
|
||||
$data[strtolower($article->article_nature->name ?? '')] = $article->description;
|
||||
}
|
||||
$siblings = self::getSiblingsDescriptions($article->id);
|
||||
$siblings = ArticleSiblings::getSiblingsDescriptions($article->id);
|
||||
if ($siblings) {
|
||||
array_push($data, $siblings);
|
||||
}
|
||||
@@ -195,7 +168,7 @@ class Articles
|
||||
'id' => $article->id,
|
||||
'article_nature_id' => $article->article_nature_id,
|
||||
'description' => $article->description ? $article->description : $article->product->description,
|
||||
'image' => self::getFullImageByArticle($article),
|
||||
'image' => ArticleImages::getFullImageByArticle($article),
|
||||
'product_type' => $article->product_type,
|
||||
'product_id' => $article->product_id,
|
||||
'product_name' => $article->product->name,
|
||||
@@ -315,87 +288,14 @@ class Articles
|
||||
{
|
||||
$article = self::get($id);
|
||||
$data = $article->toArray();
|
||||
$data['inherited'] = self::getInherited($id);
|
||||
$data['categories'] = self::getCategoriesByArticle($article);
|
||||
$data['tags'] = self::getTagsByArticle($article);
|
||||
$data['inherited'] = ArticleInherited::getInherited($id);
|
||||
$data['categories'] = ArticleCategories::getCategoriesByArticle($article);
|
||||
$data['tags'] = ArticleTags::getTagsByArticle($article);
|
||||
$data['comments'] = Comments::getByModel($article);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function getInherited($id)
|
||||
{
|
||||
$article = Article::with('product.tags.tag_group')->findOrFail($id);
|
||||
|
||||
return self::getInheritedByProduct($article->product_id, $article->product_type);
|
||||
}
|
||||
|
||||
public static function getInheritedByProduct($product_id, $product_type)
|
||||
{
|
||||
$data = [];
|
||||
switch ($product_type) {
|
||||
case 'App\Models\Botanic\Variety':
|
||||
$product = Varieties::get($product_id);
|
||||
if (! $product) {
|
||||
break;
|
||||
}
|
||||
$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);
|
||||
if (! $product) {
|
||||
break;
|
||||
}
|
||||
$data[] = [
|
||||
'name' => 'Espèces',
|
||||
'description' => $product->description,
|
||||
'tags' => $product->tags->toArray(),
|
||||
];
|
||||
break;
|
||||
case 'App\Models\Shop\Merchandise':
|
||||
$product = Merchandises::get($product_id);
|
||||
if (! $product) {
|
||||
break;
|
||||
}
|
||||
$data[] = [
|
||||
'name' => 'Marchandise',
|
||||
'description' => $product->description,
|
||||
'tags' => $product->tags->toArray(),
|
||||
];
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
return $data ?? false;
|
||||
}
|
||||
|
||||
public static function getInheritedImagesByProduct($product_id, $product_type)
|
||||
{
|
||||
switch ($product_type) {
|
||||
case 'App\Models\Botanic\Variety':
|
||||
$images = Varieties::getImages($product_id);
|
||||
break;
|
||||
case 'App\Models\Botanic\Specie':
|
||||
$images = Species::getImages($product_id);
|
||||
break;
|
||||
case 'App\Models\Shop\Merchandise':
|
||||
$images = Merchandises::getImages($product_id);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
return $images ?? false ? ['images' => $images] : false;
|
||||
}
|
||||
|
||||
public static function getMeta(&$data = [])
|
||||
{
|
||||
switch ($data['article']['product_type'] ?? false) {
|
||||
@@ -424,163 +324,11 @@ class Articles
|
||||
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 getProductTypeByCategory($category_id)
|
||||
{
|
||||
$models = self::getProductTypesModelsByCategory($category_id);
|
||||
|
||||
return ($models[0] ?? false) === Merchandise::class ? 'merchandise' : 'botanic';
|
||||
}
|
||||
|
||||
public static function getProductTypesModelsByCategory($category_id)
|
||||
{
|
||||
return Article::byCategory($category_id)->select('product_type')->distinct()->get();
|
||||
}
|
||||
|
||||
public static function countProductTypesByCategory($category_id)
|
||||
{
|
||||
return Article::byCategory($category_id)->select('product_type')->distinct()->count();
|
||||
}
|
||||
|
||||
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 getFullTagsSlugByArticle($article)
|
||||
{
|
||||
$data = [];
|
||||
|
||||
switch ($article->product_type) {
|
||||
case 'App\Models\Botanic\Variety':
|
||||
$data += $article->product->tags->toArray();
|
||||
if ($article->product->specie ?? false) {
|
||||
$data += $article->product->specie->tags->toArray();
|
||||
}
|
||||
break;
|
||||
case 'App\Models\Botanic\Specie':
|
||||
$data += $article->product->tags->toArray();
|
||||
break;
|
||||
case 'App\Models\Shop\Merchandise':
|
||||
$data += $article->product->tags->toArray();
|
||||
$data += $article->product->producer->tags->toArray();
|
||||
break;
|
||||
default:
|
||||
}
|
||||
$data += $article->tags->toArray();
|
||||
|
||||
foreach ($data as $tag) {
|
||||
if (! isset($tags[$tag['group']][$tag['name']])) {
|
||||
$tags[$tag['group']][] = $tag['name'];
|
||||
}
|
||||
}
|
||||
|
||||
return $tags ?? null;
|
||||
}
|
||||
|
||||
public static function getPricesByArticle($article)
|
||||
{
|
||||
return Prices::getByArticle($article->id);
|
||||
}
|
||||
|
||||
public static function getFullImagesByArticleId($id)
|
||||
{
|
||||
$article = self::get($id);
|
||||
|
||||
return $article ? self::getFullImagesByArticle($article) : false;
|
||||
}
|
||||
|
||||
public static function countFullImagesByArticleId($id)
|
||||
{
|
||||
$article = self::get($id);
|
||||
|
||||
return $article ? self::countFullImagesByArticle($article) : 0;
|
||||
}
|
||||
|
||||
public static function countFullImagesByArticle($article)
|
||||
{
|
||||
return count(self::getFullImagesByArticle($article));
|
||||
}
|
||||
|
||||
public static function getFullImagesByArticle($article)
|
||||
{
|
||||
$images = count($article->images) ? $article->images : collect([]);
|
||||
switch ($article->product_type) {
|
||||
case 'App\Models\Botanic\Variety':
|
||||
$variety = $article->product ?? false;
|
||||
$specie = $variety->specie ?? false;
|
||||
if ($variety) {
|
||||
$images = count($variety->images ?? []) ? $images->merge($variety->images) : $images;
|
||||
}
|
||||
if ($specie) {
|
||||
$images = count($specie->images ?? []) ? $images->merge($specie->images) : $images;
|
||||
}
|
||||
break;
|
||||
case 'App\Models\Botanic\Specie':
|
||||
$specie = $article->product ?? false;
|
||||
$images = count($specie->images ?? []) ? $specie->images : $images;
|
||||
break;
|
||||
case 'App\Models\Shop\Merchandise':
|
||||
$merchandise = $article->product ?? false;
|
||||
$images = count($merchandise->images ?? []) ? $merchandise->images : $images;
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
return $images;
|
||||
}
|
||||
|
||||
public static function getFullImageById($id)
|
||||
{
|
||||
return self::getFullImageByArticle(self::get($id));
|
||||
}
|
||||
|
||||
public static function getFullImageByArticle($article)
|
||||
{
|
||||
$image = $article->image;
|
||||
if ($image) {
|
||||
return $image;
|
||||
}
|
||||
switch ($article->product_type) {
|
||||
case 'App\Models\Botanic\Variety':
|
||||
$image = $article->product->image ?? ($article->product->specie->image ?? false);
|
||||
break;
|
||||
case 'App\Models\Botanic\Specie':
|
||||
$image = $article->product->image ?? false;
|
||||
break;
|
||||
case 'App\Models\Shop\Merchandise':
|
||||
$image = $article->product->image ?? false;
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
public static function storeFull($data)
|
||||
{
|
||||
$images = $data['images'] ?? false;
|
||||
@@ -594,31 +342,12 @@ class Articles
|
||||
|
||||
$article = self::store($data);
|
||||
self::storeImages($article, $images);
|
||||
self::storeCategories($article, $categories);
|
||||
self::storeTags($article, $tags);
|
||||
ArticleCategories::storeCategories($article, $categories);
|
||||
ArticleTags::storeTags($article, $tags);
|
||||
|
||||
return $article->id;
|
||||
}
|
||||
|
||||
public static function storeCategories($article, $categories)
|
||||
{
|
||||
if (! $categories) {
|
||||
return false;
|
||||
}
|
||||
$categories = collect($categories)->transform(
|
||||
function ($item, $key) {
|
||||
return (int) $item;
|
||||
}
|
||||
)->toArray();
|
||||
|
||||
return $article->syncCategories($categories, true);
|
||||
}
|
||||
|
||||
public static function storeTags($article, $tags)
|
||||
{
|
||||
return Tag::storeTags($article, $tags);
|
||||
}
|
||||
|
||||
public static function toggleVisible($id, $visible)
|
||||
{
|
||||
return self::update(['visible' => $visible], $id);
|
||||
|
||||
Reference in New Issue
Block a user