Index: branches/unlabeled/unlabeled-1.2.2/kernel/units/general/helpers/recursive_helper.php =================================================================== diff -u -N -r5626 -r5627 --- branches/unlabeled/unlabeled-1.2.2/kernel/units/general/helpers/recursive_helper.php (.../recursive_helper.php) (revision 5626) +++ branches/unlabeled/unlabeled-1.2.2/kernel/units/general/helpers/recursive_helper.php (.../recursive_helper.php) (revision 5627) @@ -27,16 +27,8 @@ $temp_handler =& $this->Application->recallObject('c_TempHandler', 'kTempTablesHandler'); // 2. delete items this have this category as primary - $sql = 'SELECT ItemPrefix, ItemResourceId - FROM '.$ci_table.' - WHERE ('.$id_field.' = '.$category_id.') AND (PrimaryCat = 1)'; - $category_items = $this->Conn->GetCol($sql, 'ItemResourceId'); + $delete_ids = $this->getCategoryItems($category_id, true); - $delete_ids = Array(); - foreach ($category_items as $resource_id => $item_prefix) { - $delete_ids[$item_prefix][] = $resource_id; - } - foreach ($delete_ids as $item_prefix => $resource_ids) { if (!$item_prefix) { // not ItemPrefix filled -> old categoryitem linking @@ -84,7 +76,7 @@ $sql = 'UPDATE '.$table_name.' SET ParentId = '.$dest_category_id.' - WHERE '.$id_field.' IN ('.$category_ids.')'; + WHERE '.$id_field.' IN ('.implode(',', $category_ids).')'; $this->Conn->Query($sql); $this->Application->StoreVar('PermCache_UpdateRequired', 1); @@ -97,34 +89,37 @@ */ function PasteCategory($category_id) { + $backup_category_id = $this->Application->GetVar('m_cat_id'); + + // 1. clone category + $temp_handler->BuildTables('c', Array($category_id)); + $new_category_id = array_pop( $temp_handler->CloneItems('c', '', Array($category_id)) ); + $this->Application->SetVar('m_cat_id', $new_category_id); + $id_field = $this->Application->getUnitOption('c', 'IDField'); $table_name = $this->Application->getUnitOption('c', 'TableName'); - $sql = 'SELECT '.$id_field.' - FROM '.$table_name.' - WHERE ParentId = '.$category_id; + // 2. assign supplemental items to current category to new category + $paste_ids = $this->getCategoryItems($category_id, false); - $sub_categories = $this->Conn->GetCol($sql); - if ($sub_categories) { - foreach ($sub_categories as $sub_category_id) { - $this->PasteCategory($sub_category_id); + foreach ($paste_ids as $item_prefix => $resource_ids) { + if (!$item_prefix) { + // not ItemPrefix filled -> old categoryitem linking + continue; } + + $item_object =& $this->Application->recallObject($item_prefix.'.-item', null, Array('skip_autoload' => true)); + foreach ($resource_ids as $item_resource_id) { + $item_object->Load($item_resource_id, 'ResourceId'); + $item_object->assignToCategory($new_category_id, false); + } } - $ci_table = $this->Application->getUnitOption('ci', 'TableName'); + // 3. clone items that have current category as primary $temp_handler =& $this->Application->recallObject('c_TempHandler', 'kTempTablesHandler'); - // 1. select all items from this category - $sql = 'SELECT ItemPrefix, ItemResourceId - FROM '.$ci_table.' - WHERE ('.$id_field.' = '.$category_id.')'; - $category_items = $this->Conn->GetCol($sql, 'ItemResourceId'); + $paste_ids = $this->getCategoryItems($category_id, true); - $paste_ids = Array(); - foreach ($category_items as $resource_id => $item_prefix) { - $paste_ids[$item_prefix][] = $resource_id; - } - foreach ($paste_ids as $item_prefix => $resource_ids) { if (!$item_prefix) { // not ItemPrefix filled -> old categoryitem linking @@ -137,10 +132,43 @@ $temp_handler->CloneItems($item_prefix, '', $item_ids); } - // 3. clone this category - $temp_handler->BuildTables('c', Array($category_id)); - $temp_handler->CloneItems('c', '', Array($category_id)); + // 4. do same stuff for each subcategory + $sql = 'SELECT '.$id_field.' + FROM '.$table_name.' + WHERE ParentId = '.$category_id; + + $sub_categories = $this->Conn->GetCol($sql); + if ($sub_categories) { + foreach ($sub_categories as $sub_category_id) { + $this->PasteCategory($sub_category_id); + } + } + + $this->Application->SetVar('m_cat_id', $backup_category_id); } + + /** + * Returns grouped category items + * + * @param int $category_id + * @param bool $item_primary_category + * @return Array + */ + function getCategoryItems($category_id, $item_primary_category = true) + { + $ci_table = $this->Application->getUnitOption('ci', 'TableName'); + + $sql = 'SELECT ItemPrefix, ItemResourceId + FROM '.$ci_table.' + WHERE (CategoryId = '.$category_id.') AND (PrimaryCat = '.($item_primary_category ? 1 : 0).')'; + $category_items = $this->Conn->GetCol($sql, 'ItemResourceId'); + + $item_ids = Array(); + foreach ($category_items as $resource_id => $item_prefix) { + $item_ids[$item_prefix][] = $resource_id; + } + return $item_ids; + } } ?> \ No newline at end of file