1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Attributes\DataTables\Adminarea; |
6
|
|
|
|
7
|
|
|
use Rinvex\Attributes\Contracts\AttributeContract; |
8
|
|
|
use Cortex\Foundation\DataTables\AbstractDataTable; |
9
|
|
|
use Cortex\Attributes\Transformers\Adminarea\AttributeTransformer; |
10
|
|
|
|
11
|
|
|
class AttributesDataTable extends AbstractDataTable |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
|
|
protected $model = AttributeContract::class; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
protected $transformer = AttributeTransformer::class; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get the query object to be processed by dataTables. |
25
|
|
|
* |
26
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection |
|
|
|
|
27
|
|
|
*/ |
28
|
|
|
public function query() |
29
|
|
|
{ |
30
|
|
|
$locale = app()->getLocale(); |
31
|
|
|
$query = app($this->model)->query()->orderBy('group', 'ASC')->orderBy('sort_order', 'ASC')->orderBy("name->\${$locale}", 'ASC'); |
|
|
|
|
32
|
|
|
|
33
|
|
|
return $this->applyScopes($query); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get default builder parameters. |
38
|
|
|
* |
39
|
|
|
* @return array |
|
|
|
|
40
|
|
|
*/ |
41
|
|
|
protected function getBuilderParameters() |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
|
|
'keys' => true, |
45
|
|
|
'autoWidth' => false, |
46
|
|
|
'dom' => "<'row'<'col-sm-6'B><'col-sm-6'f>> <'row'r><'row'<'col-sm-12't>> <'row'<'col-sm-5'i><'col-sm-7'p>>", |
|
|
|
|
47
|
|
|
'buttons' => [ |
48
|
|
|
['extend' => 'create', 'text' => '<i class="fa fa-plus"></i> '.trans('cortex/foundation::common.new')], 'print', 'reset', 'reload', 'export', |
|
|
|
|
49
|
|
|
['extend' => 'colvis', 'text' => '<i class="fa fa-columns"></i> '.trans('cortex/foundation::common.columns').' <span class="caret"/>'], |
|
|
|
|
50
|
|
|
['extend' => 'pageLength', 'text' => '<i class="fa fa-list-ol"></i> '.trans('cortex/foundation::common.limit').' <span class="caret"/>'], |
|
|
|
|
51
|
|
|
], |
52
|
|
|
'drawCallback' => 'function (settings) { |
53
|
|
|
var lastGroup = null; |
54
|
|
|
var api = this.api(); |
55
|
|
|
var colspan = api.columns(\':visible\').count(); |
56
|
|
|
var rows = api.rows({page:\'current\'}).nodes(); |
57
|
|
|
|
58
|
|
|
api.column(\'group:name\', {page:\'current\'} ).data().each(function (rowGroup, rowIndex) { |
59
|
|
|
if (lastGroup !== rowGroup) { |
60
|
|
|
$(rows).eq(rowIndex).before( |
61
|
|
|
\'<tr class="attribute-group"><td colspan="\'+colspan+\'"><strong>\'+(rowGroup ? rowGroup : "No Group")+\'</strong></td></tr>\' |
|
|
|
|
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
lastGroup = rowGroup; |
65
|
|
|
} |
66
|
|
|
}); |
67
|
|
|
}', |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Display ajax response. |
73
|
|
|
* |
74
|
|
|
* @return \Illuminate\Http\JsonResponse |
75
|
|
|
*/ |
76
|
|
|
public function ajax() |
77
|
|
|
{ |
78
|
|
|
$transformer = app($this->transformer); |
79
|
|
|
|
80
|
|
|
return datatables()->eloquent($this->query()) |
|
|
|
|
81
|
|
|
->setTransformer($transformer) |
82
|
|
|
->orderColumn('name', 'name->"$.'.app()->getLocale().'" $1') |
83
|
|
|
->make(true); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get columns. |
88
|
|
|
* |
89
|
|
|
* @return array |
|
|
|
|
90
|
|
|
*/ |
91
|
|
|
protected function getColumns() |
92
|
|
|
{ |
93
|
|
|
$link = config('cortex.foundation.route.locale_prefix') |
94
|
|
|
? '"<a href=\""+routes.route(\'adminarea.attributes.edit\', {attribute: full.slug, locale: \''.$this->request->segment(1).'\'})+"\">"+data+"</a>"' |
|
|
|
|
95
|
|
|
: '"<a href=\""+routes.route(\'adminarea.attributes.edit\', {attribute: full.slug})+"\">"+data+"</a>"'; |
96
|
|
|
|
97
|
|
|
return [ |
98
|
|
|
'name' => ['title' => trans('cortex/attributes::common.name'), 'render' => $link, 'responsivePriority' => 0], |
|
|
|
|
99
|
|
|
'slug' => ['title' => trans('cortex/attributes::common.slug')], |
100
|
|
|
'type' => ['title' => trans('cortex/attributes::common.type')], |
101
|
|
|
'group' => ['title' => trans('cortex/attributes::common.group'), 'visible' => false], |
102
|
|
|
'is_collection' => ['title' => trans('cortex/attributes::common.collection')], |
103
|
|
|
'default' => ['title' => trans('cortex/attributes::common.default')], |
104
|
|
|
'created_at' => ['title' => trans('cortex/attributes::common.created_at'), 'render' => "moment(data).format('MMM Do, YYYY')"], |
|
|
|
|
105
|
|
|
'updated_at' => ['title' => trans('cortex/attributes::common.updated_at'), 'render' => "moment(data).format('MMM Do, YYYY')"], |
|
|
|
|
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.