Index: branches/unlabeled/unlabeled-1.12.10/kernel/units/general/cat_dbitem_export.php =================================================================== diff -u -N -r3798 -r4105 --- branches/unlabeled/unlabeled-1.12.10/kernel/units/general/cat_dbitem_export.php (.../cat_dbitem_export.php) (revision 3798) +++ branches/unlabeled/unlabeled-1.12.10/kernel/units/general/cat_dbitem_export.php (.../cat_dbitem_export.php) (revision 4105) @@ -196,7 +196,7 @@ } } - if (!$ret) { + if (!$ret && $this->Application->isDebugMode()) { $this->Application->Debugger->appendHTML('Missing required for import/export:'); $this->Application->Debugger->dumpVars($missing_columns); } @@ -447,10 +447,20 @@ $field_name = preg_replace('/^Custom_(.*)/', '__CUSTOM__\\1', $field_name); // 2. convert category field (mixed version & serparated version) - $field_name = preg_replace('/^Category(Path|[0-9]+)/', '__CATEGORY__Category\\1', $field_name); + $field_name = preg_replace('/^Category(Id|Path|[0-9]+)/', '__CATEGORY__Category\\1', $field_name); $valid_fields = $object->getPossibleExportColumns(); - return isset($valid_fields[$field_name]) || isset($valid_fields['__VIRTUAL__'.$field_name]); + + $is_multilingual = preg_match('/l([\d]+)_(.*)/', $field_name, $rets); + if ($is_multilingual) { + $ml_formatter =& $this->Application->recallObject('kMultiLanguage'); + $fields = $this->Application->getUnitOption($object->Prefix, 'Fields'); + $fields[$field_name] = $fields[$ml_formatter->LangFieldName($rets[2])]; + $object->Fields[$field_name] = $fields[$ml_formatter->LangFieldName($rets[2])]; + $this->Application->setUnitOption($object->Prefix, 'Fields', $fields); + } + + return $is_multilingual || isset($valid_fields[$field_name]) || isset($valid_fields['__VIRTUAL__'.$field_name]); } /** @@ -544,6 +554,13 @@ $field_name = substr($field_name, 7); $this->customValues[$field_name] = $value; } + elseif ($field_name == '__CATEGORY__CategoryId') { + $field_name = substr($field_name, 10); + $this->curItem->SetField($field_name, $value); + } + elseif ($field_name == 'CategoryId') { + $this->curItem->SetField($field_name, $value); + } elseif ($field_name == 'CategoryPath') { $this->curItem->CategoryPath = $value ? explode($this->exportOptions['CategorySeparator'], $value) : Array(); } @@ -591,16 +608,93 @@ $this->setCurrentID(); } + /*function sortCategoryPath() + { + $category_path = $this->curItem->CategoryPath; + + foreach ($category_path as $category_index => $category_name) { + $category_path[$category_index] = $category_name.'#'.$category_index; + } + + $category_path = array_flip($category_path); + sort($category_path, SORT_NUMERIC); + $category_path = array_flip($category_path); + + foreach ($category_path as $category_index => $category_name) { + $category_path[$category_index] = substr($category_name, 0, strlen($category_name) - strlen($category_index) - 1); + } + $this->curItem->CategoryPath = $category_path; + }*/ + + function getItemCategory() + { + $ml_formatter =& $this->Application->recallObject('kMultiLanguage'); + $title_field = $ml_formatter->LangFieldName('Name'); + + $backup_category_id = $this->Application->GetVar('m_cat_id'); + $force_category_id = (int)$this->curItem->GetDBField('CategoryId'); + + // sort category path +// $this->sortCategoryPath(); + + foreach ($this->curItem->CategoryPath as $category_index => $category_name) { + if (!$category_name) continue; + $category_id = $this->getFromCache('category_names', $category_name); + if ($category_id === false) { + // get parent category path to search only in it + $current_category_id = $this->Application->GetVar('m_cat_id'); + $parent_path = $this->getParentPath($current_category_id); + + // get category id from database by name + $sql = 'SELECT CategoryId + FROM '.TABLE_PREFIX.'Category + WHERE ('.$title_field.' = '.$this->Conn->qstr($category_name).') AND (ParentPath LIKE "'.$parent_path.'%")'; + $category_id = $this->Conn->GetOne($sql); + + if ($category_id === false) { + // category not in db -> create + $category_fields = Array( $title_field => $category_name, 'Description' => $category_name, + 'Status' => STATUS_ACTIVE, 'ParentId' => $current_category_id, + 'AutomaticFilename' => 1, 'CategoryId' => $force_category_id, + ); + $this->dummyCategory->SetDBFieldsFromHash($category_fields); + + $force_id = $category_index == count($this->curItem->CategoryPath) ? $force_category_id : false; + if ($this->dummyCategory->Create($force_id)) { + $category_id = $this->dummyCategory->GetID(); + $this->addToCache('category_parent_path', $category_id, $this->dummyCategory->GetDBField('ParentPath')); + $this->addToCache('category_names', $category_name, $category_id); + } + } + else { + $this->addToCache('category_names', $category_name, $category_id); + } + } + + if ($category_id) { + $this->Application->SetVar('m_cat_id', $category_id); + } + } + if (!$this->curItem->CategoryPath) { + $category_id = $force_category_id ? $force_category_id : $backup_category_id; + } + return $category_id; + } + /** * Enter description here... * * @param kEvent $event */ function processCurrentItem(&$event, $record_data) { + // create/update categories + $backup_category_id = $this->Application->GetVar('m_cat_id'); + $save_method = 'Create'; // perform replace duplicates code if ($this->exportOptions['ReplaceDuplicates']) { + $category_id = $this->getItemCategory(); // get replace keys first, then reset current item to empty one $load_keys = Array(); if ($this->exportOptions['CheckDuplicatesMethod'] == 1) { @@ -646,50 +740,9 @@ } else { $this->resetImportObject($event, IMPORT_LIVE, $record_data); + $category_id = $this->getItemCategory(); } - // create/update categories - $backup_category_id = $this->Application->GetVar('m_cat_id'); - - foreach ($this->curItem->CategoryPath as $category_name) { - if (!$category_name) continue; - $category_id = $this->getFromCache('category_names', $category_name); - if ($category_id === false) { - // get parent category path to search only in it - $current_category_id = $this->Application->GetVar('m_cat_id'); - $parent_path = $this->getParentPath($current_category_id); - - // get category id from database by name - $sql = 'SELECT CategoryId - FROM '.TABLE_PREFIX.'Category - WHERE (Name = '.$this->Conn->qstr($category_name).') AND (ParentPath LIKE "'.$parent_path.'%")'; - $category_id = $this->Conn->GetOne($sql); - - if ($category_id === false) { - // category not in db -> create - $category_fields = Array( 'Name' => $category_name, 'Description' => $category_name, - 'Status' => STATUS_ACTIVE, 'ParentId' => $current_category_id, 'AutomaticFilename' => 1 - ); - $this->dummyCategory->SetDBFieldsFromHash($category_fields); - if ($this->dummyCategory->Create()) { - $category_id = $this->dummyCategory->GetID(); - $this->addToCache('category_parent_path', $category_id, $this->dummyCategory->GetDBField('ParentPath')); - $this->addToCache('category_names', $category_name, $category_id); - } - } - else { - $this->addToCache('category_names', $category_name, $category_id); - } - } - - if ($category_id) { - $this->Application->SetVar('m_cat_id', $category_id); - } - } - if (!$this->curItem->CategoryPath) { - $category_id = $backup_category_id; - } - // create main record if ($save_method == 'Create') { $this->fillRequiredFields($this->false, $this->curItem, true);