38
38
use Dropdown ;
39
39
use DbUtils ;
40
40
use Plugin ;
41
- use Ticket ;
42
- use Change ;
43
- use Problem ;
44
41
use Session ;
45
42
use Html ;
46
43
use OperatingSystem ;
47
44
use PluginFieldsDropdown ;
48
45
use PluginFieldsField ;
49
- use PluginFormcreatorQuestion ;
50
46
use Toolbox ;
51
47
use User ;
52
48
@@ -59,19 +55,20 @@ class FieldsField extends PluginFormcreatorAbstractField
59
55
public $ field = null ;
60
56
61
57
/**
58
+ * Get the additional field object lined to this formcreator field
62
59
*
63
- * @param array $question PluginFormcreatorQuestion instance
60
+ * @return null|PluginFieldsField
64
61
*/
65
- public function __construct (PluginFormcreatorQuestion $ question ) {
66
- $ this ->question = $ question ;
67
- if (isset ($ this ->question ->fields ['values ' ])) {
62
+ public function getField (): ?PluginFieldsField {
63
+ if ($ this ->field === null && isset ($ this ->question ->fields ['values ' ])) {
68
64
$ decodedValues = json_decode ($ this ->question ->fields ['values ' ], JSON_OBJECT_AS_ARRAY );
69
65
$ field_name = $ decodedValues ['dropdown_fields_field ' ] ?? '' ;
70
66
$ fieldObj = new PluginFieldsField ();
71
67
if ($ fieldObj ->getFromDBByCrit (['name ' => $ field_name ])) {
72
- $ this ->field = $ fieldObj ;
68
+ $ this ->field = $ fieldObj ;
73
69
}
74
70
}
71
+ return $ this ->field ;
75
72
}
76
73
77
74
public function isPrerequisites (): bool {
@@ -152,16 +149,24 @@ public function getBlocks(): array {
152
149
}
153
150
154
151
public function showForm (array $ options ): void {
152
+ if (!\Plugin::isPluginActive ('fields ' )) {
153
+ $ options ['error ' ] = __ ('Warning: Additional Fields plugin is disabled or missing ' , 'formcreator ' );
154
+ $ template = '@formcreator/field/undefinedfield.html.twig ' ;
155
+ TemplateRenderer::getInstance ()->display ($ template , [
156
+ 'item ' => $ this ->question ,
157
+ 'params ' => $ options ,
158
+ ]);
159
+ return ;
160
+ }
155
161
156
162
$ template = '@formcreator/field/ ' . $ this ->question ->fields ['fieldtype ' ] . 'field.html.twig ' ;
157
-
158
163
$ decodedValues = json_decode ($ this ->question ->fields ['values ' ], JSON_OBJECT_AS_ARRAY );
159
164
$ this ->question ->fields ['_block_id ' ] = $ decodedValues ['blocks_field ' ] ?? 0 ;
160
165
$ this ->question ->fields ['_block_list ' ] = $ this ->getBlocks ();
161
166
$ this ->question ->fields ['_drodpdown_block_label ' ] = __ ("Block " , "fields " );
162
167
163
- $ this ->question ->fields ['_field_name ' ] = $ decodedValues ['dropdown_fields_field ' ] ?? '' ;
164
- $ this ->question ->fields ['_field_list ' ] = FieldsField::getFieldsFromBlock ($ this ->question ->fields ['_block_id ' ]);
168
+ $ this ->question ->fields ['_field_name ' ] = $ decodedValues ['dropdown_fields_field ' ] ?? '' ;
169
+ $ this ->question ->fields ['_field_list ' ] = FieldsField::getFieldsFromBlock ($ this ->question ->fields ['_block_id ' ]);
165
170
$ this ->question ->fields ['_drodpdown_field_label ' ] = __ ("Field " , "fields " );
166
171
167
172
TemplateRenderer::getInstance ()->display ($ template , [
@@ -190,7 +195,7 @@ public function getRenderedHtml($domain, $canEdit = true): string {
190
195
191
196
public function prepareHtmlField ($ fieldName , $ canedit = true , $ value = '' ) {
192
197
193
- if (empty ($ this ->field ->fields )) {
198
+ if ($ this -> getField () === null || empty ($ this ->getField () ->fields )) {
194
199
return false ;
195
200
}
196
201
@@ -201,10 +206,10 @@ public function prepareHtmlField($fieldName, $canedit = true, $value = '') {
201
206
$ field ['value ' ] = $ value ;
202
207
} else {
203
208
//get default value
204
- if ($ this ->field ->fields ['default_value ' ] !== "" ) {
205
- $ value = $ this ->field ->fields ['default_value ' ];
209
+ if ($ this ->getField () ->fields ['default_value ' ] !== "" ) {
210
+ $ value = $ this ->getField () ->fields ['default_value ' ];
206
211
// shortcut for date/datetime
207
- if (in_array ($ this ->field ->fields ['type ' ], ['date ' , 'datetime ' ])
212
+ if (in_array ($ this ->getField () ->fields ['type ' ], ['date ' , 'datetime ' ])
208
213
&& $ value == 'now ' ) {
209
214
$ value = $ _SESSION ["glpi_currenttime " ];
210
215
}
@@ -213,10 +218,10 @@ public function prepareHtmlField($fieldName, $canedit = true, $value = '') {
213
218
}
214
219
215
220
$ html = "" ;
216
- $ readonly = ($ this ->field ->fields ['is_readonly ' ] || !$ canedit );
217
- $ this ->question ->fields ['required ' ] = $ this ->field ->fields ['mandatory ' ];
221
+ $ readonly = ($ this ->getField () ->fields ['is_readonly ' ] || !$ canedit );
222
+ $ this ->question ->fields ['required ' ] = $ this ->getField () ->fields ['mandatory ' ];
218
223
219
- switch ($ this ->field ->fields ['type ' ]) {
224
+ switch ($ this ->getField () ->fields ['type ' ]) {
220
225
case 'number ' :
221
226
case 'text ' :
222
227
$ value = Html::cleanInputText ($ value );
@@ -263,19 +268,19 @@ public function prepareHtmlField($fieldName, $canedit = true, $value = '') {
263
268
break ;
264
269
case 'dropdown ' :
265
270
if ($ canedit && !$ readonly ) {
266
- if (strpos ($ this ->field ->fields ['name ' ], "dropdowns_id " ) !== false ) {
271
+ if (strpos ($ this ->getField () ->fields ['name ' ], "dropdowns_id " ) !== false ) {
267
272
$ dropdown_itemtype = getItemTypeForTable (
268
- getTableNameForForeignKeyField ($ this ->field ->fields ['name ' ]));
273
+ getTableNameForForeignKeyField ($ this ->getField () ->fields ['name ' ]));
269
274
} else {
270
- $ dropdown_itemtype = PluginFieldsDropdown::getClassname ($ this ->field ->fields ['name ' ]);
275
+ $ dropdown_itemtype = PluginFieldsDropdown::getClassname ($ this ->getField () ->fields ['name ' ]);
271
276
}
272
277
$ html .= Dropdown::show ($ dropdown_itemtype ,
273
278
['value ' => $ value ,
274
279
'name ' => $ fieldName ,
275
280
'entity ' => $ _SESSION ['glpiactiveentities ' ],
276
281
'display ' => false ]);
277
282
} else {
278
- $ dropdown_table = "glpi_plugin_fields_ " .$ this ->field ->fields ['name ' ]."dropdowns " ;
283
+ $ dropdown_table = "glpi_plugin_fields_ " .$ this ->getField () ->fields ['name ' ]."dropdowns " ;
279
284
$ html .= Dropdown::getDropdownName ($ dropdown_table , $ value );
280
285
}
281
286
@@ -417,18 +422,18 @@ public function getValueForApi(): string {
417
422
418
423
public function isValidValue ($ value ): bool {
419
424
420
- if (is_null ($ this ->field )) {
425
+ if (is_null ($ this ->getField () )) {
421
426
return false ;
422
427
}
423
428
424
429
//check data type for input number / url
425
430
$ valid = true ;
426
- if ($ this ->field ->fields ['type ' ] == 'number ' && !empty ($ this ->value ) && !is_numeric ($ this ->value )) {
427
- $ number_errors [] = $ this ->field ->fields ['label ' ];
431
+ if ($ this ->getField () ->fields ['type ' ] == 'number ' && !empty ($ this ->value ) && !is_numeric ($ this ->value )) {
432
+ $ number_errors [] = $ this ->getField () ->fields ['label ' ];
428
433
$ valid = false ;
429
- } else if ($ this ->field ->fields ['type ' ] == 'url ' && !empty ($ this ->value )) {
434
+ } else if ($ this ->getField () ->fields ['type ' ] == 'url ' && !empty ($ this ->value )) {
430
435
if (filter_var ($ this ->value , FILTER_VALIDATE_URL ) === false ) {
431
- $ url_errors [] = $ this ->field ->fields ['label ' ];
436
+ $ url_errors [] = $ this ->getField () ->fields ['label ' ];
432
437
$ valid = false ;
433
438
}
434
439
}
@@ -452,9 +457,9 @@ public function isValidValue($value): bool {
452
457
}
453
458
454
459
public function isValid (): bool {
455
- if (!is_null ($ this ->field )) {
460
+ if (!is_null ($ this ->getField () )) {
456
461
// If the field is required it can't be empty
457
- if ($ this ->field ->fields ['mandatory ' ] && $ this ->value == '' ) {
462
+ if ($ this ->getField () ->fields ['mandatory ' ] && $ this ->value == '' ) {
458
463
Session::addMessageAfterRedirect (
459
464
__ ('A required field is empty: ' , 'formcreator ' ) . ' ' . $ this ->getLabel (),
460
465
false ,
0 commit comments