|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Crocodicstudio\Crudbooster\Controllers\CBController; |
|
4
|
|
|
|
|
5
|
|
|
use Crocodicstudio\Crudbooster\CBCoreModule\Search; |
|
6
|
|
|
use Illuminate\Support\Facades\Cache; |
|
7
|
|
|
use Illuminate\Support\Facades\DB; |
|
8
|
|
|
|
|
9
|
|
|
trait IndexAjax |
|
10
|
|
|
{ |
|
11
|
|
|
public function getDataTable() |
|
12
|
|
|
{ |
|
13
|
|
|
$table = request('table'); |
|
14
|
|
|
$label = request('label'); |
|
15
|
|
|
$datatableWhere = urldecode(request('datatable_where')); |
|
|
|
|
|
|
16
|
|
|
$foreignKeyName = request('fk_name'); |
|
17
|
|
|
$foreignKeyValue = request('fk_value'); |
|
18
|
|
|
if (! $table || ! $label || ! $foreignKeyName || ! $foreignKeyValue) { |
|
19
|
|
|
return response()->json([]); |
|
|
|
|
|
|
20
|
|
|
} |
|
21
|
|
|
$query = DB::table($table); |
|
|
|
|
|
|
22
|
|
|
if ($datatableWhere) { |
|
23
|
|
|
$query->whereRaw($datatableWhere); |
|
24
|
|
|
} |
|
25
|
|
|
$query->select('id as select_value', $label.' as select_label'); |
|
|
|
|
|
|
26
|
|
|
$query->where($foreignKeyName, $foreignKeyValue); |
|
27
|
|
|
$query->orderby($label, 'asc'); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
return response()->json($query->get()); |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getDataModalDatatable() |
|
33
|
|
|
{ |
|
34
|
|
|
$data = base64_decode(json_decode(request('data'), true)); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
$columns = explode(',', $data['columns']); |
|
37
|
|
|
|
|
38
|
|
|
$result = DB::table($data['table']); |
|
39
|
|
|
$this->applyQ($result, $columns); |
|
40
|
|
|
|
|
41
|
|
|
if ($data['sql_where']) { |
|
42
|
|
|
$result->whereraw($data['sql_where']); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$this->applyOrderBy($data, $result); |
|
46
|
|
|
$limit = ($data['limit']) ?: 6; |
|
47
|
|
|
|
|
48
|
|
|
return view('crudbooster::form.type_components.datamodal.browser', ['result' => $result->paginate($limit), 'data' => $data]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getDataQuery() |
|
52
|
|
|
{ |
|
53
|
|
|
$key = request('query'); |
|
54
|
|
|
if (! Cache::has($key)) { |
|
|
|
|
|
|
55
|
|
|
return response()->json(['items' => []]); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
$query = Cache::get($key); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
$fk_name = request('fk_name'); |
|
60
|
|
|
$fk_value = request('fk_value'); |
|
61
|
|
|
|
|
62
|
|
|
$condition = ' where '; |
|
63
|
|
|
if (strpos(strtolower($query), 'where') !== false) { |
|
64
|
|
|
$condition = ' and '; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (strpos(strtolower($query), 'order by')) { |
|
68
|
|
|
$query = str_replace('ORDER BY', 'order by', $query); |
|
69
|
|
|
$qraw = explode('order by', $query); |
|
70
|
|
|
$query = $qraw[0].$condition.$fk_name." = '$fk_value' $qraw[1]"; |
|
|
|
|
|
|
71
|
|
|
} else { |
|
72
|
|
|
$query .= $condition.$fk_name." = '$fk_value'"; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$query = DB::select(DB::raw($query)); |
|
76
|
|
|
|
|
77
|
|
|
return response()->json(['items' => $query]); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function postFindData() |
|
81
|
|
|
{ |
|
82
|
|
|
$items = app(Search::class)->searchData(request('data'), request('q'), request('id')); |
|
83
|
|
|
|
|
84
|
|
|
return response()->json(['items' => $items]); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param $result |
|
89
|
|
|
* @param $columns |
|
90
|
|
|
*/ |
|
91
|
|
|
private function applyQ($result, $columns) |
|
92
|
|
|
{ |
|
93
|
|
|
if (request('q')) { |
|
94
|
|
|
return; |
|
95
|
|
|
} |
|
96
|
|
|
$result->where(function ($where) use ($columns) { |
|
97
|
|
|
foreach ($columns as $c => $col) { |
|
98
|
|
|
if ($c == 0) { |
|
99
|
|
|
$where->where($col, 'like', '%'.request('q').'%'); |
|
|
|
|
|
|
100
|
|
|
} else { |
|
101
|
|
|
$where->orWhere($col, 'like', '%'.request('q').'%'); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
}); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param $data |
|
109
|
|
|
* @param $result |
|
110
|
|
|
*/ |
|
111
|
|
|
private function applyOrderBy($data, $result) |
|
112
|
|
|
{ |
|
113
|
|
|
if ($data['sql_orderby']) { |
|
114
|
|
|
$result->orderByRaw($data['sql_orderby']); |
|
115
|
|
|
} else { |
|
116
|
|
|
$result->orderBy($data['column_value'], 'desc'); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |