/**
  * import rules in glpi after user validation
  *
  * @since version 0.85
  *
  * @return true if all ok
  **/
 static function processImportRules()
 {
     global $DB;
     $ruleCriteria = new RuleCriteria();
     $ruleAction = new RuleAction();
     $entity = new Entity();
     //get session vars
     $rules = $_SESSION['glpi_import_rules'];
     $rules_refused = $_SESSION['glpi_import_rules_refused'];
     $rr_keys = array_keys($rules_refused);
     unset($_SESSION['glpi_import_rules']);
     unset($_SESSION['glpi_import_rules_refused']);
     // unset all refused rules
     foreach ($rules['rule'] as $k_rule => &$rule) {
         if (in_array($k_rule, $rr_keys)) {
             //Do not process rule with actions or criterias refused
             if (isset($rules_refused[$k_rule]['criterias']) || isset($rules_refused[$k_rule]['actions'])) {
                 unset($rules['rule'][$k_rule]);
             } else {
                 // accept rule with only entity not found (change entity)
                 $rule['entities_id'] = $_REQUEST['new_entities'][$rule['uuid']];
             }
         }
     }
     //import all right rules
     while (!empty($rules['rule'])) {
         $current_rule = array_shift($rules['rule']);
         $add_criteria_and_actions = false;
         $params = array();
         $itemtype = $current_rule['sub_type'];
         $item = new $itemtype();
         //Find a rule by it's uuid
         $found = $item->find("`uuid`='" . $current_rule['uuid'] . "'");
         $params = Toolbox::addslashes_deep($current_rule);
         unset($params['rulecriteria']);
         unset($params['ruleaction']);
         if (!$item->isEntityAssign()) {
             $params['entities_id'] = 0;
         } else {
             $entities_found = $entity->find("completename = '" . $rule['entities_id'] . "'");
             if (!empty($entities_found)) {
                 $entity_found = array_shift($entities_found);
                 $params['entities_id'] = $entity_found['id'];
             } else {
                 $params['entities_id'] = 0;
             }
         }
         foreach (array('is_recursive', 'is_active') as $field) {
             //Should not be necessary but without it there's an sql error...
             if (!isset($params[$field]) || $params[$field] == '') {
                 $params[$field] = 0;
             }
         }
         //if uuid not exist, create rule
         if (empty($found)) {
             //Manage entity
             $params['_add'] = true;
             $rules_id = $item->add($params);
             if ($rules_id) {
                 Event::log($rules_id, "rules", 4, "setup", sprintf(__('%1$s adds the item %2$s'), $_SESSION["glpiname"], $rules_id));
                 $add_criteria_and_actions = true;
             }
         } else {
             //if uuid exists, then update the rule
             $tmp = array_shift($found);
             $params['id'] = $tmp['id'];
             $params['_update'] = true;
             $rules_id = $tmp['id'];
             if ($item->update($params)) {
                 Event::log($rules_id, "rules", 4, "setup", sprintf(__('%s updates an item'), $_SESSION["glpiname"]));
                 //remove all dependent criterias and action
                 $ruleCriteria->deleteByCriteria(array("rules_id" => $rules_id));
                 $ruleAction->deleteByCriteria(array("rules_id" => $rules_id));
                 $add_criteria_and_actions = true;
             }
         }
         if ($add_criteria_and_actions) {
             //Add criteria
             if (isset($current_rule['rulecriteria'])) {
                 foreach ($current_rule['rulecriteria'] as $criteria) {
                     $criteria['rules_id'] = $rules_id;
                     //fix array in value key
                     //(simplexml bug, empty xml node are converted in empty array instead of null)
                     if (is_array($criteria['pattern'])) {
                         $criteria['pattern'] = null;
                     }
                     $criteria = Toolbox::addslashes_deep($criteria);
                     $ruleCriteria->add($criteria);
                 }
             }
             //Add actions
             if (isset($current_rule['ruleaction'])) {
                 foreach ($current_rule['ruleaction'] as $action) {
                     $action['rules_id'] = $rules_id;
                     //fix array in value key
                     //(simplexml bug, empty xml node are converted in empty array instead of null)
                     if (is_array($action['value'])) {
                         $action['value'] = null;
                     }
                     $action = Toolbox::addslashes_deep($action);
                     $ruleAction->add($action);
                 }
             }
         }
     }
     Session::addMessageAfterRedirect(__('Successful importation'));
     return true;
 }