1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: User |
5
|
|
|
* Date: 4/25/2019 |
6
|
|
|
* Time: 9:28 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace crocodicstudio\crudbooster\controllers; |
10
|
|
|
|
11
|
|
|
use crocodicstudio\crudbooster\exceptions\CBValidationException; |
12
|
|
|
use crocodicstudio\crudbooster\helpers\ModuleGenerator; |
13
|
|
|
use Illuminate\Support\Facades\Artisan; |
14
|
|
|
use Illuminate\Support\Facades\DB; |
15
|
|
|
use Illuminate\Support\Facades\Schema; |
16
|
|
|
use Illuminate\Support\Str; |
17
|
|
|
use Illuminate\Support\Facades\Cache; |
18
|
|
|
use Symfony\Component\Process\Process; |
19
|
|
|
|
20
|
|
|
class DeveloperModulesController extends Controller |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
private $view = "crudbooster::dev_layouts.modules.modules"; |
24
|
|
|
|
25
|
|
|
public function __construct() |
26
|
|
|
{ |
27
|
|
|
view()->share(['page_title'=>'Module Manager']); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
public function getIndex() { |
32
|
|
|
$data = []; |
33
|
|
|
$data['result'] = DB::table("cb_modules")->get(); |
34
|
|
|
return view($this->view.'.index',$data); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getAdd() { |
38
|
|
|
$data = []; |
39
|
|
|
$data['tables'] = cb()->listAllTable(); |
40
|
|
|
$data['module'] = (request('modules_id'))?cb()->find("cb_modules", request("modules_id")):null; |
41
|
|
|
return view($this->view.'.add', $data); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getTables() |
45
|
|
|
{ |
46
|
|
|
return response()->json(cb()->listAllTable()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getAllColumn($table) { |
50
|
|
|
$data = cb()->listAllColumns($table); |
51
|
|
|
$pk = cb()->findPrimaryKey($table); |
52
|
|
|
$result = []; |
53
|
|
|
foreach($data as $item) { |
54
|
|
|
|
55
|
|
|
if(Str::contains(strtolower($item),["title","name","label"])) { |
56
|
|
|
$display = true; |
57
|
|
|
}else{ |
58
|
|
|
$display = false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$result[] = [ |
62
|
|
|
"column"=>$item, |
63
|
|
|
"primary_key"=>($pk==$item)?true:false, |
64
|
|
|
"display"=>$display |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
return response()->json($result); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getColumns($table) |
71
|
|
|
{ |
72
|
|
|
if(request("modules_id")) { |
73
|
|
|
$module = cb()->find("cb_modules",request("modules_id")); |
74
|
|
|
if($module->last_column_build) { |
75
|
|
|
return response()->json(json_decode($module->last_column_build)); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$columns = cb()->listAllColumns($table); |
80
|
|
|
$pk = cb()->findPrimaryKey($table); |
81
|
|
|
$result = []; |
82
|
|
|
foreach($columns as $column) { |
83
|
|
|
if($column != $pk) { |
84
|
|
|
|
85
|
|
|
// Skip Column |
86
|
|
|
if($column == 'deleted_at') continue; |
87
|
|
|
|
88
|
|
|
// Check if any relation table candidate |
89
|
|
|
$optionTable = ""; |
90
|
|
|
if(Str::substr(strtolower($column),-3,3) == "_id") { |
91
|
|
|
$relationTable = Str::substr($column,0,-3); |
92
|
|
|
if(Schema::hasTable($relationTable)) { |
93
|
|
|
$optionTable = $relationTable; |
94
|
|
|
} |
95
|
|
|
}elseif (Str::substr(strtolower($column),0,3) == "id_") { |
96
|
|
|
$relationTable = Str::substr($column,3); |
97
|
|
|
if(Schema::hasTable($relationTable)) { |
98
|
|
|
$optionTable = $relationTable; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$label = trim(Str::title(str_replace(["id_","_id","_"]," ",$column))); |
103
|
|
|
$label = Str::singular($label); |
104
|
|
|
$type = "text"; |
105
|
|
|
|
106
|
|
|
if(Str::contains(strtolower($label),["photo","image","picture","gambar"])) { |
107
|
|
|
$type = "image"; |
108
|
|
|
}elseif (Str::contains(strtolower($label),["email","mail"])) { |
109
|
|
|
$type = "email"; |
110
|
|
|
}elseif (Str::contains(strtolower($label),["description","content","detail"])) { |
111
|
|
|
$type = "wysiwyg"; |
112
|
|
|
}elseif (Str::contains(strtolower($label),["price","money","grand_total","tax"])) { |
113
|
|
|
$type = "money"; |
114
|
|
|
}elseif (Str::contains(strtolower($label),["quantity","qty","total","phone","telp"])) { |
115
|
|
|
$type = "number"; |
116
|
|
|
}elseif (Str::contains(strtolower($label),["date"])) { |
117
|
|
|
$type = "date"; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (Str::substr(strtolower($column),-3,3) == "_id") { |
121
|
|
|
$type = "select_table"; |
122
|
|
|
} elseif (Str::substr($column, -3, 3) == "_at") { |
123
|
|
|
$type = "datetime"; |
124
|
|
|
} elseif (Str::substr($column,0, 3) == "id_") { |
125
|
|
|
$type = "select_table"; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$columnAdd = "on"; |
129
|
|
|
$columnEdit = "on"; |
130
|
|
|
$columnMandatory = "on"; |
131
|
|
|
if(in_array($column,['created_at','updated_at'])) { |
132
|
|
|
$columnAdd = ""; |
133
|
|
|
$columnEdit = ""; |
134
|
|
|
$columnMandatory = ""; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$result[] = [ |
138
|
|
|
'column_label'=>$label, |
139
|
|
|
'column_field'=> $column, |
140
|
|
|
'column_type'=>$type, |
141
|
|
|
'column_file_encrypt'=>"on", |
142
|
|
|
'column_image_width'=>'', |
143
|
|
|
'column_image_height'=>'', |
144
|
|
|
'column_option_table'=>$optionTable, |
145
|
|
|
'column_date_format'=>'', |
146
|
|
|
'column_text_display_limit'=>150, |
147
|
|
|
'column_text_max'=>255, |
148
|
|
|
'column_text_min'=>0, |
149
|
|
|
'column_money_prefix'=>'', |
150
|
|
|
'column_money_precision'=>'', |
151
|
|
|
'column_money_thousand_separator'=>'', |
152
|
|
|
'column_money_decimal_separator'=>'', |
153
|
|
|
'column_option_value'=> "", |
154
|
|
|
'column_option_display'=> "", |
155
|
|
|
'column_option_sql_condition'=> "", |
156
|
|
|
'column_options'=> [], |
157
|
|
|
'column_sql_query'=> "", |
158
|
|
|
'column_help'=> "", |
159
|
|
|
'column_mandatory'=> $columnMandatory, |
160
|
|
|
'column_browse'=> "on", |
161
|
|
|
'column_detail'=> "on", |
162
|
|
|
'column_edit'=> $columnEdit, |
163
|
|
|
'column_add'=> $columnAdd, |
164
|
|
|
'listTableColumns'=> [] |
165
|
|
|
]; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
return response()->json($result); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
private function findComposer() |
172
|
|
|
{ |
173
|
|
|
if (file_exists(getcwd().'/composer.phar')) { |
174
|
|
|
return '"'.PHP_BINARY.'" '.getcwd().'/composer.phar'; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return 'composer'; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
private function composingAutoLoad() |
181
|
|
|
{ |
182
|
|
|
$composer = $this->findComposer(); |
183
|
|
|
$process = new Process($composer.' dump-autoload'); |
|
|
|
|
184
|
|
|
$process->setWorkingDirectory(base_path())->run(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function postCreateMigration() |
188
|
|
|
{ |
189
|
|
|
try { |
190
|
|
|
cb()->validation(['table_name','structures']); |
191
|
|
|
|
192
|
|
|
$tableName = request("table_name"); |
193
|
|
|
|
194
|
|
|
if(!Schema::hasTable($tableName)) { |
|
|
|
|
195
|
|
|
$filenameMigration = date("Y_m_d_His")."_".$tableName.".php"; |
|
|
|
|
196
|
|
|
$createTemplate = file_get_contents(base_path("vendor/laravel/framework/src/Illuminate/Database/Migrations/stubs/create.stub")); |
197
|
|
|
$className = Str::studly($tableName); |
|
|
|
|
198
|
|
|
$createTemplate = str_replace("DummyClass", $className, $createTemplate); |
199
|
|
|
$createTemplate = str_replace("DummyTable", $tableName, $createTemplate); |
200
|
|
|
$createTemplate = str_replace("\$table->increments('id');","",$createTemplate); |
201
|
|
|
|
202
|
|
|
$structureItems = ""; |
203
|
|
|
|
204
|
|
|
if(request("timestamp")) { |
205
|
|
|
$structureItems .= "\$table->timestamps();\n\t\t\t"; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if(request("soft_deletes")) { |
209
|
|
|
$structureItems .= "\$table->softDeletes();\n\t\t\t"; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
foreach(request("structures") as $item) { |
213
|
|
|
|
214
|
|
|
$nullable = ""; |
215
|
|
|
|
216
|
|
|
if(!in_array($item['type_data'],["bigIncrements","increments","mediumIncrements","smallIncrements"])) { |
217
|
|
|
$nullable = "->nullable()"; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
if($item['length']) { |
221
|
|
|
$structureItems .= "\$table->".$item['type_data']."('".$item['field_name']."', ".$item['length'].")$nullable;\n\t\t\t"; |
222
|
|
|
}else{ |
223
|
|
|
$structureItems .= "\$table->".$item['type_data']."('".$item['field_name']."')$nullable;\n\t\t\t"; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
$createTemplate = str_replace("\$table->timestamps();", $structureItems, $createTemplate); |
227
|
|
|
|
228
|
|
|
// Put File onto the migration folders |
229
|
|
|
file_put_contents(database_path("migrations/".$filenameMigration), $createTemplate); |
230
|
|
|
|
231
|
|
|
// Composing |
232
|
|
|
$this->composingAutoLoad(); |
233
|
|
|
|
234
|
|
|
// Migrate |
235
|
|
|
Artisan::call("migrate"); |
236
|
|
|
} else { |
237
|
|
|
throw new \Exception("The table $tableName has already exists!"); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
} catch (CBValidationException $e) { |
241
|
|
|
return response()->json(['status'=>false,'message'=>$e->getMessage()]); |
242
|
|
|
} catch (\Exception $e) { |
243
|
|
|
return response()->json(['status'=>false,'message'=>$e->getMessage()]); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return response()->json(['status'=>true,'message'=>'Migration successfully!']); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function postCheckExistModule() |
250
|
|
|
{ |
251
|
|
|
try { |
252
|
|
|
cb()->validation(['name', 'table']); |
253
|
|
|
|
254
|
|
|
if(DB::table("cb_modules")->where("table_name",request("table"))->where("name",request("name"))->count()) { |
255
|
|
|
return response()->json(['status'=>true]); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
} catch (CBValidationException $e) { |
259
|
|
|
return response()->json(['status'=>false,'message'=>$e->getMessage()]); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return response()->json(['status'=>false]); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function postAddSave() { |
266
|
|
|
|
267
|
|
|
try { |
268
|
|
|
cb()->validation(['name', 'table','icon','columns']); |
269
|
|
|
|
270
|
|
|
$module = (new ModuleGenerator(request('table'), request('name'), request('icon'), request("columns"), request("rebuild")))->make(); |
271
|
|
|
|
272
|
|
|
} catch (CBValidationException $e) { |
273
|
|
|
return response()->json(['status'=>false,'message'=>$e->getMessage()]); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return response()->json(['status'=>true, 'message'=>'Please remember that you can still modify the structure by edit the controller file '.$module['controller'].' :)']); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function getDelete($id) { |
280
|
|
|
$module = cb()->find("cb_modules",$id); |
281
|
|
|
@unlink(app_path("Http/Controllers/".$module->controller.".php")); |
|
|
|
|
282
|
|
|
DB::table("cb_modules")->where("id", $id)->delete(); |
283
|
|
|
DB::table("cb_menus")->where("cb_modules_id", $id)->delete(); |
284
|
|
|
return cb()->redirectBack("The module has been deleted!","success"); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
public function getDeleteSoft($id) { |
288
|
|
|
$module = cb()->find("cb_modules",$id); |
|
|
|
|
289
|
|
|
DB::table("cb_modules")->where("id", $id)->delete(); |
290
|
|
|
DB::table("cb_menus")->where("cb_modules_id", $id)->delete(); |
291
|
|
|
return cb()->redirectBack("The module has been deleted!","success"); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
} |