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