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 ********************************************************************************/
40 require_once('modules/Import/ImportCacheFiles.php');
41 require_once('modules/Import/ImportFieldSanitize.php');
42 require_once('modules/Import/ImportDuplicateCheck.php');
48 * @var ImportFieldSanitizer
55 private $defaultUserCurrency;
60 private $importColumns;
65 private $importSource;
70 private $isUpdateOnly;
78 * @var sugarToExternalSourceFieldMap
80 private $sugarToExternalSourceFieldMap = array();
83 public function __construct($importSource, $bean)
85 global $mod_strings, $sugar_config;
87 $this->importSource = $importSource;
89 //Vanilla copy of the bean object.
92 // use our own error handler
93 set_error_handler(array('Importer','handleImportErrors'),E_ALL);
95 // Increase the max_execution_time since this step can take awhile
96 ini_set("max_execution_time", max($sugar_config['import_max_execution_time'],3600));
99 TrackerManager::getInstance()->pause();
101 // set the default locale settings
102 $this->ifs = $this->getFieldSanitizer();
104 //Get the default user currency
105 $this->defaultUserCurrency = new Currency();
106 $this->defaultUserCurrency->retrieve('-99');
108 //Get our import column definitions
109 $this->importColumns = $this->getImportColumns();
110 $this->isUpdateOnly = ( isset($_REQUEST['import_type']) && $_REQUEST['import_type'] == 'update' );
113 public function import()
115 foreach($this->importSource as $row)
117 $this->importRow($row);
120 // save mapping if requested
121 if ( isset($_REQUEST['save_map_as']) && $_REQUEST['save_map_as'] != '' )
123 $this->saveMappingFile();
126 $this->importSource->writeStatus();
128 //All done, remove file.
132 protected function importRow($row)
134 global $sugar_config, $mod_strings, $current_user;
136 $focus = clone $this->bean;
137 $focus->unPopulateDefaultValues();
138 $focus->save_from_post = false;
139 $focus->team_id = null;
140 $this->ifs->createdBeans = array();
141 $this->importSource->resetRowErrorCounter();
144 for ( $fieldNum = 0; $fieldNum < $_REQUEST['columncount']; $fieldNum++ )
146 // loop if this column isn't set
147 if ( !isset($this->importColumns[$fieldNum]) )
150 // get this field's properties
151 $field = $this->importColumns[$fieldNum];
152 $fieldDef = $focus->getFieldDefinition($field);
153 $fieldTranslated = translate((isset($fieldDef['vname'])?$fieldDef['vname']:$fieldDef['name']), $focus->module_dir)." (".$fieldDef['name'].")";
154 $defaultRowValue = '';
155 // Bug 37241 - Don't re-import over a field we already set during the importing of another field
156 if ( !empty($focus->$field) )
163 $locale = new Localization();
165 if ( isset($row[$fieldNum]) )
167 $rowValue = $locale->translateCharset(strip_tags(trim($row[$fieldNum])),$this->importSource->importlocale_charset,$sugar_config['default_charset']);
169 else if( isset($this->sugarToExternalSourceFieldMap[$field]) && isset($row[$this->sugarToExternalSourceFieldMap[$field]]) )
171 $rowValue = $locale->translateCharset(strip_tags(trim($row[$this->sugarToExternalSourceFieldMap[$field]])),$this->importSource->importlocale_charset,$sugar_config['default_charset']);
178 // If there is an default value then use it instead
179 if ( !empty($_REQUEST[$field]) )
181 $defaultRowValue = $this->populateDefaultMapValue($field, $_REQUEST[$field], $fieldDef);
184 if( empty($rowValue))
186 $rowValue = $defaultRowValue;
187 //reset the default value to empty
192 // Bug 22705 - Don't update the First Name or Last Name value if Full Name is set
193 if ( in_array($field, array('first_name','last_name')) && !empty($focus->full_name) )
196 // loop if this value has not been set
197 if ( !isset($rowValue) )
200 // If the field is required and blank then error out
201 if ( array_key_exists($field,$focus->get_import_required_fields()) && empty($rowValue) && $rowValue!='0')
203 $this->importSource->writeError( $mod_strings['LBL_REQUIRED_VALUE'],$fieldTranslated,'NULL');
207 // Handle the special case "Sync to Outlook"
208 if ( $focus->object_name == "Contact" && $field == 'sync_contact' )
211 * Bug #41194 : if true used as value of sync_contact - add curent user to list to sync
213 if ( true == $rowValue || 'true' == strtolower($rowValue)) {
214 $focus->sync_contact = $focus->id;
215 } elseif (false == $rowValue || 'false' == strtolower($rowValue)) {
216 $focus->sync_contact = '';
218 $bad_names = array();
219 $returnValue = $this->ifs->synctooutlook($rowValue,$fieldDef,$bad_names);
220 // try the default value on fail
221 if ( !$returnValue && !empty($defaultRowValue) )
222 $returnValue = $this->ifs->synctooutlook($defaultRowValue, $fieldDef, $bad_names);
225 $this->importSource->writeError($mod_strings['LBL_ERROR_SYNC_USERS'], $fieldTranslated, $bad_names);
228 $focus->sync_contact = $returnValue;
233 // Handle email1 and email2 fields ( these don't have the type of email )
234 if ( $field == 'email1' || $field == 'email2' )
236 $returnValue = $this->ifs->email($rowValue, $fieldDef, $focus);
237 // try the default value on fail
238 if ( !$returnValue && !empty($defaultRowValue) )
239 $returnValue = $this->ifs->email( $defaultRowValue, $fieldDef);
240 if ( $returnValue === FALSE )
243 $this->importSource->writeError( $mod_strings['LBL_ERROR_INVALID_EMAIL'], $fieldTranslated, $rowValue);
247 $rowValue = $returnValue;
248 // check for current opt_out and invalid email settings for this email address
249 // if we find any, set them now
250 $emailres = $focus->db->query( "SELECT opt_out, invalid_email FROM email_addresses WHERE email_address = '".$focus->db->quote($rowValue)."'");
251 if ( $emailrow = $focus->db->fetchByAssoc($emailres) )
253 $focus->email_opt_out = $emailrow['opt_out'];
254 $focus->invalid_email = $emailrow['invalid_email'];
259 // Handle splitting Full Name into First and Last Name parts
260 if ( $field == 'full_name' && !empty($rowValue) )
262 $this->ifs->fullname($rowValue,$fieldDef,$focus);
265 // to maintain 451 compatiblity
266 if(!isset($fieldDef['module']) && $fieldDef['type']=='relate')
267 $fieldDef['module'] = ucfirst($fieldDef['table']);
269 if(isset($fieldDef['custom_type']) && !empty($fieldDef['custom_type']))
270 $fieldDef['type'] = $fieldDef['custom_type'];
272 // If the field is empty then there is no need to check the data
273 if( !empty($rowValue) )
276 $rowValue = $this->sanitizeFieldValueByType($rowValue, $fieldDef, $defaultRowValue, $focus, $fieldTranslated);
277 if($rowValue === FALSE)
282 // if the parent type is in singular form, get the real module name for parent_type
283 if (isset($fieldDef['type']) && $fieldDef['type']=='parent_type') {
284 $rowValue = get_module_from_singular($rowValue);
287 $focus->$field = $rowValue;
288 unset($defaultRowValue);
291 // Now try to validate flex relate fields
292 if ( isset($focus->field_defs['parent_name']) && isset($focus->parent_name) && ($focus->field_defs['parent_name']['type'] == 'parent') )
294 // populate values from the picker widget if the import file doesn't have them
295 $parent_idField = $focus->field_defs['parent_name']['id_name'];
296 if ( empty($focus->$parent_idField) && !empty($_REQUEST[$parent_idField]) )
297 $focus->$parent_idField = $_REQUEST[$parent_idField];
299 $parent_typeField = $focus->field_defs['parent_name']['type_name'];
301 if ( empty($focus->$parent_typeField) && !empty($_REQUEST[$parent_typeField]) )
302 $focus->$parent_typeField = $_REQUEST[$parent_typeField];
304 $returnValue = $this->ifs->parent($focus->parent_name,$focus->field_defs['parent_name'],$focus, empty($_REQUEST['parent_name']));
305 if ( !$returnValue && !empty($_REQUEST['parent_name']) )
306 $returnValue = $this->ifs->parent( $_REQUEST['parent_name'],$focus->field_defs['parent_name'], $focus);
309 // check to see that the indexes being entered are unique.
310 if (isset($_REQUEST['enabled_dupes']) && $_REQUEST['enabled_dupes'] != "")
312 $toDecode = html_entity_decode ($_REQUEST['enabled_dupes'], ENT_QUOTES);
313 $enabled_dupes = json_decode($toDecode);
314 $idc = new ImportDuplicateCheck($focus);
316 if ( $idc->isADuplicateRecord($enabled_dupes) )
318 $this->importSource->markRowAsDuplicate($idc->_dupedFields);
319 $this->_undoCreatedBeans($this->ifs->createdBeans);
323 //Allow fields to be passed in for dup check as well (used by external adapters)
324 else if( !empty($_REQUEST['enabled_dup_fields']) )
326 $toDecode = html_entity_decode ($_REQUEST['enabled_dup_fields'], ENT_QUOTES);
327 $enabled_dup_fields = json_decode($toDecode);
328 $idc = new ImportDuplicateCheck($focus);
329 if ( $idc->isADuplicateRecordByFields($enabled_dup_fields) )
331 $this->importSource->markRowAsDuplicate($idc->_dupedFields);
332 $this->_undoCreatedBeans($this->ifs->createdBeans);
337 // if the id was specified
339 if ( !empty($focus->id) )
341 $focus->id = $this->_convertId($focus->id);
343 // check if it already exists
344 $query = "SELECT * FROM {$focus->table_name} WHERE id='".$focus->db->quote($focus->id)."'";
345 $result = $focus->db->query($query)
346 or sugar_die("Error selecting sugarbean: ");
348 $dbrow = $focus->db->fetchByAssoc($result);
350 if (isset ($dbrow['id']) && $dbrow['id'] != -1)
352 // if it exists but was deleted, just remove it
353 if (isset ($dbrow['deleted']) && $dbrow['deleted'] == 1 && $this->isUpdateOnly ==false)
355 $this->removeDeletedBean($focus);
356 $focus->new_with_id = true;
360 if( ! $this->isUpdateOnly )
362 $this->importSource->writeError($mod_strings['LBL_ID_EXISTS_ALREADY'],'ID',$focus->id);
363 $this->_undoCreatedBeans($this->ifs->createdBeans);
367 $clonedBean = $this->cloneExistingBean($focus);
368 if($clonedBean === FALSE)
370 $this->importSource->writeError($mod_strings['LBL_RECORD_CANNOT_BE_UPDATED'],'ID',$focus->id);
371 $this->_undoCreatedBeans($this->ifs->createdBeans);
376 $focus = $clonedBean;
383 $focus->new_with_id = true;
389 $this->saveImportBean($focus, $newRecord);
390 // Update the created/updated counter
391 $this->importSource->markRowAsImported($newRecord);
394 $this->_undoCreatedBeans($this->ifs->createdBeans);
396 unset($defaultRowValue);
401 protected function sanitizeFieldValueByType($rowValue, $fieldDef, $defaultRowValue, $focus, $fieldTranslated)
403 global $mod_strings, $app_list_strings;
404 switch ($fieldDef['type'])
408 if ( isset($fieldDef['type']) && $fieldDef['type'] == "multienum" )
409 $returnValue = $this->ifs->multienum($rowValue,$fieldDef);
411 $returnValue = $this->ifs->enum($rowValue,$fieldDef);
412 // try the default value on fail
413 if ( !$returnValue && !empty($defaultRowValue) )
415 if ( isset($fieldDef['type']) && $fieldDef['type'] == "multienum" )
416 $returnValue = $this->ifs->multienum($defaultRowValue,$fieldDef);
418 $returnValue = $this->ifs->enum($defaultRowValue,$fieldDef);
420 if ( $returnValue === FALSE )
422 $this->importSource->writeError($mod_strings['LBL_ERROR_NOT_IN_ENUM'] . implode(",",$app_list_strings[$fieldDef['options']]), $fieldTranslated,$rowValue);
431 $returnValue = $this->ifs->relate($rowValue, $fieldDef, $focus, empty($defaultRowValue));
432 if (!$returnValue && !empty($defaultRowValue))
433 $returnValue = $this->ifs->relate($defaultRowValue,$fieldDef, $focus);
434 // Bug 33623 - Set the id value found from the above method call as an importColumn
435 if ($returnValue !== false)
436 $this->importColumns[] = $fieldDef['id_name'];
440 $this->ifs->teamset($rowValue,$fieldDef,$focus);
441 $this->importColumns[] = 'team_set_id';
442 $this->importColumns[] = 'team_id';
449 $fieldtype = $fieldDef['type'];
450 $returnValue = $this->ifs->$fieldtype($rowValue, $fieldDef, $focus);
451 // try the default value on fail
452 if ( !$returnValue && !empty($defaultRowValue) )
453 $returnValue = $this->ifs->$fieldtype($defaultRowValue,$fieldDef, $focus);
456 $this->importSource->writeError($mod_strings['LBL_ERROR_INVALID_'.strtoupper($fieldDef['type'])],$fieldTranslated,$rowValue,$focus);
463 protected function cloneExistingBean($focus)
465 $existing_focus = clone $this->bean;
466 if ( !( $existing_focus->retrieve($focus->id) instanceOf SugarBean ) )
472 $newData = $focus->toArray();
473 foreach ( $newData as $focus_key => $focus_value )
474 if ( in_array($focus_key,$this->importColumns) )
475 $existing_focus->$focus_key = $focus_value;
477 return $existing_focus;
481 protected function removeDeletedBean($focus)
485 $query2 = "DELETE FROM {$focus->table_name} WHERE id='".$focus->db->quote($focus->id)."'";
486 $result2 = $focus->db->query($query2) or sugar_die($mod_strings['LBL_ERROR_DELETING_RECORD']." ".$focus->id);
487 if ($focus->hasCustomFields())
489 $query3 = "DELETE FROM {$focus->table_name}_cstm WHERE id_c='".$focus->db->quote($focus->id)."'";
490 $result2 = $focus->db->query($query3);
494 protected function saveImportBean($focus, $newRecord)
496 global $timedate, $current_user;
498 // Populate in any default values to the bean
499 $focus->populateDefaultValues();
501 if ( !isset($focus->assigned_user_id) || $focus->assigned_user_id == '' && $newRecord )
503 $focus->assigned_user_id = $current_user->id;
506 * Bug 34854: Added all conditions besides the empty check on date modified.
508 if ( ( !empty($focus->new_with_id) && !empty($focus->date_modified) ) ||
509 ( empty($focus->new_with_id) && $timedate->to_db($focus->date_modified) != $timedate->to_db($timedate->to_display_date_time($focus->fetched_row['date_modified'])) )
511 $focus->update_date_modified = false;
513 $focus->optimistic_lock = false;
514 if ( $focus->object_name == "Contact" && isset($focus->sync_contact) )
516 //copy the potential sync list to another varible
517 $list_of_users=$focus->sync_contact;
518 //and set it to false for the save
519 $focus->sync_contact=false;
521 else if($focus->object_name == "User" && !empty($current_user) && $focus->is_admin && !is_admin($current_user) && is_admin_for_module($current_user, 'Users')) {
522 sugar_die($GLOBALS['mod_strings']['ERR_IMPORT_SYSTEM_ADMININSTRATOR']);
524 //bug# 40260 setting it true as the module in focus is involved in an import
525 $focus->in_import=true;
526 // call any logic needed for the module preSave
527 $focus->beforeImportSave();
529 // if modified_user_id is set, set the flag to false so SugarBEan will not reset it
530 if (isset($focus->modified_user_id) && $focus->modified_user_id) {
531 $focus->update_modified_by = false;
533 // if created_by is set, set the flag to false so SugarBEan will not reset it
534 if (isset($focus->created_by) && $focus->created_by) {
535 $focus->set_created_by = false;
538 if ( $focus->object_name == "Contact" && isset($list_of_users) )
539 $focus->process_sync_to_outlook($list_of_users);
543 //now that save is done, let's make sure that parent and related id's were saved as relationships
544 //this takes place before the afterImportSave()
545 $this->checkRelatedIDsAfterSave($focus);
547 // call any logic needed for the module postSave
548 $focus->afterImportSave();
550 // Add ID to User's Last Import records
552 $this->importSource->writeRowToLastImport( $_REQUEST['import_module'],($focus->object_name == 'Case' ? 'aCase' : $focus->object_name),$focus->id);
556 protected function saveMappingFile()
558 global $current_user;
560 $firstrow = unserialize(base64_decode($_REQUEST['firstrow']));
561 $mappingValsArr = $this->importColumns;
562 $mapping_file = new ImportMap();
563 if ( isset($_REQUEST['has_header']) && $_REQUEST['has_header'] == 'on')
565 $header_to_field = array ();
566 foreach ($this->importColumns as $pos => $field_name)
568 if (isset($firstrow[$pos]) && isset($field_name))
570 $header_to_field[$firstrow[$pos]] = $field_name;
574 $mappingValsArr = $header_to_field;
576 //get array of values to save for duplicate and locale settings
577 $advMapping = $this->retrieveAdvancedMapping();
579 //merge with mappingVals array
580 if(!empty($advMapping) && is_array($advMapping))
582 $mappingValsArr = array_merge($mappingValsArr,$advMapping);
586 $mapping_file->setMapping($mappingValsArr);
588 // save default fields
589 $defaultValues = array();
590 for ( $i = 0; $i < $_REQUEST['columncount']; $i++ )
592 if (isset($this->importColumns[$i]) && !empty($_REQUEST[$this->importColumns[$i]]))
594 $field = $this->importColumns[$i];
595 $fieldDef = $this->bean->getFieldDefinition($field);
596 if(!empty($fieldDef['custom_type']) && $fieldDef['custom_type'] == 'teamset')
598 require_once('include/SugarFields/Fields/Teamset/SugarFieldTeamset.php');
599 $sugar_field = new SugarFieldTeamset('Teamset');
600 $teams = $sugar_field->getTeamsFromRequest($field);
601 if(isset($_REQUEST['primary_team_name_collection']))
603 $primary_index = $_REQUEST['primary_team_name_collection'];
606 //If primary_index was selected, ensure that the first Array entry is the primary team
607 if(isset($primary_index))
610 $new_teams = array();
611 foreach($teams as $id=>$name)
613 if($primary_index == $count++)
615 $new_teams[$id] = $name;
621 foreach($teams as $id=>$name)
623 $new_teams[$id] = $name;
628 $json = getJSONobj();
629 $defaultValues[$field] = $json->encode($teams);
633 $defaultValues[$field] = $_REQUEST[$this->importColumns[$i]];
637 $mapping_file->setDefaultValues($defaultValues);
638 $result = $mapping_file->save( $current_user->id, $_REQUEST['save_map_as'], $_REQUEST['import_module'], $_REQUEST['source'],
639 ( isset($_REQUEST['has_header']) && $_REQUEST['has_header'] == 'on'), $_REQUEST['custom_delimiter'], html_entity_decode($_REQUEST['custom_enclosure'],ENT_QUOTES)
644 protected function populateDefaultMapValue($field, $fieldValue, $fieldDef)
646 global $timedate, $current_user;
648 if ( is_array($fieldValue) )
649 $defaultRowValue = encodeMultienumValue($fieldValue);
651 $defaultRowValue = $_REQUEST[$field];
652 // translate default values to the date/time format for the import file
653 if( $fieldDef['type'] == 'date' && $this->ifs->dateformat != $timedate->get_date_format() )
654 $defaultRowValue = $timedate->swap_formats($defaultRowValue, $this->ifs->dateformat, $timedate->get_date_format());
656 if( $fieldDef['type'] == 'time' && $this->ifs->timeformat != $timedate->get_time_format() )
657 $defaultRowValue = $timedate->swap_formats($defaultRowValue, $this->ifs->timeformat, $timedate->get_time_format());
659 if( ($fieldDef['type'] == 'datetime' || $fieldDef['type'] == 'datetimecombo') && $this->ifs->dateformat.' '.$this->ifs->timeformat != $timedate->get_date_time_format() )
660 $defaultRowValue = $timedate->swap_formats($defaultRowValue, $this->ifs->dateformat.' '.$this->ifs->timeformat,$timedate->get_date_time_format());
662 if ( in_array($fieldDef['type'],array('currency','float','int','num')) && $this->ifs->num_grp_sep != $current_user->getPreference('num_grp_sep') )
663 $defaultRowValue = str_replace($current_user->getPreference('num_grp_sep'), $this->ifs->num_grp_sep,$defaultRowValue);
665 if ( in_array($fieldDef['type'],array('currency','float')) && $this->ifs->dec_sep != $current_user->getPreference('dec_sep') )
666 $defaultRowValue = str_replace($current_user->getPreference('dec_sep'), $this->ifs->dec_sep,$defaultRowValue);
668 $user_currency_symbol = $this->defaultUserCurrency->symbol;
669 if ( $fieldDef['type'] == 'currency' && $this->ifs->currency_symbol != $user_currency_symbol )
670 $defaultRowValue = str_replace($user_currency_symbol, $this->ifs->currency_symbol,$defaultRowValue);
672 return $defaultRowValue;
675 protected function getImportColumns()
677 $importable_fields = $this->bean->get_importable_fields();
678 $importColumns = array();
679 foreach ($_REQUEST as $name => $value)
681 // only look for var names that start with "fieldNum"
682 if (strncasecmp($name, "colnum_", 7) != 0)
685 // pull out the column position for this field name
686 $pos = substr($name, 7);
688 if ( isset($importable_fields[$value]) )
690 // now mark that we've seen this field
691 $importColumns[$pos] = $value;
695 return $importColumns;
698 protected function getFieldSanitizer()
700 $ifs = new ImportFieldSanitize();
701 $copyFields = array('dateformat','timeformat','timezone','default_currency_significant_digits','num_grp_sep','dec_sep','default_locale_name_format');
702 foreach($copyFields as $field)
704 $fieldKey = "importlocale_$field";
705 $ifs->$field = $this->importSource->$fieldKey;
708 $currency = new Currency();
709 $currency->retrieve($this->importSource->importlocale_currency);
710 $ifs->currency_symbol = $currency->symbol;
716 * Sets a translation map from sugar field key to external source key used while importing a row. This allows external sources
717 * to return a data set that is an associative array rather than numerically indexed.
722 public function setFieldKeyTranslator($translator)
724 $this->sugarToExternalSourceFieldMap = $translator;
728 * If a bean save is not done for some reason, this method will undo any of the beans that were created
730 * @param array $ids ids of user_last_import records created
732 protected function _undoCreatedBeans( array $ids )
734 $focus = new UsersLastImport();
735 foreach ($ids as $id)
736 $focus->undoById($id);
740 * clean id's when being imported
742 * @param string $string
745 protected function _convertId($string)
747 return preg_replace_callback(
748 '|[^A-Za-z0-9\-\_]|',
750 // single quotes are essential here,
751 // or alternative escape all $ as \$
753 'return ord($matches[0]);'
758 public function retrieveAdvancedMapping()
760 $advancedMappingSettings = array();
762 //harvest the dupe index settings
763 if( isset($_REQUEST['enabled_dupes']) )
765 $toDecode = html_entity_decode ($_REQUEST['enabled_dupes'], ENT_QUOTES);
766 $dupe_ind = json_decode($toDecode);
768 foreach($dupe_ind as $dupe)
770 $advancedMappingSettings['dupe_'.$dupe] = $dupe;
774 foreach($_REQUEST as $rk=>$rv)
776 //harvest the import locale settings
777 if(strpos($rk,'portlocale_')>0)
779 $advancedMappingSettings[$rk] = $rv;
783 return $advancedMappingSettings;
786 public static function getImportableModules()
789 $importableModules = array();
790 foreach ($beanList as $moduleName => $beanName)
792 if( class_exists($beanName) )
794 $tmp = new $beanName();
795 if( isset($tmp->importable) && $tmp->importable )
797 $label = isset($GLOBALS['app_list_strings']['moduleList'][$moduleName]) ? $GLOBALS['app_list_strings']['moduleList'][$moduleName] : $moduleName;
798 $importableModules[$moduleName] = $label;
803 asort($importableModules);
804 return $importableModules;
809 * Replaces PHP error handler in Step4
812 * @param string $errstr
813 * @param string $errfile
814 * @param string $errline
816 public static function handleImportErrors($errno, $errstr, $errfile, $errline)
818 $GLOBALS['log']->fatal("Caught error: $errstr");
820 if ( !defined('E_DEPRECATED') )
821 define('E_DEPRECATED','8192');
822 if ( !defined('E_USER_DEPRECATED') )
823 define('E_USER_DEPRECATED','16384');
829 $message = "ERROR: [$errno] $errstr on line $errline in file $errfile<br />\n";
834 $message = "WARNING: [$errno] $errstr on line $errline in file $errfile<br />\n";
838 $message = "NOTICE: [$errno] $errstr on line $errline in file $errfile<br />\n";
842 case E_USER_DEPRECATED:
843 // don't worry about these
844 // $message = "STRICT ERROR: [$errno] $errstr on line $errline in file $errfile<br />\n";
848 $message = "Unknown error type: [$errno] $errstr on line $errline in file $errfile<br />\n";
852 // check to see if current reporting level should be included based upon error_reporting() setting, if not
854 if (error_reporting() & $errno)
867 * upon bean save, the relationships are saved by SugarBean->save_relationship_changes() method, but those values depend on
868 * the request object and is not reliable during import. This function makes sure any defined related or parent id's are processed
869 * and their relationship saved.
871 public function checkRelatedIDsAfterSave($focus)
877 //check relationship fields first
878 if(!empty($focus->parent_id) && !empty($focus->parent_type)){
879 $relParentName = strtolower($focus->parent_type);
880 $relParentID = strtolower($focus->parent_id);
882 if(!empty($focus->related_id) && !empty($focus->related_type)){
883 $relName = strtolower($focus->related_type);
884 $relID = strtolower($focus->related_id);
887 //now refresh the bean and process for parent relationship
888 $focus->retrieve($focus->id);
889 if(!empty($relParentName) && !empty($relParentID)){
891 //grab the relationship and any available ids
892 if(!empty($focus->$relParentName)){
894 $focus->load_relationship($relParentName);
895 $rel_ids = $focus->$relParentName->get();
897 //if the current parent_id is not part of the stored rels, then add it
898 if(!in_array($relParentID, $rel_ids)){
899 $focus->$relParentName->add($relParentID);
904 //now lets process any related fields
905 if(!empty($relName) && !empty($relID)){
906 if(!empty($focus->$relName)){
908 $focus->load_relationship($relName);
909 $rel_ids = $focus->$relName->get();
911 //if the related_id is not part of the stored rels, then add it
912 if(!in_array($relID, $rel_ids)){
913 $focus->$relName->add($relID);