1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\DataTables\Services; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Yajra\DataTables\Services\DataTable as BaseDataTable; |
7
|
|
|
|
8
|
|
|
abstract class DataTable extends BaseDataTable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Get attributes for a "static" column that can not be |
12
|
|
|
* ordered, searched, or exported. |
13
|
|
|
* |
14
|
|
|
* @param string $name |
15
|
|
|
* @param array $attributes |
16
|
|
|
* @return array |
17
|
|
|
*/ |
18
|
|
|
protected function staticColumn($name, array $attributes = []) |
19
|
|
|
{ |
20
|
|
|
return array_merge([ |
21
|
|
|
'data' => $name, |
22
|
|
|
'name' => $name, |
23
|
|
|
'title' => $this->getQualifiedTitle($name), |
24
|
|
|
'defaultContent' => '', |
25
|
|
|
'render' => null, |
26
|
|
|
'orderable' => false, |
27
|
|
|
'searchable' => false, |
28
|
|
|
'exportable' => false, |
29
|
|
|
'printable' => true, |
30
|
|
|
'footer' => '', |
31
|
|
|
], $attributes); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Convert string into a readable title. |
36
|
|
|
* |
37
|
|
|
* @param string $title |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
protected function getQualifiedTitle($title) |
41
|
|
|
{ |
42
|
|
|
return Str::title(str_replace(['.', '_'], ' ', Str::snake($title))); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get render Closure for an <a> link. |
47
|
|
|
* |
48
|
|
|
* @param string $url "/uri/to/{data}/{full.otherData}" |
49
|
|
|
* @param string $content "{data} content" |
50
|
|
|
* @param string $class |
51
|
|
|
* @return Closure |
|
|
|
|
52
|
|
|
*/ |
53
|
|
|
protected function linkRender($url = '{data}', $content = '{data}', $class = '') |
54
|
|
|
{ |
55
|
|
|
$url = $this->getRenderedJsString($url); |
56
|
|
|
$content = $this->getRenderedJsString($content); |
57
|
|
|
$class = $class ? ' class="'.$class.'"' : ''; |
58
|
|
|
|
59
|
|
|
return function () use ($url, $content, $class) { |
60
|
|
|
return <<<JS |
61
|
|
|
function (data, type, full, meta) { |
62
|
|
|
if (type === 'display' && data) { |
63
|
|
|
return '<a href=\"'+ {$url} +'\"{$class}>'+ {$content} +'</a>'; |
64
|
|
|
} |
65
|
|
|
return data; |
66
|
|
|
} |
67
|
|
|
JS; |
68
|
|
|
}; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Convert PHP string value to a rendered JavaScript string. |
73
|
|
|
* |
74
|
|
|
* @param string $string |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
protected function getRenderedJsString($string) |
78
|
|
|
{ |
79
|
|
|
return "'".str_replace(['{', '}'], ["'+", "+'"], $string)."'"; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|