29
29
*
30
30
* */
31
31
class PluginRulesRule extends Rule {
32
+
32
33
/**
33
34
* If do history.
34
35
* @var bool
@@ -79,6 +80,16 @@ class PluginRulesRule extends Rule {
79
80
*/
80
81
//const ONPURGE = 8;
81
82
83
+ /**
84
+ * For criteria screen
85
+ */
86
+ const CRITERIA = 1 ;
87
+
88
+ /**
89
+ * For action screen
90
+ */
91
+ const ACTION = 2 ;
92
+
82
93
/**
83
94
* Return the asset of actual class. Based on name.
84
95
* @return String
@@ -164,49 +175,91 @@ function showSpecificCriteriasForPreview($fields) {
164
175
}
165
176
}
166
177
167
- public function getCriteriasFromObject (CommonDBTM $ object ) {
178
+ /**
179
+ *
180
+ * @param CommonDBTM $object
181
+ * @param type $criteria_or_action See constants.
182
+ * @return string
183
+ */
184
+ public function getCriteriasFromObject (CommonDBTM $ object , $ criteria_or_action ) {
168
185
$ criterias = $ object ->getSearchOptions ();
186
+
187
+ // for infocom table
188
+ $ criterias = array_merge ($ criterias , $ this ::getInfocomCriterias ($ object , $ criteria_or_action ));
169
189
170
190
foreach ($ criterias as $ key => $ criteria ) {
171
191
if (!is_numeric ($ key )) {
172
192
unset($ criterias [$ key ]);
193
+
173
194
// on actions can't view groups
174
- //$criterias_final[$key] = $object->getType() . ' - ' . $key;
195
+ if ($ criteria_or_action == $ this ::CRITERIA ) {
196
+ $ criterias_final [$ key ] = $ object ->getTypeName () . ' - ' . $ key ;
197
+ }
198
+
175
199
continue ;
176
200
}
177
201
178
- if ($ criterias [$ key ]['table ' ] != $ object ->getTable ()) {
202
+ // foreing key table
203
+ if ($ criteria ['table ' ] != $ object ->getTable ()) {
204
+ $ sufix = "_id " ;
205
+
179
206
// if it's foreingkey but don't dropdown
180
- if ($ criterias [$ key ]['datatype ' ] != 'dropdown ' ) {
181
- unset($ criterias [$ key ]);
182
- continue ;
207
+ if ($ criteria ['datatype ' ] != 'dropdown ' ) {
208
+ if ($ criteria_or_action == $ this ::ACTION ) {
209
+ $ sufix = "_ " . $ criteria ['field ' ];
210
+ $ item = str_replace ("glpi_ " , "" , $ criteria ['table ' ]);
211
+ $ item = ucfirst (substr ($ item , 0 , strlen ($ item ) - 1 ));
212
+ if (class_exists ($ item )) {
213
+ $ criterias [$ key ]['name ' ] = $ item ::getTypeName (1 ) . " - " . $ criterias [$ key ]['name ' ];
214
+ } else {
215
+ unset($ criterias [$ key ]);
216
+ continue ;
217
+ }
218
+ } else {
219
+ unset($ criterias [$ key ]);
220
+ continue ;
221
+ }
183
222
}
184
223
185
- $ field = $ criterias [$ key ]['linkfield ' ] = str_replace ("glpi_ " , "" , $ criterias [$ key ]['table ' ]) . "_id " ;
224
+ $ field = $ criterias [$ key ]['linkfield ' ] = str_replace ("glpi_ " , "" , $ criterias [$ key ]['table ' ]) . $ sufix ;
225
+
186
226
} else {
187
227
$ field = $ criterias [$ key ]['field ' ];
188
228
}
189
229
190
230
// the index must be named like the field not a number
191
231
$ criterias_final [$ field ] = $ criterias [$ key ];
192
-
232
+
193
233
// type == datatype
194
234
$ criterias_final [$ field ]['type ' ] = $ criterias [$ key ]['datatype ' ];
195
-
235
+
196
236
// unset the number index
197
237
//unset($criterias[$key]);
198
238
}
199
239
200
240
return $ criterias_final ;
201
241
}
202
242
243
+ /**
244
+ * Special item infocom for actions.
245
+ * @param type $criteria_or_action See constants.
246
+ */
247
+ static function getInfocomCriterias (CommonDBTM $ object , $ criteria_or_action ) {
248
+ if ($ criteria_or_action == self ::ACTION ) {
249
+ $ infocom = new Infocom ();
250
+ return $ infocom ->getSearchOptionsToAdd (get_class ($ object ));
251
+ }
252
+
253
+ return array ();
254
+ }
255
+
203
256
function getCriterias () {
204
257
$ criterias = array ();
205
258
206
259
$ asset = self ::getItem ();
207
260
$ asset = new $ asset ();
208
- $ criterias = $ this ->getCriteriasFromObject ($ asset );
209
-
261
+ $ criterias = $ this ->getCriteriasFromObject ($ asset, $ this :: CRITERIA );
262
+
210
263
return $ criterias ;
211
264
}
212
265
@@ -215,7 +268,7 @@ function getActions() {
215
268
216
269
$ asset = self ::getItem ();
217
270
$ asset = new $ asset ();
218
- $ criterias = $ this ->getCriteriasFromObject ($ asset );
271
+ $ criterias = $ this ->getCriteriasFromObject ($ asset, $ this :: ACTION );
219
272
220
273
return $ criterias ;
221
274
}
@@ -227,11 +280,11 @@ function getActions() {
227
280
function processRules (CommonDBTM $ item , $ condition = 0 ) {
228
281
$ criterias = $ this ->getCriteriasFromObject ($ item );
229
282
230
- $ ruleCollection = 'PluginRulesRule ' . $ this ->getItem (). 'Collection ' ;
283
+ $ ruleCollection = 'PluginRulesRule ' . $ this ->getItem () . 'Collection ' ;
231
284
$ ruleCollection = new $ ruleCollection ();
232
285
233
286
$ ruleCollection ->setEntity ($ item ->input ['entities_id ' ]);
234
-
287
+
235
288
$ fields_affected_by_rules = $ ruleCollection ->processAllRules (
236
289
$ item ->input , array (), array (), array (
237
290
'condition ' => $ condition
@@ -242,7 +295,51 @@ function processRules(CommonDBTM $item, $condition = 0) {
242
295
unset($ fields_affected_by_rules ['_rule_process ' ]);
243
296
244
297
foreach ($ fields_affected_by_rules as $ key => $ value ) {
245
- $ item ->input [$ key ] = $ value ;
298
+ // hack for dates
299
+ if ($ value == "{today} " ) {
300
+ $ value = date ("Y-m-d " );
301
+ }
302
+
303
+ if (isset ($ item ->input [$ key ])) {
304
+ $ item ->input [$ key ] = $ value ;
305
+ } else {
306
+ // else is a foreing field
307
+ // parsing to extract item and field
308
+ list ($ subitem , $ field ) = explode ("_ " , $ key , 2 );
309
+ // linkfield with the table
310
+ $ linkfield = $ subitem . "_id " ;
311
+ // extract the last s
312
+ $ subitem = ucfirst (substr ($ subitem , 0 , strlen ($ subitem ) - 1 ));
313
+ // check for caution
314
+ if (class_exists ($ subitem )) {
315
+ $ subitem = new $ subitem ();
316
+ $ input = array ();
317
+
318
+ // hack for infocom
319
+ if (get_class ($ subitem ) == 'Infocom ' ) {
320
+ if (!$ subitem ->getFromDBByQuery ("WHERE items_id = " . $ item ->input ['id ' ] . " AND itemtype = ' " . get_class ($ item ) . "' " )) {
321
+ // new infocom
322
+ $ subitem ->add (array (
323
+ 'items_id ' =>$ item ->input ['id ' ],
324
+ 'itemtype ' =>get_class ($ item )
325
+ ));
326
+ }
327
+ $ input ["id " ] = $ subitem ->fields ['id ' ];
328
+
329
+ // hack for dates, the dates don't must change
330
+ if (strstr ($ field , "date " ) and $ subitem ->fields [$ field ] != "" ) {
331
+ continue ;
332
+ }
333
+
334
+ } else {
335
+ $ input ["id " ] = $ item ->input [$ linkfield ];
336
+ }
337
+
338
+ $ input [$ field ] = $ value ;
339
+ $ subitem ->update ($ input );
340
+ }
341
+ }
246
342
}
247
343
}
344
+
248
345
}
0 commit comments