2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
38 /*********************************************************************************
40 * Description: view handler for step 1 of the import process
41 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42 * All Rights Reserved.
43 ********************************************************************************/
44 require_once('modules/Import/views/ImportView.php');
45 require_once('modules/Import/sources/ImportFile.php');
46 require_once('modules/Import/ImportFileSplitter.php');
47 require_once('modules/Import/CsvAutoDetect.php');
49 require_once('include/upload_file.php');
51 class ImportViewConfirm extends ImportView
53 const SAMPLE_ROW_SIZE = 3;
54 protected $pageTitleKey = 'LBL_CONFIRM_TITLE';
55 protected $errorScript = "";
58 * @see SugarView::display()
60 public function display()
62 global $mod_strings, $app_strings, $current_user;
63 global $sugar_config, $locale;
65 $this->ss->assign("IMPORT_MODULE", $_REQUEST['import_module']);
66 $this->ss->assign("TYPE",( !empty($_REQUEST['type']) ? $_REQUEST['type'] : "import" ));
67 $this->ss->assign("SOURCE_ID", $_REQUEST['source_id']);
69 $this->instruction = 'LBL_SELECT_PROPERTY_INSTRUCTION';
70 $this->ss->assign('INSTRUCTION', $this->getInstruction());
72 $this->ss->assign("MODULE_TITLE", $this->getModuleTitle(false), ENT_NOQUOTES);
73 $this->ss->assign("CURRENT_STEP", $this->currentStep);
74 $sugar_config['import_max_records_per_file'] = ( empty($sugar_config['import_max_records_per_file']) ? 1000 : $sugar_config['import_max_records_per_file'] );
75 $importSource = isset($_REQUEST['source']) ? $_REQUEST['source'] : 'csv' ;
77 // Clear out this user's last import
78 $seedUsersLastImport = new UsersLastImport();
79 $seedUsersLastImport->mark_deleted_by_user_id($current_user->id);
80 ImportCacheFiles::clearCacheFiles();
82 // handle uploaded file
83 $uploadFile = new UploadFile('userfile');
84 if (isset($_FILES['userfile']) && $uploadFile->confirm_upload())
86 $uploadFile->final_move('IMPORT_'.$this->bean->object_name.'_'.$current_user->id);
87 $uploadFileName = $uploadFile->get_upload_path('IMPORT_'.$this->bean->object_name.'_'.$current_user->id);
89 elseif( !empty($_REQUEST['tmp_file']) )
91 $uploadFileName = "upload://".basename($_REQUEST['tmp_file']);
95 $this->_showImportError($mod_strings['LBL_IMPORT_MODULE_ERROR_NO_UPLOAD'],$_REQUEST['import_module'],'Step2', true, null, true);
99 //check the file size, we dont want to process an empty file
100 if(isset($_FILES['userfile']['size']) && $_FILES['userfile']['size'] == 0){
101 //this file is empty, throw error message
102 $this->_showImportError($mod_strings['LBL_NO_LINES'],$_REQUEST['import_module'],'Step2', false, null, true);
108 //check to see if the file mime type is not a form of text or application octed streramand fire error if not
109 if(isset($_FILES['userfile']['type']) && strpos($_FILES['userfile']['type'],'octet-stream') === false && strpos($_FILES['userfile']['type'],'text') === false
110 && strpos($_FILES['userfile']['type'],'application/vnd.ms-excel') === false) {
111 //this file does not have a known text or application type of mime type, issue the warning
112 $error_msgs[] = $mod_strings['LBL_MIME_TYPE_ERROR_1'];
113 $error_msgs[] = $mod_strings['LBL_MIME_TYPE_ERROR_2'];
114 $this->_showImportError($error_msgs,$_REQUEST['import_module'],'Step2', true, $mod_strings['LBL_OK']);
118 $this->ss->assign("FILE_NAME", $uploadFileName);
120 // Now parse the file and look for errors
121 $importFile = new ImportFile( $uploadFileName, $_REQUEST['custom_delimiter'], html_entity_decode($_REQUEST['custom_enclosure'],ENT_QUOTES), FALSE);
123 if( $this->shouldAutoDetectProperties($importSource) )
125 $GLOBALS['log']->debug("Auto detecing csv properties...");
126 $autoDetectOk = $importFile->autoDetectCSVProperties();
127 $importFileMap = array();
128 $this->ss->assign("SOURCE", 'csv');
129 if($autoDetectOk === FALSE)
131 //show error only if previous mime type check has passed
133 $this->ss->assign("AUTO_DETECT_ERROR", $mod_strings['LBL_AUTO_DETECT_ERROR']);
138 $dateFormat = $importFile->getDateFormat();
139 $timeFormat = $importFile->getTimeFormat();
141 $importFileMap['importlocale_dateformat'] = $dateFormat;
144 $importFileMap['importlocale_timeformat'] = $timeFormat;
150 $impotMapSeed = $this->getImportMap($importSource);
151 $importFile->setImportFileMap($impotMapSeed);
152 $importFileMap = $impotMapSeed->getMapping($_REQUEST['import_module']);
155 $delimeter = $importFile->getFieldDelimeter();
156 $enclosure = $importFile->getFieldEnclosure();
157 $hasHeader = $importFile->hasHeaderRow();
159 $encodeOutput = TRUE;
160 //Handle users navigating back through the wizard.
161 if( !empty($_REQUEST['previous_action']) && $_REQUEST['previous_action'] == 'Confirm')
163 $encodeOutput = FALSE;
164 $importFileMap = $this->overloadImportFileMapFromRequest($importFileMap);
165 $delimeter = !empty($_REQUEST['custom_delimiter']) ? $_REQUEST['custom_delimiter'] : $delimeter;
166 $enclosure = isset($_REQUEST['custom_enclosure']) ? $_REQUEST['custom_enclosure'] : $enclosure;
167 $enclosure = html_entity_decode($enclosure, ENT_QUOTES);
168 $hasHeader = !empty($_REQUEST['has_header']) ? $_REQUEST['has_header'] : $hasHeader;
169 if ($hasHeader == 'on') {
171 } else if ($hasHeader == 'off') {
176 $this->ss->assign("IMPORT_ENCLOSURE_OPTIONS", $this->getEnclosureOptions($enclosure));
177 $this->ss->assign("IMPORT_DELIMETER_OPTIONS", $this->getDelimeterOptions($delimeter));
178 $this->ss->assign("CUSTOM_DELIMITER", $delimeter);
179 $this->ss->assign("CUSTOM_ENCLOSURE", htmlentities($enclosure, ENT_QUOTES));
180 $hasHeaderFlag = $hasHeader ? " CHECKED" : "";
181 $this->ss->assign("HAS_HEADER_CHECKED", $hasHeaderFlag);
183 if ( !$importFile->fileExists() ) {
184 $this->_showImportError($mod_strings['LBL_CANNOT_OPEN'],$_REQUEST['import_module'],'Step2', false, null, true);
188 //Check if we will exceed the maximum number of records allowed per import.
189 $maxRecordsExceeded = FALSE;
190 $maxRecordsWarningMessg = "";
191 $lineCount = $importFile->getNumberOfLinesInfile();
192 $maxLineCount = isset($sugar_config['import_max_records_total_limit'] ) ? $sugar_config['import_max_records_total_limit'] : 5000;
193 if( !empty($maxLineCount) && ($lineCount > $maxLineCount) )
195 $maxRecordsExceeded = TRUE;
196 $maxRecordsWarningMessg = string_format($mod_strings['LBL_IMPORT_ERROR_MAX_REC_LIMIT_REACHED'], array($lineCount, $maxLineCount) );
199 //Retrieve a sample set of data
200 $rows = $this->getSampleSet($importFile);
201 $this->ss->assign('column_count', $this->getMaxColumnsInSampleSet($rows) );
202 $this->ss->assign('HAS_HEADER', $importFile->hasHeaderRow(FALSE) );
203 $this->ss->assign('getNumberJs', $locale->getNumberJs());
204 $this->setImportFileCharacterSet($importFile, $importFileMap);
205 $this->setDateTimeProperties($importFileMap);
206 $this->setCurrencyOptions($importFileMap);
207 $this->setNumberFormatOptions($importFileMap);
208 $this->setNameFormatProperties($importFileMap);
210 $importMappingJS = $this->getImportMappingJS();
212 $this->ss->assign("SAMPLE_ROWS",$rows);
213 $JS = $this->_getJS($maxRecordsExceeded, $maxRecordsWarningMessg, $importMappingJS, $importFileMap );
214 $this->ss->assign("JAVASCRIPT", $JS);
215 $content = $this->ss->fetch('modules/Import/tpls/confirm.tpl');
216 $this->ss->assign("CONTENT",$content);
217 $this->ss->display('modules/Import/tpls/wizardWrapper.tpl');
221 private function getDelimeterOptions($selctedDelim)
223 $selctedDelim = $selctedDelim == "\t" ? '\t' : $selctedDelim;
224 return get_select_options_with_id($GLOBALS['app_list_strings']['import_delimeter_options'], $selctedDelim);
227 private function getEnclosureOptions($enclosure)
230 foreach ($GLOBALS['app_list_strings']['import_enclosure_options'] as $k => $v)
232 $results[htmlentities($k, ENT_QUOTES)] = $v;
235 return get_select_options_with_id($results, htmlentities($enclosure, ENT_QUOTES));
238 private function overloadImportFileMapFromRequest($importFileMap)
240 $overideKeys = array(
241 'importlocale_dateformat','importlocale_timeformat','importlocale_timezone','importlocale_charset',
242 'importlocale_currency','importlocale_default_currency_significant_digits','importlocale_num_grp_sep',
243 'importlocale_dec_sep','importlocale_default_locale_name_format','custom_delimiter', 'custom_enclosure'
246 foreach($overideKeys as $key)
248 if( !empty( $_REQUEST[$key]) )
249 $importFileMap[$key] = $_REQUEST[$key];
251 return $importFileMap;
254 private function shouldAutoDetectProperties($importSource)
256 if(empty($importSource) || $importSource == 'csv' )
262 private function getImportMap($importSource)
264 if ( strncasecmp("custom:",$importSource,7) == 0)
266 $id = substr($importSource,7);
267 $import_map_seed = new ImportMap();
268 $import_map_seed->retrieve($id, false);
270 $this->ss->assign("SOURCE_ID", $import_map_seed->id);
271 $this->ss->assign("SOURCE_NAME", $import_map_seed->name);
272 $this->ss->assign("SOURCE", $import_map_seed->source);
276 $classname = 'ImportMap' . ucfirst($importSource);
277 if ( file_exists("modules/Import/maps/{$classname}.php") )
278 require_once("modules/Import/maps/{$classname}.php");
279 elseif ( file_exists("custom/modules/Import/maps/{$classname}.php") )
280 require_once("custom/modules/Import/maps/{$classname}.php");
283 require_once("custom/modules/Import/maps/ImportMapOther.php");
284 $classname = 'ImportMapOther';
285 $importSource = 'other';
287 if ( class_exists($classname) )
289 $import_map_seed = new $classname;
290 $this->ss->assign("SOURCE", $importSource);
294 return $import_map_seed;
297 private function setNameFormatProperties($field_map = array())
299 global $locale, $current_user;
301 $localized_name_format = isset($field_map['importlocale_default_locale_name_format'])? $field_map['importlocale_default_locale_name_format'] : $locale->getLocaleFormatMacro($current_user);
302 $this->ss->assign('default_locale_name_format', $localized_name_format);
303 $this->ss->assign('getNameJs', $locale->getNameJs());
307 private function setNumberFormatOptions($field_map = array())
309 global $locale, $current_user, $sugar_config;
311 $num_grp_sep = isset($field_map['importlocale_num_grp_sep'])? $field_map['importlocale_num_grp_sep'] : $current_user->getPreference('num_grp_sep');
312 $dec_sep = isset($field_map['importlocale_dec_sep'])? $field_map['importlocale_dec_sep'] : $current_user->getPreference('dec_sep');
314 $this->ss->assign("NUM_GRP_SEP",( empty($num_grp_sep) ? $sugar_config['default_number_grouping_seperator'] : $num_grp_sep ));
315 $this->ss->assign("DEC_SEP",( empty($dec_sep)? $sugar_config['default_decimal_seperator'] : $dec_sep ));
318 $significantDigits = isset($field_map['importlocale_default_currency_significant_digits']) ? $field_map['importlocale_default_currency_significant_digits']
319 : $locale->getPrecedentPreference('default_currency_significant_digits', $current_user);
322 for($i=0; $i<=6; $i++)
324 if($significantDigits == $i)
326 $sigDigits .= '<option value="'.$i.'" selected="true">'.$i.'</option>';
329 $sigDigits .= '<option value="'.$i.'">'.$i.'</option>';
333 $this->ss->assign('sigDigits', $sigDigits);
337 private function setCurrencyOptions($field_map = array() )
339 global $locale, $current_user;
340 $cur_id = isset($field_map['importlocale_currency'])? $field_map['importlocale_currency'] : $locale->getPrecedentPreference('currency', $current_user);
341 // get currency preference
342 require_once('modules/Currencies/ListCurrency.php');
343 $currency = new ListCurrency();
345 $selectCurrency = $currency->getSelectOptions($cur_id);
347 $selectCurrency = $currency->getSelectOptions();
349 $this->ss->assign("CURRENCY", $selectCurrency);
351 $currenciesVars = "";
353 foreach($locale->currencies as $id => $arrVal)
355 $currenciesVars .= "currencies[{$i}] = '{$arrVal['symbol']}';\n";
358 $currencySymbolsJs = <<<eoq
359 var currencies = new Object;
361 function setSymbolValue(id) {
362 document.getElementById('symbol').value = currencies[id];
365 return $currencySymbolsJs;
370 private function setDateTimeProperties( $field_map = array() )
372 global $current_user, $sugar_config;
374 $timeFormat = $current_user->getUserDateTimePreferences();
375 $defaultTimeOption = isset($field_map['importlocale_timeformat'])? $field_map['importlocale_timeformat'] : $timeFormat['time'];
376 $defaultDateOption = isset($field_map['importlocale_dateformat'])? $field_map['importlocale_dateformat'] : $timeFormat['date'];
378 $timeOptions = get_select_options_with_id($sugar_config['time_formats'], $defaultTimeOption);
379 $dateOptions = get_select_options_with_id($sugar_config['date_formats'], $defaultDateOption);
381 // get list of valid timezones
382 $userTZ = isset($field_map['importlocale_timezone'])? $field_map['importlocale_timezone'] : $current_user->getPreference('timezone');
384 $userTZ = TimeDate::userTimezone();
386 $this->ss->assign('TIMEZONE_CURRENT', $userTZ);
387 $this->ss->assign('TIMEOPTIONS', $timeOptions);
388 $this->ss->assign('DATEOPTIONS', $dateOptions);
389 $this->ss->assign('TIMEZONEOPTIONS', TimeDate::getTimezoneList());
392 private function setImportFileCharacterSet($importFile, $field_map = array())
395 $charset_for_import = isset($field_map['importlocale_charset']) ? $field_map['importlocale_charset'] : $importFile->autoDetectCharacterSet();
396 $charsetOptions = get_select_options_with_id( $locale->getCharsetSelect(), $charset_for_import);//wdong, bug 25927, here we should use the charset testing results from above.
397 $this->ss->assign('CHARSETOPTIONS', $charsetOptions);
400 protected function getImportMappingJS()
403 $importMappings = array('ImportMapSalesforce', 'ImportMapOutlook');
404 foreach($importMappings as $importMap)
406 $tmpFile = "modules/Import/maps/$importMap.php";
407 if( file_exists($tmpFile) )
409 require_once($tmpFile);
410 $t = new $importMap();
411 $results[$t->name] = array('delim' => $t->delimiter, 'enclos' => $t->enclosure, 'has_header' => $t->has_header);
417 public function getMaxColumnsInSampleSet($sampleSet)
420 foreach($sampleSet as $v)
422 if(count($v) > $maxColumns)
423 $maxColumns = count($v);
431 public function getSampleSet($importFile)
434 for($i=0; $i < self::SAMPLE_ROW_SIZE; $i++)
436 $rows[] = $importFile->getNextRow();
439 if( ! $importFile->hasHeaderRow(FALSE) )
441 array_unshift($rows, array_fill(0,1,'') );
444 // to be displayed in UTF-8 format
446 $encoding = $importFile->autoDetectCharacterSet();
447 if (!empty($encoding) && $encoding != 'UTF-8') {
448 foreach ($rows as &$row) {
449 if (is_array($row)) {
450 foreach ($row as &$val) {
451 $val = $locale->translateCharset($val, $encoding);
461 * Returns JS used in this view
463 private function _getJS($maxRecordsExceeded, $maxRecordsWarningMessg, $importMappingJS, $importFileMap)
465 global $mod_strings, $locale;
466 $maxRecordsExceededJS = $maxRecordsExceeded?"true":"false";
467 $importMappingJS = json_encode($importMappingJS);
469 $currencySymbolJs = $this->setCurrencyOptions($importFileMap);
470 $getNumberJs = $locale->getNumberJs();
471 $getNameJs = $locale->getNameJs();
473 return <<<EOJAVASCRIPT
476 var import_mapping_js = $importMappingJS;
477 document.getElementById('goback').onclick = function()
479 document.getElementById('importconfirm').action.value = 'Step2';
483 document.getElementById('gonext').onclick = function()
485 document.getElementById('importconfirm').action.value = 'Step3';
489 document.getElementById('custom_enclosure').onchange = function()
491 document.getElementById('importconfirm').custom_enclosure_other.style.display = ( this.value == 'other' ? '' : 'none' );
494 document.getElementById('custom_delimiter').onchange = function()
496 document.getElementById('importconfirm').custom_delimiter_other.style.display = ( this.value == 'other' ? '' : 'none' );
499 document.getElementById('toggleImportOptions').onclick = function() {
500 if (document.getElementById('importOptions').style.display == 'none'){
501 document.getElementById('importOptions').style.display = '';
502 document.getElementById('toggleImportOptions').value=' {$mod_strings['LBL_HIDE_ADVANCED_OPTIONS']} ';
503 document.getElementById('toggleImportOptions').title='{$mod_strings['LBL_HIDE_ADVANCED_OPTIONS']}';
506 document.getElementById('importOptions').style.display = 'none';
507 document.getElementById('toggleImportOptions').value=' {$mod_strings['LBL_SHOW_ADVANCED_OPTIONS']} ';
508 document.getElementById('toggleImportOptions').title='{$mod_strings['LBL_SHOW_ADVANCED_OPTIONS']}';
512 YAHOO.util.Event.onDOMReady(function(){
513 if($maxRecordsExceededJS)
515 var contImport = confirm('$maxRecordsWarningMessg');
518 var module = document.getElementById('importconfirm').import_module.value;
519 var source = document.getElementById('importconfirm').source.value;
520 var returnUrl = "index.php?module=Import&action=Step2&import_module=" + module + "&source=" + source;
521 document.location.href = returnUrl;
525 function refreshDataTable(e)
528 success: function(o) {
529 document.getElementById('confirm_table').innerHTML = o.responseText;
531 failure: function(o) {},
534 var importFile = document.getElementById('importconfirm').file_name.value;
535 var fieldDelimeter = document.getElementById('custom_delimiter').value;
536 if(fieldDelimeter == 'other')
537 fieldDelimeter = document.getElementById('custom_delimiter_other').value;
539 var fieldQualifier = document.getElementById('custom_enclosure').value;
540 var hasHeader = document.getElementById('importconfirm').has_header.checked ? 'true' : '';
542 if(fieldQualifier == 'other' && this.id == 'custom_enclosure')
546 else if( fieldQualifier == 'other' )
548 fieldQualifier = document.getElementById('custom_enclosure_other').value;
551 var url = 'index.php?action=RefreshMapping&module=Import&importFile=' + importFile
552 + '&delim=' + fieldDelimeter + '&qualif=' + fieldQualifier + "&header=" + hasHeader;
554 YAHOO.util.Connect.asyncRequest('GET', url, callback);
556 var subscribers = ["custom_delimiter", "custom_enclosure", "custom_enclosure_other", "has_header", "importlocale_charset", "custom_delimiter_other"];
557 YAHOO.util.Event.addListener(subscribers, "change", refreshDataTable);
559 function setMappingProperties(el)
561 var sourceEl = document.getElementById('source');
562 if(sourceEl.value != '' && sourceEl.value != 'csv' && sourceEl.value != 'salesforce' && sourceEl.value != 'outlook')
564 if( !confirm(SUGAR.language.get('Import','LBL_CONFIRM_MAP_OVERRIDE')) )
566 deSelectExternalSources();
570 var selectedMap = this.value;
571 if( typeof(import_mapping_js[selectedMap]) == 'undefined')
574 sourceEl.value = selectedMap;
575 document.getElementById('custom_delimiter').value = import_mapping_js[selectedMap].delim;
576 document.getElementById('custom_enclosure').value = import_mapping_js[selectedMap].enclos;
577 document.getElementById('has_header').checked = import_mapping_js[selectedMap].has_header;
582 function deSelectExternalSources()
584 var els = document.getElementsByName('external_source');
585 for(i=0;i<els.length;i++)
587 els[i].checked = false;
590 YAHOO.util.Event.addListener(['sf_map', 'outlook_map'], "click", setMappingProperties);
592 var deselectEl = document.getElementById('deselect');
595 deselectEl.onclick = function() {
596 var els = document.getElementsByName('external_source');
597 for(i=0;i<els.length;i++)
599 els[i].checked = false;
608 setSymbolValue(document.getElementById('currency_select').selectedIndex);
614 * Displays the Smarty template for an error
616 * @param string $message error message to show
617 * @param string $module what module we were importing into
618 * @param string $action what page we should go back to
620 protected function _showImportError($message,$module,$action = 'Step1',$showCancel = false, $cancelLabel = null, $display = false)
622 if(!is_array($message)){
623 $message = array($message);
625 $ss = new Sugar_Smarty();
627 foreach($message as $m){
628 $display_msg .= '<p>'.htmlentities($m, ENT_QUOTES).'</p><br>';
632 $ss->assign("MESSAGE",$display_msg);
633 $ss->assign("ACTION",$action);
634 $ss->assign("IMPORT_MODULE",$module);
635 $ss->assign("MOD", $GLOBALS['mod_strings']);
636 $ss->assign("SOURCE","");
637 $ss->assign("SHOWCANCEL",$showCancel);
638 if ( isset($_REQUEST['source']) )
639 $ss->assign("SOURCE", $_REQUEST['source']);
642 $ss->assign('CANCELLABEL', $cancelLabel);
645 $content = $ss->fetch('modules/Import/tpls/error.tpl');
647 echo $ss->fetch('modules/Import/tpls/error.tpl');