--
-- wp_presentation__new/12
--
create or replace function wp_presentation__new(
  timestamp with time zone,
  integer,
  character varying,
  character varying,
  character varying,
  character varying,
  integer,
  boolean,
  boolean,
  character varying,
  character varying,
  integer
) returns int4 as $$

declare
    p_creation_date             alias for $1;
    p_creation_user             alias for $2;
    p_creation_ip               alias for $3;
    p_pres_title                alias for $4;
    p_page_signature            alias for $5;
    p_copyright_notice          alias for $6;
    p_style                     alias for $7;
    p_public_p                  alias for $8;
    p_show_modified_p           alias for $9;
    p_aud                       alias for $10;
    p_back                      alias for $11;
    p_parent_id                 alias for $12;
    v_item_id                   cr_items.item_id%TYPE;
    v_audience_item_id          cr_items.item_id%TYPE;
    v_background_item_id        cr_items.item_id%TYPE;
    v_revision_id               cr_revisions.revision_id%TYPE;
    v_audience_revision_id      cr_revisions.revision_id%TYPE;
    v_background_revision_id    cr_revisions.revision_id%TYPE;
    v_max_id                    integer;
    v_name                      cr_wp_presentations.pres_title%TYPE;
begin 
    select coalesce(max(item_id),0) into v_max_id
    from cr_items
    where content_type = 'cr_wp_presentation'
    and   name like p_pres_title || '%';
    
    v_name := p_pres_title || '_' || v_max_id;

    v_item_id := content_item__new( 
        v_name,
        p_parent_id,
        null,
        null,
        p_creation_date,
        p_creation_user,
        null,
        p_creation_ip,
        'content_item',
        'cr_wp_presentation',
         null,
         null,
         'text/plain',
         null,
         null,
         'text'
    );

    v_revision_id := content_revision__new(
        null,
        null,
        current_timestamp,
        'text/plain',
        null,
        null,
        v_item_id,
        null,
        p_creation_date,
        p_creation_user,
        p_creation_ip
    );
    
    perform content_item__set_live_revision(v_revision_id);

    --jackp: Actually place the information entered 
    --       by the user into the table

    insert into cr_wp_presentations (
        presentation_id,
        pres_title,
        page_signature,
        copyright_notice,
        style,
        public_p,
        show_modified_p
    ) values (
        v_revision_id,
        p_pres_title,
        p_page_signature,
        p_copyright_notice,
        p_style,
        p_public_p,
        p_show_modified_p
    );
   
    v_audience_item_id := content_item__new(    
        p_aud,
        v_item_id,
        null,
        null,
        p_creation_date,
        p_creation_user,
        null,
        p_creation_ip,
        'content_item',
        'cr_wp_presentation_aud',
         null,
         null,
         'text/plain',
         null,
         null,
         'text'
    );

    v_audience_revision_id := content_revision__new(
        null,
        null,
        current_timestamp,
        'text/plain',
        null,
        p_aud,
        v_audience_item_id,
        null,
        p_creation_date,
        p_creation_user,
        p_creation_ip
     );
    
    perform content_item__set_live_revision(v_audience_revision_id);
      
    insert into cr_wp_presentations_aud 
    (id, presentation_id) 
    values 
    (v_audience_revision_id, v_revision_id);
    
    v_background_item_id := content_item__new(
        p_back,
        v_item_id,
        null,
        null,
        p_creation_date,
        p_creation_user,
        null,
        p_creation_ip,
        'content_item',
        'cr_wp_presentation_back',
         null,
         null,
         'text/plain',
         null,
         null,
         'text'
    );
    
    v_background_revision_id := content_revision__new(
        null,
        null,
        current_timestamp,
        'text/plain',
        null,
        p_back,
        v_background_item_id,
        null,
        p_creation_date,
        p_creation_user,
        p_creation_ip
    );
    
    perform content_item__set_live_revision(v_background_revision_id);
     
    insert into cr_wp_presentations_back
    (id, presentation_id) 
    values 
    (v_background_revision_id, v_revision_id);
    
    return v_item_id;
end;$$ language plpgsql;