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

declare
    p_pres_item_id                  alias for $1;
    v_audience_item_id              cr_items.item_id%TYPE;
    v_background_item_id            cr_items.item_id%TYPE;
    del_rec                         record;
begin
    for del_rec in 
        select item_id as slide_item_id
          from cr_items
          where content_type = 'cr_wp_slide'
          and   parent_id = p_pres_item_id 
    loop 
       perform wp_slide__delete(del_rec.slide_item_id);
    end loop;
    
    select item_id into v_audience_item_id
      from cr_items
      where content_type = 'cr_wp_presentation_aud'
      and   parent_id = p_pres_item_id;
    
    perform wp_presentation__delete_audience(v_audience_item_id);
    
    select item_id into v_background_item_id
      from cr_items
      where content_type = 'cr_wp_presentation_back'
      and   parent_id = p_pres_item_id;
    
    perform wp_presentation__delete_background(v_background_item_id);
    
    delete from acs_permissions where object_id = p_pres_item_id;

   -- update acs_objects set context_id=null where context_id = p_pres_item_id;

    delete from cr_wp_presentations where exists 
        (select 1 from cr_revisions 
         where cr_revisions.revision_id = cr_wp_presentations.presentation_id 
         and cr_revisions.item_id = p_pres_item_id);

    perform content_item__delete(p_pres_item_id);

    return 0;
end;$$ language plpgsql;