--
-- content_item__del/1
--
create or replace function content_item__del(
  delete__item_id integer
) returns int4 as $$

BEGIN

  -- Also child relationships must be deleted. On delete cascade
  -- would not help here, as related acs_object would stay.
  PERFORM acs_object__delete(object_id)
    from acs_objects where object_id in 
    (select rel_id from cr_child_rels where
         child_id  = delete__item_id or
         parent_id = delete__item_id);

  --
  -- Delete all revisions of this item
  --
  -- On delete cascade should work for us, but not in case of
  -- relationships. Therefore, we call acs_object__delete explicitly
  -- on the revisions. Is is also safer in general, as referential
  -- integrity might not have been enforced everytime.
  --
  PERFORM acs_object__delete(revision_id)
    from cr_revisions where item_id = delete__item_id;

  --
  -- Delete all children of this item via a recursive call.
  --
  -- On delete cascade should work for us, but not in case of
  -- relationships. Therefore, we call acs_object__delete explicitly
  -- on the revisions. Is is also safer in general, as referential
  -- integrity might not have been enforced everytime.
  --
  PERFORM content_item__delete(item_id)
    from cr_items where parent_id = delete__item_id;

  --
  -- Finally, delete the acs_object of the item.
  --      
  PERFORM acs_object__delete(delete__item_id);

  return 0; 
END;
$$ language plpgsql;