Index: branches/5.3.x/core/units/helpers/cat_dbitem_export_helper.php =================================================================== diff -u -N -r15483 -r15698 --- branches/5.3.x/core/units/helpers/cat_dbitem_export_helper.php (.../cat_dbitem_export_helper.php) (revision 15483) +++ branches/5.3.x/core/units/helpers/cat_dbitem_export_helper.php (.../cat_dbitem_export_helper.php) (revision 15698) @@ -1,6 +1,6 @@ Application->getUnitOption($this->curItem->Prefix, 'CatalogItem') ) { + if ( !$this->curItem->getUnitConfig()->getCatalogItem() ) { return $this->GetPlainExportSQL($count_only); // in case this is not a CategoryItem } @@ -496,7 +496,7 @@ $custom_sql = $this->getCustomSQL(); if ( $custom_sql ) { - $custom_table = $this->Application->getUnitOption($this->curItem->Prefix . '-cdata', 'TableName'); + $custom_table = $this->Application->getUnitConfig($this->curItem->Prefix . '-cdata')->getTableName(); $join_clauses[$custom_table . ' custom_data'] = 'custom_data.ResourceId = item_table.ResourceId'; } @@ -551,40 +551,40 @@ { $this->exportOptions = $this->loadOptions($event); $this->exportFields = $this->exportOptions['ExportColumns']; - $this->curItem = $event->getObject( Array('skip_autoload' => true) ); - $this->customFields = $this->Application->getUnitOption($event->Prefix, 'CustomFields'); + $this->curItem = $event->getObject(Array ('skip_autoload' => true)); + $this->customFields = $event->getUnitConfig()->getCustomFields(); $this->openFile($event); - if ($this->exportOptions['start_from'] == 0) // first export step - { - if (!getArrayValue($this->exportOptions, 'IsBaseCategory')) { + if ( $this->exportOptions['start_from'] == 0 ) { + // first export step + if ( !getArrayValue($this->exportOptions, 'IsBaseCategory') ) { $this->exportOptions['IsBaseCategory'] = 0; } - if ($this->exportOptions['IsBaseCategory'] ) { + if ( $this->exportOptions['IsBaseCategory'] ) { $sql = 'SELECT ParentPath - FROM '.TABLE_PREFIX.'Categories + FROM ' . TABLE_PREFIX . 'Categories WHERE CategoryId = ' . (int)$this->Application->GetVar('m_cat_id'); $parent_path = $this->Conn->GetOne($sql); $parent_path = explode('|', substr($parent_path, 1, -1)); - if ($parent_path && $parent_path[0] == $this->Application->getBaseCategory()) { + + if ( $parent_path && $parent_path[0] == $this->Application->getBaseCategory() ) { array_shift($parent_path); } $this->exportOptions['BaseLevel'] = count($parent_path); // level to cut from other categories } // 1. export field titles if required - if ($this->exportOptions['IncludeFieldTitles']) - { - $data_array = Array(); - foreach ($this->exportFields as $export_field) - { + if ( $this->exportOptions['IncludeFieldTitles'] ) { + $data_array = Array (); + foreach ($this->exportFields as $export_field) { $data_array = array_merge($data_array, $this->getFieldCaption($export_field)); } $this->writeRecord($data_array); } - $this->exportOptions['total_records'] = $this->Conn->GetOne( $this->getExportSQL(true) ); + + $this->exportOptions['total_records'] = $this->Conn->GetOne($this->getExportSQL(true)); } // 2. export data @@ -786,7 +786,7 @@ } $this->curItem->Clear(); $this->curItem->SetDBField('CategoryId', NULL); // since default value is import root category - $this->customFields = $this->Application->getUnitOption($event->Prefix, 'CustomFields'); + $this->customFields = $event->getUnitConfig()->getCustomFields(); if (isset($record_data)) { $this->setImportData($record_data); @@ -875,72 +875,77 @@ function processCurrentItem($event, $record_data) { $save_method = 'Create'; - $load_keys = Array(); + $load_keys = Array (); // create/update categories $backup_category_id = $this->Application->GetVar('m_cat_id'); // perform replace duplicates code - if ($this->exportOptions['ReplaceDuplicates']) { + if ( $this->exportOptions['ReplaceDuplicates'] ) { // get replace keys first, then reset current item to empty one $category_id = $this->getItemCategory(); - if ($this->exportOptions['CheckDuplicatesMethod'] == 1) { - if ($this->curItem->GetID()) { - $load_keys = Array($this->curItem->IDField => $this->curItem->GetID()); + + 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)) { + if ( count($load_keys) ) { $where_clause = ''; $language_id = (int)$this->Application->GetVar('m_lang'); - if (!$language_id) { + if ( !$language_id ) { $language_id = 1; } foreach ($load_keys as $field_name => $field_value) { - if (preg_match('/^cust_(.*)/', $field_name, $regs)) { + if ( preg_match('/^cust_(.*)/', $field_name, $regs) ) { $custom_id = array_search($regs[1], $this->customFields); - $field_name = 'l'.$language_id.'_cust_'.$custom_id; - $where_clause .= '(custom_data.`'.$field_name.'` = '.$this->Conn->qstr($field_value).') AND '; + $field_name = 'l' . $language_id . '_cust_' . $custom_id; + $where_clause .= '(custom_data.`' . $field_name . '` = ' . $this->Conn->qstr($field_value) . ') AND '; } else { - $where_clause .= '(item_table.`'.$field_name.'` = '.$this->Conn->qstr($field_value).') AND '; + $where_clause .= '(item_table.`' . $field_name . '` = ' . $this->Conn->qstr($field_value) . ') AND '; } - } $where_clause = substr($where_clause, 0, -5); $item_id = $this->getFromCache('new_ids', kUtil::crc32($where_clause)); - if (!$item_id) { - if ($this->exportOptions['CheckDuplicatesMethod'] == 2) { + + 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; + $where_clause = '(c.ParentPath LIKE "' . $parent_path . '%") AND ' . $where_clause; } - $cdata_table = $this->Application->getUnitOption($event->Prefix.'-cdata', 'TableName'); - $sql = 'SELECT '.$this->curItem->IDField.' - FROM '.$this->curItem->TableName.' item_table - LEFT JOIN '.$cdata_table.' custom_data ON custom_data.ResourceId = item_table.ResourceId - LEFT JOIN '.TABLE_PREFIX.'CategoryItems ci ON ci.ItemResourceId = item_table.ResourceId - LEFT JOIN '.TABLE_PREFIX.'Categories c ON c.CategoryId = ci.CategoryId - WHERE '.$where_clause; + $cdata_table = $this->Application->getUnitConfig($event->Prefix . '-cdata')->getTableName(); + + $sql = 'SELECT ' . $this->curItem->IDField . ' + FROM ' . $this->curItem->TableName . ' item_table + LEFT JOIN ' . $cdata_table . ' custom_data ON custom_data.ResourceId = item_table.ResourceId + LEFT JOIN ' . TABLE_PREFIX . 'CategoryItems ci ON ci.ItemResourceId = item_table.ResourceId + LEFT JOIN ' . TABLE_PREFIX . 'Categories 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'; - if ($save_method == 'Update') { + + if ( $save_method == 'Update' ) { // replace id from csv file with found id (only when ID is found in cvs file) - if (in_array($this->curItem->IDField, $this->exportFields)) { - $record_data[ array_search($this->curItem->IDField, $this->exportFields) ] = $item_id; + if ( in_array($this->curItem->IDField, $this->exportFields) ) { + $record_data[array_search($this->curItem->IDField, $this->exportFields)] = $item_id; } } } @@ -953,27 +958,29 @@ } // create main record - if ($save_method == 'Create') { + if ( $save_method == 'Create' ) { $this->fillRequiredFields($this->false, $this->curItem, true); } // $sql_start = microtime(true); - if (!$this->curItem->$save_method()) { + if ( !$this->curItem->$save_method() ) { $this->Application->SetVar('m_cat_id', $backup_category_id); + return false; } // $sql_end = microtime(true); // $this->saveLog('SQL ['.$save_method.'] Time: '.($sql_end - $sql_start).'s'); - if ($load_keys && ($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', kUtil::crc32($where_clause), $this->curItem->GetID() ); + $this->addToCache('new_ids', kUtil::crc32($where_clause), $this->curItem->GetID()); } // assign item to categories $this->curItem->assignToCategory($category_id, false); $this->Application->SetVar('m_cat_id', $backup_category_id); + return true; } @@ -1251,26 +1258,26 @@ */ function prepareExportColumns($event) { - $object = $event->getObject( Array('skip_autoload' => true) ); + $object = $event->getObject(Array ('skip_autoload' => true)); /* @var $object kCatDBItem */ if ( !$object->isField('ExportColumns') ) { // import/export prefix was used (see kDBEventHandler::prepareObject) but object don't plan to be imported/exported - return ; + return; } - $available_columns = Array(); + $available_columns = Array (); - if ($this->Application->getUnitOption($event->Prefix, 'CatalogItem')) { + if ( $event->getUnitConfig()->getCatalogItem() ) { // category field (mixed) $available_columns['__CATEGORY__CategoryPath'] = 'CategoryPath'; - if ($event->Special == 'import') { + if ( $event->Special == 'import' ) { // category field (separated fields) $max_level = $this->Application->ConfigValue('MaxImportCategoryLevels'); $i = 0; - while ($i < $max_level) { - $available_columns['__CATEGORY__Category'.($i + 1)] = 'Category'.($i + 1); + while ( $i < $max_level ) { + $available_columns['__CATEGORY__Category' . ($i + 1)] = 'Category' . ($i + 1); $i++; } } @@ -1281,38 +1288,35 @@ foreach ($fields as $field_name => $field_options) { if ( !$object->skipField($field_name) ) { - $available_columns[$field_name] = $field_name.( $object->isRequired($field_name) ? '*' : ''); + $available_columns[$field_name] = $field_name . ($object->isRequired($field_name) ? '*' : ''); } } - $handler = $this->Application->recallObject($event->Prefix.'_EventHandler'); + $handler = $this->Application->recallObject($event->Prefix . '_EventHandler'); /* @var $handler kDBEventHandler */ $available_columns = array_merge($available_columns, $handler->getCustomExportColumns($event)); // custom fields $custom_fields = $object->getCustomFields(); - foreach ($custom_fields as $custom_id => $custom_name) - { - $available_columns['__CUSTOM__'.$custom_name] = $custom_name; + foreach ($custom_fields as $custom_id => $custom_name) { + $available_columns['__CUSTOM__' . $custom_name] = $custom_name; } // columns already in use - $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); - if ($items_info) - { + $items_info = $this->Application->GetVar($event->getPrefixSpecial(true)); + if ( $items_info ) { list($item_id, $field_values) = each($items_info); $export_keys = $field_values['ExportColumns']; - $export_keys = $export_keys ? explode('|', substr($export_keys, 1, -1) ) : Array(); + $export_keys = $export_keys ? explode('|', substr($export_keys, 1, -1)) : Array (); } else { - $export_keys = Array(); + $export_keys = Array (); } - $export_columns = Array(); - foreach ($export_keys as $field_key) - { + $export_columns = Array (); + foreach ($export_keys as $field_key) { $field_name = $this->getExportField($field_key); $export_columns[$field_key] = $field_name; unset($available_columns[$field_key]); @@ -1424,7 +1428,7 @@ */ function getModuleName($event) { - $module_path = $this->Application->getUnitOption($event->Prefix, 'ModuleFolder') . '/'; + $module_path = $event->getUnitConfig()->getModuleFolder() . '/'; $module_name = $this->Application->findModule('Path', $module_path, 'Name'); return mb_strtolower($module_name);