Index: trunk/kernel/units/general/cat_event_handler.php =================================================================== diff -u -r2004 -r2041 --- trunk/kernel/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 2004) +++ trunk/kernel/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 2041) @@ -13,7 +13,7 @@ $this->Application->StoreVar($event->getPrefixSpecial().'_clipboard', implode(',', $ids)); $this->Application->StoreVar($event->getPrefixSpecial().'_clipboard_mode', 'copy'); - $this->Application->StoreVar('clipboard', 'COPY-0.'.$object->TableName.'.ResourceId=0'); + $this->Application->StoreVar('ClipBoard', 'COPY-0.'.$object->TableName.'.ResourceId=0'); $event->redirect_params = Array('opener' => 's', 'pass_events'=>true); //do not go up - STAY } @@ -25,7 +25,7 @@ $this->Application->StoreVar($event->getPrefixSpecial().'_clipboard', implode(',', $ids)); $this->Application->StoreVar($event->getPrefixSpecial().'_clipboard_mode', 'cut'); - $this->Application->StoreVar('clipboard', 'CUT-0.'.$object->TableName.'.ResourceId=0'); + $this->Application->StoreVar('ClipBoard', 'CUT-0.'.$object->TableName.'.ResourceId=0'); $event->redirect_params = Array('opener' => 's', 'pass_events'=>true); //do not go up - STAY } @@ -39,7 +39,7 @@ //recalling by different name, because we may get kDBList, if we recall just by prefix $object =& $this->Application->recallObject($event->getPrefixSpecial().'.item', $event->Prefix); - $this->prepareObject($object,$event); + $this->prepareObject($object, $event); if ($this->Application->RecallVar($event->getPrefixSpecial().'_clipboard_mode') == 'copy') { $ids_arr = explode(',', $ids); @@ -68,7 +68,7 @@ */ function OnCatPaste(&$event) { - $inp_clipboard = $this->Application->RecallVar('clipboard'); + $inp_clipboard = $this->Application->RecallVar('ClipBoard'); $inp_clipboard = explode('-', $inp_clipboard, 2); if($inp_clipboard[0] == 'COPY') @@ -79,7 +79,7 @@ $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField'); $table = $this->Application->getUnitOption($event->Prefix, 'TableName'); $ids_sql = 'SELECT '.$id_field.' FROM '.$table.' WHERE ResourceId IN (%s)'; - $resource_ids_sql = 'SELECT ItemResourceId FROM '.TABLE_PREFIX.'CategoryItems WHERE CategoryId = %s'; + $resource_ids_sql = 'SELECT ItemResourceId FROM '.TABLE_PREFIX.'CategoryItems WHERE CategoryId = %s AND PrimaryCat = 1'; $this->Application->setUnitOption($event->Prefix,'AutoLoad', false); 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 Index: trunk/core/units/category_items/category_items_event_handler.php =================================================================== diff -u -r1566 -r2041 --- trunk/core/units/category_items/category_items_event_handler.php (.../category_items_event_handler.php) (revision 1566) +++ trunk/core/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 Index: trunk/core/units/general/cat_event_handler.php =================================================================== diff -u -r2004 -r2041 --- trunk/core/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 2004) +++ trunk/core/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 2041) @@ -13,7 +13,7 @@ $this->Application->StoreVar($event->getPrefixSpecial().'_clipboard', implode(',', $ids)); $this->Application->StoreVar($event->getPrefixSpecial().'_clipboard_mode', 'copy'); - $this->Application->StoreVar('clipboard', 'COPY-0.'.$object->TableName.'.ResourceId=0'); + $this->Application->StoreVar('ClipBoard', 'COPY-0.'.$object->TableName.'.ResourceId=0'); $event->redirect_params = Array('opener' => 's', 'pass_events'=>true); //do not go up - STAY } @@ -25,7 +25,7 @@ $this->Application->StoreVar($event->getPrefixSpecial().'_clipboard', implode(',', $ids)); $this->Application->StoreVar($event->getPrefixSpecial().'_clipboard_mode', 'cut'); - $this->Application->StoreVar('clipboard', 'CUT-0.'.$object->TableName.'.ResourceId=0'); + $this->Application->StoreVar('ClipBoard', 'CUT-0.'.$object->TableName.'.ResourceId=0'); $event->redirect_params = Array('opener' => 's', 'pass_events'=>true); //do not go up - STAY } @@ -39,7 +39,7 @@ //recalling by different name, because we may get kDBList, if we recall just by prefix $object =& $this->Application->recallObject($event->getPrefixSpecial().'.item', $event->Prefix); - $this->prepareObject($object,$event); + $this->prepareObject($object, $event); if ($this->Application->RecallVar($event->getPrefixSpecial().'_clipboard_mode') == 'copy') { $ids_arr = explode(',', $ids); @@ -68,7 +68,7 @@ */ function OnCatPaste(&$event) { - $inp_clipboard = $this->Application->RecallVar('clipboard'); + $inp_clipboard = $this->Application->RecallVar('ClipBoard'); $inp_clipboard = explode('-', $inp_clipboard, 2); if($inp_clipboard[0] == 'COPY') @@ -79,7 +79,7 @@ $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField'); $table = $this->Application->getUnitOption($event->Prefix, 'TableName'); $ids_sql = 'SELECT '.$id_field.' FROM '.$table.' WHERE ResourceId IN (%s)'; - $resource_ids_sql = 'SELECT ItemResourceId FROM '.TABLE_PREFIX.'CategoryItems WHERE CategoryId = %s'; + $resource_ids_sql = 'SELECT ItemResourceId FROM '.TABLE_PREFIX.'CategoryItems WHERE CategoryId = %s AND PrimaryCat = 1'; $this->Application->setUnitOption($event->Prefix,'AutoLoad', false); Index: trunk/core/units/general/cat_dbitem.php =================================================================== diff -u -r1566 -r2041 --- trunk/core/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 1566) +++ trunk/core/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 Index: trunk/core/units/category_items/category_items_config.php =================================================================== diff -u -r1566 -r2041 --- trunk/core/units/category_items/category_items_config.php (.../category_items_config.php) (revision 1566) +++ trunk/core/units/category_items/category_items_config.php (.../category_items_config.php) (revision 2041) @@ -17,9 +17,9 @@ 'TableName' => TABLE_PREFIX.'CategoryItems', 'ParentTableKey'=> 'ResourceId', 'ForeignKey' => 'ItemResourceId', - 'ParentPrefix' => 'p', + 'ParentPrefix' => 'p', 'AutoDelete' => true, - 'AutoClone' => false, // Critical !!! + 'AutoClone' => false, 'CalculatedFields' => Array( '' => Array ( Index: trunk/core/kernel/event_handler.php =================================================================== diff -u -r1560 -r2041 --- trunk/core/kernel/event_handler.php (.../event_handler.php) (revision 1560) +++ trunk/core/kernel/event_handler.php (.../event_handler.php) (revision 2041) @@ -108,7 +108,7 @@ * @param kEvent $event * @access protected */ - function prepareObject(&$object,&$event) + function prepareObject(&$object, &$event) { // processing here } 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 Index: trunk/core/kernel/db/db_event_handler.php =================================================================== diff -u -r2011 -r2041 --- trunk/core/kernel/db/db_event_handler.php (.../db_event_handler.php) (revision 2011) +++ trunk/core/kernel/db/db_event_handler.php (.../db_event_handler.php) (revision 2041) @@ -203,7 +203,7 @@ function dbBuild(&$object,&$event) { $object->Configure(); - $this->PrepareObject($event); + $this->PrepareObject($object, $event); $live_table = $event->getEventParam('live_table'); @@ -223,17 +223,6 @@ } /** - * Apply custom object modifications - * after it's initialization - * - * @param kEvent $event - */ - function PrepareObject(&$event) - { - - } - - /** * Builds item (loads if needed) * * @param kEvent $event Index: trunk/kernel/units/category_items/category_items_config.php =================================================================== diff -u -r1566 -r2041 --- trunk/kernel/units/category_items/category_items_config.php (.../category_items_config.php) (revision 1566) +++ trunk/kernel/units/category_items/category_items_config.php (.../category_items_config.php) (revision 2041) @@ -17,9 +17,9 @@ 'TableName' => TABLE_PREFIX.'CategoryItems', 'ParentTableKey'=> 'ResourceId', 'ForeignKey' => 'ItemResourceId', - 'ParentPrefix' => 'p', + 'ParentPrefix' => 'p', 'AutoDelete' => true, - 'AutoClone' => false, // Critical !!! + 'AutoClone' => false, 'CalculatedFields' => Array( '' => Array (