]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Import/Importer.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Import / Importer.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3
4 /*********************************************************************************
5  * SugarCRM Community Edition is a customer relationship management program developed by
6  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7  * 
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.
14  * 
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
18  * details.
19  * 
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
23  * 02110-1301 USA.
24  * 
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.
27  * 
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.
31  * 
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  ********************************************************************************/
38
39
40 require_once('modules/Import/ImportCacheFiles.php');
41 require_once('modules/Import/ImportFieldSanitize.php');
42 require_once('modules/Import/ImportDuplicateCheck.php');
43
44
45 class Importer
46 {
47     /**
48      * @var ImportFieldSanitizer
49      */
50     private $ifs;
51
52     /**
53      * @var Currency
54      */
55     private $defaultUserCurrency;
56
57     /**
58      * @var importColumns
59      */
60     private $importColumns;
61
62     /**
63      * @var importSource
64      */
65     private $importSource;
66
67     /**
68      * @var $isUpdateOnly
69      */
70     private $isUpdateOnly;
71
72     /**
73      * @var  $bean
74      */
75     private $bean;
76
77     /**
78      * @var sugarToExternalSourceFieldMap
79      */
80     private $sugarToExternalSourceFieldMap = array();
81
82
83     public function __construct($importSource, $bean)
84     {
85         global $mod_strings, $sugar_config;
86
87         $this->importSource = $importSource;
88
89         //Vanilla copy of the bean object.
90         $this->bean = $bean;
91
92         // use our own error handler
93         set_error_handler(array('Importer','handleImportErrors'),E_ALL);
94
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));
97
98         // stop the tracker
99         TrackerManager::getInstance()->pause();
100
101         // set the default locale settings
102         $this->ifs = $this->getFieldSanitizer();
103
104         //Get the default user currency
105         $this->defaultUserCurrency = new Currency();
106         $this->defaultUserCurrency->retrieve('-99');
107
108         //Get our import column definitions
109         $this->importColumns = $this->getImportColumns();
110         $this->isUpdateOnly = ( isset($_REQUEST['import_type']) && $_REQUEST['import_type'] == 'update' );
111     }
112
113     public function import()
114     {
115         foreach($this->importSource as $row)
116         {
117             $this->importRow($row);
118         }
119
120         // save mapping if requested
121         if ( isset($_REQUEST['save_map_as']) && $_REQUEST['save_map_as'] != '' )
122         {
123             $this->saveMappingFile();
124         }
125
126         $this->importSource->writeStatus();
127
128         //All done, remove file.
129     }
130
131
132     protected function importRow($row)
133     {
134         global $sugar_config, $mod_strings;
135
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();
142         $do_save = true;
143
144         for ( $fieldNum = 0; $fieldNum < $_REQUEST['columncount']; $fieldNum++ )
145         {
146             // loop if this column isn't set
147             if ( !isset($this->importColumns[$fieldNum]) )
148                 continue;
149
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) )
157                 continue;
158
159             // translate strings
160             global $locale;
161             if(empty($locale))
162             {
163                 $locale = new Localization();
164             }
165             if ( isset($row[$fieldNum]) )
166             {
167                 $rowValue = $locale->translateCharset(strip_tags(trim($row[$fieldNum])),$this->importSource->importlocale_charset,$sugar_config['default_charset']);
168             }
169             else if( isset($this->sugarToExternalSourceFieldMap[$field]) && isset($row[$this->sugarToExternalSourceFieldMap[$field]]) )
170             {
171                 $rowValue = $locale->translateCharset(strip_tags(trim($row[$this->sugarToExternalSourceFieldMap[$field]])),$this->importSource->importlocale_charset,$sugar_config['default_charset']);
172             }
173             else
174             {
175                 $rowValue = '';
176             }
177
178             // If there is an default value then use it instead
179             if ( !empty($_REQUEST[$field]) )
180             {
181                 $defaultRowValue = $this->populateDefaultMapValue($field, $_REQUEST[$field], $fieldDef);
182
183
184                 if( empty($rowValue))
185                 {
186                     $rowValue = $defaultRowValue;
187                     //reset the default value to empty
188                     $defaultRowValue='';
189                 }
190             }
191
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) )
194                 continue;
195
196             // loop if this value has not been set
197             if ( !isset($rowValue) )
198                 continue;
199
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')
202             {
203                 $this->importSource->writeError( $mod_strings['LBL_REQUIRED_VALUE'],$fieldTranslated,'NULL');
204                 $do_save = false;
205             }
206
207             // Handle the special case "Sync to Outlook"
208             if ( $focus->object_name == "Contacts" && $field == 'sync_contact' )
209             {
210                 $bad_names = array();
211                 $returnValue = $this->ifs->synctooutlook($rowValue,$fieldDef,$bad_names);
212                 // try the default value on fail
213                 if ( !$returnValue && !empty($defaultRowValue) )
214                     $returnValue = $this->ifs->synctooutlook($defaultRowValue, $fieldDef, $bad_names);
215                 if ( !$returnValue )
216                 {
217                     $this->importSource->writeError($mod_strings['LBL_ERROR_SYNC_USERS'], $fieldTranslated, explode(",",$bad_names));
218                     $do_save = 0;
219                 }
220             }
221
222             // Handle email1 and email2 fields ( these don't have the type of email )
223             if ( $field == 'email1' || $field == 'email2' )
224             {
225                 $returnValue = $this->ifs->email($rowValue, $fieldDef, $focus);
226                 // try the default value on fail
227                 if ( !$returnValue && !empty($defaultRowValue) )
228                     $returnValue = $this->ifs->email( $defaultRowValue, $fieldDef);
229                 if ( $returnValue === FALSE )
230                 {
231                     $do_save=0;
232                     $this->importSource->writeError( $mod_strings['LBL_ERROR_INVALID_EMAIL'], $fieldTranslated, $rowValue);
233                 }
234                 else
235                 {
236                     $rowValue = $returnValue;
237                     // check for current opt_out and invalid email settings for this email address
238                     // if we find any, set them now
239                     $emailres = $focus->db->query( "SELECT opt_out, invalid_email FROM email_addresses WHERE email_address = '".$focus->db->quote($rowValue)."'");
240                     if ( $emailrow = $focus->db->fetchByAssoc($emailres) )
241                     {
242                         $focus->email_opt_out = $emailrow['opt_out'];
243                         $focus->invalid_email = $emailrow['invalid_email'];
244                     }
245                 }
246             }
247
248             // Handle splitting Full Name into First and Last Name parts
249             if ( $field == 'full_name' && !empty($rowValue) )
250             {
251                 $this->ifs->fullname($rowValue,$fieldDef,$focus);
252             }
253
254             // to maintain 451 compatiblity
255             if(!isset($fieldDef['module']) && $fieldDef['type']=='relate')
256                 $fieldDef['module'] = ucfirst($fieldDef['table']);
257
258             if(isset($fieldDef['custom_type']) && !empty($fieldDef['custom_type']))
259                 $fieldDef['type'] = $fieldDef['custom_type'];
260
261             // If the field is empty then there is no need to check the data
262             if( !empty($rowValue) )
263             {
264                 //Start
265                 $rowValue = $this->sanitizeFieldValueByType($rowValue, $fieldDef, $defaultRowValue, $focus, $fieldTranslated);
266                 if($rowValue === FALSE)
267                     continue;
268
269             }
270
271             // if the parent type is in singular form, get the real module name for parent_type
272             if (isset($fieldDef['type']) && $fieldDef['type']=='parent_type') {
273                 $rowValue = get_module_from_singular($rowValue);
274             }
275
276             $focus->$field = $rowValue;
277             unset($defaultRowValue);
278         }
279
280         // Now try to validate flex relate fields
281         if ( isset($focus->field_defs['parent_name']) && isset($focus->parent_name) && ($focus->field_defs['parent_name']['type'] == 'parent') )
282         {
283             // populate values from the picker widget if the import file doesn't have them
284             $parent_idField = $focus->field_defs['parent_name']['id_name'];
285             if ( empty($focus->$parent_idField) && !empty($_REQUEST[$parent_idField]) )
286                 $focus->$parent_idField = $_REQUEST[$parent_idField];
287
288             $parent_typeField = $focus->field_defs['parent_name']['type_name'];
289
290             if ( empty($focus->$parent_typeField) && !empty($_REQUEST[$parent_typeField]) )
291                 $focus->$parent_typeField = $_REQUEST[$parent_typeField];
292             // now validate it
293             $returnValue = $this->ifs->parent($focus->parent_name,$focus->field_defs['parent_name'],$focus, empty($_REQUEST['parent_name']));
294             if ( !$returnValue && !empty($_REQUEST['parent_name']) )
295                 $returnValue = $this->ifs->parent( $_REQUEST['parent_name'],$focus->field_defs['parent_name'], $focus);
296         }
297
298         // check to see that the indexes being entered are unique.
299         if (isset($_REQUEST['enabled_dupes']) && $_REQUEST['enabled_dupes'] != "")
300         {
301             $toDecode = html_entity_decode  ($_REQUEST['enabled_dupes'], ENT_QUOTES);
302             $enabled_dupes = json_decode($toDecode);
303             $idc = new ImportDuplicateCheck($focus);
304
305             if ( $idc->isADuplicateRecord($enabled_dupes) )
306             {
307                 $this->importSource->markRowAsDuplicate($idc->_dupedFields);
308                 $this->_undoCreatedBeans($this->ifs->createdBeans);
309                 return;
310             }
311         }
312         //Allow fields to be passed in for dup check as well (used by external adapters)
313         else if( !empty($_REQUEST['enabled_dup_fields']) )
314         {
315             $toDecode = html_entity_decode  ($_REQUEST['enabled_dup_fields'], ENT_QUOTES);
316             $enabled_dup_fields = json_decode($toDecode);
317             $idc = new ImportDuplicateCheck($focus);
318             if ( $idc->isADuplicateRecordByFields($enabled_dup_fields) )
319             {
320                 $this->importSource->markRowAsDuplicate($idc->_dupedFields);
321                 $this->_undoCreatedBeans($this->ifs->createdBeans);
322                 return;
323             }
324         }
325
326         // if the id was specified
327         $newRecord = true;
328         if ( !empty($focus->id) )
329         {
330             $focus->id = $this->_convertId($focus->id);
331
332             // check if it already exists
333             $query = "SELECT * FROM {$focus->table_name} WHERE id='".$focus->db->quote($focus->id)."'";
334             $result = $focus->db->query($query)
335             or sugar_die("Error selecting sugarbean: ");
336
337             $dbrow = $focus->db->fetchByAssoc($result);
338
339             if (isset ($dbrow['id']) && $dbrow['id'] != -1)
340             {
341                 // if it exists but was deleted, just remove it
342                 if (isset ($dbrow['deleted']) && $dbrow['deleted'] == 1 && $this->isUpdateOnly ==false)
343                 {
344                     $this->removeDeletedBean($focus);
345                     $focus->new_with_id = true;
346                 }
347                 else
348                 {
349                     if( ! $this->isUpdateOnly )
350                     {
351                         $this->importSource->writeError($mod_strings['LBL_ID_EXISTS_ALREADY'],'ID',$focus->id);
352                         $this->_undoCreatedBeans($this->ifs->createdBeans);
353                         return;
354                     }
355
356                     $clonedBean = $this->cloneExistingBean($focus);
357                     if($clonedBean === FALSE)
358                     {
359                         $this->importSource->writeError($mod_strings['LBL_RECORD_CANNOT_BE_UPDATED'],'ID',$focus->id);
360                         $this->_undoCreatedBeans($this->ifs->createdBeans);
361                         return;
362                     }
363                     else
364                     {
365                         $focus = $clonedBean;
366                         $newRecord = FALSE;
367                     }
368                 }
369             }
370             else
371             {
372                 $focus->new_with_id = true;
373             }
374         }
375
376         if ($do_save)
377         {
378             $this->saveImportBean($focus, $newRecord);
379             // Update the created/updated counter
380             $this->importSource->markRowAsImported($newRecord);
381         }
382         else
383             $this->_undoCreatedBeans($this->ifs->createdBeans);
384
385         unset($defaultRowValue);
386
387     }
388
389
390     protected function sanitizeFieldValueByType($rowValue, $fieldDef, $defaultRowValue, $focus, $fieldTranslated)
391     {
392         global $mod_strings, $app_list_strings;
393         switch ($fieldDef['type'])
394         {
395             case 'enum':
396             case 'multienum':
397                 if ( isset($fieldDef['type']) && $fieldDef['type'] == "multienum" )
398                     $returnValue = $this->ifs->multienum($rowValue,$fieldDef);
399                 else
400                     $returnValue = $this->ifs->enum($rowValue,$fieldDef);
401                 // try the default value on fail
402                 if ( !$returnValue && !empty($defaultRowValue) )
403                 {
404                     if ( isset($fieldDef['type']) && $fieldDef['type'] == "multienum" )
405                         $returnValue = $this->ifs->multienum($defaultRowValue,$fieldDef);
406                     else
407                         $returnValue = $this->ifs->enum($defaultRowValue,$fieldDef);
408                 }
409                 if ( $returnValue === FALSE )
410                 {
411                     $this->importSource->writeError($mod_strings['LBL_ERROR_NOT_IN_ENUM'] . implode(",",$app_list_strings[$fieldDef['options']]), $fieldTranslated,$rowValue);
412                     return FALSE;
413                 }
414                 else
415                     return $returnValue;
416
417                 break;
418             case 'relate':
419             case 'parent':
420                 $returnValue = $this->ifs->relate($rowValue, $fieldDef, $focus, empty($defaultRowValue));
421                 if (!$returnValue && !empty($defaultRowValue))
422                     $returnValue = $this->ifs->relate($defaultRowValue,$fieldDef, $focus);
423                 // Bug 33623 - Set the id value found from the above method call as an importColumn
424                 if ($returnValue !== false)
425                     $this->importColumns[] = $fieldDef['id_name'];
426                 return $rowValue;
427                 break;
428             case 'teamset':
429                 $this->ifs->teamset($rowValue,$fieldDef,$focus);
430                 $this->importColumns[] = 'team_set_id';
431                 $this->importColumns[] = 'team_id';
432                 return $rowValue;
433                 break;
434             case 'fullname':
435                 return $rowValue;
436                 break;
437             default:
438                 $fieldtype = $fieldDef['type'];
439                 $returnValue = $this->ifs->$fieldtype($rowValue, $fieldDef, $focus);
440                 // try the default value on fail
441                 if ( !$returnValue && !empty($defaultRowValue) )
442                     $returnValue = $this->ifs->$fieldtype($defaultRowValue,$fieldDef, $focus);
443                 if ( !$returnValue )
444                 {
445                     $this->importSource->writeError($mod_strings['LBL_ERROR_INVALID_'.strtoupper($fieldDef['type'])],$fieldTranslated,$rowValue,$focus);
446                     return FALSE;
447                 }
448                 return $returnValue;
449         }
450     }
451
452     protected function cloneExistingBean($focus)
453     {
454         $existing_focus = clone $this->bean;
455         if ( !( $existing_focus->retrieve($focus->id) instanceOf SugarBean ) )
456         {
457             return FALSE;
458         }
459         else
460         {
461             $newData = $focus->toArray();
462             foreach ( $newData as $focus_key => $focus_value )
463                 if ( in_array($focus_key,$this->importColumns) )
464                     $existing_focus->$focus_key = $focus_value;
465
466             return $existing_focus;
467         }
468     }
469
470     protected function removeDeletedBean($focus)
471     {
472         global $mod_strings;
473
474         $query2 = "DELETE FROM {$focus->table_name} WHERE id='".$focus->db->quote($focus->id)."'";
475         $result2 = $focus->db->query($query2) or sugar_die($mod_strings['LBL_ERROR_DELETING_RECORD']." ".$focus->id);
476         if ($focus->hasCustomFields())
477         {
478             $query3 = "DELETE FROM {$focus->table_name}_cstm WHERE id_c='".$focus->db->quote($focus->id)."'";
479             $result2 = $focus->db->query($query3);
480         }
481     }
482
483     protected function saveImportBean($focus, $newRecord)
484     {
485         global $timedate, $current_user;
486
487         // Populate in any default values to the bean
488         $focus->populateDefaultValues();
489
490         if ( !isset($focus->assigned_user_id) || $focus->assigned_user_id == '' && $newRecord )
491         {
492             $focus->assigned_user_id = $current_user->id;
493         }
494         /*
495         * Bug 34854: Added all conditions besides the empty check on date modified.
496         */
497         if ( ( !empty($focus->new_with_id) && !empty($focus->date_modified) ) ||
498              ( empty($focus->new_with_id) && $timedate->to_db($focus->date_modified) != $timedate->to_db($timedate->to_display_date_time($focus->fetched_row['date_modified'])) )
499         )
500             $focus->update_date_modified = false;
501
502         $focus->optimistic_lock = false;
503         if ( $focus->object_name == "Contacts" && isset($focus->sync_contact) )
504         {
505             //copy the potential sync list to another varible
506             $list_of_users=$focus->sync_contact;
507             //and set it to false for the save
508             $focus->sync_contact=false;
509         }
510         else if($focus->object_name == "User" && !empty($current_user) && $focus->is_admin && !is_admin($current_user) && is_admin_for_module($current_user, 'Users')) {
511             sugar_die($GLOBALS['mod_strings']['ERR_IMPORT_SYSTEM_ADMININSTRATOR']);
512         }
513         //bug# 40260 setting it true as the module in focus is involved in an import
514         $focus->in_import=true;
515         // call any logic needed for the module preSave
516         $focus->beforeImportSave();
517
518         // if modified_user_id is set, set the flag to false so SugarBEan will not reset it
519         if (isset($focus->modified_user_id) && $focus->modified_user_id) {
520             $focus->update_modified_by = false;
521         }
522         // if created_by is set, set the flag to false so SugarBEan will not reset it
523         if (isset($focus->created_by) && $focus->created_by) {
524             $focus->set_created_by = false;
525         }
526
527         $focus->save(false);
528
529         // call any logic needed for the module postSave
530         $focus->afterImportSave();
531
532         if ( $focus->object_name == "Contacts" && isset($list_of_users) )
533             $focus->process_sync_to_outlook($list_of_users);
534
535         // Add ID to User's Last Import records
536         if ( $newRecord )
537             $this->importSource->writeRowToLastImport( $_REQUEST['import_module'],($focus->object_name == 'Case' ? 'aCase' : $focus->object_name),$focus->id);
538
539     }
540
541     protected function saveMappingFile()
542     {
543         global $current_user;
544
545         $firstrow    = unserialize(base64_decode($_REQUEST['firstrow']));
546         $mappingValsArr = $this->importColumns;
547         $mapping_file = new ImportMap();
548         if ( isset($_REQUEST['has_header']) && $_REQUEST['has_header'] == 'on')
549         {
550             $header_to_field = array ();
551             foreach ($this->importColumns as $pos => $field_name)
552             {
553                 if (isset($firstrow[$pos]) && isset($field_name))
554                 {
555                     $header_to_field[$firstrow[$pos]] = $field_name;
556                 }
557             }
558
559             $mappingValsArr = $header_to_field;
560         }
561         //get array of values to save for duplicate and locale settings
562         $advMapping = $this->retrieveAdvancedMapping();
563
564         //merge with mappingVals array
565         if(!empty($advMapping) && is_array($advMapping))
566         {
567             $mappingValsArr = array_merge($mappingValsArr,$advMapping);
568         }
569
570         //set mapping
571         $mapping_file->setMapping($mappingValsArr);
572
573         // save default fields
574         $defaultValues = array();
575         for ( $i = 0; $i < $_REQUEST['columncount']; $i++ )
576         {
577             if (isset($this->importColumns[$i]) && !empty($_REQUEST[$this->importColumns[$i]]))
578             {
579                 $field = $this->importColumns[$i];
580                 $fieldDef = $this->bean->getFieldDefinition($field);
581                 if(!empty($fieldDef['custom_type']) && $fieldDef['custom_type'] == 'teamset')
582                 {
583                     require_once('include/SugarFields/Fields/Teamset/SugarFieldTeamset.php');
584                     $sugar_field = new SugarFieldTeamset('Teamset');
585                     $teams = $sugar_field->getTeamsFromRequest($field);
586                     if(isset($_REQUEST['primary_team_name_collection']))
587                     {
588                         $primary_index = $_REQUEST['primary_team_name_collection'];
589                     }
590
591                     //If primary_index was selected, ensure that the first Array entry is the primary team
592                     if(isset($primary_index))
593                     {
594                         $count = 0;
595                         $new_teams = array();
596                         foreach($teams as $id=>$name)
597                         {
598                             if($primary_index == $count++)
599                             {
600                                 $new_teams[$id] = $name;
601                                 unset($teams[$id]);
602                                 break;
603                             }
604                         }
605
606                         foreach($teams as $id=>$name)
607                         {
608                             $new_teams[$id] = $name;
609                         }
610                         $teams = $new_teams;
611                     } //if
612
613                     $json = getJSONobj();
614                     $defaultValues[$field] = $json->encode($teams);
615                 }
616                 else
617                 {
618                     $defaultValues[$field] = $_REQUEST[$this->importColumns[$i]];
619                 }
620             }
621         }
622         $mapping_file->setDefaultValues($defaultValues);
623         $result = $mapping_file->save( $current_user->id,  $_REQUEST['save_map_as'], $_REQUEST['import_module'], $_REQUEST['source'],
624             ( isset($_REQUEST['has_header']) && $_REQUEST['has_header'] == 'on'), $_REQUEST['custom_delimiter'], html_entity_decode($_REQUEST['custom_enclosure'],ENT_QUOTES)
625         );
626     }
627
628
629     protected function populateDefaultMapValue($field, $fieldValue, $fieldDef)
630     {
631         global $timedate, $current_user;
632
633         if ( is_array($fieldValue) )
634             $defaultRowValue = encodeMultienumValue($fieldValue);
635         else
636             $defaultRowValue = $_REQUEST[$field];
637         // translate default values to the date/time format for the import file
638         if( $fieldDef['type'] == 'date' && $this->ifs->dateformat != $timedate->get_date_format() )
639             $defaultRowValue = $timedate->swap_formats($defaultRowValue, $this->ifs->dateformat, $timedate->get_date_format());
640
641         if( $fieldDef['type'] == 'time' && $this->ifs->timeformat != $timedate->get_time_format() )
642             $defaultRowValue = $timedate->swap_formats($defaultRowValue, $this->ifs->timeformat, $timedate->get_time_format());
643
644         if( ($fieldDef['type'] == 'datetime' || $fieldDef['type'] == 'datetimecombo') && $this->ifs->dateformat.' '.$this->ifs->timeformat != $timedate->get_date_time_format() )
645             $defaultRowValue = $timedate->swap_formats($defaultRowValue, $this->ifs->dateformat.' '.$this->ifs->timeformat,$timedate->get_date_time_format());
646
647         if ( in_array($fieldDef['type'],array('currency','float','int','num')) && $this->ifs->num_grp_sep != $current_user->getPreference('num_grp_sep') )
648             $defaultRowValue = str_replace($current_user->getPreference('num_grp_sep'), $this->ifs->num_grp_sep,$defaultRowValue);
649
650         if ( in_array($fieldDef['type'],array('currency','float')) && $this->ifs->dec_sep != $current_user->getPreference('dec_sep') )
651             $defaultRowValue = str_replace($current_user->getPreference('dec_sep'), $this->ifs->dec_sep,$defaultRowValue);
652
653         $user_currency_symbol = $this->defaultUserCurrency->symbol;
654         if ( $fieldDef['type'] == 'currency' && $this->ifs->currency_symbol != $user_currency_symbol )
655             $defaultRowValue = str_replace($user_currency_symbol, $this->ifs->currency_symbol,$defaultRowValue);
656
657         return $defaultRowValue;
658     }
659
660     protected function getImportColumns()
661     {
662         $importable_fields = $this->bean->get_importable_fields();
663         $importColumns = array();
664         foreach ($_REQUEST as $name => $value)
665         {
666             // only look for var names that start with "fieldNum"
667             if (strncasecmp($name, "colnum_", 7) != 0)
668                 continue;
669
670             // pull out the column position for this field name
671             $pos = substr($name, 7);
672
673             if ( isset($importable_fields[$value]) )
674             {
675                 // now mark that we've seen this field
676                 $importColumns[$pos] = $value;
677             }
678         }
679
680         return $importColumns;
681     }
682
683     protected function getFieldSanitizer()
684     {
685         $ifs = new ImportFieldSanitize();
686         $copyFields = array('dateformat','timeformat','timezone','default_currency_significant_digits','num_grp_sep','dec_sep','default_locale_name_format');
687         foreach($copyFields as $field)
688         {
689             $fieldKey = "importlocale_$field";
690             $ifs->$field = $this->importSource->$fieldKey;
691         }
692
693         $currency = new Currency();
694         $currency->retrieve($this->importSource->importlocale_currency);
695         $ifs->currency_symbol = $currency->symbol;
696
697         return $ifs;
698     }
699
700     /**
701      * Sets a translation map from sugar field key to external source key used while importing a row.  This allows external sources
702      * to return a data set that is an associative array rather than numerically indexed.
703      *
704      * @param  $translator
705      * @return void
706      */
707     public function setFieldKeyTranslator($translator)
708     {
709         $this->sugarToExternalSourceFieldMap = $translator;
710     }
711
712     /**
713      * If a bean save is not done for some reason, this method will undo any of the beans that were created
714      *
715      * @param array $ids ids of user_last_import records created
716      */
717     protected function _undoCreatedBeans( array $ids )
718     {
719         $focus = new UsersLastImport();
720         foreach ($ids as $id)
721             $focus->undoById($id);
722     }
723
724     /**
725      * clean id's when being imported
726      *
727      * @param  string $string
728      * @return string
729      */
730     protected function _convertId($string)
731     {
732         return preg_replace_callback(
733             '|[^A-Za-z0-9\-\_]|',
734             create_function(
735             // single quotes are essential here,
736             // or alternative escape all $ as \$
737             '$matches',
738             'return ord($matches[0]);'
739                  ) ,
740             $string);
741     }
742
743     public function retrieveAdvancedMapping()
744     {
745         $advancedMappingSettings = array();
746
747         //harvest the dupe index settings
748         if( isset($_REQUEST['enabled_dupes']) )
749         {
750             $toDecode = html_entity_decode  ($_REQUEST['enabled_dupes'], ENT_QUOTES);
751             $dupe_ind = json_decode($toDecode);
752
753             foreach($dupe_ind as $dupe)
754             {
755                 $advancedMappingSettings['dupe_'.$dupe] = $dupe;
756             }
757         }
758
759         foreach($_REQUEST as $rk=>$rv)
760         {
761             //harvest the import locale settings
762             if(strpos($rk,'portlocale_')>0)
763             {
764                 $advancedMappingSettings[$rk] = $rv;
765             }
766
767         }
768         return $advancedMappingSettings;
769     }
770
771     public static function getImportableModules()
772     {
773         global $beanList;
774         $importableModules = array();
775         foreach ($beanList as $moduleName => $beanName)
776         {
777             if( class_exists($beanName) )
778             {
779                 $tmp = new $beanName();
780                 if( isset($tmp->importable) && $tmp->importable )
781                 {
782                     $label = isset($GLOBALS['app_list_strings']['moduleList'][$moduleName]) ? $GLOBALS['app_list_strings']['moduleList'][$moduleName] : $moduleName;
783                     $importableModules[$moduleName] = $label;
784                 }
785             }
786         }
787
788         asort($importableModules);
789         return $importableModules;
790     }
791
792
793     /**
794      * Replaces PHP error handler in Step4
795      *
796      * @param int    $errno
797      * @param string $errstr
798      * @param string $errfile
799      * @param string $errline
800      */
801     public static function handleImportErrors($errno, $errstr, $errfile, $errline)
802     {
803         $GLOBALS['log']->fatal("Caught error: $errstr");
804
805         if ( !defined('E_DEPRECATED') )
806             define('E_DEPRECATED','8192');
807         if ( !defined('E_USER_DEPRECATED') )
808             define('E_USER_DEPRECATED','16384');
809
810         $isFatal = false;
811         switch ($errno)
812         {
813             case E_USER_ERROR:
814                 $message = "ERROR: [$errno] $errstr on line $errline in file $errfile<br />\n";
815                 $isFatal = true;
816                 break;
817             case E_USER_WARNING:
818             case E_WARNING:
819                 $message = "WARNING: [$errno] $errstr on line $errline in file $errfile<br />\n";
820                 break;
821             case E_USER_NOTICE:
822             case E_NOTICE:
823                 $message = "NOTICE: [$errno] $errstr on line $errline in file $errfile<br />\n";
824                 break;
825             case E_STRICT:
826             case E_DEPRECATED:
827             case E_USER_DEPRECATED:
828                 // don't worry about these
829                 // $message = "STRICT ERROR: [$errno] $errstr on line $errline in file $errfile<br />\n";
830                 $message = "";
831                 break;
832             default:
833                 $message = "Unknown error type: [$errno] $errstr on line $errline in file $errfile<br />\n";
834                 break;
835         }
836
837         // check to see if current reporting level should be included based upon error_reporting() setting, if not
838         // then just return
839         if (error_reporting() & $errno)
840         {
841             echo $message;
842         }
843
844         if ($isFatal)
845         {
846             exit(1);
847         }
848     }
849 }