public function testWriteErrorRecord() { $file = SugarTestImportUtilities::createFile(3, 2); $importFile = new ImportFile($file, ',', '"', TRUE, FALSE); $row = $importFile->getNextRow(); $importFile->writeErrorRecord(); $fp = sugar_fopen(ImportCacheFiles::getErrorRecordsWithoutErrorFileName(), 'r'); $errorrecordrow = fgetcsv($fp); fclose($fp); $this->assertEquals($row, $errorrecordrow); }
/** * Actually split the file into parts * * @param string $delimiter * @param string $enclosure * @param bool $has_header true if file has a header row */ public function splitSourceFile($delimiter = ',', $enclosure = '"', $has_header = false) { if (!$this->fileExists()) { return false; } $importFile = new ImportFile($this->_sourceFile, $delimiter, $enclosure, false); $filecount = 0; $fw = sugar_fopen("{$this->_sourceFile}-{$filecount}", "w"); $count = 0; // skip first row if we have a header row if ($has_header && $importFile->getNextRow()) { // mark as duplicate to stick header row in the dupes file $importFile->markRowAsDuplicate(); // same for error records file $importFile->writeErrorRecord(); } while ($row = $importFile->getNextRow()) { // after $this->_recordThreshold rows, close this import file and goto the next one if ($count >= $this->_recordThreshold) { fclose($fw); $filecount++; $fw = sugar_fopen("{$this->_sourceFile}-{$filecount}", "w"); $count = 0; } // Bug 25119: Trim the enclosure string to remove any blank spaces that may have been added. $enclosure = trim($enclosure); if (!empty($enclosure)) { foreach ($row as $key => $v) { $row[$key] = preg_replace("/{$enclosure}/", "{$enclosure}{$enclosure}", $v); } } $line = $enclosure . implode($enclosure . $delimiter . $enclosure, $row) . $enclosure . PHP_EOL; //Would normally use fputcsv() here. But when enclosure character is used and the field value doesn't include delimiter, enclosure, escape character, "\n", "\r", "\t", or " ", php default function 'fputcsv' will not use enclosure for this string. fputs($fw, $line); $count++; } fclose($fw); $this->_fileCount = $filecount; $this->_recordCount = $filecount * $this->_recordThreshold + $count; // increment by one to get true count of files created ++$this->_fileCount; }
/** * Actually split the file into parts * * @param string $delimiter * @param string $enclosure * @param bool $has_header true if file has a header row */ public function splitSourceFile($delimiter = ',', $enclosure = '"', $has_header = false) { if (!$this->fileExists()) { return false; } $importFile = new ImportFile($this->_sourceFile, $delimiter, $enclosure, false); $filecount = 0; $fw = sugar_fopen("{$this->_sourceFile}-{$filecount}", "w"); $count = 0; // skip first row if we have a header row if ($has_header && $importFile->getNextRow()) { // mark as duplicate to stick header row in the dupes file $importFile->markRowAsDuplicate(); // same for error records file $importFile->writeErrorRecord(); } while ($row = $importFile->getNextRow()) { // after $this->_recordThreshold rows, close this import file and goto the next one if ($count >= $this->_recordThreshold) { fclose($fw); $filecount++; $fw = sugar_fopen("{$this->_sourceFile}-{$filecount}", "w"); $count = 0; } // Bug 25119: Trim the enclosure string to remove any blank spaces that may have been added. $enclosure = trim($enclosure); if (empty($enclosure)) { fputs($fw, implode($delimiter, $row) . PHP_EOL); } else { fputcsv($fw, $row, $delimiter, $enclosure); } $count++; } fclose($fw); $this->_fileCount = $filecount; $this->_recordCount = $filecount * $this->_recordThreshold + $count; // increment by one to get true count of files created ++$this->_fileCount; }