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

declare 
    p_style_id          alias for $1;
    v_item_id           integer;
    one_image           record;
begin
    for one_image in 
        select * from wp_style_images where wp_style_images_id = 
          (select background_image from wp_styles where style_id = p_style_id)
    loop
        delete from wp_style_images 
        where wp_style_images_id = one_image.wp_style_images_id;

        select item_id into v_item_id 
        from cr_revisions 
        where revision_id = one_image.wp_style_images_id;

        perform content_item__delete(v_item_id);
    end loop;
    
    update cr_wp_slides set style = -1 where style = p_style_id;
    update cr_wp_presentations set style = -1 where style = p_style_id;
    delete from wp_styles where style_id = p_style_id;

    return 0;
end;$$ language plpgsql;