1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers\CrudFeatures; |
4
|
|
|
|
5
|
|
|
trait AjaxTable |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Respond with the JSON of one or more rows, depending on the POST parameters. |
9
|
|
|
* @return JSON Array of cells in HTML form. |
10
|
|
|
*/ |
11
|
|
|
private $input; |
12
|
|
|
private $totalRows = 0; |
13
|
|
|
private $filteredRows = 0; |
14
|
|
|
private $versionTransformer; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The search function. This function it's called by the data table. |
18
|
|
|
* |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
|
|
public function search() |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$this->crud->hasAccessOrFail('list'); |
|
|
|
|
24
|
|
|
|
25
|
|
|
$this->input = $_REQUEST; |
26
|
|
|
$this->totalRows = $this->filteredRows = $this->crud->getEntries()->count(); |
27
|
|
|
|
28
|
|
|
$data = $this->crud->getEntriesWithConditions( |
29
|
|
|
$this->input['length'], |
30
|
|
|
$this->input['start'], |
31
|
|
|
$this->addAjaxOrderBy()[0], |
32
|
|
|
$this->addAjaxOrderBy()[1], |
33
|
|
|
$this->addSearchConditions() |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
if ($this->addSearchConditions() !== null) { |
37
|
|
|
$this->filteredRows = $data->count(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $this->make($data); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Formats the row of the table from the entry(instance of a model). |
45
|
|
|
* |
46
|
|
|
* @param $entry |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
private function format($entry) |
50
|
|
|
{ |
51
|
|
|
$row_items = $this->crud->getRowViews($entry, $this->crud); |
52
|
|
|
|
53
|
|
|
// add the buttons as the last column |
54
|
|
|
if ($this->crud->buttons->where('stack', 'line')->count()) { |
55
|
|
|
$row_items[] = \View::make('crud::inc.button_stack', ['stack' => 'line']) |
56
|
|
|
->with('crud', $this->crud) |
57
|
|
|
->with('entry', $entry) |
58
|
|
|
->render(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// add the details_row buttons as the first column |
62
|
|
|
if ($this->crud->details_row) { |
63
|
|
|
array_unshift($row_items, \View::make('crud::columns.details_row_button') |
64
|
|
|
->with('crud', $this->crud) |
65
|
|
|
->with('entry', $entry) |
66
|
|
|
->render()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $row_items; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Created the array to be fed to the data table. |
74
|
|
|
* @param $data |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
private function make($data) |
78
|
|
|
{ |
79
|
|
|
$rows = []; |
80
|
|
|
foreach ($data as $row) { |
81
|
|
|
$rows[] = $this->format($row); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return [ |
85
|
|
|
'draw' => (isset($this->input['draw']) ? (int) $this->input['draw'] : 0), |
86
|
|
|
'recordsTotal' => $this->totalRows, |
87
|
|
|
'recordsFiltered' => $this->filteredRows, |
88
|
|
|
'data' => $rows, |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Checks of teh user has anything written in the search bar and if so a string with the filtered is returned. |
94
|
|
|
* In case no filter is detected null is returned. |
95
|
|
|
* |
96
|
|
|
* @return null | string $filter |
97
|
|
|
*/ |
98
|
|
|
private function addSearchConditions() |
99
|
|
|
{ |
100
|
|
|
if (isset($this->input['search']) && isset($this->input['search']['value'])) { |
101
|
|
|
$filter = $this->input['search']['value']; |
102
|
|
|
if ($filter !== '') { |
103
|
|
|
return $filter; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Checks if the user tried to order a column and if so an array with the |
110
|
|
|
* column to be order and the direction are returned. |
111
|
|
|
* |
112
|
|
|
* @return array [column, direction] |
113
|
|
|
*/ |
114
|
|
|
public function addAjaxOrderBy() |
115
|
|
|
{ |
116
|
|
|
if (isset($this->input['order']) && isset($this->input['order'][0])) { |
117
|
|
|
$orderBy = $this->input['order'][0]['column']; |
118
|
|
|
if (isset($this->crud->columns[(int) $orderBy]['name'])) { |
119
|
|
|
return [$this->crud->columns[(int) $orderBy]['name'], $this->input['order'][0]['dir']]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return [null, null]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: