1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
|
10
|
|
|
class DataTables |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* DataTables request object. |
14
|
|
|
* |
15
|
|
|
* @var \Yajra\DataTables\Utilities\Request |
16
|
|
|
*/ |
17
|
|
|
protected $request; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* HTML builder instance. |
21
|
|
|
* |
22
|
|
|
* @var \Yajra\DataTables\Html\Builder |
23
|
|
|
*/ |
24
|
|
|
protected $html; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Make a DataTable instance from source. |
28
|
|
|
* Alias of make for backward compatibility. |
29
|
|
|
* |
30
|
|
|
* @param mixed $source |
31
|
|
|
* @return mixed |
32
|
|
|
* @throws \Exception |
33
|
|
|
*/ |
34
|
|
|
public function of($source) |
35
|
|
|
{ |
36
|
|
|
return $this->make($source); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Make a DataTable instance from source. |
41
|
|
|
* |
42
|
|
|
* @param mixed $source |
43
|
|
|
* @return mixed |
44
|
|
|
* @throws \Exception |
45
|
|
|
*/ |
46
|
|
|
public function make($source) |
47
|
|
|
{ |
48
|
|
|
if (is_array($source)) { |
49
|
|
|
$source = new Collection($source); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ($engine = $this->getEngineForSource($source)) { |
53
|
|
|
return $this->createDataTable($engine, $source); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
throw new Exception('No available engine for ' . get_class($source)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the optimum engine for the given data source. |
61
|
|
|
* |
62
|
|
|
* @param mixed $source |
63
|
|
|
* @return string|null |
64
|
|
|
*/ |
65
|
|
|
protected function getEngineForSource($source) |
66
|
|
|
{ |
67
|
|
|
$result = null; |
68
|
|
|
|
69
|
|
|
foreach (config('datatables.builders') as $type => $engine) { |
70
|
|
|
if ($source instanceof $type) { |
71
|
|
|
if (! isset($tmpType) || is_subclass_of($type, $tmpType)) { |
|
|
|
|
72
|
|
|
$tmpType = $type; |
73
|
|
|
$result = $engine; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $result; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create a new DataTable instance. |
83
|
|
|
* |
84
|
|
|
* @param string $engine |
85
|
|
|
* @param mixed $source |
86
|
|
|
* @return mixed |
87
|
|
|
* @throws \Exception |
88
|
|
|
*/ |
89
|
|
|
protected function createDataTable($engine, $source) |
90
|
|
|
{ |
91
|
|
|
if ($class = class_exists($engine) ? $engine : Arr::get(config('datatables.engines'), $engine)) { |
92
|
|
|
return new $class($source); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
throw new Exception("Unsupported DataTable engine [$engine]"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get request object. |
100
|
|
|
* |
101
|
|
|
* @return \Yajra\DataTables\Utilities\Request |
102
|
|
|
*/ |
103
|
|
|
public function getRequest() |
104
|
|
|
{ |
105
|
|
|
return app('datatables.request'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get config instance. |
110
|
|
|
* |
111
|
|
|
* @return \Yajra\DataTables\Utilities\Config |
112
|
|
|
*/ |
113
|
|
|
public function getConfig() |
114
|
|
|
{ |
115
|
|
|
return app('datatables.config'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @deprecated Please use query() instead, this method will be removed in a next version. |
120
|
|
|
* @param $builder |
121
|
|
|
* @return QueryDataTable |
122
|
|
|
*/ |
123
|
|
|
public function queryBuilder($builder) |
124
|
|
|
{ |
125
|
|
|
return $this->query($builder); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get html builder instance. |
130
|
|
|
* |
131
|
|
|
* @return \Yajra\DataTables\Html\Builder |
132
|
|
|
* @throws \Exception |
133
|
|
|
*/ |
134
|
|
|
public function getHtmlBuilder() |
135
|
|
|
{ |
136
|
|
|
if (! class_exists('\Yajra\DataTables\Html\Builder')) { |
137
|
|
|
throw new \Exception('Please install yajra/laravel-datatables-html to be able to use this function.'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this->html ?: $this->html = app('datatables.html'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Make a DataTable instance by using method name as engine. |
145
|
|
|
* |
146
|
|
|
* @param string $method |
147
|
|
|
* @param array $parameters |
148
|
|
|
* @return mixed |
149
|
|
|
*/ |
150
|
|
|
public function __call($method, $parameters) |
151
|
|
|
{ |
152
|
|
|
return $this->createDataTable(Str::snake($method), ...$parameters); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|