2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
4 /*********************************************************************************
5 * SugarCRM is a customer relationship management program developed by
6 * SugarCRM, Inc. Copyright (C) 2004-2011 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 processing an import file
42 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
43 * All Rights Reserved.
44 ********************************************************************************/
46 require_once('modules/Import/ImportCacheFiles.php');
51 * Delimiter string we are using (i.e. , or ;)
56 * Enclosure string we are using (i.e. ' or ")
61 * Stores whether or not we are deleting the import file in the destructor
66 * File pointer returned from fopen() call
71 * Filename of file we are importing
76 * Array of the values in the current array we are in
81 * Count of rows processed
83 private $_rowsCount = 0;
86 * Count of rows with errors
88 private $_errorCount = 0;
91 * Count of duplicate rows
93 private $_dupeCount = 0;
96 * Count of newly created rows
98 private $_createdCount = 0;
101 * Count of updated rows
103 private $_updatedCount = 0;
106 * True if the current row has already had an error it in, so we don't increase the $_errorCount
108 private $_rowCountedForErrors = false;
113 * @param string $filename
114 * @param string $delimiter
115 * @param string $enclosure
116 * @param bool $deleteFile
118 public function __construct(
125 if ( !is_file($filename) || !is_readable($filename) ) {
129 // turn on auto-detection of line endings to fix bug #10770
130 ini_set('auto_detect_line_endings', '1');
132 $this->_fp = sugar_fopen($filename,'r');
133 $this->_filename = $filename;
134 $this->_deleteFile = $deleteFile;
135 $this->_delimiter = ( empty($delimiter) ? ',' : $delimiter );
136 $this->_enclosure = ( empty($enclosure) ? '' : trim($enclosure) );
138 // Bug 39494 - Remove the BOM (Byte Order Mark) from the beginning of the import row if it exists
139 $bomCheck = fread($this->_fp, 3);
140 if($bomCheck != pack("CCC",0xef,0xbb,0xbf)) {
148 * Deletes $_importFile if $_deleteFile is true
150 public function __destruct()
152 if ( $this->_deleteFile && $this->fileExists() ) {
154 //Make sure the file exists before unlinking
155 if(file_exists($this->_filename)) {
156 unlink($this->_filename);
160 ini_restore('auto_detect_line_endings');
164 * Returns true if the filename given exists and is readable
168 public function fileExists()
170 return !$this->_fp ? false : true;
174 * Gets the next row from $_importFile
176 * @return array current row of file
178 public function getNextRow()
180 if (!$this->fileExists())
183 // explode on delimiter instead if enclosure is an empty string
184 if ( empty($this->_enclosure) ) {
185 $row = explode($this->_delimiter,rtrim(fgets($this->_fp, 8192),"\r\n"));
186 if ($row !== false && !( count($row) == 1 && trim($row[0]) == '') )
187 $this->_currentRow = $row;
192 $row = fgetcsv($this->_fp, 8192, $this->_delimiter, $this->_enclosure);
193 if ($row !== false && $row != array(null))
194 $this->_currentRow = $row;
199 // Bug 26219 - Convert all line endings to the same style as PHP_EOL
200 foreach ( $this->_currentRow as $key => $value )
201 $this->_currentRow[$key] = str_replace(array("\r\n", "\n", "\r"),PHP_EOL,$value);
204 $this->_rowCountedForErrors = false;
206 return $this->_currentRow;
210 * Returns the number of fields in the current row
212 * @return int count of fiels in the current row
214 public function getFieldCount()
216 return count($this->_currentRow);
220 * Writes the row out to the ImportCacheFiles::getDuplicateFileName() file
222 public function markRowAsDuplicate()
224 $fp = sugar_fopen(ImportCacheFiles::getDuplicateFileName(),'a');
225 if ( empty($this->_enclosure) )
226 fputs($fp,implode($this->_delimiter,$this->_currentRow).PHP_EOL);
228 fputcsv($fp,$this->_currentRow, $this->_delimiter, $this->_enclosure);
235 * Writes the row out to the ImportCacheFiles::getErrorFileName() file
237 * @param $error string
238 * @param $fieldName string
239 * @param $fieldValue mixed
241 public function writeError(
247 $fp = sugar_fopen(ImportCacheFiles::getErrorFileName(),'a');
248 fputcsv($fp,array($error,$fieldName,$fieldValue,$this->_rowsCount));
251 if ( !$this->_rowCountedForErrors ) {
252 $this->_errorCount++;
253 $this->_rowCountedForErrors = true;
254 $this->writeErrorRecord();
259 * Writes the row out to the ImportCacheFiles::getErrorRecordsFileName() file
261 public function writeErrorRecord()
263 $fp = sugar_fopen(ImportCacheFiles::getErrorRecordsFileName(),'a');
264 if ( empty($this->_enclosure) )
265 fputs($fp,implode($this->_delimiter,$this->_currentRow).PHP_EOL);
267 fputcsv($fp,$this->_currentRow, $this->_delimiter, $this->_enclosure);
272 * Writes the totals and filename out to the ImportCacheFiles::getStatusFileName() file
274 public function writeStatus()
276 $fp = sugar_fopen(ImportCacheFiles::getStatusFileName(),'a');
277 fputcsv($fp,array($this->_rowsCount,$this->_errorCount,$this->_dupeCount,
278 $this->_createdCount,$this->_updatedCount,$this->_filename));
283 * Add this row to the UsersLastImport table
285 * @param string $import_module name of the module we are doing the import into
286 * @param string $module name of the bean we are creating for this import
287 * @param string $id id of the recorded created in the $module
289 public static function writeRowToLastImport(
295 // cache $last_import instance
298 if ( !($last_import instanceof UsersLastImport) )
299 $last_import = new UsersLastImport();
301 $last_import->id = null;
302 $last_import->deleted = null;
303 $last_import->assigned_user_id = $GLOBALS['current_user']->id;
304 $last_import->import_module = $import_module;
305 if ( $module == 'Case' ) {
308 $last_import->bean_type = $module;
309 $last_import->bean_id = $id;
310 return $last_import->save();
314 * Marks whether this row created a new record or not
316 * @param $createdRecord bool true if record is created, false if it is just updated
318 public function markRowAsImported(
319 $createdRecord = true
322 if ( $createdRecord )
323 ++$this->_createdCount;
325 ++$this->_updatedCount;