Index: branches/unlabeled/unlabeled-1.2.2/core/units/general/helpers/recursive_helper.php =================================================================== diff -u -r5551 -r5594 --- branches/unlabeled/unlabeled-1.2.2/core/units/general/helpers/recursive_helper.php (.../recursive_helper.php) (revision 5551) +++ branches/unlabeled/unlabeled-1.2.2/core/units/general/helpers/recursive_helper.php (.../recursive_helper.php) (revision 5594) @@ -18,19 +18,60 @@ } } + $ci_table = $this->Application->getUnitOption('ci', 'TableName'); // 1. remove category items from this category if it is supplemental (non-primary) category to them - $sql = 'DELETE FROM '.TABLE_PREFIX.'CategoryItems + $sql = 'DELETE FROM '.$ci_table.' WHERE ('.$id_field.' = '.$category_id.') AND (PrimaryCat = 0)'; $this->Conn->Query($sql); $temp_handler =& $this->Application->recallObject('c_TempHandler', 'kTempTablesHandler'); // 2. delete items this have this category as primary -// $temp_handler->DeleteItems($item_prefix, '', $item_ids); + $sql = 'SELECT ItemPrefix, ItemResourceId + FROM '.$ci_table.' + WHERE ('.$id_field.' = '.$category_id.') AND (PrimaryCat = 1)'; + $category_items = $this->Conn->GetCol($sql, 'ItemResourceId'); + $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 + continue; + } + $item_ids = $this->GetItemIDs($item_prefix, $resource_ids); + $temp_handler->BuildTables($item_prefix, $item_ids); + $temp_handler->DeleteItems($item_prefix, '', $item_ids); + } + // 3. delete this category + $temp_handler->BuildTables('c', Array($category_id)); $temp_handler->DeleteItems('c', '', Array($category_id)); + } + + /** + * Converts resource ids list to id field list for given prefix + * + * @param string $prefix + * @param Array $resource_ids + * @return Array + */ + function GetItemIDs($prefix, $resource_ids) + { + if (!$resource_ids) { + return Array(); + } + $id_field = $this->Application->getUnitOption($prefix, 'IDField'); + $table_name = $this->Application->getUnitOption($prefix, 'TableName'); + + $sql = 'SELECT '.$id_field.' + FROM '.$table_name.' + WHERE ResourceId IN ('.implode(',', $resource_ids).')'; + return $this->Conn->GetCol($sql); } }