/** * CSV File parsing methods * * @param $fic * @param $data * @param $encoding (default 1) **/ static function parseLine($fic, $data, $encoding = 1) { global $DB; $csv = array(); $num = count($data); for ($c = 0; $c < $num; $c++) { //If field is not the last, or if field is the last of the line and is not empty if ($c < $num - 1 || $c == $num - 1 && $data[$num - 1] != PluginDatainjectionCommonInjectionLib::EMPTY_VALUE) { $tmp = trim($DB->escape($data[$c])); switch ($encoding) { //If file is ISO8859-1 : encode the datas in utf8 case PluginDatainjectionBackend::ENCODING_ISO8859_1: if (!Toolbox::seems_utf8($tmp)) { $csv[0][] = utf8_encode($tmp); } else { $csv[0][] = $tmp; } break; case PluginDatainjectionBackend::ENCODING_UFT8: $csv[0][] = $tmp; break; default: //PluginDatainjectionBackend :: ENCODING_AUTO : $csv[0][] = PluginDatainjectionBackend::toUTF8($tmp); } } } return $csv; }
/** * Try to parse an input file * * @return true if the file is a CSV file **/ function isFileCorrect() { $field_in_error = false; //Get CSV file first line $header = PluginDatainjectionBackend::getHeader($this->injectionData, $this->specific_model->isHeaderPresent()); //If file columns don't match number of mappings in DB $nb = count($this->getMappings()); if ($nb != count($header)) { $error_message = __('The number of columns of the file is incorrect.', 'datainjection') . "\n"; $error_message .= sprintf(_n('%d awaited column', '%d awaited columns', $nb, 'datainjection'), $nb) . "\n"; $error_message .= sprintf(_n('%d found column', '%d found columns', count($header), 'datainjection'), count($header)); return array('status' => PluginDatainjectionCommonInjectionLib::FAILED, 'field_in_error' => false, 'error_message' => $error_message); } //If no header in the CSV file, exit method if (!$this->specific_model->isHeaderPresent()) { return array('status' => PluginDatainjectionCommonInjectionLib::SUCCESS, 'field_in_error' => false, 'error_message' => ''); } $error = array('status' => PluginDatainjectionCommonInjectionLib::SUCCESS, 'field_in_error' => false, 'error_message' => ''); //Check each mapping to be sure it has exactly the same name foreach ($this->getMappings() as $key => $mapping) { if (!isset($header[$key])) { $error['status'] = PluginDatainjectionCommonInjectionLib::FAILED; $error['field_in_error'] = $key; } else { //If name of the mapping is not equal in the csv file header and in the DB $name_from_file = trim(mb_strtoupper(stripslashes($header[$mapping->getRank()]), 'UTF-8')); $name_from_db = trim(mb_strtoupper(stripslashes($mapping->getName()), 'UTF-8')); if ($name_from_db != $name_from_file) { if ($error['error_message'] == '') { $error['error_message'] = __('At least one column is incorrect', 'datainjection'); } $error['status'] = PluginDatainjectionCommonInjectionLib::FAILED; $error['field_in_error'] = false; $error['error_message'] .= "<br>" . sprintf(__('%1$s: %2$s'), __('Into the file', 'datainjection'), $name_from_file) . "\n"; $error['error_message'] .= sprintf(__('%1$s: %2$s'), __('From the model', 'datainjection'), $name_from_db); } } } return $error; }