public function _new()
 {
     parent::_new();
     $datadefdetail = $this->_uses[$this->modeltype];
     if ($datadefdetail->isLoaded()) {
         $cc = new ConstraintChain();
         $cc->add(new Constraint('data_definition_id', '=', $datadefdetail->data_definition_id));
         $this->view->set('parent', $datadefdetail->getAll($cc));
     } elseif (isset($this->_data['data_definition_id'])) {
         $datadefdetail = new DataDefinitionDetail();
         $cc = new ConstraintChain();
         $cc->add(new Constraint('data_definition_id', '=', $this->_data['data_definition_id']));
         $this->view->set('parent', $datadefdetail->getAll($cc));
     } else {
         $this->view->set('parent', array());
     }
 }
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;
 }
Example #3
0
 private function csv2xml($filepath)
 {
     $name = trim(str_replace(' ', '_', $this->name));
     $xml = new SimpleXMLElement("<{$name}></{$name}>");
     $ddd1 = new DataDefinitionDetail();
     $ddd1->loadBy(array('data_definition_id', 'element'), array($this->id, $this->name));
     $ddd2 = new DataDefinitionDetail();
     $ddd2->loadBy(array('data_definition_id', 'parent_id'), array($this->id, $ddd1->id));
     $ddd3 = new DataDefinitionDetail();
     $tags = array_values($ddd3->getAllByDef($this->id, $ddd2->id));
     if ($handle = fopen($filepath, 'r')) {
         $line = fgetcsv($handle, 0, $this->field_separator, $this->text_delimiter);
         $line_counter = 0;
         while (!feof($handle)) {
             $line_counter++;
             $xml_line = $xml->addChild($ddd2->element);
             $xml_line->addAttribute('line_no', $line_counter);
             foreach ($line as $index => $field) {
                 if (!empty($tags[$index])) {
                     $xml_line->addChild($tags[$index], str_replace('&', '&#38;', $field));
                 }
             }
             $line = fgetcsv($handle, 0, $this->field_separator, $this->text_delimiter);
         }
     }
     fclose($handle);
     return $xml->asXML();
 }