Example #1
0
 protected function importRow($row)
 {
     global $sugar_config, $mod_strings, $current_user;
     $focus = clone $this->bean;
     $focus->unPopulateDefaultValues();
     $focus->save_from_post = false;
     $focus->team_id = null;
     $this->ifs->createdBeans = array();
     $this->importSource->resetRowErrorCounter();
     $do_save = true;
     for ($fieldNum = 0; $fieldNum < $_REQUEST['columncount']; $fieldNum++) {
         // loop if this column isn't set
         if (!isset($this->importColumns[$fieldNum])) {
             continue;
         }
         // get this field's properties
         $field = $this->importColumns[$fieldNum];
         $fieldDef = $focus->getFieldDefinition($field);
         $fieldTranslated = translate(isset($fieldDef['vname']) ? $fieldDef['vname'] : $fieldDef['name'], $focus->module_dir) . " (" . $fieldDef['name'] . ")";
         $defaultRowValue = '';
         // Bug 37241 - Don't re-import over a field we already set during the importing of another field
         if (!empty($focus->{$field})) {
             continue;
         }
         // translate strings
         global $locale;
         if (empty($locale)) {
             $locale = new Localization();
         }
         if (isset($row[$fieldNum])) {
             $rowValue = $locale->translateCharset(strip_tags(trim($row[$fieldNum])), $this->importSource->importlocale_charset, $sugar_config['default_charset']);
         } else {
             if (isset($this->sugarToExternalSourceFieldMap[$field]) && isset($row[$this->sugarToExternalSourceFieldMap[$field]])) {
                 $rowValue = $locale->translateCharset(strip_tags(trim($row[$this->sugarToExternalSourceFieldMap[$field]])), $this->importSource->importlocale_charset, $sugar_config['default_charset']);
             } else {
                 $rowValue = '';
             }
         }
         // If there is an default value then use it instead
         if (!empty($_REQUEST[$field])) {
             $defaultRowValue = $this->populateDefaultMapValue($field, $_REQUEST[$field], $fieldDef);
             if (empty($rowValue)) {
                 $rowValue = $defaultRowValue;
                 //reset the default value to empty
                 $defaultRowValue = '';
             }
         }
         // Bug 22705 - Don't update the First Name or Last Name value if Full Name is set
         if (in_array($field, array('first_name', 'last_name')) && !empty($focus->full_name)) {
             continue;
         }
         // loop if this value has not been set
         if (!isset($rowValue)) {
             continue;
         }
         // If the field is required and blank then error out
         if (array_key_exists($field, $focus->get_import_required_fields()) && empty($rowValue) && $rowValue != '0') {
             $this->importSource->writeError($mod_strings['LBL_REQUIRED_VALUE'], $fieldTranslated, 'NULL');
             $do_save = false;
         }
         // Handle the special case "Sync to Outlook"
         if ($focus->object_name == "Contact" && $field == 'sync_contact') {
             /**
              * Bug #41194 : if true used as value of sync_contact - add curent user to list to sync
              */
             if (true == $rowValue || 'true' == strtolower($rowValue)) {
                 $focus->sync_contact = $focus->id;
             } elseif (false == $rowValue || 'false' == strtolower($rowValue)) {
                 $focus->sync_contact = '';
             } else {
                 $bad_names = array();
                 $returnValue = $this->ifs->synctooutlook($rowValue, $fieldDef, $bad_names);
                 // try the default value on fail
                 if (!$returnValue && !empty($defaultRowValue)) {
                     $returnValue = $this->ifs->synctooutlook($defaultRowValue, $fieldDef, $bad_names);
                 }
                 if (!$returnValue) {
                     $this->importSource->writeError($mod_strings['LBL_ERROR_SYNC_USERS'], $fieldTranslated, $bad_names);
                     $do_save = 0;
                 } else {
                     $focus->sync_contact = $returnValue;
                 }
             }
         }
         // Handle email field, if it's a semi-colon separated export
         if ($field == 'email_addresses_non_primary' && !empty($rowValue)) {
             if (strpos($rowValue, ';') !== false) {
                 $rowValue = explode(';', $rowValue);
             } else {
                 $rowValue = array($rowValue);
             }
         }
         // Handle email1 and email2 fields ( these don't have the type of email )
         if ($field == 'email1' || $field == 'email2') {
             $returnValue = $this->ifs->email($rowValue, $fieldDef, $focus);
             // try the default value on fail
             if (!$returnValue && !empty($defaultRowValue)) {
                 $returnValue = $this->ifs->email($defaultRowValue, $fieldDef);
             }
             if ($returnValue === FALSE) {
                 $do_save = 0;
                 $this->importSource->writeError($mod_strings['LBL_ERROR_INVALID_EMAIL'], $fieldTranslated, $rowValue);
             } else {
                 $rowValue = $returnValue;
                 // check for current opt_out and invalid email settings for this email address
                 // if we find any, set them now
                 $emailres = $focus->db->query("SELECT opt_out, invalid_email FROM email_addresses WHERE email_address = '" . $focus->db->quote($rowValue) . "'");
                 if ($emailrow = $focus->db->fetchByAssoc($emailres)) {
                     $focus->email_opt_out = $emailrow['opt_out'];
                     $focus->invalid_email = $emailrow['invalid_email'];
                 }
             }
         }
         // Handle splitting Full Name into First and Last Name parts
         if ($field == 'full_name' && !empty($rowValue)) {
             $this->ifs->fullname($rowValue, $fieldDef, $focus);
         }
         // to maintain 451 compatiblity
         if (!isset($fieldDef['module']) && $fieldDef['type'] == 'relate') {
             $fieldDef['module'] = ucfirst($fieldDef['table']);
         }
         if (isset($fieldDef['custom_type']) && !empty($fieldDef['custom_type'])) {
             $fieldDef['type'] = $fieldDef['custom_type'];
         }
         // If the field is empty then there is no need to check the data
         if (!empty($rowValue)) {
             // If it's an array of non-primary e-mails, check each mail
             if ($field == "email_addresses_non_primary" && is_array($rowValue)) {
                 foreach ($rowValue as $tempRow) {
                     $tempRow = $this->sanitizeFieldValueByType($tempRow, $fieldDef, $defaultRowValue, $focus, $fieldTranslated);
                     if ($tempRow === FALSE) {
                         $rowValue = false;
                         $do_save = false;
                         break;
                     }
                 }
             } else {
                 $rowValue = $this->sanitizeFieldValueByType($rowValue, $fieldDef, $defaultRowValue, $focus, $fieldTranslated);
             }
             if ($rowValue === false) {
                 /* BUG 51213 - jeff @ neposystems.com */
                 $do_save = false;
                 continue;
             }
         }
         // if the parent type is in singular form, get the real module name for parent_type
         if (isset($fieldDef['type']) && $fieldDef['type'] == 'parent_type') {
             $rowValue = get_module_from_singular($rowValue);
         }
         $focus->{$field} = $rowValue;
         unset($defaultRowValue);
     }
     // Now try to validate flex relate fields
     if (isset($focus->field_defs['parent_name']) && isset($focus->parent_name) && $focus->field_defs['parent_name']['type'] == 'parent') {
         // populate values from the picker widget if the import file doesn't have them
         $parent_idField = $focus->field_defs['parent_name']['id_name'];
         if (empty($focus->{$parent_idField}) && !empty($_REQUEST[$parent_idField])) {
             $focus->{$parent_idField} = $_REQUEST[$parent_idField];
         }
         $parent_typeField = $focus->field_defs['parent_name']['type_name'];
         if (empty($focus->{$parent_typeField}) && !empty($_REQUEST[$parent_typeField])) {
             $focus->{$parent_typeField} = $_REQUEST[$parent_typeField];
         }
         // now validate it
         $returnValue = $this->ifs->parent($focus->parent_name, $focus->field_defs['parent_name'], $focus, empty($_REQUEST['parent_name']));
         if (!$returnValue && !empty($_REQUEST['parent_name'])) {
             $returnValue = $this->ifs->parent($_REQUEST['parent_name'], $focus->field_defs['parent_name'], $focus);
         }
     }
     // check to see that the indexes being entered are unique.
     if (isset($_REQUEST['enabled_dupes']) && $_REQUEST['enabled_dupes'] != "") {
         $toDecode = html_entity_decode($_REQUEST['enabled_dupes'], ENT_QUOTES);
         $enabled_dupes = json_decode($toDecode);
         $idc = new ImportDuplicateCheck($focus);
         if ($idc->isADuplicateRecord($enabled_dupes)) {
             $this->importSource->markRowAsDuplicate($idc->_dupedFields);
             $this->_undoCreatedBeans($this->ifs->createdBeans);
             return;
         }
     } else {
         if (!empty($_REQUEST['enabled_dup_fields'])) {
             $toDecode = html_entity_decode($_REQUEST['enabled_dup_fields'], ENT_QUOTES);
             $enabled_dup_fields = json_decode($toDecode);
             $idc = new ImportDuplicateCheck($focus);
             if ($idc->isADuplicateRecordByFields($enabled_dup_fields)) {
                 $this->importSource->markRowAsDuplicate($idc->_dupedFields);
                 $this->_undoCreatedBeans($this->ifs->createdBeans);
                 return;
             }
         }
     }
     // if the id was specified
     $newRecord = true;
     if (!empty($focus->id)) {
         $focus->id = $this->_convertId($focus->id);
         // check if it already exists
         $query = "SELECT * FROM {$focus->table_name} WHERE id='" . $focus->db->quote($focus->id) . "'";
         $result = $focus->db->query($query) or sugar_die("Error selecting sugarbean: ");
         $dbrow = $focus->db->fetchByAssoc($result);
         if (isset($dbrow['id']) && $dbrow['id'] != -1) {
             // if it exists but was deleted, just remove it
             if (isset($dbrow['deleted']) && $dbrow['deleted'] == 1 && $this->isUpdateOnly == false) {
                 $this->removeDeletedBean($focus);
                 $focus->new_with_id = true;
             } else {
                 if (!$this->isUpdateOnly) {
                     $this->importSource->writeError($mod_strings['LBL_ID_EXISTS_ALREADY'], 'ID', $focus->id);
                     $this->_undoCreatedBeans($this->ifs->createdBeans);
                     return;
                 }
                 $clonedBean = $this->cloneExistingBean($focus);
                 if ($clonedBean === FALSE) {
                     $this->importSource->writeError($mod_strings['LBL_RECORD_CANNOT_BE_UPDATED'], 'ID', $focus->id);
                     $this->_undoCreatedBeans($this->ifs->createdBeans);
                     return;
                 } else {
                     $focus = $clonedBean;
                     $newRecord = FALSE;
                 }
             }
         } else {
             $focus->new_with_id = true;
         }
     }
     if ($do_save) {
         $this->saveImportBean($focus, $newRecord);
         // Update the created/updated counter
         $this->importSource->markRowAsImported($newRecord);
     } else {
         $this->_undoCreatedBeans($this->ifs->createdBeans);
     }
     unset($defaultRowValue);
 }
Example #2
0
 /** 
  * @see SugarView::display()
  */
 public function display()
 {
     global $sugar_config;
     // Increase the max_execution_time since this step can take awhile
     ini_set("max_execution_time", max($sugar_config['import_max_execution_time'], 3600));
     // stop the tracker
     TrackerManager::getInstance()->pause();
     // use our own error handler
     set_error_handler('handleImportErrors', E_ALL);
     global $mod_strings, $app_strings, $current_user, $import_bean_map;
     global $app_list_strings, $timedate;
     $update_only = isset($_REQUEST['import_type']) && $_REQUEST['import_type'] == 'update';
     $firstrow = unserialize(base64_decode($_REQUEST['firstrow']));
     // All the Look Up Caches are initialized here
     $enum_lookup_cache = array();
     // Let's try and load the import bean
     $focus = loadImportBean($_REQUEST['import_module']);
     if (!$focus) {
         trigger_error($mod_strings['LBL_ERROR_IMPORTS_NOT_SET_UP'], E_USER_ERROR);
     }
     // setup the importable fields array.
     $importable_fields = $focus->get_importable_fields();
     // loop through all request variables
     $importColumns = array();
     foreach ($_REQUEST as $name => $value) {
         // only look for var names that start with "fieldNum"
         if (strncasecmp($name, "colnum_", 7) != 0) {
             continue;
         }
         // pull out the column position for this field name
         $pos = substr($name, 7);
         if (isset($importable_fields[$value])) {
             // now mark that we've seen this field
             $importColumns[$pos] = $value;
         }
     }
     // set the default locale settings
     $ifs = new ImportFieldSanitize();
     $ifs->dateformat = $_REQUEST['importlocale_dateformat'];
     $ifs->timeformat = $_REQUEST['importlocale_timeformat'];
     $ifs->timezone = $_REQUEST['importlocale_timezone'];
     $currency = new Currency();
     $currency->retrieve($_REQUEST['importlocale_currency']);
     $ifs->currency_symbol = $currency->symbol;
     $ifs->default_currency_significant_digits = $_REQUEST['importlocale_default_currency_significant_digits'];
     $ifs->num_grp_sep = $_REQUEST['importlocale_num_grp_sep'];
     $ifs->dec_sep = $_REQUEST['importlocale_dec_sep'];
     $ifs->default_locale_name_format = $_REQUEST['importlocale_default_locale_name_format'];
     // Check to be sure we are getting an import file that is in the right place
     if (realpath(dirname($_REQUEST['tmp_file']) . '/') != realpath($sugar_config['upload_dir'])) {
         trigger_error($mod_strings['LBL_CANNOT_OPEN'], E_USER_ERROR);
     }
     // Open the import file
     $importFile = new ImportFile($_REQUEST['tmp_file'], $_REQUEST['custom_delimiter'], html_entity_decode($_REQUEST['custom_enclosure'], ENT_QUOTES));
     if (!$importFile->fileExists()) {
         trigger_error($mod_strings['LBL_CANNOT_OPEN'], E_USER_ERROR);
     }
     $fieldDefs = $focus->getFieldDefinitions();
     unset($focus);
     while ($row = $importFile->getNextRow()) {
         $focus = loadImportBean($_REQUEST['import_module']);
         $focus->unPopulateDefaultValues();
         $focus->save_from_post = false;
         $focus->team_id = null;
         $ifs->createdBeans = array();
         $do_save = true;
         for ($fieldNum = 0; $fieldNum < $_REQUEST['columncount']; $fieldNum++) {
             // loop if this column isn't set
             if (!isset($importColumns[$fieldNum])) {
                 continue;
             }
             // get this field's properties
             $field = $importColumns[$fieldNum];
             $fieldDef = $focus->getFieldDefinition($field);
             $fieldTranslated = translate(isset($fieldDef['vname']) ? $fieldDef['vname'] : $fieldDef['name'], $_REQUEST['module']) . " (" . $fieldDef['name'] . ")";
             // Bug 37241 - Don't re-import over a field we already set during the importing of another field
             if (!empty($focus->{$field})) {
                 continue;
             }
             //DETERMINE WHETHER OR NOT $fieldDef['name'] IS DATE_MODIFIED AND SET A VAR, USE DOWN BELOW
             // translate strings
             global $locale;
             if (empty($locale)) {
                 $locale = new Localization();
             }
             if (isset($row[$fieldNum])) {
                 $rowValue = $locale->translateCharset(strip_tags(trim($row[$fieldNum])), $_REQUEST['importlocale_charset'], $sugar_config['default_charset']);
             } else {
                 $rowValue = '';
             }
             // If there is an default value then use it instead
             if (!empty($_REQUEST[$field])) {
                 if (is_array($_REQUEST[$field])) {
                     $defaultRowValue = encodeMultienumValue($_REQUEST[$field]);
                 } else {
                     $defaultRowValue = $_REQUEST[$field];
                 }
                 // translate default values to the date/time format for the import file
                 if ($fieldDef['type'] == 'date' && $ifs->dateformat != $timedate->get_date_format()) {
                     $defaultRowValue = $timedate->swap_formats($defaultRowValue, $ifs->dateformat, $timedate->get_date_format());
                 }
                 if ($fieldDef['type'] == 'time' && $ifs->timeformat != $timedate->get_time_format()) {
                     $defaultRowValue = $timedate->swap_formats($defaultRowValue, $ifs->timeformat, $timedate->get_time_format());
                 }
                 if (($fieldDef['type'] == 'datetime' || $fieldDef['type'] == 'datetimecombo') && $ifs->dateformat . ' ' . $ifs->timeformat != $timedate->get_date_time_format()) {
                     $defaultRowValue = $timedate->swap_formats($defaultRowValue, $ifs->dateformat . ' ' . $ifs->timeformat, $timedate->get_date_time_format());
                 }
                 if (in_array($fieldDef['type'], array('currency', 'float', 'int', 'num')) && $ifs->num_grp_sep != $current_user->getPreference('num_grp_sep')) {
                     $defaultRowValue = str_replace($current_user->getPreference('num_grp_sep'), $ifs->num_grp_sep, $defaultRowValue);
                 }
                 if (in_array($fieldDef['type'], array('currency', 'float')) && $ifs->dec_sep != $current_user->getPreference('dec_sep')) {
                     $defaultRowValue = str_replace($current_user->getPreference('dec_sep'), $ifs->dec_sep, $defaultRowValue);
                 }
                 $currency->retrieve('-99');
                 $user_currency_symbol = $currency->symbol;
                 if ($fieldDef['type'] == 'currency' && $ifs->currency_symbol != $user_currency_symbol) {
                     $defaultRowValue = str_replace($user_currency_symbol, $ifs->currency_symbol, $defaultRowValue);
                 }
                 if (empty($rowValue)) {
                     $rowValue = $defaultRowValue;
                     unset($defaultRowValue);
                 }
             }
             // Bug 22705 - Don't update the First Name or Last Name value if Full Name is set
             if (in_array($field, array('first_name', 'last_name')) && !empty($focus->full_name)) {
                 continue;
             }
             // loop if this value has not been set
             if (!isset($rowValue)) {
                 continue;
             }
             // If the field is required and blank then error out
             if (array_key_exists($field, $focus->get_import_required_fields()) && empty($rowValue) && $rowValue != '0') {
                 $importFile->writeError($mod_strings['LBL_REQUIRED_VALUE'], $fieldTranslated, 'NULL');
                 $do_save = false;
             }
             // Handle the special case "Sync to Outlook"
             if ($focus->object_name == "Contacts" && $field == 'sync_contact') {
                 $bad_names = array();
                 $returnValue = $ifs->synctooutlook($rowValue, $fieldDef, $bad_names);
                 // try the default value on fail
                 if (!$returnValue && !empty($defaultRowValue)) {
                     $returnValue = $ifs->synctooutlook($defaultRowValue, $fieldDef, $bad_names);
                 }
                 if (!$returnValue) {
                     $importFile->writeError($mod_strings['LBL_ERROR_SYNC_USERS'], $fieldTranslated, explode(",", $bad_names));
                     $do_save = 0;
                 }
             }
             // Handle email1 and email2 fields ( these don't have the type of email )
             if ($field == 'email1' || $field == 'email2') {
                 $returnValue = $ifs->email($rowValue, $fieldDef);
                 // try the default value on fail
                 if (!$returnValue && !empty($defaultRowValue)) {
                     $returnValue = $ifs->email($defaultRowValue, $fieldDef);
                 }
                 if ($returnValue === FALSE) {
                     $do_save = 0;
                     $importFile->writeError($mod_strings['LBL_ERROR_INVALID_EMAIL'], $fieldTranslated, $rowValue);
                 } else {
                     $rowValue = $returnValue;
                     // check for current opt_out and invalid email settings for this email address
                     // if we find any, set them now
                     $emailres = $focus->db->query("SELECT opt_out, invalid_email FROM email_addresses \n                                WHERE email_address = '" . $focus->db->quote($rowValue) . "'");
                     if ($emailrow = $focus->db->fetchByAssoc($emailres)) {
                         $focus->email_opt_out = $emailrow['opt_out'];
                         $focus->invalid_email = $emailrow['invalid_email'];
                     }
                 }
             }
             // Handle splitting Full Name into First and Last Name parts
             if ($field == 'full_name' && !empty($rowValue)) {
                 $ifs->fullname($rowValue, $fieldDef, $focus);
             }
             // to maintain 451 compatiblity
             if (!isset($fieldDef['module']) && $fieldDef['type'] == 'relate') {
                 $fieldDef['module'] = ucfirst($fieldDef['table']);
             }
             if (isset($fieldDef['custom_type']) && !empty($fieldDef['custom_type'])) {
                 $fieldDef['type'] = $fieldDef['custom_type'];
             }
             // If the field is empty then there is no need to check the data
             if (!empty($rowValue)) {
                 switch ($fieldDef['type']) {
                     case 'enum':
                     case 'multienum':
                         if (isset($fieldDef['type']) && $fieldDef['type'] == "multienum") {
                             $returnValue = $ifs->multienum($rowValue, $fieldDef);
                         } else {
                             $returnValue = $ifs->enum($rowValue, $fieldDef);
                         }
                         // try the default value on fail
                         if (!$returnValue && !empty($defaultRowValue)) {
                             if (isset($fieldDef['type']) && $fieldDef['type'] == "multienum") {
                                 $returnValue = $ifs->multienum($defaultRowValue, $fieldDef);
                             } else {
                                 $returnValue = $ifs->enum($defaultRowValue, $fieldDef);
                             }
                         }
                         if ($returnValue === FALSE) {
                             $importFile->writeError($mod_strings['LBL_ERROR_NOT_IN_ENUM'] . implode(",", $app_list_strings[$fieldDef['options']]), $fieldTranslated, $rowValue);
                             $do_save = 0;
                         } else {
                             $rowValue = $returnValue;
                         }
                         break;
                     case 'relate':
                     case 'parent':
                         $returnValue = $ifs->relate($rowValue, $fieldDef, $focus, empty($defaultRowValue));
                         if (!$returnValue && !empty($defaultRowValue)) {
                             $returnValue = $ifs->relate($defaultRowValue, $fieldDef, $focus);
                         }
                         // Bug 33623 - Set the id value found from the above method call as an importColumn
                         if ($returnValue !== false) {
                             $importColumns[] = $fieldDef['id_name'];
                         }
                         break;
                     case 'teamset':
                         $returnValue = $ifs->teamset($rowValue, $fieldDef, $focus);
                         $importColumns[] = 'team_set_id';
                         $importColumns[] = 'team_id';
                         break;
                     case 'fullname':
                         break;
                     default:
                         if (method_exists('ImportFieldSanitize', $fieldDef['type'])) {
                             $fieldtype = $fieldDef['type'];
                             $returnValue = $ifs->{$fieldtype}($rowValue, $fieldDef);
                             // try the default value on fail
                             if (!$returnValue && !empty($defaultRowValue)) {
                                 $returnValue = $ifs->{$fieldtype}($defaultRowValue, $fieldDef);
                             }
                             if (!$returnValue) {
                                 $do_save = 0;
                                 $importFile->writeError($mod_strings['LBL_ERROR_INVALID_' . strtoupper($fieldDef['type'])], $fieldTranslated, $rowValue);
                             } else {
                                 $rowValue = $returnValue;
                             }
                         }
                 }
             }
             $focus->{$field} = $rowValue;
             unset($defaultRowValue);
         }
         // Now try to validate flex relate fields
         if (isset($focus->field_defs['parent_name']) && isset($focus->parent_name) && $focus->field_defs['parent_name']['type'] == 'parent') {
             // populate values from the picker widget if the import file doesn't have them
             $parent_idField = $focus->field_defs['parent_name']['id_name'];
             if (empty($focus->{$parent_idField}) && !empty($_REQUEST[$parent_idField])) {
                 $focus->{$parent_idField} = $_REQUEST[$parent_idField];
             }
             $parent_typeField = $focus->field_defs['parent_name']['type_name'];
             if (empty($focus->{$parent_typeField}) && !empty($_REQUEST[$parent_typeField])) {
                 $focus->{$parent_typeField} = $_REQUEST[$parent_typeField];
             }
             // now validate it
             $returnValue = $ifs->parent($focus->parent_name, $focus->field_defs['parent_name'], $focus, empty($_REQUEST['parent_name']));
             if (!$returnValue && !empty($_REQUEST['parent_name'])) {
                 $returnValue = $ifs->parent($_REQUEST['parent_name'], $focus->field_defs['parent_name'], $focus);
             }
         }
         // check to see that the indexes being entered are unique.
         if (isset($_REQUEST['display_tabs_def']) && $_REQUEST['display_tabs_def'] != "") {
             $idc = new ImportDuplicateCheck($focus);
             if ($idc->isADuplicateRecord(explode('&', $_REQUEST['display_tabs_def']))) {
                 $importFile->markRowAsDuplicate();
                 $this->_undoCreatedBeans($ifs->createdBeans);
                 continue;
             }
         }
         // if the id was specified
         $newRecord = true;
         if (!empty($focus->id)) {
             $focus->id = $this->_convertId($focus->id);
             // check if it already exists
             $query = "SELECT * FROM {$focus->table_name} WHERE id='" . $focus->db->quote($focus->id) . "'";
             $result = $focus->db->query($query) or sugar_die("Error selecting sugarbean: ");
             $dbrow = $focus->db->fetchByAssoc($result);
             if (isset($dbrow['id']) && $dbrow['id'] != -1) {
                 // if it exists but was deleted, just remove it
                 if (isset($dbrow['deleted']) && $dbrow['deleted'] == 1 && $update_only == false) {
                     $query2 = "DELETE FROM {$focus->table_name} WHERE id='" . $focus->db->quote($focus->id) . "'";
                     $result2 = $focus->db->query($query2) or sugar_die($mod_strings['LBL_ERROR_DELETING_RECORD'] . " " . $focus->id);
                     if ($focus->hasCustomFields()) {
                         $query3 = "DELETE FROM {$focus->table_name}_cstm WHERE id_c='" . $focus->db->quote($focus->id) . "'";
                         $result2 = $focus->db->query($query3);
                     }
                     $focus->new_with_id = true;
                 } else {
                     if (!$update_only) {
                         $do_save = 0;
                         $importFile->writeError($mod_strings['LBL_ID_EXISTS_ALREADY'], 'ID', $focus->id);
                         $this->_undoCreatedBeans($ifs->createdBeans);
                         continue;
                     }
                     $existing_focus = loadImportBean($_REQUEST['import_module']);
                     $newRecord = false;
                     if (!$existing_focus->retrieve($dbrow['id']) instanceof SugarBean) {
                         $do_save = 0;
                         $importFile->writeError($mod_strings['LBL_RECORD_CANNOT_BE_UPDATED'], 'ID', $focus->id);
                         $this->_undoCreatedBeans($ifs->createdBeans);
                         continue;
                     } else {
                         $newData = $focus->toArray();
                         foreach ($newData as $focus_key => $focus_value) {
                             if (in_array($focus_key, $importColumns)) {
                                 $existing_focus->{$focus_key} = $focus_value;
                             }
                         }
                         $focus = $existing_focus;
                     }
                     unset($existing_focus);
                 }
             } else {
                 $focus->new_with_id = true;
             }
         }
         if ($do_save) {
             // Populate in any default values to the bean
             $focus->populateDefaultValues();
             if (!isset($focus->assigned_user_id) || $focus->assigned_user_id == '' && $newRecord) {
                 $focus->assigned_user_id = $current_user->id;
             }
             /*
              * Bug 34854: Added all conditions besides the empty check on date modified. Currently, if
              * we do an update to a record, it doesn't update the date_modified value.
              * Hack note: I'm doing a to_display and back to_db on the fetched row to make sure that any truncating that happens
              * when $focus->date_modified goes to_display and back to_db also happens on the fetched db value. Otherwise,
              * in some cases we truncate the seconds on one and not the other, and the comparison fails when it should pass
              */
             if (!empty($focus->new_with_id) && !empty($focus->date_modified) || empty($focus->new_with_id) && $timedate->to_db($focus->date_modified) != $timedate->to_db($timedate->to_display_date_time($focus->fetched_row['date_modified']))) {
                 $focus->update_date_modified = false;
             }
             $focus->optimistic_lock = false;
             if ($focus->object_name == "Contacts" && isset($focus->sync_contact)) {
                 //copy the potential sync list to another varible
                 $list_of_users = $focus->sync_contact;
                 //and set it to false for the save
                 $focus->sync_contact = false;
             } else {
                 if ($focus->object_name == "User" && !empty($current_user) && $focus->is_admin && !is_admin($current_user) && is_admin_for_module($current_user, 'Users')) {
                     sugar_die($GLOBALS['mod_strings']['ERR_IMPORT_SYSTEM_ADMININSTRATOR']);
                 }
             }
             //bug# 40260 setting it true as the module in focus is involved in an import
             $focus->in_import = true;
             // call any logic needed for the module preSave
             $focus->beforeImportSave();
             $focus->save(false);
             // call any logic needed for the module postSave
             $focus->afterImportSave();
             if ($focus->object_name == "Contacts" && isset($list_of_users)) {
                 $focus->process_sync_to_outlook($list_of_users);
             }
             // Update the created/updated counter
             $importFile->markRowAsImported($newRecord);
             // Add ID to User's Last Import records
             if ($newRecord) {
                 ImportFile::writeRowToLastImport($_REQUEST['import_module'], $focus->object_name == 'Case' ? 'aCase' : $focus->object_name, $focus->id);
             }
         } else {
             $this->_undoCreatedBeans($ifs->createdBeans);
         }
     }
     // save mapping if requested
     if (isset($_REQUEST['save_map_as']) && $_REQUEST['save_map_as'] != '') {
         $mapping_file = new ImportMap();
         if (isset($_REQUEST['has_header']) && $_REQUEST['has_header'] == 'on') {
             $header_to_field = array();
             foreach ($importColumns as $pos => $field_name) {
                 if (isset($firstrow[$pos]) && isset($field_name)) {
                     $header_to_field[$firstrow[$pos]] = $field_name;
                 }
             }
             $mapping_file->setMapping($header_to_field);
         } else {
             $mapping_file->setMapping($importColumns);
         }
         // save default fields
         $defaultValues = array();
         for ($i = 0; $i < $_REQUEST['columncount']; $i++) {
             if (isset($importColumns[$i]) && !empty($_REQUEST[$importColumns[$i]])) {
                 $field = $importColumns[$i];
                 $fieldDef = $focus->getFieldDefinition($field);
                 if (!empty($fieldDef['custom_type']) && $fieldDef['custom_type'] == 'teamset') {
                     require_once 'include/SugarFields/Fields/Teamset/SugarFieldTeamset.php';
                     $sugar_field = new SugarFieldTeamset('Teamset');
                     $teams = $sugar_field->getTeamsFromRequest($field);
                     if (isset($_REQUEST['primary_team_name_collection'])) {
                         $primary_index = $_REQUEST['primary_team_name_collection'];
                     }
                     //If primary_index was selected, ensure that the first Array entry is the primary team
                     if (isset($primary_index)) {
                         $count = 0;
                         $new_teams = array();
                         foreach ($teams as $id => $name) {
                             if ($primary_index == $count++) {
                                 $new_teams[$id] = $name;
                                 unset($teams[$id]);
                                 break;
                             }
                         }
                         foreach ($teams as $id => $name) {
                             $new_teams[$id] = $name;
                         }
                         $teams = $new_teams;
                     }
                     //if
                     $json = getJSONobj();
                     $defaultValues[$field] = $json->encode($teams);
                 } else {
                     $defaultValues[$field] = $_REQUEST[$importColumns[$i]];
                 }
             }
         }
         $mapping_file->setDefaultValues($defaultValues);
         $result = $mapping_file->save($current_user->id, $_REQUEST['save_map_as'], $_REQUEST['import_module'], $_REQUEST['source'], isset($_REQUEST['has_header']) && $_REQUEST['has_header'] == 'on', $_REQUEST['custom_delimiter'], html_entity_decode($_REQUEST['custom_enclosure'], ENT_QUOTES));
     }
     $importFile->writeStatus();
 }
 public function testCompareOnlySelectedIndexesFromDupeCheck()
 {
     //create a bean, values, populate and save
     $focus = loadBean('Contacts');
     $focus->first_name = 'first ' . date("YmdHis");
     $focus->last_name = 'last ' . date("YmdHis");
     $focus->assigned_user_id = '1';
     $focus->save();
     //create the importDuplicateCheck object and get the list of duplicateCheckIndexes
     $idc = new ImportDuplicateCheck($focus);
     //we are going to test agains the first name, last name, full name, and assigned to indexes
     //to prove that only selected indexes are being used.
     //lets do a straight dupe check with the same bean on first name, should return true
     $this->assertTrue($idc->isADuplicateRecord(array('idx_cont_last_first::first_name')), 'simulated check against first name index (idx_cont_last_first::first_name) failed  (returned false instead of true).');
     //now lets test on full name index should also return true
     $this->assertTrue($idc->isADuplicateRecord(array('full_name::full_name')), 'first simulated check against full name index (full_name::full_name) failed (returned false instead of true).  This check means BOTH first AND last name must match.');
     //now lets remove the first name and redo the check, should return false
     $focus->first_name = '';
     $idc = new ImportDuplicateCheck($focus);
     $this->assertFalse($idc->isADuplicateRecord(array('idx_cont_last_first::first_name')), 'simulated check against first name index (idx_cont_last_first::first_name) failed (returned true instead of false).  This is wrong because we removed the first name so there should be no match.');
     //lets retest on full name index should return false now as first AND last do not match the original
     $this->assertFalse($idc->isADuplicateRecord(array('full_name::full_name')), 'second simulated check against full name index (full_name::full_name) failed (returned true instead of false).  This check means BOTH first AND last name must match and is wrong because we removed the first name so there should be no match.');
     //now lets rename the contact and test on assigned user, should return true
     $focus->first_name = 'first ' . date("YmdHis");
     $focus->last_name = 'last ' . date("YmdHis");
     $idc = new ImportDuplicateCheck($focus);
     $this->assertTrue($idc->isADuplicateRecord(array('idx_del_id_user::assigned_user_id')), 'simulated check against assigned user index (idx_del_id_user::assigned_user_id) failed (returned false instead of true).  This is wrong because we have not changed this field and it should remain a duplicate');
     //we're done, lets delete the focus bean now
     $focus->mark_deleted($focus->id);
 }
Example #4
0
 protected function importRow($row)
 {
     global $sugar_config, $mod_strings, $current_user;
     $focus = BeanFactory::getBean($this->bean->module_dir);
     $focus->unPopulateDefaultValues();
     $focus->save_from_post = false;
     $focus->team_id = null;
     $this->ifs->createdBeans = array();
     $this->importSource->resetRowErrorCounter();
     $do_save = true;
     // set the currency for the row, if it has a currency_id in the row
     if ($this->currencyFieldPosition !== false && !empty($row[$this->currencyFieldPosition])) {
         $currency_id = $row[$this->currencyFieldPosition];
         if (!isset($this->cachedCurrencySymbols[$currency_id])) {
             /** @var Currency $currency */
             $currency = BeanFactory::getBean('Currencies', $currency_id);
             $this->cachedCurrencySymbols[$currency_id] = $currency->symbol;
             unset($currency);
         }
         $this->ifs->currency_symbol = $this->cachedCurrencySymbols[$currency_id];
         $this->ifs->currency_id = $currency_id;
     }
     // Collect email addresses, and add them before save
     $emailAddresses = array('non-primary' => array());
     $fields_order = $this->getImportColumnsOrder($focus->getFieldDefinitions());
     foreach ($fields_order as $fieldNum) {
         // loop if this column isn't set
         if (!isset($this->importColumns[$fieldNum])) {
             continue;
         }
         // get this field's properties
         $field = $this->importColumns[$fieldNum];
         $fieldDef = $focus->getFieldDefinition($field);
         $fieldTranslated = translate(isset($fieldDef['vname']) ? $fieldDef['vname'] : $fieldDef['name'], $focus->module_dir) . " (" . $fieldDef['name'] . ")";
         $defaultRowValue = '';
         // Bug 37241 - Don't re-import over a field we already set during the importing of another field
         if (!empty($focus->{$field})) {
             continue;
         }
         // translate strings
         global $locale;
         if (empty($locale)) {
             $locale = Localization::getObject();
         }
         if (isset($row[$fieldNum])) {
             $rowValue = strip_tags(trim($row[$fieldNum]));
         } else {
             if (isset($this->sugarToExternalSourceFieldMap[$field]) && isset($row[$this->sugarToExternalSourceFieldMap[$field]])) {
                 $rowValue = strip_tags(trim($row[$this->sugarToExternalSourceFieldMap[$field]]));
             } else {
                 $rowValue = '';
             }
         }
         // If there is an default value then use it instead
         if (!empty($_REQUEST[$field])) {
             $defaultRowValue = $this->populateDefaultMapValue($field, $_REQUEST[$field], $fieldDef);
             if (!empty($fieldDef['custom_type']) && $fieldDef['custom_type'] == 'teamset' && empty($rowValue)) {
                 require_once 'include/SugarFields/Fields/Teamset/SugarFieldTeamset.php';
                 $sugar_field = new SugarFieldTeamset('Teamset');
                 $rowValue = implode(', ', $sugar_field->getTeamsFromRequest($field));
             }
             if (empty($rowValue)) {
                 $rowValue = $defaultRowValue;
                 //reset the default value to empty
                 $defaultRowValue = '';
             }
         }
         // Bug 22705 - Don't update the First Name or Last Name value if Full Name is set
         if (in_array($field, array('first_name', 'last_name')) && !empty($focus->full_name)) {
             continue;
         }
         // loop if this value has not been set
         if (!isset($rowValue)) {
             continue;
         }
         // If the field is required and blank then error out
         if (array_key_exists($field, $focus->get_import_required_fields()) && empty($rowValue) && $rowValue != '0') {
             $this->importSource->writeError($mod_strings['LBL_REQUIRED_VALUE'], $fieldTranslated, 'NULL');
             $do_save = false;
         }
         // Handle the special case 'Sync to Mail Client'
         if ($focus->object_name == "Contact" && $field == 'sync_contact') {
             /**
              * Bug #41194 : if true used as value of sync_contact - add curent user to list to sync
              */
             if (true == $rowValue || 'true' == strtolower($rowValue)) {
                 $focus->sync_contact = $focus->id;
             } elseif (false == $rowValue || 'false' == strtolower($rowValue)) {
                 $focus->sync_contact = '';
             } else {
                 $bad_names = array();
                 $returnValue = $this->ifs->synctooutlook($rowValue, $fieldDef, $bad_names);
                 // try the default value on fail
                 if (!$returnValue && !empty($defaultRowValue)) {
                     $returnValue = $this->ifs->synctooutlook($defaultRowValue, $fieldDef, $bad_names);
                 }
                 if (!$returnValue) {
                     $this->importSource->writeError($mod_strings['LBL_ERROR_SYNC_USERS'], $fieldTranslated, $bad_names);
                     $do_save = 0;
                 } else {
                     $focus->sync_contact = $returnValue;
                 }
             }
         }
         // Handle email1 and email2 fields ( these don't have the type of email )
         if ($field == 'email1' || $field == 'email2') {
             $returnValue = $this->ifs->email($rowValue, $fieldDef, $focus);
             // try the default value on fail
             if (!$returnValue && !empty($defaultRowValue)) {
                 $returnValue = $this->ifs->email($defaultRowValue, $fieldDef);
             }
             if ($returnValue === FALSE) {
                 $do_save = 0;
                 $this->importSource->writeError($mod_strings['LBL_ERROR_INVALID_EMAIL'], $fieldTranslated, $rowValue);
             } else {
                 $rowValue = $returnValue;
                 $address = array('email_address' => $rowValue, 'primary_address' => $field == 'email1', 'invalid_email' => false, 'opt_out' => false);
                 // check for current opt_out and invalid email settings for this email address
                 // if we find any, set them now
                 $emailres = $focus->db->query("SELECT opt_out, invalid_email FROM email_addresses WHERE email_address = '" . $focus->db->quote($rowValue) . "'");
                 if ($emailrow = $focus->db->fetchByAssoc($emailres)) {
                     $address = array_merge($address, $emailrow);
                 }
                 if ($field === 'email1') {
                     //flip the array so we can use it to get the key #
                     $flippedVals = array_flip($this->importColumns);
                     //if the opt out column is set, then attempt to retrieve the values
                     if (isset($flippedVals['email_opt_out'])) {
                         //if the string for this value has a length, then use it.
                         if (isset($row[$flippedVals['email_opt_out']]) && strlen($row[$flippedVals['email_opt_out']]) > 0) {
                             $address['opt_out'] = $row[$flippedVals['email_opt_out']];
                         }
                     }
                     //if the invalid email column is set, then attempt to retrieve the values
                     if (isset($flippedVals['invalid_email'])) {
                         //if the string for this value has a length, then use it.
                         if (isset($row[$flippedVals['invalid_email']]) && strlen($row[$flippedVals['invalid_email']]) > 0) {
                             $address['invalid_email'] = $row[$flippedVals['invalid_email']];
                         }
                     }
                     $emailAddresses['primary'] = $address;
                 } else {
                     $emailAddresses['non-primary'][] = $address;
                 }
             }
         }
         if ($field == 'email_addresses_non_primary') {
             $nonPrimaryAddresses = $this->handleNonPrimaryEmails($rowValue, $defaultRowValue, $fieldTranslated);
             $emailAddresses['non-primary'] = array_merge($emailAddresses['non-primary'], $nonPrimaryAddresses);
         }
         // Handle splitting Full Name into First and Last Name parts
         if ($field == 'full_name' && !empty($rowValue)) {
             $this->ifs->fullname($rowValue, $fieldDef, $focus);
         }
         // to maintain 451 compatiblity
         if (!isset($fieldDef['module']) && $fieldDef['type'] == 'relate') {
             $fieldDef['module'] = ucfirst($fieldDef['table']);
         }
         if (isset($fieldDef['custom_type']) && !empty($fieldDef['custom_type'])) {
             $fieldDef['type'] = $fieldDef['custom_type'];
         }
         // If the field is empty then there is no need to check the data
         if (!empty($rowValue)) {
             //Start
             $rowValue = $this->sanitizeFieldValueByType($rowValue, $fieldDef, $defaultRowValue, $focus, $fieldTranslated);
             if ($rowValue === FALSE) {
                 /* BUG 51213 - jeff @ neposystems.com */
                 $do_save = false;
                 continue;
             }
         }
         // if the parent type is in singular form, get the real module name for parent_type
         if (isset($fieldDef['type']) && $fieldDef['type'] == 'parent_type') {
             $rowValue = get_module_from_singular($rowValue);
         }
         $focus->{$field} = $rowValue;
         unset($defaultRowValue);
     }
     // Now try to validate flex relate fields
     if (isset($focus->field_defs['parent_name']) && isset($focus->parent_name) && $focus->field_defs['parent_name']['type'] == 'parent') {
         // populate values from the picker widget if the import file doesn't have them
         $parent_idField = $focus->field_defs['parent_name']['id_name'];
         if (empty($focus->{$parent_idField}) && !empty($_REQUEST[$parent_idField])) {
             $focus->{$parent_idField} = $_REQUEST[$parent_idField];
         }
         $parent_typeField = $focus->field_defs['parent_name']['type_name'];
         if (empty($focus->{$parent_typeField}) && !empty($_REQUEST[$parent_typeField])) {
             $focus->{$parent_typeField} = $_REQUEST[$parent_typeField];
         }
         // now validate it
         $returnValue = $this->ifs->parent($focus->parent_name, $focus->field_defs['parent_name'], $focus, empty($_REQUEST['parent_name']));
         if (!$returnValue && !empty($_REQUEST['parent_name'])) {
             $returnValue = $this->ifs->parent($_REQUEST['parent_name'], $focus->field_defs['parent_name'], $focus);
         }
     }
     // check to see that the indexes being entered are unique.
     if (isset($_REQUEST['enabled_dupes']) && $_REQUEST['enabled_dupes'] != "") {
         $toDecode = html_entity_decode($_REQUEST['enabled_dupes'], ENT_QUOTES);
         $enabled_dupes = json_decode($toDecode);
         $idc = new ImportDuplicateCheck($focus);
         if ($idc->isADuplicateRecord($enabled_dupes)) {
             $this->importSource->markRowAsDuplicate($idc->_dupedFields);
             $this->_undoCreatedBeans($this->ifs->createdBeans);
             return;
         }
     } else {
         if (!empty($_REQUEST['enabled_dup_fields'])) {
             $toDecode = html_entity_decode($_REQUEST['enabled_dup_fields'], ENT_QUOTES);
             $enabled_dup_fields = json_decode($toDecode);
             $idc = new ImportDuplicateCheck($focus);
             if ($idc->isADuplicateRecordByFields($enabled_dup_fields)) {
                 $this->importSource->markRowAsDuplicate($idc->_dupedFields);
                 $this->_undoCreatedBeans($this->ifs->createdBeans);
                 return;
             }
         }
     }
     // if the id was specified
     $newRecord = true;
     if (!empty($focus->id)) {
         $focus->id = $this->_convertId($focus->id);
         // check if it already exists
         $query = "SELECT * FROM {$focus->table_name} WHERE id='" . $focus->db->quote($focus->id) . "'";
         $result = $focus->db->query($query) or sugar_die("Error selecting sugarbean: ");
         $dbrow = $focus->db->fetchByAssoc($result);
         if (isset($dbrow['id']) && $dbrow['id'] != -1) {
             // if it exists but was deleted, just remove it
             if (isset($dbrow['deleted']) && $dbrow['deleted'] == 1 && $this->isUpdateOnly == false) {
                 $this->removeDeletedBean($focus);
                 $focus->new_with_id = true;
             } else {
                 if (!$this->isUpdateOnly) {
                     $this->importSource->writeError($mod_strings['LBL_ID_EXISTS_ALREADY'], 'ID', $focus->id);
                     $this->_undoCreatedBeans($this->ifs->createdBeans);
                     return;
                 }
                 $clonedBean = $this->cloneExistingBean($focus);
                 if ($clonedBean === FALSE) {
                     $this->importSource->writeError($mod_strings['LBL_RECORD_CANNOT_BE_UPDATED'], 'ID', $focus->id);
                     $this->_undoCreatedBeans($this->ifs->createdBeans);
                     return;
                 } else {
                     $focus = $clonedBean;
                     $newRecord = FALSE;
                 }
             }
         } else {
             $focus->new_with_id = true;
         }
     }
     try {
         // Update e-mails here, because we're calling retrieve, and it overwrites the emailAddress object
         if ($focus->hasEmails()) {
             $this->handleEmailUpdate($focus, $emailAddresses);
         }
     } catch (Exception $e) {
         $this->importSource->writeError($e->getMessage(), $fieldTranslated, $focus->id);
         $do_save = false;
     }
     if ($do_save) {
         $this->saveImportBean($focus, $newRecord);
         // Update the created/updated counter
         $this->importSource->markRowAsImported($newRecord);
     } else {
         $this->_undoCreatedBeans($this->ifs->createdBeans);
     }
     unset($defaultRowValue);
 }
 public function testIsADuplicateRecordEmailNotFound()
 {
     $email = date("YmdHis") . '@badfoobar.com';
     $focus = loadBean('Contacts');
     $focus->email1 = $email;
     $idc = new ImportDuplicateCheck($focus);
     $this->assertFalse($idc->isADuplicateRecord(array('special_idx_email1')));
 }
Example #6
0
 /**
  * @group 51264
  */
 public function testIsADuplicateRecord()
 {
     $contact = new Contact();
     $contact->email1 = $this->contact->email1;
     $idc = new ImportDuplicateCheck($contact);
     $result = $idc->isADuplicateRecord(array('special_idx_email1::email1'));
     $this->assertTrue($result);
 }