|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator\ControllerGenerator; |
|
4
|
|
|
|
|
5
|
|
|
use crocodicstudio\crudbooster\helpers\DbInspector; |
|
6
|
|
|
use crocodicstudio\crudbooster\Modules\ModuleGenerator\FileManipulator; |
|
7
|
|
|
use Illuminate\Support\Facades\Schema; |
|
8
|
|
|
use CB; |
|
9
|
|
|
|
|
10
|
|
|
class FormConfigGenerator |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @param $table |
|
14
|
|
|
* @param $coloms |
|
15
|
|
|
* @return array |
|
16
|
|
|
*/ |
|
17
|
|
|
static function generateFormConfig($table, $coloms) |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
$formArrayString = []; |
|
20
|
|
|
foreach ($coloms as $i => $colName) { |
|
21
|
|
|
//$attribute = []; |
|
22
|
|
|
$input = [ |
|
23
|
|
|
'label' => self::getLabel($colName), |
|
24
|
|
|
'name' => $colName, |
|
25
|
|
|
'type' => '', |
|
26
|
|
|
'options' => '', |
|
27
|
|
|
'required' => true, |
|
28
|
|
|
'validation' => '', |
|
29
|
|
|
'help' => '', |
|
30
|
|
|
'placeholder' => '', |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
if (FieldDetector::isExceptional($colName)) { |
|
34
|
|
|
continue; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$typeData = DbInspector::getFieldTypes($table, $colName); |
|
38
|
|
|
|
|
39
|
|
|
list($input['type'], $input['validation'], $input['options']) = self::parseFieldType($typeData); |
|
40
|
|
|
|
|
41
|
|
|
if (FieldDetector::isForeignKey($colName)) { |
|
42
|
|
|
list($input['type'], $input['options']) = self::handleForeignKey($colName); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (substr($colName, 0, 3) == 'is_') { |
|
46
|
|
|
$input['type'] = 'radio_dataenum'; |
|
47
|
|
|
$label = ucwords(substr($colName, 3)); |
|
48
|
|
|
$input['options'] = [ |
|
49
|
|
|
'enum' => ['In '.$label, $label], |
|
50
|
|
|
'value' => [0, 1], |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (FieldDetector::isPassword($colName)) { |
|
55
|
|
|
$input['type'] = 'password'; |
|
56
|
|
|
$input['validation'] = 'min:5|max:32|required'; |
|
57
|
|
|
$input['help'] = cbTrans("text_default_help_password"); |
|
58
|
|
|
}elseif (FieldDetector::isImage($colName)) { |
|
59
|
|
|
$input['type'] = 'upload'; |
|
60
|
|
|
$input['validation'] = 'required|image'; |
|
61
|
|
|
$input['help'] = cbTrans('text_default_help_upload'); |
|
62
|
|
|
}elseif (FieldDetector::isGeographical($colName)) { |
|
63
|
|
|
$input['type'] = 'hidden'; |
|
64
|
|
|
$input['validation'] = 'required|numeric'; |
|
65
|
|
|
}elseif (FieldDetector::isPhone($colName)) { |
|
66
|
|
|
$input['type'] = 'number'; |
|
67
|
|
|
$input['validation'] = 'required|numeric'; |
|
68
|
|
|
$input['placeholder'] = cbTrans('text_default_help_number'); |
|
69
|
|
|
}elseif (FieldDetector::isEmail($colName)) { |
|
70
|
|
|
$input['type'] = 'email'; |
|
71
|
|
|
$input['validation'] = 'require|email|unique:'.$table; |
|
72
|
|
|
$input['placeholder'] = cbTrans('text_default_help_email'); |
|
73
|
|
|
}elseif ($input['type'] == 'text' && FieldDetector::isNameField($colName)) { |
|
74
|
|
|
$input['validation'] = 'required|string|min:3|max:70'; |
|
75
|
|
|
$input['placeholder'] = cbTrans('text_default_help_text'); |
|
76
|
|
|
}elseif ($input['type'] == 'text' && FieldDetector::isUrlField($colName)) { |
|
77
|
|
|
$input['validation'] = 'required|url'; |
|
78
|
|
|
$input['placeholder'] = cbTrans('text_default_help_url'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$formArrayString[] = FileManipulator::stringify($input, " "); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $formArrayString; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param $c |
|
89
|
|
|
* @return mixed|string |
|
90
|
|
|
*/ |
|
91
|
|
|
private static function getLabel($c) |
|
92
|
|
|
{ |
|
93
|
|
|
$label = str_replace("id_", "", $c); |
|
94
|
|
|
$label = ucwords(str_replace("_", " ", $label)); |
|
95
|
|
|
|
|
96
|
|
|
return $label; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param $typeData |
|
101
|
|
|
* |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
private static function parseFieldType($typeData) |
|
105
|
|
|
{ |
|
106
|
|
|
$typeData = array_get([ |
|
107
|
|
|
'longtext' => 'text', |
|
108
|
|
|
'integer' => 'int', |
|
109
|
|
|
'timestamp' => 'datetime', |
|
110
|
|
|
], $typeData, $typeData); |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
$default = ["text", "min:1|max:255", []]; |
|
114
|
|
|
return array_get([ |
|
115
|
|
|
'text' => ['textarea', "string|min:5", []], |
|
116
|
|
|
'date' => ['date', "date", ['php_format' => 'M, d Y', 'datepicker_format' => 'M, dd YYYY',]], |
|
117
|
|
|
'datetime' => ['datetime', "date_format:Y-m-d H:i:s", ['php_format' => 'M, d Y H:i',]], |
|
118
|
|
|
'time' => ['time', 'date_format:H:i:s', []], |
|
119
|
|
|
'double' => ['money', "integer|min:0", []], |
|
120
|
|
|
'int' => ['number', 'integer|min:0', []], |
|
121
|
|
|
], $typeData, $default); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param $field |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
|
|
private static function handleForeignKey($field) |
|
129
|
|
|
{ |
|
130
|
|
|
$jointable = str_replace(['id_', '_id'], '', $field); |
|
131
|
|
|
if (!Schema::hasTable($jointable)) { |
|
132
|
|
|
return ['', '']; |
|
133
|
|
|
} |
|
134
|
|
|
$options = [ |
|
135
|
|
|
'table' => $jointable, |
|
136
|
|
|
'field_label' => DbInspector::colName(DbInspector::getTableCols($jointable)), |
|
137
|
|
|
'field_value' => DbInspector::findPk($jointable), |
|
138
|
|
|
]; |
|
139
|
|
|
|
|
140
|
|
|
return ['select2_datatable', $options]; |
|
141
|
|
|
} |
|
142
|
|
|
} |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.