2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
4 /*********************************************************************************
5 * SugarCRM Community Edition is a customer relationship management program developed by
6 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Affero General Public License version 3 as published by the
10 * Free Software Foundation with the addition of the following permission added
11 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
12 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
13 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
20 * You should have received a copy of the GNU Affero General Public License along with
21 * this program; if not, see http://www.gnu.org/licenses or write to the Free
22 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
26 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
28 * The interactive user interfaces in modified source and object code versions
29 * of this program must display Appropriate Legal Notices, as required under
30 * Section 5 of the GNU Affero General Public License version 3.
32 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
33 * these Appropriate Legal Notices must retain the display of the "Powered by
34 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
35 * technical reasons, the Appropriate Legal Notices must display the words
36 * "Powered by SugarCRM".
37 ********************************************************************************/
39 /*********************************************************************************
41 * Description: Class to handle splitting a file into seperate parts
42 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
43 * All Rights Reserved.
44 ********************************************************************************/
46 class ImportFileSplitter
49 * Filename of file we are splitting
54 * Count of files that we split the $_sourceFile into
59 * Count of records in $_sourceFile
61 private $_recordCount;
64 * Maximum number of records per file
66 private $_recordThreshold;
71 * @param string $source filename we are splitting
73 public function __construct(
75 $recordThreshold = 1000
78 // sanitize crazy values to the default value
79 if ( !is_int($recordThreshold) || $recordThreshold < 1 ){
80 //if this is not an int but is still a
81 //string representation of a number, then cast to an int
82 if(!is_int($recordThreshold) && is_numeric($recordThreshold)){
83 //cast the string to an int
84 $recordThreshold = (int)$recordThreshold;
86 //if not a numeric string, or less than 1, then default to 100
87 $recordThreshold = 100;
90 $this->_recordThreshold = $recordThreshold;
91 $this->_sourceFile = $source;
95 * Returns true if the filename given exists and is readable
99 public function fileExists()
101 if ( !is_file($this->_sourceFile) || !is_readable($this->_sourceFile)) {
109 * Actually split the file into parts
111 * @param string $delimiter
112 * @param string $enclosure
113 * @param bool $has_header true if file has a header row
115 public function splitSourceFile(
121 if (!$this->fileExists())
123 $importFile = new ImportFile($this->_sourceFile,$delimiter,$enclosure,false);
125 $fw = sugar_fopen("{$this->_sourceFile}-{$filecount}","w");
127 // skip first row if we have a header row
128 if ( $has_header && $importFile->getNextRow() ) {
129 // mark as duplicate to stick header row in the dupes file
130 $importFile->markRowAsDuplicate();
131 // same for error records file
132 $importFile->writeErrorRecord();
134 while ( $row = $importFile->getNextRow() ) {
135 // after $this->_recordThreshold rows, close this import file and goto the next one
136 if ( $count >= $this->_recordThreshold ) {
139 $fw = sugar_fopen("{$this->_sourceFile}-{$filecount}","w");
142 // Bug 25119: Trim the enclosure string to remove any blank spaces that may have been added.
143 $enclosure = trim($enclosure);
144 if(!empty($enclosure)) {
145 foreach($row as $key => $v){
146 $row[$key] =preg_replace("/$enclosure/","$enclosure$enclosure", $v);
149 $line = $enclosure.implode($enclosure.$delimiter.$enclosure, $row).$enclosure.PHP_EOL;
150 //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.
156 $this->_fileCount = $filecount;
157 $this->_recordCount = ($filecount * $this->_recordThreshold) + $count;
158 // increment by one to get true count of files created
163 * Return the count of records in the file, if it's been processed with splitSourceFile()
165 * @return int count of records in the file
167 public function getRecordCount()
169 if ( !isset($this->_recordCount) )
172 return $this->_recordCount;
176 * Return the count of files created by the split, if it's been processed with splitSourceFile()
178 * @return int count of files created by the split
180 public function getFileCount()
182 if ( !isset($this->_fileCount) )
185 return $this->_fileCount;
189 * Return file name of one of the split files
191 * @param int $filenumber which split file we want
193 * @return string filename
195 public function getSplitFileName(
199 if ( $filenumber >= $this->getFileCount())
202 return "{$this->_sourceFile}-{$filenumber}";