Index: trunk/kernel/units/general/cat_dbitem_export.php =================================================================== diff -u -r3757 -r3760 --- trunk/kernel/units/general/cat_dbitem_export.php (.../cat_dbitem_export.php) (revision 3757) +++ trunk/kernel/units/general/cat_dbitem_export.php (.../cat_dbitem_export.php) (revision 3760) @@ -3,6 +3,9 @@ define('EXPORT_STEP', 200); // export by 200 items (e.g. links) define('IMPORT_CHUNK', 50120); // 5 KB + define('IMPORT_TEMP', 1); + define('IMPORT_LIVE', 2); + class kCatDBItemExportHelper extends kHelper { var $false = false; @@ -462,8 +465,6 @@ $this->exportOptions = unserialize($this->Application->RecallVar($event->getPrefixSpecial().'_options')); } - $this->curItem =& $event->getObject( Array('skip_autoload' => true) ); - $backup_category_id = $this->Application->GetVar('m_cat_id'); $this->Application->SetVar('m_cat_id', (int)$this->Application->RecallVar('ImportCategory') ); @@ -503,12 +504,12 @@ $this->customValues = Array(); $data = $this->readRecord(); if ($data) { - $this->curItem->Clear(); - foreach ($data as $field_index => $field_value) { - $this->setFieldValue($field_index, $field_value); + if ($this->exportOptions['ReplaceDuplicates']) { + // set fields used as keys for replace duplicates code + $this->resetImportObject($event, IMPORT_TEMP, $data); } - $this->setCurrentID(); - $this->processCurrentItem($event); + + $this->processCurrentItem($event, $data); } $bytes_imported = ftell($this->filePointer) - $this->exportOptions['start_from']; } @@ -555,26 +556,88 @@ } } - /** - * Returns temporary items for import procedures - * - * @param kEvent $event - * @return kCatDBItem - */ - function &getTempItem(&$event) + function resetImportObject(&$event, $object_type, $record_data = null) { - return $this->Application->recallObject($event->Prefix.'.-tmpitem'.$event->Special, $event->Prefix, Array('skip_autoload' => true)); + switch ($object_type) { + case IMPORT_TEMP: + $this->curItem =& $event->getObject( Array('skip_autoload' => true) ); + break; + + case IMPORT_LIVE: + $this->curItem =& $this->Application->recallObject($event->Prefix.'.-tmpitem'.$event->Special, $event->Prefix, Array('skip_autoload' => true)); + break; + } + $this->curItem->Clear(); + + if (isset($record_data)) { + $this->setImportData($record_data); + } } + function setImportData($record_data) + { + foreach ($record_data as $field_index => $field_value) { + $this->setFieldValue($field_index, $field_value); + } + $this->setCurrentID(); + } + /** * Enter description here... * * @param kEvent $event */ - function processCurrentItem(&$event) + function processCurrentItem(&$event, $record_data) { - $tmp_item =& $this->getTempItem($event); - $tmp_item->Clear(); + $save_method = 'Create'; + // perform replace duplicates code + if ($this->exportOptions['ReplaceDuplicates']) { + // get replace keys first, then reset current item to empty one + $load_keys = Array(); + if ($this->exportOptions['CheckDuplicatesMethod'] == 1) { + if ($this->curItem->GetID()) { + $load_keys = Array($this->curItem->IDField => $this->curItem->GetID()); + } + } + else { + $key_fields = $this->exportOptions['DuplicateCheckFields']; + foreach ($key_fields as $key_field) { + $load_keys[$key_field] = $this->curItem->GetDBField($key_field); + } + } + + $this->resetImportObject($event, IMPORT_LIVE); + + 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 && $this->curItem->Load($item_id) ? 'Update' : 'Create'; + } + + $this->setImportData($record_data); + } + else { + $this->resetImportObject($event, IMPORT_LIVE, $record_data); + } // create/update categories $backup_category_id = $this->Application->GetVar('m_cat_id'); @@ -619,75 +682,34 @@ } // create main record - $save_method = 'Create'; - if ($this->exportOptions['ReplaceDuplicates']) { - $load_keys = Array(); - if ($this->exportOptions['CheckDuplicatesMethod'] == 1) { - if ($this->curItem->GetID()) { - $load_keys = Array($this->curItem->IDField => $this->curItem->GetID()); - } - } - else { - $key_fields = $this->exportOptions['DuplicateCheckFields']; - foreach ($key_fields as $key_field) { - $load_keys[$key_field] = $this->curItem->GetDBField($key_field); - } - } - - 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'; - } - } + $resource_id = $this->curItem->isLoaded() ? $this->curItem->GetDBField('ResourceId') : 0; - $resource_id = $tmp_item->isLoaded() ? $tmp_item->GetDBField('ResourceId') : 0; - $tmp_item->SetDBFieldsFromHash($this->curItem->FieldValues); if( ($save_method == 'Update') && $resource_id ) { - $tmp_item->SetDBField('ResourceId', $resource_id); - $tmp_item->setID($item_id); + $this->curItem->SetDBField('ResourceId', $resource_id); + $this->curItem->setID($item_id); } if ($save_method == 'Create') { - $this->fillRequiredFields($this->false, $tmp_item, true); + $this->fillRequiredFields($this->false, $this->curItem, true); } - if (!$tmp_item->$save_method()) { + if (!$this->curItem->$save_method()) { return false; } if ($load_keys && ($save_method == 'Create') && $this->exportOptions['ReplaceDuplicates']) { // map new id to old id - $this->addToCache('new_ids', $where_clause, $tmp_item->GetID() ); + $this->addToCache('new_ids', $where_clause, $this->curItem->GetID() ); } // set custom fields foreach ($this->customValues as $custom_field => $custom_value) { if (($save_method == 'Create') && !$custom_value) continue; - $tmp_item->SetCustomField($custom_field, $custom_value); + $this->curItem->SetCustomField($custom_field, $custom_value); } // assign item to categories - $tmp_item->assignToCategory($category_id, false); + $this->curItem->assignToCategory($category_id, false); $this->Application->SetVar('m_cat_id', $backup_category_id); return true;