_recordThreshold = $recordThreshold; $this->_sourceFile = $source; } /** * Returns true if the filename given exists and is readable * * @return bool */ public function fileExists() { if ( !is_file($this->_sourceFile) || !is_readable($this->_sourceFile)) { return false; } return true; } /** * 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; } /** * Return the count of records in the file, if it's been processed with splitSourceFile() * * @return int count of records in the file */ public function getRecordCount() { if ( !isset($this->_recordCount) ) return false; return $this->_recordCount; } /** * Return the count of files created by the split, if it's been processed with splitSourceFile() * * @return int count of files created by the split */ public function getFileCount() { if ( !isset($this->_fileCount) ) return false; return $this->_fileCount; } /** * Return file name of one of the split files * * @param int $filenumber which split file we want * * @return string filename */ public function getSplitFileName( $filenumber = 0 ) { if ( $filenumber >= $this->getFileCount()) return false; return "{$this->_sourceFile}-{$filenumber}"; } }