Index: trunk/kernel/units/category_items/category_items_event_handler.php =================================================================== diff -u -r1566 -r2041 --- trunk/kernel/units/category_items/category_items_event_handler.php (.../category_items_event_handler.php) (revision 1566) +++ trunk/kernel/units/category_items/category_items_event_handler.php (.../category_items_event_handler.php) (revision 2041) @@ -96,6 +96,55 @@ } } + /** + * Removes primary mark from cloned category items record + * + * @param kEvent $event + */ + function OnAfterClone(&$event) + { + $id = $event->getEventParam('id'); + $table = $this->Application->getUnitOption($event->Prefix, 'TableName'); + $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField'); + $sql = 'UPDATE %s SET PrimaryCat = 0 WHERE %s = %s'; + + $this->Conn->Query( sprintf($sql, $table, $id_field, $id) ); + } + + /** + * Deletes items of requested type from requested categories. + * In case if item is deleted from it's last category, then delete item too. + * + * @param kEvent $event + */ + function OnDeleteFromCategory(&$event) + { + $category_ids = $event->getEventParam('category_ids'); + if(!$category_ids) return false; + + $item_prefix = $event->getEventParam('item_prefix'); + $this->Application->setUnitOption($item_prefix, 'AutoLoad', false); + $item =& $this->Application->recallObject($item_prefix.'.-item'); + + $ci_table = $this->Application->getUnitOption($event->Prefix, 'TableName'); + $item_table = $this->Application->getUnitOption($item_prefix, 'TableName'); + + $sql = 'SELECT ItemResourceId, CategoryId FROM %1$s INNER JOIN %2$s ON (%1$s.ResourceId = %2$s.ItemResourceId) WHERE CategoryId IN (%3$s)'; + $category_items = $this->Conn->Query( sprintf($sql, $item_table, $ci_table, implode(',', $category_ids) ) ); + + $item_hash = Array(); + foreach($category_items as $ci_row) + { + $item_hash[ $ci_row['ItemResourceId'] ][] = $ci_row['CategoryId']; + } + + foreach($item_hash as $item_resource_id => $delete_category_ids) + { + $item->Load($item_resource_id, 'ResourceId'); + $item->DeleteFromCategories($delete_category_ids); + } + } + } ?> \ No newline at end of file