Index: include/database/DBHelper.php
===================================================================
--- include/database/DBHelper.php (revision 73)
+++ include/database/DBHelper.php (working copy)
@@ -1185,8 +1185,8 @@
$before_value=(float)$bean->fetched_row[$field];
$after_value=(float)$bean->$field;
} else {
+ $before_value=$bean->fetched_row[$field];
+ $after_value=@$bean->$field;
}
//Because of bug #25078(sqlserver haven't 'date' type, trim extra "00:00:00" when insert into *_cstm table). so when we read the audit datetime field from sqlserver, we have to replace the extra "00:00:00" again.
Index: modules/Import/Importer.php
===================================================================
--- modules/Import/Importer.php (revision 73)
+++ modules/Import/Importer.php (working copy)
@@ -59,6 +59,7 @@
*/
private $importColumns;
+ private $importRelatedColumns;
/**
* @var importSource
*/
@@ -104,9 +105,11 @@
//Get the default user currency
$this->defaultUserCurrency = new Currency();
$this->defaultUserCurrency->retrieve('-99');
//Get our import column definitions
$this->importColumns = $this->getImportColumns();
+ $this->importRelatedColumns = $this->getRelatedImportColumns();
+
$this->isUpdateOnly = ( isset($_REQUEST['import_type']) && $_REQUEST['import_type'] == 'update' );
}
@@ -132,7 +135,7 @@
protected function importRow($row)
{
global $sugar_config, $mod_strings;
$focus = clone $this->bean;
$focus->unPopulateDefaultValues();
$focus->save_from_post = false;
@@ -140,6 +143,11 @@
$this->ifs->createdBeans = array();
$this->importSource->resetRowErrorCounter();
$do_save = true;
+
+ $primary_key = (isset($_REQUEST['primary_key']) && !empty($_REQUEST['primary_key'])) ? $_REQUEST['primary_key'] : false;
+ if(!$primary_key){
+ $primary_key = 'id';
+ }
for ( $fieldNum = 0; $fieldNum < $_REQUEST['columncount']; $fieldNum++ )
{
@@ -322,46 +330,58 @@
return;
}
}
+ $focus_array = array();
+ $newRecord_array = array();
+
// if the id was specified
$newRecord = true;
+ if ( !empty($focus->$primary_key) )
{
+ $focus->id = $this->_convertId($focus->$primary_key);
// check if it already exists
+ $join = '';
+ $_def = $focus->getFieldDefinition($primary_key);
+ if($_def['source'] == 'custom_fields') $join = "JOIN {$focus->table_name}_cstm ON id_c = id";
+ $query = "SELECT * FROM {$focus->table_name} $join WHERE $primary_key='".$focus->db->quote($focus->$primary_key)."'";
$result = $focus->db->query($query)
or sugar_die("Error selecting sugarbean: ");
+ while($dbrow = $focus->db->fetchByAssoc($result)){
+ if (isset ($dbrow[$primary_key]) && $dbrow[$primary_key] != -1)
{
+ $focus->id = $dbrow['id'];
+ // if it exists but was deleted, just remove it //skiping deleted rows
+ if (isset ($dbrow['deleted']) && $dbrow['deleted'] == 1 && $this->isUpdateOnly == false /*&& $primary_key=='id'*/)
{
$this->removeDeletedBean($focus);
$focus->new_with_id = true;
}
else
{
+ //skiping deleted rows
+ if(isset ($dbrow['deleted']) && $dbrow['deleted'] == 1 ) continue;
if( ! $this->isUpdateOnly )
{
+ $this->importSource->writeError($mod_strings['LBL_ID_EXISTS_ALREADY'],$primary_key,$focus->$primary_key);
$this->_undoCreatedBeans($this->ifs->createdBeans);
return;
}
$clonedBean = $this->cloneExistingBean($focus);
if($clonedBean === FALSE)
{
+ $this->importSource->writeError($mod_strings['LBL_RECORD_CANNOT_BE_UPDATED'],$primary_key,$focus->$primary_key);
$this->_undoCreatedBeans($this->ifs->createdBeans);
return;
}
else
{
+ $focus_array[] = clone $clonedBean;
+ $newRecord_array[] = FALSE;
+
$focus = $clonedBean;
$newRecord = FALSE;
}
@@ -369,19 +389,100 @@
}
else
{
+ if($focus->id){
$focus->new_with_id = true;
+ }else{
+ $focus->new_with_id = false;
+ $focus_array[] = $this->cloneExistingBean($focus);
+ $newRecord_array[] = true;
}
}
+ }
+ if(count($focus_array)==0){
+ $focus_array[] = $focus;
+ $newRecord_array[] = $newRecord;
+ }
+ for($focus_i=0,$focus_count=count($focus_array);$focus_i<$focus_count;$focus_i++){
+ $focus = $focus_array[$focus_i];
+ $newRecord = $newRecord_array[$focus_i];
if ($do_save)
{
$this->saveImportBean($focus, $newRecord);
+ $mainModuleId = $focus->id;
+ //Save records for related module
+ foreach($this->importRelatedColumns as $k=>$v)
+ {
+ $exploedFields = explode("$",$v);
+ $relatedModule[] = $exploedFields[0];
+ $relatedModuleField[$exploedFields[0]][] = $exploedFields[1];
+ }
+ if(isset($relatedModule) && is_array($relatedModule))
+ $relatedModules = array_unique($relatedModule);
+ if(isset($relatedModule) && is_array($relatedModule))
+ foreach($relatedModuleField as $modName => $fldsArr)
+ {
+ $haveValue = 0;
+ $relatedModuleFile = $beanList[$modName];
+ include_once($beanFiles[$relatedModuleFile]);
+ $relatedModuleObj = new $relatedModuleFile();
+ $whereArr = array();
+ $is_custom = false;
+ foreach($fldsArr as $k=>$fldName)
+ {
+ $fld = $modName."$".$fldName;
+ $fieldNo = array_search($fld,$this->importRelatedColumns);
+ if($row[$fieldNo]!="")
+ {
+ $haveValue++;
+ $relatedModuleObj->$fldName = $row[$fieldNo];
+ $whereArr[] = $fldName ."='". $row[$fieldNo]."'";
+ if(@$relatedModuleObj->field_defs[$fldName]['source'] == 'custom_fields') $is_custom = true;
+ }
+ }
+ if($haveValue)
+ {
+ $where = "";
+ $where = implode(" AND ",$whereArr);
+ $sql = "select id from ".$relatedModuleObj->table_name;
+ if($is_custom) $sql .= ' inner join '.$relatedModuleObj->table_name.'_cstm on id=id_c';
+ $sql .= " where ".$where." and deleted=0";
+ $result2 = $relatedModuleObj->db->query($sql);
+ $rowQuery = $relatedModuleObj->db->fetchByAssoc($result2);
+ if(count($rowQuery)==0 || empty($rowQuery))
+ {
+ $this->saveImportBean($relatedModuleObj, $newRecord);
+ $relatedId = $relatedModuleObj->id;
+ }
+ else
+ {
+ $relatedId = $rowQuery['id'];
+ }
+ $rel_table = strtolower($relatedModuleObj->table_name);
+ $foc_table = strtolower($focus->table_name);
+ if(strlen($rel_table)>16) $rel_table = substr($rel_table,0,11).substr($rel_table,-5);
+ if(strlen($foc_table)>16) $foc_table = substr($foc_table,0,11).substr($foc_table,-5);
+ $rel_field = $foc_table.'_'.$rel_table;
+ if(!$focus->load_relationship($rel_field)){
+ $rel_field = $rel_table.'_'.$foc_table;
+ if(!$focus->load_relationship($rel_field)){
+ $GLOBALS['log']->error("SugarBean.load_relationships, relationship not found.");
+ $rel_field = false;
+ }
+ }
+ if($rel_field){
+ $focus->$rel_field->add($relatedId,array());
+ }
+ $focus->save();
+ }
+ }
+
// Update the created/updated counter
$this->importSource->markRowAsImported($newRecord);
}
else
$this->_undoCreatedBeans($this->ifs->createdBeans);
unset($defaultRowValue);
}
@@ -544,11 +645,15 @@
$firstrow = unserialize(base64_decode($_REQUEST['firstrow']));
$mappingValsArr = $this->importColumns;
+ $mappingRelatedArr = $this->importRelatedColumns;
+ $mappArr = $mappingRelatedArr+$mappingValsArr;
+ $mapping_file = new ImportMap();
+
+
if ( isset($_REQUEST['has_header']) && $_REQUEST['has_header'] == 'on')
{
$header_to_field = array ();
+ foreach ($mappArr as $pos => $field_name)
{
if (isset($firstrow[$pos]) && isset($field_name))
{
@@ -557,6 +662,8 @@
}
$mappingValsArr = $header_to_field;
+ } else{
+ $mappingValsArr = $mappArr;
}
//get array of values to save for duplicate and locale settings
$advMapping = $this->retrieveAdvancedMapping();
@@ -679,7 +786,26 @@
return $importColumns;
}
+ protected function getRelatedImportColumns()
+ {
+ $importRelatedColumns = 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 ( strpos($value,"$") )
+ {
+ // now mark that we've seen this field
+ $importRelatedColumns[$pos] = $value;
+ }
+ }
+ return $importRelatedColumns;
+ }
protected function getFieldSanitizer()
{
$ifs = new ImportFieldSanitize();
Index: modules/Import/tpls/confirm.tpl
===================================================================
--- modules/Import/tpls/confirm.tpl (revision 73)
+++ modules/Import/tpls/confirm.tpl (working copy)
@@ -170,8 +170,30 @@
</tr>
<tr>
<td colspan="2"><div class="hr" style="margin-top: 0px;"></div></td>
+ </tr>
+ {if $TYPE != 'import'}
+ <tr>
+ <td colspan="2"><h3>Select primaty field {sugar_help text="It will update the row with the field equal to the value of the csv row"}</h3></td>
</tr>
<tr>
+ <td colspan="2">
+ <select name="primary_key">
+ {foreach from=$SAMPLE_ROWS item=row name=row}
+ {foreach from=$row item=value}
+ {if $smarty.foreach.row.first}
+ {if $HAS_HEADER}
+ <option value="{$value}"{if $value|lower == 'id'} selected='selected' {/if}>{$value}</option>
+ {/if}
+ {/if}
+ {/foreach}
+ {/foreach}
+ </select>
+ </td>
+ </tr>
+ {else}
+ <input name="primary_key" value="" type="hidden" />
+ {/if}
+ <tr>
<td colspan="2"><h3>{$MOD.LBL_THIRD_PARTY_CSV_SOURCES} {sugar_help text=$MOD.LBL_THIRD_PARTY_CSV_SOURCES_HELP}</h3></td>
</tr>
<tr>
Index: modules/Import/tpls/dupcheck.tpl
===================================================================
--- modules/Import/tpls/dupcheck.tpl (revision 73)
+++ modules/Import/tpls/dupcheck.tpl (working copy)
@@ -84,6 +84,7 @@
<input type="hidden" id="enabled_dupes" name="enabled_dupes" value="">
<input type="hidden" id="disabled_dupes" name="disabled_dupes" value="">
<input type="hidden" id="current_step" name="current_step" value="{$CURRENT_STEP}">
+<input type="hidden" name="primary_key" value="{$primary_field}">
<div class="hr"></div>
<div>
Index: modules/Import/tpls/step3.tpl
===================================================================
--- modules/Import/tpls/step3.tpl (revision 73)
+++ modules/Import/tpls/step3.tpl (working copy)
@@ -118,10 +118,34 @@
{/if}
<tr>
{if $HAS_HEADER == 'on'}
+ <td id="row_{$smarty.foreach.rows.index}_header">
+ {if $primary_field==$item.cell1}
+ <b>{$item.cell1}
+ <small>*Primary Field</small>
+ </b>
+ <input type="hidden" name="primary_key" value="{$primary_field}" id="primary_key" />
+ <script>
+ {literal}
+ (function(obj, evType, fn){
+ if (obj.addEventListener){
+ obj.addEventListener(evType, fn, false);
+ return true;
+ } else if (obj.attachEvent){
+ var r = obj.attachEvent("on"+evType, fn);
+ return r;
+ } else {
+ return false;
+ }
+ })(window,'load',function(){document.getElementById('primary_key').value=document.getElementById('primary_key_list').value})
+ {/literal}
+ </script>
+ {else}
+ {$item.cell1}
{/if}
+ </td>
+ {/if}
<td valign="top" align="left" id="row_{$smarty.foreach.rows.index}_col_0">
+ <select class='fixedwidth' name="colnum_{$smarty.foreach.rows.index}"{if $primary_field==$item.cell1} id="primary_key_list" onblur="document.getElementById('primary_key').value = this.value" {/if}>
<option value="-1">{$MOD.LBL_DONT_MAP}</option>
{$item.field_choices}
</select>
Index: modules/Import/views/view.confirm.php
===================================================================
--- modules/Import/views/view.confirm.php (revision 73)
+++ modules/Import/views/view.confirm.php (working copy)
@@ -63,7 +63,7 @@
global $sugar_config, $locale;
$this->ss->assign("IMPORT_MODULE", $_REQUEST['import_module']);
+ $this->ss->assign("TYPE",( !empty($_REQUEST['type']) ? $_REQUEST['type'] : ( !empty($_REQUEST['import_type'])?$_REQUEST['import_type'] : "import") ));
$this->ss->assign("SOURCE_ID", $_REQUEST['source_id']);
$this->instruction = 'LBL_SELECT_PROPERTY_INSTRUCTION';
Index: modules/Import/views/view.dupcheck.php
===================================================================
--- modules/Import/views/view.dupcheck.php (revision 73)
+++ modules/Import/views/view.dupcheck.php (working copy)
@@ -73,6 +73,7 @@
$this->ss->assign("IMPORT_MODULE", $_REQUEST['import_module']);
$this->ss->assign("CURRENT_STEP", $this->currentStep);
$this->ss->assign("JAVASCRIPT", $this->_getJS());
+ $this->ss->assign("primary_field", @$_REQUEST['primary_key'] ? $_REQUEST['primary_key'] : 'id' );
$content = $this->ss->fetch('modules/Import/tpls/dupcheck.tpl');
$this->ss->assign("CONTENT", $content);
Index: modules/Import/views/view.last.php
===================================================================
--- modules/Import/views/view.last.php (revision 73)
+++ modules/Import/views/view.last.php (working copy)
@@ -109,6 +109,8 @@
$this->ss->assign("errorrecordsFile",ImportCacheFiles::getErrorRecordsWithoutErrorFileName());
$this->ss->assign("dupeFile",ImportCacheFiles::getDuplicateFileName());
+ $this->ss->assign("primary_field", @$_REQUEST['primary_key'] ? $_REQUEST['primary_key'] : 'id' );
+
if ( $this->bean->object_name == "Prospect" )
{
$this->ss->assign("PROSPECTLISTBUTTON", $this->_addToProspectListButton());
Index: modules/Import/views/view.step3.php
===================================================================
--- modules/Import/views/view.step3.php (revision 73)
+++ modules/Import/views/view.step3.php (working copy)
@@ -63,7 +63,7 @@
public function display()
{
global $mod_strings, $app_strings, $current_user, $sugar_config, $app_list_strings, $locale;
+ global $beanList,$beanFiles;
$this->ss->assign("IMPORT_MODULE", $_REQUEST['import_module']);
$has_header = ( isset( $_REQUEST['has_header']) ? 1 : 0 );
$sugar_config['import_max_records_per_file'] = ( empty($sugar_config['import_max_records_per_file']) ? 1000 : $sugar_config['import_max_records_per_file'] );
@@ -200,7 +200,91 @@
if (!empty($importColumns)) {
$column_sel_from_req = true;
}
+ {
+ if(0 == strcmp($field['type'],'link') && (!empty($field['module']) || !empty($field['relationship'])))
+ {
+ if(!empty($field['module']))
+ {
+ $related_module = $field['module'];
+ }
+ elseif(!empty($field['relationship']))
+ {
+ require_once("data/Relationships/RelationshipFactory.php");
+ $test = SugarRelationshipFactory::getInstance()->getRelationship($field['relationship']);
+ if($test->def['relationships'][$field['relationship']]['lhs_module'] == $this->bean->module_dir)
+ {
+ $relatedMods[] = $test->def['relationships'][$field['relationship']]['rhs_module'];
+ }
+ else
+ {
+ $relatedMods[] = $test->def['relationships'][$field['relationship']]['lhs_module'];
+ }
+ }
+ }
+ }
+ //add extra related fields
+ foreach($relatedMods as $key=>$relatedModule)
+ {
+ if(array_key_exists($relatedModule,$beanList))
+ {
+ $relatedModuleFile = $beanList[$relatedModule];
+ include_once($beanFiles[$relatedModuleFile]);
+ $relatedModuleObj = new $relatedModuleFile();
+ $fieldsRel = $relatedModuleObj->get_importable_fields();
+ $relModuleStrings = return_module_language($current_language, $relatedModuleObj->module_dir);
+ foreach ( $fieldsRel as $fieldname => $properties ) {
+ if($properties['relationship'])
+ continue;
+ // get field name
+ if (!empty($relModuleStrings['LBL_EXPORT_'.strtoupper($fieldname)]) )
+ {
+ $displayname = str_replace(":","", $relModuleStrings['LBL_EXPORT_'.strtoupper($fieldname)] );
+ }
+ else if (!empty ($properties['vname']))
+ {
+ $displayname = str_replace(":","",translate($properties['vname'] ,$relatedModuleObj->module_dir));
+ }
+ else
+ $displayname = str_replace(":","",translate($properties['name'] ,$relatedModuleObj->module_dir));
+
+ $selected = '';
+ if ($column_sel_from_req && isset($importColumns[$field_count])) {
+ if ($fieldname == $importColumns[$field_count]) {
+ //$selected = ' selected="selected" ';
+ $defaultField = $fieldname;
+ $mappedFields[] = $fieldname;
+ }
+ } else {
+ if ( !empty($defaultValue) && !in_array($fieldname,$mappedFields)
+ && !in_array($fieldname,$ignored_fields) )
+ {
+ if ( strtolower($fieldname) == strtolower($defaultValue)
+ || strtolower($fieldname) == str_replace(" ","_",strtolower($defaultValue))
+ || strtolower($displayname) == strtolower($defaultValue)
+ || strtolower($displayname) == str_replace(" ","_",strtolower($defaultValue)) )
+ {
+ //$selected = ' selected="selected" ';
+ $defaultField = $fieldname;
+ $mappedFields[] = $fieldname;
+ }
+ }
+ }
+ // get field type information
+ $fieldtype = '';
+ if ( isset($properties['type'])
+ && isset($mod_strings['LBL_IMPORT_FIELDDEF_' . strtoupper($properties['type'])]) )
+ $fieldtype = ' [' . $mod_strings['LBL_IMPORT_FIELDDEF_' . strtoupper($properties['type'])] . '] ';
+ if ( isset($properties['comment']) )
+ $fieldtype .= ' - ' . $properties['comment'];
+
+ $optionsRelated[$relatedModuleObj->module_dir.'$'.$fieldname] = '<option value="'.$relatedModuleObj->module_dir.'$'.$fieldname.'" title="'. $displayname . htmlentities($fieldtype) . '"'
+ . $selected . $req_class . '>'. $relatedModuleObj->module_dir.".". $displayname . '</option>\n';
+ }
+
+ }
+ }
+
for($field_count = 0; $field_count < $ret_field_count; $field_count++) {
// See if we have any field map matches
$defaultValue = "";
@@ -225,7 +309,17 @@
$defaultField = '';
global $current_language;
$moduleStrings = return_module_language($current_language, $this->bean->module_dir);
+
+ $related_options_clone = $optionsRelated;
+ if(
+ isset($firstrow_name) &&
+ isset($field_map[$firstrow_name]) &&
+ isset($related_options_clone[$field_map[$firstrow_name]])
+ ){
+ $related_options_clone[$field_map[$firstrow_name]] = str_replace('<option ','<option selected="selected" ',$related_options_clone[$field_map[$firstrow_name]]);
+ }
+
foreach ( $fields as $fieldname => $properties ) {
// get field name
if (!empty($moduleStrings['LBL_EXPORT_'.strtoupper($fieldname)]) )
@@ -278,7 +372,6 @@
$options[$displayname.$fieldname] = '<option value="'.$fieldname.'" title="'. $displayname . htmlentities($fieldtype) . '"'
. $selected . $req_class . '>' . $displayname . $req_mark . '</option>\n';
}
$defaultFieldHTML = '';
if ( !empty($defaultField) ) {
@@ -306,6 +399,11 @@
$cellOneData = isset($rows[0][$field_count]) ? $rows[0][$field_count] : '';
$cellTwoData = isset($rows[1][$field_count]) ? $rows[1][$field_count] : '';
$cellThreeData = isset($rows[2][$field_count]) ? $rows[2][$field_count] : '';
+
+ if(count($optionsRelated)>0 && @$_REQUEST['primary_key'] != $firstrow_name)
+ {
+ $options = array_merge($options, $related_options_clone);
+ }
$columns[] = array(
'field_choices' => implode('',$options),
'default_field' => $defaultFieldHTML,
@@ -382,6 +480,7 @@
$this->ss->assign("COLUMNCOUNT",$ret_field_count);
$this->ss->assign("rows",$columns);
+ $this->ss->assign("primary_field", @$_REQUEST['primary_key'] ? $_REQUEST['primary_key'] : 'id' );
$this->ss->assign('datetimeformat', $GLOBALS['timedate']->get_cal_date_time_format());
@@ -768,4 +867,4 @@
EOJAVASCRIPT;
}
+}
\ No newline at end of file