|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\Modules\ApiGeneratorModule; |
|
4
|
|
|
|
|
5
|
|
|
use crocodicstudio\crudbooster\controllers\CBController; |
|
6
|
|
|
use crocodicstudio\crudbooster\Modules\ModuleGenerator\ControllerGenerator\FieldDetector; |
|
7
|
|
|
use Illuminate\Support\Facades\Request; |
|
8
|
|
|
use Illuminate\Support\Facades\DB; |
|
9
|
|
|
use Illuminate\Support\Facades\Route; |
|
10
|
|
|
|
|
11
|
|
|
class AdminColumnsTableController extends CBController |
|
12
|
|
|
{ |
|
13
|
|
|
public function cbInit() |
|
14
|
|
|
{ |
|
15
|
|
|
$this->table = 'cms_apicustom'; |
|
16
|
|
|
$this->primaryKey = "id"; |
|
17
|
|
|
$this->title_field = "nama"; |
|
18
|
|
|
$this->buttonShow = false; |
|
19
|
|
|
$this->button_new = false; |
|
|
|
|
|
|
20
|
|
|
$this->deleteBtn = false; |
|
21
|
|
|
$this->buttonAdd = false; |
|
22
|
|
|
$this->button_import = false; |
|
23
|
|
|
$this->buttonExport = false; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function getColumnTable($table, $type = 'list') |
|
27
|
|
|
{ |
|
28
|
|
|
$this->cbLoader(); |
|
29
|
|
|
$except = ['created_at', 'deleted_at', 'updated_at']; |
|
30
|
|
|
|
|
31
|
|
|
$result = \Schema::getColumnListing($table); |
|
32
|
|
|
$newResult = []; |
|
33
|
|
|
foreach ($result as $row) { |
|
34
|
|
|
|
|
35
|
|
|
if (in_array($row, $except)) { |
|
36
|
|
|
continue; |
|
37
|
|
|
} |
|
38
|
|
|
$type_field = \Schema::getColumnType($table, $row); |
|
39
|
|
|
$newResult[] = ['name' => $row, 'type' => $this->getFieldType($row, $type_field)]; |
|
40
|
|
|
|
|
41
|
|
|
if (! in_array($type, ['list', 'detail']) || ! starts_with($row, 'id_')) { |
|
42
|
|
|
continue; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$newResult = $this->prepareResults($row, $newResult); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return response()->json($newResult); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param $ro string |
|
53
|
|
|
* @param $default string |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
private function getFieldType($ro, $default) |
|
57
|
|
|
{ |
|
58
|
|
|
$MAP = [ |
|
59
|
|
|
'isEmail' => "email", |
|
60
|
|
|
'isImage' => "image", |
|
61
|
|
|
'isPassword' => "password", |
|
62
|
|
|
'isForeignKey' => "integer", |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
|
|
foreach ($MAP as $methodName => $type) { |
|
66
|
|
|
if (FieldDetector::$methodName($ro)) { |
|
67
|
|
|
return $type; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $default; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param $table2 |
|
76
|
|
|
* @param $ro |
|
77
|
|
|
* @param $new_result |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
private function prepareResults($ro, $new_result) |
|
81
|
|
|
{ |
|
82
|
|
|
if (starts_with($ro, 'id_')) { |
|
83
|
|
|
return $new_result; |
|
84
|
|
|
} |
|
85
|
|
|
$table2 = substr($ro, 3); |
|
86
|
|
|
$columns = DB::getSchemaBuilder()->getColumnListing($table2); |
|
87
|
|
|
$columns = array_filter($columns, function ($col) { |
|
88
|
|
|
return ! FieldDetector::isExceptional($col); |
|
89
|
|
|
}); |
|
90
|
|
|
|
|
91
|
|
|
foreach ($columns as $col) { |
|
92
|
|
|
$col = str_replace("_$table2", "", $col); |
|
93
|
|
|
$new_result[] = ['name' => $table2.'_'.$col, 'type' => \Schema::getColumnType($table2, $col)]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $new_result; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|