|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use crocodicstudio\crudbooster\CBCoreModule\Hooks; |
|
6
|
|
|
use crocodicstudio\crudbooster\CBCoreModule\Index; |
|
7
|
|
|
use crocodicstudio\crudbooster\controllers\CBController\CbFormLoader; |
|
8
|
|
|
use crocodicstudio\crudbooster\controllers\CBController\CbIndexLoader; |
|
9
|
|
|
use crocodicstudio\crudbooster\controllers\CBController\CbLayoutLoader; |
|
10
|
|
|
use crocodicstudio\crudbooster\controllers\CBController\Deleter; |
|
11
|
|
|
use crocodicstudio\crudbooster\controllers\CBController\ExportData; |
|
12
|
|
|
use crocodicstudio\crudbooster\controllers\CBController\FormSubmitHandlers; |
|
13
|
|
|
use crocodicstudio\crudbooster\controllers\CBController\ImportData; |
|
14
|
|
|
use crocodicstudio\crudbooster\controllers\CBController\IndexAjax; |
|
15
|
|
|
use crocodicstudio\crudbooster\helpers\DbInspector; |
|
16
|
|
|
use Illuminate\Support\Facades\DB; |
|
17
|
|
|
use crocodicstudio\crudbooster\helpers\CRUDBooster; |
|
18
|
|
|
use Schema; |
|
19
|
|
|
|
|
20
|
|
|
abstract class CBController extends Controller |
|
21
|
|
|
{ |
|
22
|
|
|
abstract public function cbInit(); |
|
23
|
|
|
|
|
24
|
|
|
use Hooks; |
|
25
|
|
|
use Deleter, CbFormLoader, CbIndexLoader, CbLayoutLoader, IndexAjax, ImportData, ExportData, FormSubmitHandlers; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
public $table = ''; |
|
28
|
|
|
|
|
29
|
|
|
public $titleField = ''; |
|
30
|
|
|
|
|
31
|
|
|
public $primaryKey = 'id'; |
|
32
|
|
|
|
|
33
|
|
|
public $form = []; |
|
34
|
|
|
|
|
35
|
|
|
public $data = []; |
|
36
|
|
|
|
|
37
|
|
|
public $parent_field = null; |
|
38
|
|
|
|
|
39
|
|
|
public function cbView($template, $data) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->cbLayoutLoader(); |
|
42
|
|
|
view()->share($this->data); |
|
43
|
|
|
return view($template, $data); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function cbLoader() |
|
47
|
|
|
{ |
|
48
|
|
|
$this->genericLoader(); |
|
49
|
|
|
$this->cbLayoutLoader(); |
|
50
|
|
|
$this->cbFormLoader(); |
|
51
|
|
|
$this->cbIndexLoader(); |
|
52
|
|
|
$this->checkHideForm(); |
|
53
|
|
|
|
|
54
|
|
|
view()->share($this->data); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getIndex() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->genericLoader(); |
|
60
|
|
|
$this->cbIndexLoader(); |
|
61
|
|
|
$data = app(Index::class)->index($this); |
|
62
|
|
|
|
|
63
|
|
|
return $this->cbView('crudbooster::index.index', $data); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getUpdateSingle() |
|
67
|
|
|
{ |
|
68
|
|
|
$table = request('table'); |
|
69
|
|
|
$column = request('column'); |
|
70
|
|
|
$value = request('value'); |
|
71
|
|
|
$id = request('id'); |
|
72
|
|
|
DB::table($table)->where(DbInspector::findPk($table), $id)->update([$column => $value]); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
backWithMsg(cbTrans('alert_update_data_success')); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getAdd() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->genericLoader(); |
|
80
|
|
|
$page_title = cbTrans('add_data_page_title', ['module' => CRUDBooster::getCurrentModule()->name]); |
|
81
|
|
|
$command = 'add'; |
|
82
|
|
|
return $this->cbForm(compact('page_title', 'command')); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $tableName |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
|
|
public function table($tableName = null) |
|
90
|
|
|
{ |
|
91
|
|
|
$tableName = $tableName ?: $this->table; |
|
92
|
|
|
return \DB::table($tableName); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getEdit($id) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->genericLoader(); |
|
98
|
|
|
$row = $this->findFirst($id); |
|
99
|
|
|
|
|
100
|
|
|
$page_title = cbTrans("edit_data_page_title", ['module' => CRUDBooster::getCurrentModule()->name, 'name' => $row->{$this->titleField}]); |
|
101
|
|
|
$command = 'edit'; |
|
102
|
|
|
session()->put('current_row_id', $id); |
|
103
|
|
|
|
|
104
|
|
|
return $this->cbForm(compact('id', 'row', 'page_title', 'command')); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param $id |
|
109
|
|
|
* @return mixed |
|
110
|
|
|
*/ |
|
111
|
|
|
public function findRow($id) |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->table()->where($this->primaryKey, $id); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function getDetail($id) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->genericLoader(); |
|
119
|
|
|
$this->cbFormLoader(); |
|
120
|
|
|
$row = $this->findFirst($id); |
|
121
|
|
|
|
|
122
|
|
|
$page_title = cbTrans('detail_data_page_title', ['module' => CRUDBooster::getCurrentModule()->name, 'name' => $row->{$this->titleField}]); |
|
123
|
|
|
|
|
124
|
|
|
session()->put('current_row_id', $id); |
|
125
|
|
|
return $this->cbView('crudbooster::form.details', compact('row', 'page_title', 'id')); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function actionButtonSelected($id_selected, $button_name) |
|
129
|
|
|
{ |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param $data |
|
134
|
|
|
* @return mixed |
|
135
|
|
|
*/ |
|
136
|
|
|
private function cbForm($data) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->cbFormLoader(); |
|
139
|
|
|
return $this->cbView('crudbooster::form.form', $data); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
protected function genericLoader() |
|
143
|
|
|
{ |
|
144
|
|
|
$this->cbInit(); |
|
145
|
|
|
$this->primaryKey = $this->primaryKey?: DbInspector::findPk($this->table); |
|
146
|
|
|
$this->data_inputan = $this->form; |
|
147
|
|
|
$this->data['pk'] = $this->primaryKey; |
|
148
|
|
|
$this->data['hide_form'] = $this->hide_form; |
|
149
|
|
|
$this->data['table'] = $this->table; |
|
150
|
|
|
$this->data['titleField'] = $this->titleField; |
|
151
|
|
|
$this->data['appname'] = cbGetsetting('appname'); |
|
152
|
|
|
$this->data['indexButton'] = $this->indexButton; |
|
153
|
|
|
|
|
154
|
|
|
$this->data['sub_module'] = $this->sub_module; |
|
155
|
|
|
$this->data['parent_field'] = (request('parent_field')) ?: $this->parent_field; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param $id |
|
160
|
|
|
* @return mixed |
|
161
|
|
|
*/ |
|
162
|
|
|
private function findFirst($id) |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->findRow($id)->first(); |
|
165
|
|
|
} |
|
166
|
|
|
} |