Index: openacs-4/packages/categories/categories.info =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/categories/categories.info,v diff -u -N -r1.21 -r1.21.2.1 --- openacs-4/packages/categories/categories.info 31 Jan 2018 08:23:12 -0000 1.21 +++ openacs-4/packages/categories/categories.info 16 Apr 2019 13:52:12 -0000 1.21.2.1 @@ -10,7 +10,7 @@ t categories - + timo@timohentschel.de Manage categories in category trees and let users map objects to categories. 2017-08-06 @@ -19,7 +19,7 @@ GPL 1 - + Index: openacs-4/packages/categories/sql/postgresql/category-package.sql =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/categories/sql/postgresql/category-package.sql,v diff -u -N -r1.9 -r1.9.2.1 --- openacs-4/packages/categories/sql/postgresql/category-package.sql 7 Aug 2017 23:48:05 -0000 1.9 +++ openacs-4/packages/categories/sql/postgresql/category-package.sql 16 Apr 2019 13:52:12 -0000 1.9.2.1 @@ -314,7 +314,7 @@ and left_ind > v_old_left_ind; if (p_parent_id is null) then - select 1, max(right_ind)+1 into v_new_left_ind, v_new_right_ind + select 1, coalesce(max(right_ind)+1, 1) into v_new_left_ind, v_new_right_ind from categories where tree_id = p_tree_id; else Index: openacs-4/packages/categories/sql/postgresql/upgrade/upgrade-1.1.4-1.1.5d1.sql =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/categories/sql/postgresql/upgrade/Attic/upgrade-1.1.4-1.1.5d1.sql,v diff -u -N --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openacs-4/packages/categories/sql/postgresql/upgrade/upgrade-1.1.4-1.1.5d1.sql 16 Apr 2019 13:52:12 -0000 1.1.2.1 @@ -0,0 +1,89 @@ + +-- +-- procedure category__change_parent/3 +-- +CREATE OR REPLACE FUNCTION category__change_parent( + p_category_id integer, + p_tree_id integer, + p_parent_id integer +) RETURNS integer AS $$ +DECLARE + + v_old_left_ind integer; + v_old_right_ind integer; + v_new_left_ind integer; + v_new_right_ind integer; + v_width integer; +BEGIN + update categories + set parent_id = p_parent_id + where category_id = p_category_id; + + -- first save the subtree, then compact tree, then expand tree to make room + -- for subtree, then insert it + + select left_ind, right_ind into v_old_left_ind, v_old_right_ind + from categories + where category_id = p_category_id; + + v_width := v_old_right_ind - v_old_left_ind + 1; + + -- cut out old subtree + update categories + set left_ind = -left_ind, right_ind = -right_ind + where tree_id = p_tree_id + and left_ind >= v_old_left_ind + and right_ind <= v_old_right_ind; + + -- compact parent trees + update categories + set right_ind = right_ind - v_width + where tree_id = p_tree_id + and left_ind < v_old_left_ind + and right_ind > v_old_right_ind; + + -- compact right tree portion + update categories + set left_ind = left_ind - v_width, + right_ind = right_ind - v_width + where tree_id = p_tree_id + and left_ind > v_old_left_ind; + + if (p_parent_id is null) then + select 1, coalesce(max(right_ind)+1, 1) into v_new_left_ind, v_new_right_ind + from categories + where tree_id = p_tree_id; + else + select left_ind, right_ind into v_new_left_ind, v_new_right_ind + from categories + where category_id = p_parent_id; + end if; + + -- move parent trees to make room + update categories + set right_ind = right_ind + v_width + where tree_id = p_tree_id + and left_ind <= v_new_left_ind + and right_ind >= v_new_right_ind; + + -- move right tree portion to make room + update categories + set left_ind = left_ind + v_width, + right_ind = right_ind + v_width + where tree_id = p_tree_id + and left_ind > v_new_right_ind; + + -- insert subtree at correct place + update categories + set left_ind = -left_ind + (v_new_right_ind - v_old_left_ind), + right_ind = -right_ind + (v_new_right_ind - v_old_left_ind) + where tree_id = p_tree_id + and left_ind < 0; + + -- for debugging reasons + perform category_tree__check_nested_ind(p_tree_id); + + return 0; +END; + +$$ LANGUAGE plpgsql;