declare
v_rec record;
begin
for v_rec in
-- We want to delete all cr_items entries, starting from the leaves all
-- the way up the root folder (old.folder_id).
select c1.item_id, c1.content_type
from cr_items c1, cr_items c2
where c2.item_id = old.folder_id
and c1.tree_sortkey between c2.tree_sortkey and tree_right(c2.tree_sortkey)
and c1.item_id <> old.folder_id
order by c1.tree_sortkey desc
loop
-- We delete the item. On delete cascade should take care
-- of deletion of revisions.
if v_rec.content_type = 'file_storage_object'
then
raise notice 'Deleting item_id = %',v_rec.item_id;
PERFORM content_item__delete(v_rec.item_id);
end if;
-- Instead of doing an if-else, we make sure we are deleting a folder.
if v_rec.content_type = 'content_folder'
then
raise notice 'Deleting folder_id = %',v_rec.item_id;
PERFORM content_folder__delete(v_rec.item_id);
end if;
-- We may have to delete other items here, e.g., symlinks (future feature)
end loop;
-- We need to return something for the trigger to be activated
return old;
end;