62
62
use PassiveDCEquipment ;
63
63
use PluginAppliancesAppliance ;
64
64
use Plugin ;
65
+ use CommonTreeDropdown ;
65
66
66
67
use GlpiPlugin \Formcreator \Exception \ComparisonException ;
67
68
@@ -116,14 +117,25 @@ public function getDesignSpecializationField(): array {
116
117
if ((new Plugin ())->isActivated ('appliances ' )) {
117
118
$ optgroup [__ ("Assets " )][PluginAppliancesAppliance::class] = PluginAppliancesAppliance::getTypeName (2 ) . ' ( ' . _n ('Plugin ' , 'Plugins ' , 1 ) . ') ' ;
118
119
}
120
+
121
+ $ decodedValues = json_decode ($ this ->question ->fields ['values ' ], JSON_OBJECT_AS_ARRAY );
122
+ if ($ decodedValues === null ) {
123
+ $ itemtype = $ this ->question ->fields ['values ' ];
124
+ } else {
125
+ $ itemtype = $ decodedValues ['itemtype ' ];
126
+ }
127
+
119
128
array_unshift ($ optgroup , '--- ' );
120
129
$ field = Dropdown::showFromArray ('glpi_objects ' , $ optgroup , [
121
- 'value ' => $ this -> question -> fields [ ' values ' ] ,
130
+ 'value ' => $ itemtype ,
122
131
'rand ' => $ rand ,
123
132
'on_change ' => 'plugin_formcreator_changeGlpiObjectItemType(); ' ,
124
133
'display ' => false ,
125
134
]);
126
135
136
+ $ root = $ decodedValues ['show_tree_root ' ] ?? '0 ' ;
137
+ $ maxDepth = $ decodedValues ['show_tree_depth ' ] ?? Dropdown::EMPTY_VALUE ;
138
+
127
139
$ additions = '<tr class="plugin_formcreator_question_specific"> ' ;
128
140
$ additions .= '<td> ' ;
129
141
$ additions .= '<label for="dropdown_default_values ' . $ rand . '"> ' ;
@@ -132,10 +144,19 @@ public function getDesignSpecializationField(): array {
132
144
$ additions .= '</td> ' ;
133
145
$ additions .= '<td id="dropdown_default_value_field"> ' ;
134
146
$ additions .= '</td> ' ;
135
- $ additions .= '<td></td> ' ;
136
- $ additions .= '<td></td> ' ;
147
+ $ additions .= '<td> ' ;
148
+ $ additions .= "<input id='commonTreeDropdownRoot' type='hidden' value=' $ root'> " ;
149
+ $ additions .= "<input id='commonTreeDropdownMaxDepth' type='hidden' value=' $ maxDepth'> " ;
150
+ $ additions .= '</td> ' ;
151
+ $ additions .= '<td> ' ;
152
+ $ additions .= '</td> ' ;
137
153
$ additions .= '</tr> ' ;
138
154
$ additions .= Html::scriptBlock ("plugin_formcreator_changeGlpiObjectItemType( $ rand); " );
155
+
156
+ $ additions .= '<tr class="plugin_formcreator_question_specific plugin_formcreator_dropdown"> ' ;
157
+ // This row will be generated by an AJAX request
158
+ $ additions .= '</tr> ' ;
159
+
139
160
return [
140
161
'label ' => $ label ,
141
162
'field ' => $ field ,
@@ -168,9 +189,24 @@ public function prepareQuestionInputForSave($input) {
168
189
return [];
169
190
}
170
191
171
- $ input ['values ' ] = $ input ['glpi_objects ' ];
192
+ $ itemtype = $ input ['glpi_objects ' ];
193
+ $ input ['values ' ] = [
194
+ 'itemtype ' => $ itemtype
195
+ ];
172
196
$ input ['default_values ' ] = isset ($ input ['dropdown_default_value ' ]) ? $ input ['dropdown_default_value ' ] : '' ;
197
+
198
+ // Params for CommonTreeDropdown fields
199
+ if (is_a ($ itemtype , CommonTreeDropdown::class, true )) {
200
+ // Set default for depth setting
201
+ $ input ['values ' ]['show_tree_depth ' ] = (int ) ($ input ['show_tree_depth ' ] ?? '-1 ' );
202
+ $ input ['values ' ]['show_tree_root ' ] = ($ input ['show_tree_root ' ] ?? '' );
203
+ }
204
+
205
+ $ input ['values ' ] = json_encode ($ input ['values ' ]);
206
+
173
207
unset($ input ['dropdown_default_value ' ]);
208
+ unset($ input ['show_tree_root ' ]);
209
+ unset($ input ['show_tree_depth ' ]);
174
210
175
211
return $ input ;
176
212
}
0 commit comments