1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Gui\FormBuilder; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class FormBuilder |
6
|
|
|
* |
7
|
|
|
* @package Gui |
8
|
|
|
* @subpackage FormBuilder |
9
|
|
|
* @author Dodonov A.A. |
10
|
|
|
* @version v.1.0 (2019/08/13) |
11
|
|
|
* @copyright Copyright (c) 2019, aeon.org |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Form builder class |
16
|
|
|
*/ |
17
|
|
|
class FormBuilder |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Fields algorithms |
22
|
|
|
*/ |
23
|
|
|
private $fieldsAlgorithms = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Session id |
27
|
|
|
*/ |
28
|
|
|
private $sessionId = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Entity name |
32
|
|
|
*/ |
33
|
|
|
private $entityName = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Layout |
37
|
|
|
*/ |
38
|
|
|
private $layout = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Multiple forms |
42
|
|
|
*/ |
43
|
|
|
private $batch = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor |
47
|
|
|
* |
48
|
|
|
* @param \Mezon\Gui\FieldsAlgorithms $fieldsAlgorithms |
49
|
|
|
* Fields algorithms |
50
|
|
|
* @param string $sessionId |
51
|
|
|
* Session id |
52
|
|
|
* @param string $entityName |
53
|
|
|
* Entity name |
54
|
|
|
* @param array $layout |
55
|
|
|
* Fields layout |
56
|
|
|
* @param bool $batch |
57
|
|
|
* Batch operations available |
58
|
|
|
*/ |
59
|
|
|
public function __construct( |
60
|
|
|
\Mezon\Gui\FieldsAlgorithms $fieldsAlgorithms, |
61
|
|
|
string $sessionId = '', |
62
|
|
|
string $entityName = 'default', |
63
|
|
|
$layout = false, |
64
|
|
|
bool $batch = false) |
65
|
|
|
{ |
66
|
|
|
$this->fieldsAlgorithms = $fieldsAlgorithms; |
67
|
|
|
|
68
|
|
|
$this->sessionId = $sessionId; |
69
|
|
|
|
70
|
|
|
$this->entityName = $entityName; |
71
|
|
|
|
72
|
|
|
$this->layout = $layout; |
73
|
|
|
|
74
|
|
|
$this->batch = $batch; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Method compiles form without layout |
79
|
|
|
* |
80
|
|
|
* @return string Compiled control |
81
|
|
|
*/ |
82
|
|
|
protected function compileForFieldsWithNoLayout(): string |
83
|
|
|
{ |
84
|
|
|
$content = ''; |
85
|
|
|
|
86
|
|
|
foreach ($this->fieldsAlgorithms->getFieldsNames() as $name) { |
87
|
|
|
$field = $this->fieldsAlgorithms->getObject($name); |
88
|
|
|
if ($name == 'id' || $name == 'domain_id' || $name == 'creation_date' || $name == 'modification_date' || |
89
|
|
|
$field->isVisible() === false) { |
90
|
|
|
continue; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$content .= '<div class="form-group ' . $this->entityName . '">' . '<label class="control-label" >' . |
94
|
|
|
$field->getTitle() . ($field->isRequired($name) ? ' <span class="required">*</span>' : '') . '</label>' . |
95
|
|
|
$field->html() . '</div>'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $content; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Method compiles atoic field |
103
|
|
|
* |
104
|
|
|
* @param array $field |
105
|
|
|
* Field description |
106
|
|
|
* @param string $name |
107
|
|
|
* HTML field name |
108
|
|
|
* @return string Compiled field |
109
|
|
|
*/ |
110
|
|
|
protected function compileField($field, $name) |
111
|
|
|
{ |
112
|
|
|
$control = $this->fieldsAlgorithms->getCompiledField($name); |
113
|
|
|
|
114
|
|
|
$fieldObject = $this->fieldsAlgorithms->getObject($name); |
115
|
|
|
|
116
|
|
|
if ($fieldObject->fillAllRow()) { |
117
|
|
|
return $control->html(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($fieldObject->isVisible() === false) { |
121
|
|
|
return ''; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$content = '<div class="form-group ' . $this->entityName . ' col-md-' . $field['width'] . '">'; |
125
|
|
|
|
126
|
|
|
if ($fieldObject->hasLabel()) { |
127
|
|
|
$content .= '<label class="control-label" style="text-align: left;">' . $fieldObject->getTitle() . |
128
|
|
|
($fieldObject->isRequired($name) ? ' <span class="required">*</span>' : '') . '</label>'; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $content . $control . '</div>'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Method compiles form with layout |
136
|
|
|
* |
137
|
|
|
* @param array $record |
138
|
|
|
* Record |
139
|
|
|
* @return string Compiled fields |
140
|
|
|
*/ |
141
|
|
|
protected function compileForFieldsWithLayout(array $record = []): string |
142
|
|
|
{ |
143
|
|
|
$content = ''; |
144
|
|
|
|
145
|
|
|
foreach ($this->layout['rows'] as $row) { |
146
|
|
|
foreach ($row as $name => $field) { |
147
|
|
|
$content .= $this->compileField($field, $name, $record); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $content; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Method returns amount of columns in the form |
156
|
|
|
* |
157
|
|
|
* @return string|int Width of the column |
158
|
|
|
*/ |
159
|
|
|
protected function getFormWidth() |
160
|
|
|
{ |
161
|
|
|
if (isset($_GET['form-width'])) { |
162
|
|
|
return intval($_GET['form-width']); |
163
|
|
|
} elseif ($this->layout === false || count($this->layout) === 0) { |
|
|
|
|
164
|
|
|
return 6; |
165
|
|
|
} else { |
166
|
|
|
return $this->layout['width']; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Method compiles form fields |
172
|
|
|
* |
173
|
|
|
* @param array $record |
174
|
|
|
* Record |
175
|
|
|
* @return string Compiled fields |
176
|
|
|
*/ |
177
|
|
|
public function compileFormFields($record = []) |
178
|
|
|
{ |
179
|
|
|
if (count($this->layout) === 0) { |
|
|
|
|
180
|
|
|
return $this->compileForFieldsWithNoLayout($record); |
|
|
|
|
181
|
|
|
} else { |
182
|
|
|
return $this->compileForFieldsWithLayout($record); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Method compiles creation form |
188
|
|
|
* |
189
|
|
|
* @return string Compiled creation form |
190
|
|
|
*/ |
191
|
|
|
public function creationForm(): string |
192
|
|
|
{ |
193
|
|
|
if (isset($_GET['no-header'])) { |
194
|
|
|
$content = file_get_contents(__DIR__ . '/res/templates/creation_form_no_header.tpl'); |
195
|
|
|
} else { |
196
|
|
|
$content = file_get_contents(__DIR__ . '/res/templates/creation_form_header.tpl'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$content .= file_get_contents(__DIR__ . '/res/templates/creation_form.tpl'); |
200
|
|
|
|
201
|
|
|
$backLink = isset($_GET['back-link']) ? $_GET['back-link'] : '../list/'; |
202
|
|
|
|
203
|
|
|
$content = str_replace('{fields}', $this->compileFormFields(), $content); |
204
|
|
|
|
205
|
|
|
$content = str_replace('{width}', $this->getFormWidth(), $content); |
206
|
|
|
|
207
|
|
|
return str_replace('{back-link}', $backLink, $content); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Method compiles updating form |
212
|
|
|
* |
213
|
|
|
* @param string $sessionId |
214
|
|
|
* Session id |
215
|
|
|
* @param array $record |
216
|
|
|
* Record to be updated |
217
|
|
|
* @return string Compiled updating form |
218
|
|
|
*/ |
219
|
|
|
public function updatingForm(string $sessionId, array $record): string |
220
|
|
|
{ |
221
|
|
|
if (isset($_GET['no-header'])) { |
222
|
|
|
$content = file_get_contents(__DIR__ . '/res/templates/updating_form_no_header.tpl'); |
223
|
|
|
} else { |
224
|
|
|
$content = file_get_contents(__DIR__ . '/res/templates/updating_form_header.tpl'); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$content .= file_get_contents(__DIR__ . '/res/templates/updating_form.tpl'); |
228
|
|
|
|
229
|
|
|
$this->sessionId = $sessionId; |
230
|
|
|
$this->fieldsAlgorithms->setSessionId($sessionId); |
231
|
|
|
|
232
|
|
|
return str_replace('{fields}', $this->compileFormFields($record), $content); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.