Index: trunk/core/units/general/cat_dbitem_export.php =================================================================== diff -u -r4085 -r4243 --- trunk/core/units/general/cat_dbitem_export.php (.../cat_dbitem_export.php) (revision 4085) +++ trunk/core/units/general/cat_dbitem_export.php (.../cat_dbitem_export.php) (revision 4243) @@ -1,7 +1,7 @@ cacheTable = TABLE_PREFIX.'ImportCache'; + } + /** * Returns value from cache if found or false otherwise * @@ -61,12 +77,48 @@ * @param int $key * @param mixed $value */ - function addToCache($type, $key, $value) + function addToCache($type, $key, $value, $is_new = true) { // if (!isset($this->cache[$type])) $this->cache[$type] = Array(); $this->cache[$type][$key] = $value; + if ($is_new) { + $this->cacheStatus[$type][$key] = true; + } } + function storeCache($cache_types) + { + $cache_types = explode(',', $cache_types); + + $values_sql = ''; + foreach ($cache_types as $cache_type) { + $sql_mask = '('.$this->Conn->qstr($cache_type).',%s,%s),'; + $cache = getArrayValue($this->cacheStatus, $cache_type); + if (!$cache) $cache = Array(); + foreach ($cache as $var_name => $cache_status) { + $var_value = $this->cache[$cache_type][$var_name]; + $values_sql .= sprintf($sql_mask, $this->Conn->qstr($var_name), $this->Conn->qstr($var_value) ); + } + } + $values_sql = preg_replace('/(.*),$/', '\\1', $values_sql); + if ($values_sql) { + $sql = 'INSERT INTO '.$this->cacheTable.'(`CacheName`,`VarName`,`VarValue`) VALUES '.$values_sql; + $this->Conn->Query($sql); + } + + } + + function loadCache() + { + $sql = 'SELECT * FROM '.$this->cacheTable; + $records = $this->Conn->Query($sql); + + $this->cache = Array(); + foreach ($records as $record) { + $this->addToCache($record['CacheName'], $record['VarName'], $record['VarValue'], false); + } + } + /** * Fill required fields with dummy values * @@ -146,7 +198,7 @@ if ($event->Special == 'import') { - $this->exportOptions = unserialize($this->Application->RecallVar($event->getPrefixSpecial().'_options')); + $this->exportOptions = $this->loadOptions($event); $automatic_fields = ($object->GetDBField('FieldTitles') == 1); $object->setRequired('ExportColumns', !$automatic_fields); @@ -238,6 +290,7 @@ } } } + $this->saveOptions($event); } return $ret; @@ -369,7 +422,7 @@ */ function performExport(&$event) { - $this->exportOptions = unserialize($this->Application->RecallVar($event->getPrefixSpecial().'_options')); + $this->exportOptions = $this->loadOptions($event); $this->exportFields = $this->exportOptions['ExportColumns']; $this->curItem =& $event->getObject( Array('skip_autoload' => true) ); @@ -421,7 +474,7 @@ $this->closeFile(); $this->exportOptions['start_from'] += $records_exported; - $this->Application->StoreVar($event->getPrefixSpecial().'_options', serialize($this->exportOptions) ); + $this->saveOptions($event); return $this->exportOptions; } @@ -460,7 +513,7 @@ { if (!$this->exportOptions) { // load import options in case if not previously loaded in verification function - $this->exportOptions = unserialize($this->Application->RecallVar($event->getPrefixSpecial().'_options')); + $this->exportOptions = $this->loadOptions($event); } $backup_category_id = $this->Application->GetVar('m_cat_id'); @@ -489,8 +542,9 @@ $this->exportOptions['total_records'] = filesize($this->getImportFilename()); } else { - $this->cache['new_ids'] = $this->exportOptions['new_ids_hash']; + $this->loadCache(); } + $this->exportFields = $this->exportOptions['ExportColumns']; $this->addToCache('category_parent_path', $this->Application->GetVar('m_cat_id'), $this->exportOptions['ImportCategoryPath']); @@ -515,9 +569,14 @@ $this->Application->SetVar('m_cat_id', $backup_category_id); $this->exportOptions['start_from'] += $bytes_imported; - $this->exportOptions['new_ids_hash'] = getArrayValue($this->cache, 'new_ids'); - $this->Application->StoreVar($event->getPrefixSpecial().'_options', serialize($this->exportOptions) ); + $this->storeCache('new_ids'); + $this->saveOptions($event); + + if ($this->exportOptions['start_from'] == $this->exportOptions['total_records']) { + $this->Conn->Query('TRUNCATE TABLE '.$this->cacheTable); + } + return $this->exportOptions; } @@ -532,7 +591,7 @@ $value = null; } - $field_name = $this->exportFields[$field_index]; + $field_name = getArrayValue($this->exportFields, $field_index); if ($field_name == 'ResourceId') { return false; } @@ -541,12 +600,15 @@ $field_name = 'cust_'.substr($field_name, 7); $this->curItem->SetField($field_name, $value); } - elseif ($field_name == 'CategoryPath') { + elseif ($field_name == 'CategoryPath' || $field_name == '__CATEGORY__CategoryPath') { $this->curItem->CategoryPath = $value ? explode($this->exportOptions['CategorySeparator'], $value) : Array(); } elseif (substr($field_name, 0, 8) == 'Category') { - $this->curItem->CategoryPath[ (int)substr($field_name, 8) ] = $value; + $this->curItem->CategoryPath[ (int)substr($field_name, 8) - 1 ] = $value; } + elseif (substr($field_name, 0, 20) == '__CATEGORY__Category') { + $this->curItem->CategoryPath[ (int)substr($field_name, 20) ] = $value; + } elseif (substr($field_name, 0, 11) == '__VIRTUAL__') { $field_name = substr($field_name, 11); $this->curItem->SetField($field_name, $value); @@ -588,6 +650,61 @@ $this->setCurrentID(); } + + function getItemCategory() + { + $backup_category_id = $this->Application->GetVar('m_cat_id'); + + $category_id = $this->getFromCache('category_names', implode(':', $this->curItem->CategoryPath)); + if ($category_id) { + $this->Application->SetVar('m_cat_id', $category_id); + return $category_id; + } + + foreach ($this->curItem->CategoryPath as $category_index => $category_name) { + if (!$category_name) continue; + $category_key = crc32( implode(':', array_slice($this->curItem->CategoryPath, 0, $category_index + 1) ) ); + + $category_id = $this->getFromCache('category_names', $category_key); + 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 (ParentId = '.$current_category_id.')'; + $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_key, $category_id); + } + } + else { + $this->addToCache('category_names', $category_key, $category_id); + } + } + + if ($category_id) { + $this->Application->SetVar('m_cat_id', $category_id); + } + } + if (!$this->curItem->CategoryPath) { + $category_id = $backup_category_id; + } + + return $category_id; + } + /** * Enter description here... * @@ -596,10 +713,15 @@ function processCurrentItem(&$event, $record_data) { $save_method = 'Create'; + $load_keys = Array(); + + // create/update categories + $backup_category_id = $this->Application->GetVar('m_cat_id'); + // perform replace duplicates code if ($this->exportOptions['ReplaceDuplicates']) { // get replace keys first, then reset current item to empty one - $load_keys = Array(); + $category_id = $this->getItemCategory(); if ($this->exportOptions['CheckDuplicatesMethod'] == 1) { if ($this->curItem->GetID()) { $load_keys = Array($this->curItem->IDField => $this->curItem->GetID()); @@ -621,7 +743,7 @@ } $where_clause = preg_replace('/(.*) AND $/', '\\1', $where_clause); - $item_id = $this->getFromCache('new_ids', $where_clause); + $item_id = $this->getFromCache('new_ids', crc32($where_clause)); if (!$item_id) { if ($this->exportOptions['CheckDuplicatesMethod'] == 2) { // by other fields @@ -643,64 +765,24 @@ } 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_index => $category_name) { - if (!$category_name) continue; - $category_key = crc32( implode(':', array_slice($this->curItem->CategoryPath, 0, $category_index + 1) ) ); - - $category_id = $this->getFromCache('category_names', $category_key); - 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_key, $category_id); - } - } - else { - $this->addToCache('category_names', $category_key, $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); } +// $sql_start = getmicrotime(); if (!$this->curItem->$save_method()) { return false; } +// $sql_end = getmicrotime(); +// $this->saveLog('SQL ['.$save_method.'] Time: '.($sql_end - $sql_start).'s'); if ($load_keys && ($save_method == 'Create') && $this->exportOptions['ReplaceDuplicates']) { // map new id to old id - $this->addToCache('new_ids', $where_clause, $this->curItem->GetID() ); + $this->addToCache('new_ids', crc32($where_clause), $this->curItem->GetID() ); } // assign item to categories @@ -709,7 +791,14 @@ $this->Application->SetVar('m_cat_id', $backup_category_id); return true; } - + + /*function saveLog($msg) + { + $fp = fopen(FULL_PATH.'/sqls.log', 'a'); + fwrite($fp, $msg."\n"); + fclose($fp); + }*/ + /** * Returns category parent path, if possible, then from cache * @@ -929,6 +1018,19 @@ { return fgetcsv($this->filePointer, 10000, $this->exportOptions['FieldsSeparatedBy'], $this->exportOptions['FieldsEnclosedBy']); } + + function saveOptions(&$event, $options = null) + { + if (!isset($options)) { + $options = $this->exportOptions; + } + $this->Application->StoreVar($event->getPrefixSpecial().'_options', serialize($options) ); + } + + function loadOptions(&$event) + { + return unserialize($this->Application->RecallVar($event->getPrefixSpecial().'_options')); + } } ?>