Files
opensem/app/Datatables/Shop/ArticlesDataTable.php
Ludovic CANDELLIER 439a339027 fixes on merchandise
2022-05-02 08:34:40 +02:00

108 lines
4.3 KiB
PHP

<?php
namespace App\Datatables\Shop;
use Yajra\DataTables\Html\Column;
use App\Datatables\ParentDataTable as DataTable;
use App\Models\Shop\Article;
use App\Repositories\Shop\Articles;
use App\Repositories\Shop\Tags;
class ArticlesDataTable extends DataTable
{
public $model_name = 'articles';
public $sortedColumn = 2;
public function query(Article $model)
{
$model = $model::with(['article_nature', 'image'])->withCount(['categories', 'images', 'offers', 'tags']);
$model = self::filterByArticleNature($model);
$model = self::filterByCategory($model);
$model = self::filterByTag($model);
return $this->buildQuery($model);
}
public static function filterByArticleNature($model, $article_nature_id = false)
{
$article_nature_id = $article_nature_id ? $article_nature_id : self::isFilteredByField('article_nature_id');
return $article_nature_id ? $model->byArticleNature($article_nature_id) : $model;
}
public static function filterByCategory($model, $category_id = false)
{
$category_id = $category_id ? $category_id : self::isFilteredByField('category_id');
return $category_id ? $model->byCategory($category_id) : $model;
}
public static function filterByTag($model, $tag_id = false)
{
$tag_id = $tag_id ? $tag_id : self::isFilteredByField('tag_id');
return $tag_id ? $model->byTag($tag_id) : $model;
}
public function modifier($datatables)
{
$datatables
->editColumn('visible', function (Article $article) {
return view("components.form.toggle", [
'name' => 'visible',
'value' => $article->visible,
'on' => __('oui'),
'off' => __('non'),
'meta' => 'data-id=' . $article->id,
'size' => 'sm',
'class' => 'visible',
]);
})
->editColumn('homepage', function (Article $article) {
return view("components.form.toggle", [
'name' => 'homepage',
'value' => $article->homepage,
'on' => __('oui'),
'off' => __('non'),
'meta' => 'data-id=' . $article->id,
'size' => 'sm',
'class' => 'homepage',
]);
})
->editColumn('thumb', function (Article $article) {
$image = Articles::getFullImageByArticle($article);
return Articles::getThumb($image, false);
})
->editColumn('article_nature.name', function (Article $article) {
return $article->article_nature ? $article->article_nature->name : '';
})
->editColumn('tags2', function (Article $article) {
$html = '';
foreach ($article->tags as $tag) {
$html .= Tags::getTagHtml($tag);
}
return $html;
})
->editColumn('images_count2', function (Article $article) {
return Articles::countFullImagesByArticle($article);
})
->rawColumns(['tags2', 'thumb', 'action']);
return parent::modifier($datatables);
}
protected function getColumns()
{
return [
Column::make('visible')->title('Visible')->searchable(false)->width(50),
Column::make('homepage')->title('Accueil')->searchable(false)->width(50),
Column::make('article_nature.name')->title('Nature'),
Column::make('thumb')->title('')->searchable(false)->orderable(false)->width(40)->class('text-center'),
Column::make('name')->title('Nom'),
Column::make('tags2')->title('Tags')->searchable(false)->orderable(false),
Column::make('tags_count')->title('#Tag')->class('text-right')->searchable(false)->width(40),
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(),
];
}
}