--
-- acs_event__shift/3
--
create or replace function acs_event__shift(
  integer,
  integer,
  integer
) returns int4 as $$

declare
       shift__event_id        alias for $1; -- default null,
       shift__start_offset    alias for $2; -- default 0,
       shift__end_offset      alias for $3; -- default 0
begin
	
       return acs_event__shift (
	            shift__event_id,
	            to_interval(shift__start_offset,'days'),
	            to_interval(shift__end_offset,'days')
		    );
				    
end;$$ language plpgsql;


--
-- acs_event__shift/3
--
create or replace function acs_event__shift(
  shift__event_id integer,
  shift__start_offset_interval interval,
  shift__end_offset_interval interval
) returns int4 as $$

DECLARE
       rec_events           record;
BEGIN

--       update acs_events_dates
--       set    start_date = start_date + shift__start_offset_interval,
--             end_date   = end_date + shift__end_offset_interval
--       where  event_id   = shift__event_id;

          -- Can not update view, so we do it the hard way
          -- (as if we make the rule anyways)
          for rec_events in
              select t.*
              from acs_events e, timespans s, time_intervals t
              where e.event_id   = shift__event_id
              and   e.timespan_id = s.timespan_id
              and   s.interval_id = t.interval_id
          loop
               update time_intervals
               set    start_date = start_date + shift__start_offset_interval,
                      end_date   = end_date + shift__end_offset_interval
               where  interval_id = rec_events.interval_id;
          end loop;

       return 0;

END;
$$ language plpgsql;