Example #1
0
 protected function processNode($node, $_dtd_element_id, $dom)
 {
     // Converts XML to DOM document format for display on screen
     foreach ($node->childNodes as $child_node) {
         if ($child_node->childNodes && $child_node->firstChild->nodeType == 3) {
             // Node has children but the child is a text node, so get the value
             // Might need to be aware of whitespace characters returning as empty text node
             // instead of the required value
             $node_value = trim($child_node->firstChild->nodeValue);
         } else {
             $node_value = '';
         }
         $external_code = $node_value;
         $defdetail = new DataDefinitionDetail();
         $defdetail->loadBy(array('parent_id', 'element'), array($_dtd_element_id, $child_node->tagName));
         $datamap = $defdetail->data_map;
         $translation = $datamaprule = $datamapdetail = '';
         if (!is_null($defdetail->data_mapping_rule_id)) {
             $datamaprule = new DataMappingRule();
             $datamaprule->load($defdetail->data_mapping_rule_id);
             $translation = $node_value;
             if ($datamaprule->isLoaded()) {
                 if (!is_null($datamaprule->external_format)) {
                     $validate_errors = array();
                     $translation = $datamaprule->validate($node_value, $validate_errors);
                     if (count($validate_errors) > 0) {
                         $this->log_errors[] = $key . ' "' . $node_value . '" : ' . implode(',', $validate_errors);
                     }
                 }
                 $datamapdetail = new DataMappingDetail();
                 // Do the mapping translation; returns original value if no translation found
                 $translation = $datamapdetail->translateCode($datamaprule->id, $translation, 'IN');
                 // Need to check if the translated value exists and return display value
                 if ($datamaprule->isLoaded() && !is_null($datamaprule->data_mapping_id)) {
                     $mapvalue = new DataMapping();
                     $mapvalue->load($datamaprule->data_mapping_id);
                     $display_value = $mapvalue->getValue($translation);
                 }
                 if ($display_value === FALSE) {
                     $display_value = 'Missing Translation';
                     $this->missing_translation = true;
                 }
             }
         } elseif ($datamap->isLoaded()) {
             if ($child_node->childNodes && !empty($child_node->firstChild->nodeType) && $child_node->firstChild->nodeType != 3) {
                 $translation = serialize('<' . XML_ROOT . '>' . $dom->saveXML($child_node) . '</' . XML_ROOT . '>');
             } else {
                 $translation = $this->translateCode($datamap, $node_value, $child_node->tagName);
                 if (empty($translation) && !is_null($defdetail->default_value)) {
                     $translation = $defdetail->default_value;
                 }
                 $display_value = $node_value;
             }
         } elseif ($child_node->childNodes) {
             if (empty($node_value)) {
                 $node_value = 'No value';
             }
             $display_value = 'not used';
         }
         if (!is_null($datamap->internal_type) && !is_null($datamap->internal_attribute)) {
             if (!is_null($defdetail->data_mapping_rule_id)) {
                 $attribute = $dom->createAttribute('data_mapping_rule_id');
                 $attribute->value = $defdetail->data_mapping_rule_id;
                 $child_node->appendChild($attribute);
             }
             if (!is_null($datamapdetail->id)) {
                 $attribute = $dom->createAttribute('id');
                 $attribute->value = $datamapdetail->id;
                 $child_node->appendChild($attribute);
             }
             $attribute = $dom->createAttribute('external_code');
             $attribute->value = str_replace('&', '&#38;', $external_code);
             $child_node->appendChild($attribute);
             $attribute = $dom->createAttribute('internal_code');
             $attribute->value = str_replace('&', '&#38;', $translation);
             $child_node->appendChild($attribute);
             $attribute = $dom->createAttribute('display_value');
             $attribute->value = str_replace('&', '&#38;', $display_value);
             $child_node->appendChild($attribute);
             $attribute = $dom->createAttribute('internal_type');
             $attribute->value = $datamap->internal_type;
             $child_node->appendChild($attribute);
             $attribute = $dom->createAttribute('internal_attribute');
             $attribute->value = $datamap->internal_attribute;
             $child_node->appendChild($attribute);
         }
         if ($child_node->childNodes && $child_node->firstChild->nodeType != 3) {
             $this->processNode($child_node, $defdetail->{$defdetail->idField}, $dom);
         }
     }
 }
Example #2
0
 function exportFile($filename, $data, &$errors = array())
 {
     $defdetail = new DataDefinitionDetail();
     $defdetail->loadBy(array('data_definition_id', 'element'), array($this->id, $this->name));
     if (!$defdetail->isLoaded()) {
         $errors[] = 'Cannot find Data Definition for ' . $this->name;
         return false;
     }
     if (is_null($defdetail->data_map->internal_type)) {
         $model = new $this->process_model();
     } else {
         $model = new $defdetail->data_map->internal_type();
     }
     if ($model instanceof DataObject) {
         $model->load($data);
         //			if ($model->isLoaded() && $model->isField('print_count')) {
         //				$model->print_count=$model->print_count+1;
         //				$model->date_printed=date(DATE_FORMAT);
         //				$model->save();
         //			}
         $result = array();
         if ($model->isLoaded()) {
             $logdata = array('internal_id' => $model->{$model->idField}, 'internal_identifier_field' => $model->identifierField, 'internal_identifier_value' => $model->getIdentifierValue());
         }
     } else {
         // This is a collection so need to load the collection for export
         // The id's are in the internal_code field of the data_mapping_details table
         // identified by the data_mapping_rule_id value from data_definition_detail
         $sh = new SearchHandler($model, false);
         if (!is_null($defdetail->data_mapping_rule_id)) {
             $datadetails = new DataMappingDetail();
             $datadetails->identifierField = 'internal_code';
             $cc = new ConstraintChain();
             $cc->add(new Constraint('data_mapping_rule_id', '=', $defdetail->data_mapping_rule_id));
             $keys = $datadetails->getAll($cc, true);
         } else {
             $keys = array();
         }
         if (!empty($keys)) {
             $sh->addConstraint(new Constraint('id', 'in', '(' . implode(',', $keys) . ')'));
         }
         $model->load($sh);
         $result = array();
     }
     if (!$model) {
         $errors[] = 'Failed to extract data for ' . $this->name;
         return false;
     }
     $array = array();
     if ($model instanceof DataObject) {
         $array = $this->createArray($defdetail, $model);
     } else {
         foreach ($model as $line) {
             $array = $this->createArray($defdetail, $line);
         }
     }
     $data = '';
     switch ($this->type) {
         case 'HTTP':
             foreach ($array as $key => $line) {
                 if (is_array(current($line))) {
                     foreach ($line as $subline) {
                         $data .= http_build_query($subline) . "\n";
                     }
                 } else {
                     $data .= http_build_query($line) . "\n";
                 }
             }
             break;
         case 'XML':
             $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><' . $this->name . '/>');
             $data = array2xml($array, $xml);
             break;
         default:
             $errors[] = $this->type . ' data type not supported';
     }
     $handle = fopen(DATA_ROOT . 'company' . EGS_COMPANY_ID . DIRECTORY_SEPARATOR . $this->working_folder . DIRECTORY_SEPARATOR . $filename, 'w');
     if (!$handle || !fwrite($handle, $data)) {
         $errors[] = 'Error writing ' . $filename . ' to ' . $this->working_folder;
     }
     $logdata['name'] = $filename;
     $logdata['action'] = 'E';
     $this->writeLog($logdata, $errors);
     if (count($errors) > 0) {
         return false;
     }
     return true;
 }
 public function _new()
 {
     $datamapdetail = $this->_uses[$this->modeltype];
     if ($datamapdetail->isLoaded()) {
         $this->_data['data_mapping_rule_id'] = $datamapdetail->data_mapping_rule_id;
         $this->_data['parent_id'] = $datamapdetail->parent_id;
         $datamappingrule = $datamapdetail->data_map_rule;
     } elseif (isset($this->_data['data_mapping_rule_id'])) {
         $datamappingrule = new DataMappingRule();
         $datamappingrule->load($this->_data['data_mapping_rule_id']);
     }
     if (!$this->CheckParams('data_mapping_rule_id')) {
         sendBack();
     }
     parent::_new();
     $cc = new ConstraintChain();
     $datamap = $datamappingrule->data_map;
     $options = array();
     if (!empty($this->_data['parent_id'])) {
         // get the parent mapping detail
         $parent_detail = new DataMappingDetail();
         $parent_detail->load($this->_data['parent_id']);
         // get the parent mapping
         $parent_mapping = $parent_detail->data_map_rule->data_map;
         $parent_id = $parent_detail->internal_code;
         // Rules need simplifying and/or explaining
         // perhaps move these to the DataMapping model
         $x = $datamap->getModel();
         $hasmany = $x->getHasMany();
         if (isset($hasmany[$datamap->internal_attribute])) {
             $x->load($parent_detail->internal_code);
             foreach ($x->{$datamap->internal_attribute} as $detail) {
                 $options[$detail->{$detail->idField}] = $detail->{$detail->getIdentifier()};
             }
         } elseif ($parent_mapping->isHasOne()) {
             $hasone = $parent_mapping->getHasOne();
             $x = new $hasone['model']();
             $hasmany = $x->getHasMany();
             if (isset($hasmany[$datamap->internal_attribute])) {
                 $x->load($parent_detail->internal_code);
                 foreach ($x->{$datamap->internal_attribute} as $detail) {
                     $options[$detail->{$detail->idField}] = $detail->{$detail->getIdentifier()};
                 }
             }
         } else {
             $belongsto = $x->belongsTo;
             foreach ($belongsto as $parent) {
                 if ($parent['model'] == $parent_mapping->internal_type) {
                     $cc->add(new Constraint($parent['field'], '=', $parent_id));
                     break;
                 }
             }
         }
         // If no constraints found from fk definitions and the parent class is the
         // same as the child class, then the parent is the child i.e. data specific
         // to the parent
         if (empty($cc->constraints) && $parent_mapping->internal_type == get_class($x)) {
             $cc->add(new Constraint($x->idField, '=', $parent_id));
         }
     }
     //		elseif (!is_null($datamappingrule->parent_id)) {
     // Get the list of parent options
     // from the parent mapping details via the parent mapping rule
     $parentmappingrule = new datamappingrule();
     $parentmappingrule->load($datamappingrule->parent_id);
     $parent_options = array();
     foreach ($parentmappingrule->data_translations as $parent_detail) {
         $parent_options[$parent_detail->id] = $parent_detail->displayValue();
     }
     $this->view->set('parent_options', $parent_options);
     $this->view->set('parent_label', $datamappingrule->parent);
     //		}
     if (is_null($datamap->internal_type)) {
         $errors[] = 'No Internal Type defined for Data Mapping ' . $datamappingrule->name;
     } else {
         if (empty($options)) {
             $options = $datamap->getDataOptions($datamap->getModel(), $cc);
         }
         $this->view->set('data_mapping_rule_id', $this->_data['data_mapping_rule_id']);
         if (!$datamap->isLoaded()) {
             $errors[] = 'Invalid Data Type';
         } else {
             if (!is_null($datamap->internal_type)) {
                 $model = $datamap->getModel();
                 if (!$model) {
                     $errors[] = 'Data Mapping for Internal Data Type is invalid';
                 } else {
                     $this->view->set('model', $model);
                     $this->view->set('internal_codes', $options);
                     $this->view->set('internal_name', $datamappingrule->name);
                 }
             }
         }
     }
     $datatranslations = new DataMappingDetailCollection(new DataMappingDetail());
     $sh = $this->setSearchHandler($datatranslations);
     $sh->addConstraint(new Constraint('data_mapping_rule_id', '=', $this->_data['data_mapping_rule_id']));
     if (isset($this->_data['parent_id'])) {
         $sh->addConstraint(new Constraint('parent_id', '=', $this->_data['parent_id']));
     }
     parent::index($datatranslations, $sh);
     $this->view->set('clickaction', 'view');
 }