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

        declare
                p_static_page_id        alias for $1;
                v_comment_row           RECORD;
		v_rec_affected		integer;
		v_static_page_id	integer;
        begin
                -- Delete all permissions on this page:

	delete from acs_permissions where object_id = p_static_page_id;

                -- Drop all comments on this page.  general-comments doesn't have
                -- a comment.delete() function, so I just do this (see the
                -- general-comments drop script):

		for v_comment_row in 
                        select comment_id from general_comments
                        where object_id = p_static_page_id
                 loop
                        delete from images
                        where image_id in (
                                select latest_revision
                                from cr_items
                                where parent_id = v_comment_row.comment_id
                       );

                        PERFORM acs_message__delete(v_comment_row.comment_id);
                end loop;

                -- Delete the page.
                -- WE SHOULDN'T NEED TO DO THIS: CONTENT_ITEM.DELETE SHOULD TAKE CARE OF
                -- DELETING FROM STATIC PAGES.

	delete from static_pages where static_page_id = p_static_page_id;

	GET DIAGNOSTICS v_rec_affected = ROW_COUNT;
	RAISE NOTICE '*** Number of rows deleted: %',v_rec_affected;
	select into v_static_page_id static_page_id from static_pages where static_page_id = p_static_page_id;
	GET DIAGNOSTICS v_rec_affected = ROW_COUNT;
	RAISE NOTICE '*** selected % rows still in static_pages',v_rec_affected;


	PERFORM content_item__delete(p_static_page_id);
return 0;
end;$$ language plpgsql;