1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator; |
4
|
|
|
|
5
|
|
|
use CRUDBooster; |
|
|
|
|
6
|
|
|
|
7
|
|
|
class Step3Handler |
8
|
|
|
{ |
9
|
|
|
public function showForm($id) |
10
|
|
|
{ |
11
|
|
|
$row = ModulesRepo::find($id);; |
12
|
|
|
|
13
|
|
|
$columns = CRUDBooster::getTableColumns($row->table_name); |
14
|
|
|
|
15
|
|
|
$code = FileManipulator::readCtrlContent($row->controller); |
16
|
|
|
|
17
|
|
|
$forms = ScaffoldingParser::parse($code, 'form'); |
18
|
|
|
|
19
|
|
|
$types = $this->getComponentTypes(); |
20
|
|
|
|
21
|
|
|
return view('CbModulesGen::step3', compact('columns', 'forms', 'types', 'id')); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
private function getComponentTypes() |
28
|
|
|
{ |
29
|
|
|
$types = []; |
30
|
|
|
foreach (glob(CB::componentsPath().'*', GLOB_ONLYDIR) as $dir) { |
31
|
|
|
array_push($types, basename($dir)); |
32
|
|
|
} |
33
|
|
|
return $types; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function handleFormSubmit() |
37
|
|
|
{ |
38
|
|
|
$scripts = $this->setFormScript(request()->all()); |
39
|
|
|
|
40
|
|
|
$controller = ModulesRepo::getControllerName(request('id')); |
41
|
|
|
$phpCode = FileManipulator::readCtrlContent($controller); |
42
|
|
|
list($top, $currentScaffold, $bottom) = FileManipulator::extractBetween($phpCode, "FORM"); |
43
|
|
|
|
44
|
|
|
//IF FOUND OLD, THEN CLEAR IT |
45
|
|
|
$bottom = $this->clearOldBackup($bottom); |
46
|
|
|
|
47
|
|
|
//ARRANGE THE FULL SCRIPT |
48
|
|
|
$fileContent = $top."\n\n"; |
49
|
|
|
$fileContent .= str_repeat(' ', 12)."# START FORM DO NOT REMOVE THIS LINE\n"; |
50
|
|
|
$fileContent .= $scripts; |
51
|
|
|
$fileContent .= "\n".str_repeat(' ', 12)."# END FORM DO NOT REMOVE THIS LINE\n\n"; |
52
|
|
|
|
53
|
|
|
//CREATE A BACKUP SCAFFOLDING TO OLD TAG |
54
|
|
|
if ($currentScaffold) { |
55
|
|
|
$fileContent = $this->backupOldTagScaffold($fileContent, $currentScaffold); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$fileContent .= str_repeat(' ', 12).($bottom); |
59
|
|
|
|
60
|
|
|
//CREATE FILE CONTROLLER |
61
|
|
|
FileManipulator::putCtrlContent($controller, $fileContent); |
62
|
|
|
|
63
|
|
|
return redirect()->route('AdminModulesControllerGetStep4', ['id' => request('id')]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $post |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
private function setFormScript($post) |
71
|
|
|
{ |
72
|
|
|
$name = $post['name']; |
73
|
|
|
$width = $post['width']; |
74
|
|
|
$type = $post['type']; |
75
|
|
|
$help = $post['help']; |
76
|
|
|
$placeholder = $post['placeholder']; |
77
|
|
|
$style = $post['style']; |
78
|
|
|
$validation = $post['validation']; |
79
|
|
|
|
80
|
|
|
$scriptForm = []; |
81
|
|
|
$scriptForm[] = str_repeat(' ', 12).'$this->form = [];'; |
82
|
|
|
|
83
|
|
|
foreach ($post['label'] as $i => $label) { |
84
|
|
|
if ($label == '') { |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
$form = [ |
88
|
|
|
'label' => $label, |
89
|
|
|
'name' => $name[$i], |
90
|
|
|
'type' => $type[$i], |
91
|
|
|
'validation' => $validation[$i], |
92
|
|
|
'width' => $width[$i], |
93
|
|
|
'placeholder' => $placeholder[$i], |
94
|
|
|
'help' => $help[$i], |
95
|
|
|
'style' => $style[$i], |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
$info = json_decode(file_get_contents(CRUDBooster::componentsPath($type[$i]).'/info.json'), true); |
99
|
|
|
if (!empty($info['options'])) { |
100
|
|
|
$form = $this->parseComponentOptions($post, $info, $form); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$scriptForm[] = str_repeat(' ', 12).'$this->form[] = '.FileManipulator::stringify($form, str_repeat(' ', 12)).';'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return implode("\n", $scriptForm); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $bottomScript |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
private function clearOldBackup($bottomScript) |
114
|
|
|
{ |
115
|
|
|
if (strpos($bottomScript, '# OLD START FORM') === false) { |
116
|
|
|
return $bottomScript; |
117
|
|
|
} |
118
|
|
|
$lineStart = strpos($bottomScript, '# OLD START FORM'); |
119
|
|
|
$lineEnd = strpos($bottomScript, '# OLD END FORM') + strlen('# OLD END FORM'); |
120
|
|
|
|
121
|
|
|
$getString = substr($bottomScript, $lineStart, $lineEnd); |
122
|
|
|
|
123
|
|
|
return str_replace($getString, '', $bottomScript); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param $fileContent |
128
|
|
|
* @param $middle |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
private function backupOldTagScaffold($fileContent, $middle) |
132
|
|
|
{ |
133
|
|
|
$middle = preg_split("/\\r\\n|\\r|\\n/", $middle); |
134
|
|
|
foreach ($middle as &$c) { |
135
|
|
|
$c = str_repeat(' ', 12)."//".trim($c); |
136
|
|
|
} |
137
|
|
|
$middle = implode("\n", $middle); |
|
|
|
|
138
|
|
|
|
139
|
|
|
$fileContent .= str_repeat(' ', 12)."# OLD START FORM\n"; |
140
|
|
|
$fileContent .= $middle."\n"; |
141
|
|
|
$fileContent .= str_repeat(' ', 12)."# OLD END FORM\n\n"; |
142
|
|
|
|
143
|
|
|
return $fileContent; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param $post |
148
|
|
|
* @param $info |
149
|
|
|
* @param $form |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
private function parseComponentOptions($post, $info, $form) |
153
|
|
|
{ |
154
|
|
|
$options = []; |
155
|
|
|
foreach ($info['options'] as $i => $opt) { |
156
|
|
|
$optionValue = $post[$opt['name']][$form['name']]; |
157
|
|
|
if ($opt['type'] == 'array') { |
158
|
|
|
$options[$opt['name']] = ($optionValue) ? explode(";", $optionValue) : []; |
159
|
|
|
} elseif ($opt['type'] == 'boolean') { |
160
|
|
|
$options[$opt['name']] = ($optionValue == 1) ? true : false; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
$form['options'] = $options; |
164
|
|
|
|
165
|
|
|
return $form; |
166
|
|
|
} |
167
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths