コード例 #1
0
 /**
  * Export rules in a xml format
  *
  * @param items array the input data to transform to xml
  *
  * @since version 0.85
  *
  * @return nothing, send attachment to browser
  **/
 static function exportRulesToXML($items = array())
 {
     if (!count($items)) {
         return false;
     }
     $rulecollection = new self();
     $rulecritera = new RuleCriteria();
     $ruleaction = new RuleAction();
     //create xml
     $xmlE = new SimpleXMLElement('<rules/>');
     //parse all rules
     foreach ($items as $key => $ID) {
         $rulecollection->getFromDB($ID);
         if (!class_exists($rulecollection->fields['sub_type'])) {
             continue;
         }
         $rule = new $rulecollection->fields['sub_type']();
         unset($rulecollection->fields['id']);
         unset($rulecollection->fields['date_mod']);
         $name = Dropdown::getDropdownName("glpi_entities", $rulecollection->fields['entities_id']);
         $rulecollection->fields['entities_id'] = $name;
         //add root node
         $xmlERule = $xmlE->addChild('rule');
         //convert rule direct indexes in XML
         foreach ($rulecollection->fields as $key => $val) {
             $xmlERule->{$key} = $val;
         }
         //find criterias
         $criterias = $rulecritera->find("`rules_id` = '{$ID}'");
         foreach ($criterias as &$criteria) {
             unset($criteria['id']);
             unset($criteria['rules_id']);
             $available_criteria = $rule->getCriterias();
             $crit = $criteria['criteria'];
             if (self::isCriteraADropdown($available_criteria, $criteria['condition'], $crit)) {
                 $criteria['pattern'] = Html::clean(Dropdown::getDropdownName($available_criteria[$crit]['table'], $criteria['pattern']));
             }
             //convert criterias in XML
             $xmlECritiera = $xmlERule->addChild('rulecriteria');
             foreach ($criteria as $key => $val) {
                 $xmlECritiera->{$key} = $val;
             }
         }
         //find actions
         $actions = $ruleaction->find("`rules_id` = '{$ID}'");
         foreach ($actions as &$action) {
             unset($action['id']);
             unset($action['rules_id']);
             //process FK (just in case of "assign" action)
             if ($action['action_type'] == "assign" && strpos($action['field'], '_id') !== false && !($action['field'] == "entities_id" && $action['value'] == 0)) {
                 $field = $action['field'];
                 if ($action['field'][0] == "_") {
                     $field = substr($action['field'], 1);
                 }
                 $table = getTableNameForForeignKeyField($field);
                 $action['value'] = Html::clean(Dropdown::getDropdownName($table, $action['value']));
             }
             //convert actions in XML
             $xmlEAction = $xmlERule->addChild('ruleaction');
             foreach ($action as $key => $val) {
                 $xmlEAction->{$key} = $val;
             }
         }
     }
     //convert SimpleXMLElement to xml string
     $xml = $xmlE->asXML();
     //send attachment to browser
     header('Content-type: application/xml');
     header('Content-Disposition: attachment; filename="rules.xml"');
     echo $xml;
     //exit;
 }