Multi-images component, refactoring medias functions
This commit is contained in:
@@ -76,10 +76,13 @@ class ArticlesDataTable extends DataTable
|
||||
->editColumn('tags2', function (Article $article) {
|
||||
$html = '';
|
||||
foreach ($article->tags as $tag) {
|
||||
$html .= '<span class="btn btn-xs btn-success pb-2">' . Tags::getFullnameByTag($tag) . '</span> ';
|
||||
$html .= '<span class="btn btn-xs btn-success pb-2 mb-1" style="font-size: 0.8em;">' . Tags::getFullnameByTag($tag) . '</span> ';
|
||||
}
|
||||
return $html;
|
||||
})
|
||||
->editColumn('images_count2', function (Article $article) {
|
||||
return Articles::countFullImagesByArticle($article);
|
||||
})
|
||||
->rawColumns(['tags2', 'thumb', 'action']);
|
||||
return parent::modifier($datatables);
|
||||
}
|
||||
@@ -97,6 +100,7 @@ class ArticlesDataTable extends DataTable
|
||||
Column::make('categories_count')->title('#Ray')->class('text-right')->searchable(false)->width(40),
|
||||
Column::make('offers_count')->title('#Ofr')->class('text-right')->searchable(false)->width(40),
|
||||
Column::make('images_count')->title('#Pho')->class('text-right')->searchable(false)->width(40),
|
||||
Column::make('images_count2')->title('#PhoH')->class('text-right')->searchable(false)->orderable(false)->width(40),
|
||||
$this->makeColumnButtons(),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ class Species
|
||||
|
||||
public static function get($id)
|
||||
{
|
||||
return Specie::with('tags.group')->findOrFail($id);
|
||||
return Specie::with('tags.tag_group')->findOrFail($id);
|
||||
}
|
||||
|
||||
public static function storeFull($data)
|
||||
|
||||
@@ -48,7 +48,7 @@ class Varieties
|
||||
|
||||
public static function get($id)
|
||||
{
|
||||
return Variety::with('tags.group')->findOrFail($id);
|
||||
return Variety::with('tags.tag_group')->findOrFail($id);
|
||||
}
|
||||
|
||||
public static function getFull($id)
|
||||
|
||||
10
app/Repositories/Core/Images.php
Normal file
10
app/Repositories/Core/Images.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Core;
|
||||
|
||||
use App\Traits\Repository\Imageable;
|
||||
|
||||
class Images
|
||||
{
|
||||
use Imageable;
|
||||
}
|
||||
@@ -80,33 +80,38 @@ class Medias
|
||||
return null;
|
||||
}
|
||||
$id = $image['id'];
|
||||
$image['name'] = str_replace(['#', '/', '\\', ' '], '-', $image['name']);
|
||||
$filename = $image['name'] . self::getExtension($image['file_name']);
|
||||
|
||||
$filename = self::getFilename($image);
|
||||
return "/storage/$id/$filename";
|
||||
}
|
||||
|
||||
public static function getThumbSrc($image)
|
||||
{
|
||||
if (!$image) {
|
||||
return null;
|
||||
}
|
||||
$id = $image['id'];
|
||||
$image['name'] = str_replace(['#', '/', '\\', ' '], '-', $image['name']);
|
||||
$filename = $image['name'] . '-thumb' . self::getExtension($image['file_name']);
|
||||
|
||||
return "/storage/$id/conversions/$filename";
|
||||
return self::getSrcByType($image, 'thumb');
|
||||
}
|
||||
|
||||
public static function getPreviewSrc($image)
|
||||
{
|
||||
if (!$image) {
|
||||
return null;
|
||||
}
|
||||
$id = $image['id'];
|
||||
$image['name'] = str_replace(['#', '/', '\\', ' '], '-', $image['name']);
|
||||
$filename = $image['name'] . '-preview' . self::getExtension($image['file_name']);
|
||||
return self::getSrcByType($image, 'preview');
|
||||
}
|
||||
|
||||
return "/storage/$id/conversions/$filename";
|
||||
public static function getNormalSrc($image)
|
||||
{
|
||||
return self::getSrcByType($image, 'normal');
|
||||
}
|
||||
|
||||
public static function getSrcByType($image, $type)
|
||||
{
|
||||
return $image ? '/storage/' . $image['id'] . '/conversions/' . self::getFilename($image, $type) : false;
|
||||
}
|
||||
|
||||
public static function getFilename($image, $type = false)
|
||||
{
|
||||
$image['name'] = self::convertName($image['name']);
|
||||
return $image['name'] . ($type ? '-' . $type : '') . self::getExtension($image['file_name']);
|
||||
}
|
||||
|
||||
public static function convertName($name)
|
||||
{
|
||||
return str_replace(['#', '/', '\\', ' '], '-', $name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,9 +82,10 @@ class Articles
|
||||
$data = $article->toArray();
|
||||
$parents = self::getInheritedByProduct($article->product_id, $article->product_type);
|
||||
$data['description'] = self::getFullDescriptionByArticle($article);
|
||||
$image = self::getFullImageByArticle($article);
|
||||
$data['image'] = self::getPreviewSrc($image);
|
||||
$data['image_big'] = self::getImageSrc($image);
|
||||
$images = self::getFullImagesByArticle($article);
|
||||
$data['image'] = self::getPreviewSrc($images[0] ?? false);
|
||||
$data['images'] = $images;
|
||||
$data['image_big'] = self::getImageSrc($images[0] ?? false);
|
||||
$data['inherited'] = self::getInherited($id);
|
||||
$data['categories'] = self::getCategoriesNameByArticle($article);
|
||||
$data['tags'] = self::getTagsSlugByArticle($article);
|
||||
@@ -233,7 +234,7 @@ class Articles
|
||||
|
||||
public static function getInherited($id)
|
||||
{
|
||||
$article = Article::with('product.tags.group')->findOrFail($id);
|
||||
$article = Article::with('product.tags.tag_group')->findOrFail($id);
|
||||
$product_type = $article->product_type;
|
||||
switch ($product_type) {
|
||||
case 'App\Models\Botanic\Variety':
|
||||
@@ -406,24 +407,23 @@ class Articles
|
||||
|
||||
public static function getFullImagesByArticle($article)
|
||||
{
|
||||
$images = count($article->images) ? $article->images : collect([]);
|
||||
switch ($article->product_type) {
|
||||
case 'App\Models\Botanic\Variety':
|
||||
$variety = $article->product;
|
||||
$specie = $variety->specie;
|
||||
$images = count($variety->images) ? $variety->images : [];
|
||||
$images = count($specie->images) ? $images->push($specie->images) : $images;
|
||||
$images = $variety ? (count($variety->images) ? $images->merge($variety->images) : $images) : $images;
|
||||
$images = $specie ? (count($specie->images) ? $images->merge($specie->images) : $images) : $images;
|
||||
break;
|
||||
case 'App\Models\Botanic\Specie':
|
||||
$specie = $article->product;
|
||||
$images = count($specie->images) ? $specie->images : [];
|
||||
$images = count($specie->images) ? $specie->images : $images;
|
||||
break;
|
||||
case 'App\Models\Shop\Merchandise':
|
||||
$merchandise = $article->product;
|
||||
$images = count($merchandise->images) ? $merchandise->images : [];
|
||||
$images = count($merchandise->images) ? $merchandise->images : $images;
|
||||
break;
|
||||
default:
|
||||
$images = [];
|
||||
}
|
||||
$images = count($article->images) ? $images->push($article->images) : $images;
|
||||
return $images;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,12 +12,12 @@ trait Imageable
|
||||
|
||||
public function images()
|
||||
{
|
||||
return $this->hasMany('App\Models\Core\Media', 'model_id')->where('model_type', get_class($this));
|
||||
return $this->hasMany(Media::class, 'model_id')->where('model_type', get_class($this));
|
||||
}
|
||||
|
||||
public function image()
|
||||
{
|
||||
return $this->hasOne('App\Models\Core\Media', 'model_id')->where('model_type', get_class($this))->latest();
|
||||
return $this->hasOne(Media::class, 'model_id')->where('model_type', get_class($this))->latest();
|
||||
}
|
||||
|
||||
public function registerMediaConversions(Media $media = null) : void
|
||||
|
||||
@@ -35,7 +35,18 @@ trait Imageable
|
||||
|
||||
public static function getPreviewSrc($image, $with_undefined = true)
|
||||
{
|
||||
return $image ? Medias::getPreviewSrc($image) : ($with_undefined ? '/img/visuel-non-disponible.jpg' :'');
|
||||
return $image ? Medias::getPreviewSrc($image) : ($with_undefined ? '/img/visuel-non-disponible.jpg' : '');
|
||||
}
|
||||
|
||||
public static function getNormal($image, $with_undefined = true)
|
||||
{
|
||||
$src = self::getNormalSrc($image, $with_undefined);
|
||||
return $src ? "<img src='$src'>" : '';
|
||||
}
|
||||
|
||||
public static function getNormalSrc($image, $with_undefined = true)
|
||||
{
|
||||
return $image ? Medias::getNormalSrc($image) : ($with_undefined ? '/img/visuel-non-disponible.jpg' : '');
|
||||
}
|
||||
|
||||
public static function getImage($image, $with_undefined = true)
|
||||
|
||||
Reference in New Issue
Block a user