1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\CBCoreModule; |
4
|
|
|
|
5
|
|
|
use crocodicstudio\crudbooster\CBCoreModule\Index\FilterIndexRows; |
6
|
|
|
use crocodicstudio\crudbooster\CBCoreModule\Index\Order; |
7
|
|
|
use crocodicstudio\crudbooster\CBCoreModule\Index\RowContent; |
8
|
|
|
use crocodicstudio\crudbooster\controllers\CBController; |
9
|
|
|
use crocodicstudio\crudbooster\helpers\CRUDBooster; |
10
|
|
|
use crocodicstudio\crudbooster\helpers\DbInspector; |
11
|
|
|
use Illuminate\Support\Facades\DB; |
12
|
|
|
use Illuminate\Support\Facades\Request; |
13
|
|
|
use Schema; |
14
|
|
|
|
15
|
|
|
class Index |
16
|
|
|
{ |
17
|
|
|
private $cb; |
18
|
|
|
|
19
|
|
|
private $table; |
20
|
|
|
|
21
|
|
|
public function index(CBController $CbCtrl) |
22
|
|
|
{ |
23
|
|
|
$this->cb = $CbCtrl; |
24
|
|
|
$table = $this->table = $CbCtrl->table; |
25
|
|
|
|
26
|
|
|
$data = []; |
27
|
|
|
if (request('parent_table')) { |
28
|
|
|
$data = $this->_handleParentTable(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$data['table'] = $CbCtrl->table; |
32
|
|
|
$data['table_pk'] = DbInspector::findPk($table); |
33
|
|
|
$data['page_title'] = CRUDBooster::getCurrentModule()->name; |
34
|
|
|
$data['page_description'] = cbTrans('default_module_description'); |
35
|
|
|
//$data['date_candidate'] = $CbCtrl->date_candidate; |
36
|
|
|
$data['limit'] = $limit = request('limit', $CbCtrl->limit); |
37
|
|
|
|
38
|
|
|
$query = $CbCtrl->table()->select(DB::raw($table.".".$CbCtrl->primaryKey)); |
39
|
|
|
|
40
|
|
|
$this->_filterForParent($query); |
41
|
|
|
|
42
|
|
|
$CbCtrl->hookQueryIndex($query); |
43
|
|
|
|
44
|
|
|
$this->_filterOutSoftDeleted($table, $query); |
45
|
|
|
|
46
|
|
|
$columns = $CbCtrl->columns_table; |
47
|
|
|
foreach ($columns as $index => $col) { |
48
|
|
|
$field = array_get($col, 'name'); |
49
|
|
|
|
50
|
|
|
if (strpos($field, '.')) { |
51
|
|
|
$columns[$index] = array_merge($columns[$index], $this->addDotField($field, $query)); |
52
|
|
|
} else { |
53
|
|
|
$columns[$index] = array_merge($columns[$index], $this->_addField($field, $query, $table)); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->_applyWhereAndQfilters($query, $columns, $table); |
58
|
|
|
|
59
|
|
|
$filter_is_orderby = false; |
60
|
|
|
if (request('filter_column')) { |
61
|
|
|
$filter_is_orderby = app(FilterIndexRows::class)->filterIndexRows($query, request('filter_column')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($filter_is_orderby === true) { |
65
|
|
|
(new Order($this->cb))->handle($query, $table); |
66
|
|
|
} |
67
|
|
|
$limit = is_string($limit) ? (int) $limit : 15; |
68
|
|
|
$data['result'] = $query->paginate($limit); |
69
|
|
|
|
70
|
|
|
$data['columns'] = $columns; |
71
|
|
|
|
72
|
|
|
if ($CbCtrl->indexReturn) { |
73
|
|
|
return $data; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
//$mainpath = CRUDBooster::mainpath(); |
77
|
|
|
//$orig_mainpath = $CbCtrl->data['mainpath']; |
78
|
|
|
//$titleField = $CbCtrl->titleField; |
79
|
|
|
$number = (request('page', 1) - 1) * $limit + 1; |
80
|
|
|
$columnsTable = array_filter($columns, function ($col) { |
81
|
|
|
return $col['visible'] !== false; |
82
|
|
|
}); |
83
|
|
|
$htmlContents = (new RowContent($CbCtrl))->calculate($data, $number, $columnsTable); //end foreach data[result] |
84
|
|
|
|
85
|
|
|
$data['html_contents'] = ['html' => $htmlContents, 'data' => $data['result']]; |
86
|
|
|
|
87
|
|
|
return $data; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
private function _handleParentTable() |
94
|
|
|
{ |
95
|
|
|
$data = []; |
96
|
|
|
$parent = (string) request('parent_table'); |
97
|
|
|
$data['parent_table'] = CRUDBooster::first(request('parent_table'), request('parent_id')); |
98
|
|
|
if (request('foreign_key')) { |
99
|
|
|
$data['parent_field'] = request('foreign_key'); |
100
|
|
|
} else { |
101
|
|
|
$data['parent_field'] = DbInspector::getRelatedTableName($parent); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (! $data['parent_field']) { |
105
|
|
|
return $data; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
foreach ($this->cb->columns_table as $i => $col) { |
109
|
|
|
if ($col['name'] == $data['parent_field']) { |
110
|
|
|
unset($this->cb->columns_table[$i]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $data; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $result |
119
|
|
|
* @return null |
120
|
|
|
*/ |
121
|
|
|
private function _filterForParent($result) |
122
|
|
|
{ |
123
|
|
|
if (! request('parent_id')) { |
124
|
|
|
return null; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$tableParent = CRUDBooster::parseSqlTable($this->table)['table']; |
128
|
|
|
$result->where($tableParent.'.'.request('foreign_key'), request('parent_id')); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param $table |
133
|
|
|
* @param $query |
134
|
|
|
*/ |
135
|
|
|
private function _filterOutSoftDeleted($table, $query) |
136
|
|
|
{ |
137
|
|
|
if (\Schema::hasColumn($table, 'deleted_at')) { |
138
|
|
|
$query->where($table.'.deleted_at', '=', null); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param $result |
144
|
|
|
* @param $field |
145
|
|
|
* @return mixed |
146
|
|
|
*/ |
147
|
|
|
private function addDotField($field, $result) |
148
|
|
|
{ |
149
|
|
|
$result->addselect($field.' as '.str_slug($field, '_')); |
150
|
|
|
$tableField = substr($field, 0, strpos($field, '.')); |
151
|
|
|
$fieldOrign = substr($field, strpos($field, '.') + 1); |
152
|
|
|
|
153
|
|
|
return [ |
154
|
|
|
'type_data' => \Schema::getColumnType($tableField, $fieldOrign), |
155
|
|
|
'field' => str_slug($field, '_'), |
156
|
|
|
'field_raw' => $field, |
157
|
|
|
'field_with' => $tableField.'.'.$fieldOrign, |
158
|
|
|
]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param $field |
163
|
|
|
* @param $table |
164
|
|
|
* @param $result |
165
|
|
|
* @return mixed |
166
|
|
|
*/ |
167
|
|
|
private function _addField($field, $result, $table) |
168
|
|
|
{ |
169
|
|
|
$t = [ |
170
|
|
|
'type_data' => 'varchar', |
171
|
|
|
'field' => $field, |
172
|
|
|
'field_raw' => $field, |
173
|
|
|
'field_with' => null, |
174
|
|
|
]; |
175
|
|
|
|
176
|
|
|
if (\Schema::hasColumn($table, $field)) { |
177
|
|
|
$result->addselect($table.'.'.$field); |
178
|
|
|
$t['type_data'] = \Schema::getColumnType($table, $field); |
179
|
|
|
$t['field_with'] = $table.'.'.$field; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $t; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param $result |
187
|
|
|
* @param $columnsTable |
188
|
|
|
* @param $table |
189
|
|
|
* @return mixed |
190
|
|
|
*/ |
191
|
|
|
private function _applyWhereAndQfilters($result, $columnsTable, $table) |
192
|
|
|
{ |
193
|
|
|
if (request('q')) { |
194
|
|
|
$result->where(function ($query) use ($columnsTable) { |
195
|
|
|
foreach ($columnsTable as $col) { |
196
|
|
|
if (! $col['field_with']) { |
197
|
|
|
continue; |
198
|
|
|
} |
199
|
|
|
$query->orwhere($col['field_with'], "like", "%".request("q")."%"); |
|
|
|
|
200
|
|
|
} |
201
|
|
|
}); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
if (request('where')) { |
205
|
|
|
foreach (request('where') as $k => $v) { |
206
|
|
|
$result->where($table.'.'.$k, $v); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
} |