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_table = $CbCtrl->columns_table; |
47
|
|
|
foreach ($columns_table as $index => $coltab) { |
48
|
|
|
$field = @$coltab['name']; |
49
|
|
|
|
50
|
|
|
if (strpos($field, '.')) { |
51
|
|
|
$columns_table[$index] = array_merge($columns_table[$index], $this->addDotField($field, $query)); |
52
|
|
|
} else { |
53
|
|
|
$columns_table[$index] = array_merge($columns_table[$index], $this->_addField($field, $query, $table)); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->_applyWhereAndQfilters($query, $columns_table, $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_table; |
71
|
|
|
|
72
|
|
|
if ($CbCtrl->indexReturn) { |
73
|
|
|
return $data; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
//LISTING INDEX HTML |
77
|
|
|
$addAction = $CbCtrl->data['addAction']; |
78
|
|
|
|
79
|
|
|
if (! empty($CbCtrl->sub_module)) { |
80
|
|
|
$addAction = $this->_handleSubModules($addAction); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
//$mainpath = CRUDBooster::mainpath(); |
84
|
|
|
//$orig_mainpath = $CbCtrl->data['mainpath']; |
85
|
|
|
//$titleField = $CbCtrl->titleField; |
86
|
|
|
$number = (request('page', 1) - 1) * $limit + 1; |
87
|
|
|
$columnsTable = array_filter($columns_table, function ($col) { |
88
|
|
|
return $col['visible'] !== false; |
89
|
|
|
}); |
90
|
|
|
$htmlContents = (new RowContent($CbCtrl))->calculate($data, $number, $columnsTable, $addAction); //end foreach data[result] |
91
|
|
|
|
92
|
|
|
$data['html_contents'] = ['html' => $htmlContents, 'data' => $data['result']]; |
93
|
|
|
|
94
|
|
|
return $data; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
private function _handleParentTable() |
101
|
|
|
{ |
102
|
|
|
$data = []; |
103
|
|
|
$parent = (string) request('parent_table'); |
104
|
|
|
$data['parent_table'] = CRUDBooster::first(request('parent_table'), request('parent_id')); |
105
|
|
|
if (request('foreign_key')) { |
106
|
|
|
$data['parent_field'] = request('foreign_key'); |
107
|
|
|
} else { |
108
|
|
|
$data['parent_field'] = DbInspector::getRelatedTableName($parent); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (! $data['parent_field']) { |
112
|
|
|
return $data; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
foreach ($this->cb->columns_table as $i => $col) { |
116
|
|
|
if ($col['name'] == $data['parent_field']) { |
117
|
|
|
unset($this->cb->columns_table[$i]); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $data; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param $result |
126
|
|
|
* @return null |
127
|
|
|
*/ |
128
|
|
|
private function _filterForParent($result) |
129
|
|
|
{ |
130
|
|
|
if (! request('parent_id')) { |
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$tableParent = CRUDBooster::parseSqlTable($this->table)['table']; |
135
|
|
|
$result->where($tableParent.'.'.request('foreign_key'), request('parent_id')); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $table |
140
|
|
|
* @param $query |
141
|
|
|
*/ |
142
|
|
|
private function _filterOutSoftDeleted($table, $query) |
143
|
|
|
{ |
144
|
|
|
if (\Schema::hasColumn($table, 'deleted_at')) { |
145
|
|
|
$query->where($table.'.deleted_at', '=', null); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param $result |
151
|
|
|
* @param $field |
152
|
|
|
* @return mixed |
153
|
|
|
*/ |
154
|
|
|
private function addDotField($field, $result) |
155
|
|
|
{ |
156
|
|
|
$result->addselect($field.' as '.str_slug($field, '_')); |
157
|
|
|
$tableField = substr($field, 0, strpos($field, '.')); |
158
|
|
|
$fieldOrign = substr($field, strpos($field, '.') + 1); |
159
|
|
|
|
160
|
|
|
return [ |
161
|
|
|
'type_data' => \Schema::getColumnType($tableField, $fieldOrign), |
162
|
|
|
'field' => str_slug($field, '_'), |
163
|
|
|
'field_raw' => $field, |
164
|
|
|
'field_with' => $tableField.'.'.$fieldOrign, |
165
|
|
|
]; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param $field |
170
|
|
|
* @param $table |
171
|
|
|
* @param $result |
172
|
|
|
* @return mixed |
173
|
|
|
*/ |
174
|
|
|
private function _addField($field, $result, $table) |
175
|
|
|
{ |
176
|
|
|
$t = [ |
177
|
|
|
'type_data' => 'varchar', |
178
|
|
|
'field_with' => null, |
179
|
|
|
'field' => $field, |
180
|
|
|
'field_raw' => $field, |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
if (\Schema::hasColumn($table, $field)) { |
184
|
|
|
$result->addselect($table.'.'.$field); |
185
|
|
|
$t['type_data'] = \Schema::getColumnType($table, $field); |
186
|
|
|
$t['field_with'] = $table.'.'.$field; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $t; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param $result |
194
|
|
|
* @param $columnsTable |
195
|
|
|
* @param $table |
196
|
|
|
* @return mixed |
197
|
|
|
*/ |
198
|
|
|
private function _applyWhereAndQfilters($result, $columnsTable, $table) |
199
|
|
|
{ |
200
|
|
|
if (request('q')) { |
201
|
|
|
$result->where(function ($query) use ($columnsTable) { |
202
|
|
|
foreach ($columnsTable as $col) { |
203
|
|
|
if (! $col['field_with']) { |
204
|
|
|
continue; |
205
|
|
|
} |
206
|
|
|
$query->orwhere($col['field_with'], "like", "%".request("q")."%"); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
}); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if (request('where')) { |
212
|
|
|
foreach (request('where') as $k => $v) { |
213
|
|
|
$result->where($table.'.'.$k, $v); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param $addAction |
220
|
|
|
* @return array |
221
|
|
|
*/ |
222
|
|
|
private function _handleSubModules($addAction) |
223
|
|
|
{ |
224
|
|
|
foreach ($this->cb->sub_module as $module) { |
225
|
|
|
$addAction[] = [ |
226
|
|
|
'label' => $module['label'], |
227
|
|
|
'icon' => $module['button_icon'], |
228
|
|
|
'url' => $this->subModuleUrl($module, CRUDBooster::parseSqlTable($this->table)['table']), |
229
|
|
|
'color' => $module['button_color'], |
230
|
|
|
'showIf' => $module['showIf'], |
231
|
|
|
]; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return $addAction; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param $module |
239
|
|
|
* @param $table_parent |
240
|
|
|
* @return string |
241
|
|
|
*/ |
242
|
|
|
private function subModuleUrl($module, $table_parent) |
243
|
|
|
{ |
244
|
|
|
return CRUDBooster::adminPath($module['path']).'?parent_table='.$table_parent.'&parent_columns=' |
245
|
|
|
.$module['parent_columns'].'&parent_columns_alias=' |
246
|
|
|
.$module['parent_columns_alias'].'&parent_id=[' |
247
|
|
|
.(! isset($module['custom_parent_id']) ? "id" : $module['custom_parent_id']) |
248
|
|
|
.']&return_url='.urlencode(Request::fullUrl()).'&foreign_key=' |
249
|
|
|
.$module['foreign_key'].'&label='.urlencode($module['label']); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
} |