/** * Import data form a CSV file * @param String filePath * @param Array fieldsData * @param Boolean useFirstLine * @param String delimiter * @param String dateFormat * @return Array */ protected function importFromCSV($filePath, $fieldsData, $useFirstLine, $delimiter, $dateFormat) { $metaData = array(); $metaData["totalRecords"] = 0; $fileHandle = OpenCSVFile($filePath, $delimiter); if ($fileHandle === FALSE) { return $metaData; } $keys = $this->pSet->getTableKeys(); $autoinc = $this->hasAutoincImportFields($fieldsData); $errorMessages = array(); $unprocessedData = array(); $addedRecords = 0; $updatedRecords = 0; if ($this->connection->dbType == nDATABASE_MSSQLServer && $autoinc) { $sql = "SET IDENTITY_INSERT " . $this->connection->addTableWrappers($this->strOriginalTableName) . " ON"; $this->connection->exec($sql); } $row = 0; // parse records from file while (($elems = GetCSVLine($fileHandle, 1000000, $delimiter)) !== FALSE) { if ($row === 0 && (!$useFirstLine || $useFirstLine === "false")) { $row++; continue; } $fieldsValuesData = array(); foreach ($elems as $idx => $elem) { if (!isset($fieldsData[$idx])) { continue; } $importFieldName = $fieldsData[$idx]["fName"]; $fType = $fieldsData[$idx]["type"]; if (IsDateFieldType($fType)) { $value = localdatetime2db($elem, $dateFormat); if ($value !== FALSE && strlen($value) && $value != 'null') { $fieldsValuesData[$importFieldName] = $value; } else { $fieldsValuesData[$importFieldName] = NULL; } } else { $fieldsValuesData[$importFieldName] = $elem; } } $this->importRecord($fieldsValuesData, $keys, $autoinc, $addedRecords, $updatedRecords, $errorMessages, $unprocessedData); $metaData["totalRecords"] = $metaData["totalRecords"] + 1; $row++; } CloseCSVFile($fileHandle); if ($this->connection->dbType == nDATABASE_MSSQLServer && $autoinc) { $sql = "SET IDENTITY_INSERT " . $this->connection->addTableWrappers($this->strOriginalTableName) . " OFF"; $this->connection->exec($sql); } $metaData["addedRecords"] = $addedRecords; $metaData["updatedRecords"] = $updatedRecords; $metaData["errorMessages"] = $errorMessages; $metaData["unprocessedData"] = $unprocessedData; return $metaData; }
function ImportFromCSV($uploadfile, $strOriginalTableName, $ext, $keys, &$keys_present, &$total_records, &$error_message, &$goodlines, $pageObject, $cipherer) { global $conn, $gSettings; $ret = 1; $fields = array(); $fields = getImportCVSFields($uploadfile); // populate field names array for ($j=0;$j<count($fields);$j++) { $fields[$j] = $fields[$j]; if(substr($fields[$j],0,1)=="\"" && substr($fields[$j],-1)=="\"") $fields[$j]=substr($fields[$j],1,-1); } $fields = getFieldNamesByHeaders($fields, $strOriginalTableName, $ext); if($fields == null) // if error happened return; $keys_present=1; for($k=0; $k<count($keys); $k++) { if (!in_array(RemoveFieldWrappers($keys[$k]),$fields)) { $keys_present=0; break; } } $autoinc = false; if(in_array("id",$fields)) $autoinc=true; if(GetDatabaseType() == 2 && $autoinc) { $sql="SET IDENTITY_INSERT ".AddTableWrappers($strOriginalTableName)." ON"; db_exec($sql,$conn); } $total_records = 0; $line = ""; $row = 0; // parse records from file if (($handle = OpenCSVFile($uploadfile)) !== FALSE) { while (($data = GetCSVLine($handle, 1000000, ",")) !== FALSE) { // first rec contain only fields names if ($row === 0) { $row++; continue; } $arr = array(); foreach($data as $key=>$val) { $type = $gSettings->getFieldType($fields[$key]); if(IsDateFieldType($type)) { $value = localdatetime2db($val); if ( $value !== FALSE && strlen($value) && $value != 'null' ) $arr[$fields[$key]] = $value; else $arr[$fields[$key]] = NULL; } elseif(IsTimeType($type)) { $value = localtime2db($val); if ( $value !== FALSE && strlen($value) && !is_null($val) && strlen($val) ) $arr[$fields[$key]] = $value; else $arr[$fields[$key]] = NULL; } else $arr[$fields[$key]] = $val; } $ret = InsertRecord($arr, $row, $error_message, $goodlines, $keys, $keys_present, $strOriginalTableName, $pageObject, $cipherer, $autoinc); $row++; } CloseCSVFile($handle); } $total_records = $row-1; if(GetDatabaseType() == 2 && $autoinc) { $sql="SET IDENTITY_INSERT ".AddTableWrappers($strOriginalTableName)." OFF"; db_exec($sql,$conn); } return $ret; }