Index: trunk/kernel/units/general/cat_dbitem.php =================================================================== diff -u -r1566 -r2041 --- trunk/kernel/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 1566) +++ trunk/kernel/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 2041) @@ -108,6 +108,37 @@ return $ret; } + + function DeleteFromCategories($delete_category_ids) + { + $id_field = $this->Application->getUnitOption($this->Prefix, 'IDField'); // because item was loaded before by ResourceId + + $ci_table = $this->Application->getUnitOption('ci', 'TableName'); + $resource_id = $this->GetDBField('ResourceId'); + + $item_cats_sql = 'SELECT CategoryId FROM %s WHERE ItemResourceId = %s'; + $delete_category_items_sql = 'DELETE FROM %s WHERE ItemResourceId = %s'; + + $category_ids = $this->Conn->GetCol( sprintf($item_cats_sql, $ci_table, $resource_id) ); + $cats_left = array_diff($category_ids, $delete_category_ids); + if(!$cats_left) + { + $sql = 'SELECT %s FROM %s WHERE ResourceId = %s'; + $ids = $this->Conn->GetCol( sprintf($sql, $id_field, $this->TableName, $resource_id) ); + + $temp =& $this->Application->recallObject($this->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler'); + $temp->DeleteItems($this->Prefix, $this->Special, $ids); + } + else + { + $this->Conn->Query( sprintf($delete_category_items_sql, $ci_table, $resource_id) ); + if( count($cats_left) == 1 ) + { + $sql = 'REPLACE INTO %s (CategoryId,ItemResourceId,PrimaryCat) VALUES (%s,%s,1)'; + $this->Conn->Query( sprintf($sql, $ci_table, end($cats_left), $resource_id ) ); + } + } + } } ?> \ No newline at end of file