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; }
function importFile($filename, &$errors = array(), &$warnings = array()) { $filepath = $this->get_file_path(); $dom = $this->check_for_validated_file($filepath, $filename); if (!$dom || $this->isMissingData($filename)) { // Either there is no current validated file // or the validated file has identified missing data // so load the data from source to ensure any missing data is created $dom = new DOMDocument(); if ($this->type == 'CSV') { // Convert CSV into XML and then load as DOMDocument $dom->loadXML($this->csv2xml($filepath . $filename)); } else { // Load XML file into DOMDocument $dom->load($filepath . $filename); } if (!$dom) { $errors[] = 'Cannot load ' . $filepath . $filename . ' file '; return false; } $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; } $this->log_errors = array(); // Process DOMDocument input data to do ETL $this->processNode($dom, null, $dom); if ($this->missing_translation) { $errors[] = 'Missing Translations'; return false; } $this->add_checksum($dom, $filepath, $filename); // Save the expanded DOMDocument as XML $dom->save($filepath . $this->validated_filename($filename)); } // Convert DOMDocument into DataObjectModel Array $data = $this->createDataObjectModel($dom); if (count($this->log_errors) > 0) { $errors = array_merge_recursive($errors, $this->log_errors); } if (!$data) { $errors[] = 'Failed to process ' . $this->name; return false; } $system = system::Instance(); if (!empty($this->process_model)) { if (!empty($this->process_function) && method_exists($this->process_model, $this->process_function)) { if ($this->process_model == get_class($this)) { return $this->{$this->process_function}($data, $errors, $warnings, $this->duplicates_action); } else { // Call the model->function defined in the Data Definition return call_user_func(array($this->process_model, $this->process_function), $data, $errors, $warnings, $this->duplicates_action); } } else { // Data Definition specifies model but not a function // so call the default save_model for the specified model via EdiController::save_model if ($system->controller->save_model($this->process_model, $data, $errors, $warnings, $this->duplicates_action)) { $id_value = $system->controller->saved_model->{$system->controller->saved_model->idField}; $identifier_field = $system->controller->saved_model->identifierField; $identifier_value = $system->controller->saved_model->getIdentifierValue(); return array('internal_id' => $id_value, 'internal_identifier_field' => $identifier_field, 'internal_identifier_value' => $identifier_value); } else { return false; } } } else { // No sepcific model defined so call the default save_model function for the calling $data_in = $this->convertData($data); $db = DB::Instance(); if ($this->abort_action == 'A') { // Set encompassing transaction if need to abort all on error $db->StartTrans(); } $flash = Flash::Instance(); foreach ($data_in as $model_data) { // Calls EdiController::save_model if (!$system->controller->save_model(key($model_data), $model_data, $errors, $warnings, $this->duplicates_action)) { if ($this->abort_action != 'S') { // Abort on Error if ($this->abort_action == 'A') { // Rollback all imported records $db->FailTrans(); $db->CompleteTrans(); } // If not abort_all_on_error, // then imported records up to this point will be saved return false; } // Skip error record and continue, so reset errors array $warnings[] = 'Ignored ' . $db->ErrorMsg(); $warnings = array_merge($warnings, $errors); $errors = array(); $flash->clear(); } } if ($this->abort_action == 'A') { // Everything is OK here, just need to complete transaction // if mode is set to abort all on error $db->CompleteTrans(); } return array('internal_id' => '', 'internal_identifier_field' => '', 'internal_identifier_value' => ''); } }