Index: trunk/core/units/general/cat_dbitem_export.php =================================================================== diff -u -r3709 -r3757 --- trunk/core/units/general/cat_dbitem_export.php (.../cat_dbitem_export.php) (revision 3709) +++ trunk/core/units/general/cat_dbitem_export.php (.../cat_dbitem_export.php) (revision 3757) @@ -5,6 +5,7 @@ class kCatDBItemExportHelper extends kHelper { + var $false = false; var $cache = Array(); @@ -82,24 +83,34 @@ * * @param kEvent $event */ - function fillRequiredFields(&$event) + function fillRequiredFields(&$event, &$object, $set_status = false) { - $object =& $event->getObject(); + if ($object == $this->false) { + $object =& $event->getObject(); + } + $has_empty = false; $fields = array_keys($object->Fields); foreach ($fields as $field_name) { $field_options =& $object->Fields[$field_name]; if (isset($object->VirtualFields[$field_name]) || !getArrayValue($field_options, 'required') ) continue; + if ( $object->GetDBField($field_name) ) continue; $formatter_class = getArrayValue($field_options, 'formatter'); if ($formatter_class) // not tested { $formatter =& $this->Application->recallObject($formatter_class); $sample_value = $formatter->GetSample($field_name, $field_options, $object); } - $object->SetDBField($field_name, isset($sample_value) && $sample_value ? $sample_value : 'dummy'); + + $has_empty = true; + $object->SetDBField($field_name, isset($sample_value) && $sample_value ? $sample_value : 'no value'); } + + if ($set_status && $has_empty) { + $object->SetDBField('Status', 0); + } } /** @@ -115,7 +126,7 @@ return false; } - $this->fillRequiredFields($event); + $this->fillRequiredFields($event, $this->false); $object =& $event->getObject(); $cross_unique_fields = Array('FieldsSeparatedBy', 'FieldsEnclosedBy'); @@ -519,6 +530,10 @@ function setFieldValue($field_index, $value) { + if (empty($value)) { + $value = null; + } + $field_name = $this->exportFields[$field_index]; if (substr($field_name, 0, 7) == 'Custom_') { @@ -559,6 +574,7 @@ function processCurrentItem(&$event) { $tmp_item =& $this->getTempItem($event); + $tmp_item->Clear(); // create/update categories $backup_category_id = $this->Application->GetVar('m_cat_id'); @@ -605,8 +621,11 @@ // create main record $save_method = 'Create'; if ($this->exportOptions['ReplaceDuplicates']) { + $load_keys = Array(); if ($this->exportOptions['CheckDuplicatesMethod'] == 1) { - $load_keys = Array($this->curItem->IDField => $this->curItem->GetID()); + if ($this->curItem->GetID()) { + $load_keys = Array($this->curItem->IDField => $this->curItem->GetID()); + } } else { $key_fields = $this->exportOptions['DuplicateCheckFields']; @@ -615,23 +634,30 @@ } } - $where_clause = ''; - foreach ($load_keys as $field_name => $field_value) { - $where_clause .= '(item_table.`'.$field_name.'` = '.$this->Conn->qstr($field_value).') AND '; + if (count($load_keys)) { + $where_clause = ''; + foreach ($load_keys as $field_name => $field_value) { + $where_clause .= '(item_table.`'.$field_name.'` = '.$this->Conn->qstr($field_value).') AND '; + } + $where_clause = preg_replace('/(.*) AND $/', '\\1', $where_clause); + + $item_id = $this->getFromCache('new_ids', $where_clause); + if (!$item_id) { + if ($this->exportOptions['CheckDuplicatesMethod'] == 2) { + // by other fields + $parent_path = $this->getParentPath($category_id); + $where_clause = '(c.ParentPath LIKE "'.$parent_path.'%") AND '.$where_clause; + } + + $sql = 'SELECT '.$this->curItem->IDField.' + FROM '.$this->curItem->TableName.' item_table + LEFT JOIN '.TABLE_PREFIX.'CategoryItems ci ON ci.ItemResourceId = item_table.ResourceId + LEFT JOIN '.TABLE_PREFIX.'Category c ON c.CategoryId = ci.CategoryId + WHERE '.$where_clause; + $item_id = $this->Conn->GetOne($sql); + } + $save_method = $item_id && $tmp_item->Load($item_id) ? 'Update' : 'Create'; } - $where_clause = preg_replace('/(.*) AND $/', '\\1', $where_clause); - - $item_id = $this->getFromCache('new_ids', $where_clause); - if (!$item_id) { - $parent_path = $this->getParentPath($category_id); - $sql = 'SELECT '.$this->curItem->IDField.' - FROM '.$this->curItem->TableName.' item_table - LEFT JOIN '.TABLE_PREFIX.'CategoryItems ci ON ci.ItemResourceId = item_table.ResourceId - LEFT JOIN '.TABLE_PREFIX.'Category c ON c.CategoryId = ci.CategoryId - WHERE (c.ParentPath LIKE "'.$parent_path.'%") AND '.$where_clause; - $item_id = $this->Conn->GetOne($sql); - } - $save_method = $item_id && $tmp_item->Load($item_id) ? 'Update' : 'Create'; } $resource_id = $tmp_item->isLoaded() ? $tmp_item->GetDBField('ResourceId') : 0; @@ -641,11 +667,15 @@ $tmp_item->setID($item_id); } + if ($save_method == 'Create') { + $this->fillRequiredFields($this->false, $tmp_item, true); + } + if (!$tmp_item->$save_method()) { return false; } - if ( ($save_method == 'Create') && $this->exportOptions['ReplaceDuplicates'] ) { + if ($load_keys && ($save_method == 'Create') && $this->exportOptions['ReplaceDuplicates']) { // map new id to old id $this->addToCache('new_ids', $where_clause, $tmp_item->GetID() ); }