Index: openacs-4/packages/calendar/tcl/cal-item-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/calendar/tcl/cal-item-procs.tcl,v diff -u -r1.16 -r1.17 --- openacs-4/packages/calendar/tcl/cal-item-procs.tcl 11 Dec 2003 21:39:59 -0000 1.16 +++ openacs-4/packages/calendar/tcl/cal-item-procs.tcl 9 Jan 2004 22:55:52 -0000 1.17 @@ -4,251 +4,246 @@ Utility functions for Calendar Applications + @author Dirk Gomez (openacs@dirkgomez.de) @author Gary Jin (gjin@arsdigita.com) + @author Ben Adida (ben@openforce.net) @creation-date Jan 11, 2001 @cvs-id $Id$ } +namespace eval calendar {} +namespace eval calendar::item {} -#------------------------------------------------ -# update the permissions of the calendar -ad_proc cal_assign_item_permission { cal_item_id - party_id - permission - {revoke ""} +ad_proc -private calendar::item::dates_valid_p { + {-start_date:required} + {-end_date:required} } { - update the permission of the specific cal_item - if revoke is set to revoke, then we revoke all permissions + A sanity check that the start time is before the end time. } { - - # adding permission + set dates_valid_p [db_string dates_valid_p_select {}] - if { ![string equal $revoke "revoke"] } { + if {[string equal $dates_valid_p 1]} { + return 1 + } else { + return 0 + } +} - # we make the assumation that permission cal_read is - # by default granted to all users who needs write, delete - # and invite permission +ad_proc -public calendar::item::new { + {-start_date:required} + {-end_date:required} + {-name:required} + {-description:required} + {-calendar_id:required} + {-item_type_id ""} +} { + if {[dates_valid_p -start_date $start_date -end_date $end_date]} { + set creation_ip [ad_conn peeraddr] + set creation_user [ad_conn user_id] - if { ![string equal $permission "cal_item_read"] } { + set activity_id [db_exec_plsql insert_activity {} ] + + # Convert from user timezone to system timezone + set start_date [lc_time_conn_to_system $start_date] + set end_date [lc_time_conn_to_system $end_date] + + set timespan_id [db_exec_plsql insert_timespan {}] + + # create the cal_item + # we are leaving the name and description fields in acs_event + # blank to abide by the definition that an acs_event is an acs_activity + # with added on temperoal information + + # by default, the cal_item permissions + # are going to be inherited from the calendar permissions + set cal_item_id [db_exec_plsql cal_item_add {}] - # grant read permission first - permission::grant -object_id $cal_item_id -party_id $party_id -privilege cal_item_read - - } - - # grant other permission + assign_permission $cal_item_id $creation_user read + assign_permission $cal_item_id $creation_user write + assign_permission $cal_item_id $creation_user delete + assign_permission $cal_item_id $creation_user admin - permission::grant -object_id $cal_item_id -party_id $party_id -privilege $permission + return $cal_item_id - - } elseif { [string equal $revoke "revoke"] } { - - # revoke the permissions - - permission::revoke -object_id $cal_item_id -party_id $party_id -privilege $permission - + } else { + ad_return_complaint 1 [_ calendar.start_time_before_end_time] + ad_script_abort } } -#------------------------------------------------ -# adding a new calendar item -ad_proc cal_item_create { start_date - end_date - name - description - on_which_calendar - creation_ip - creation_user -{item_type_id ""} +ad_proc -public calendar::item::get { + {-cal_item_id:required} + {-array:required} } { - - create a new cal_item - for this version, i am omitting recurrence - + Get the data for a calendar item } { + upvar $array row - # find out the activity_id - set activity_id [db_exec_plsql insert_activity { - begin - :1 := acs_activity.new ( - name => :name, - description => :description, - creation_user => :creation_user, - creation_ip => :creation_ip - ); - end; + if {[calendar::attachments_enabled_p]} { + set query_name select_item_data_with_attachment + } else { + set query_name select_item_data } - ] + + db_1row $query_name {} -column_array row + # Timezonize + set row(start_date_ansi) [lc_time_system_to_conn $row(start_date_ansi)] + set row(end_date_ansi) [lc_time_system_to_conn $row(end_date_ansi)] - # set the date_format - set date_format "YYYY-MM-DD HH24:MI" - - # Convert from user timezone to system timezone - set start_date [lc_time_conn_to_system $start_date] - set end_date [lc_time_conn_to_system $end_date] - - # find out the timespan_id - set timespan_id [db_exec_plsql insert_timespan { - begin - :1 := timespan.new( - start_date => to_date(:start_date,:date_format), - end_date => to_date(:end_date,:date_format) - ); - end; + if { $row(start_date_ansi) == $row(end_date_ansi) && [string equal [lc_time_fmt $row(start_date_ansi) "%T"] "00:00:00"]} { + set row(time_p) 0 + } else { + set row(time_p) 1 } - ] - # create the cal_item - # we are leaving the name and description fields in acs_event - # blank to abide by the definition that an acs_event is an acs_activity - # with added on temperoal information + # Localize + set row(start_time) [lc_time_fmt $row(start_date_ansi) "%X"] - # by default, the cal_item permissions - # are going to be inherited from the calendar permissions + # Unfortunately, SQL has weekday starting at 1 = Sunday + set row(start_date) [lc_time_fmt $row(start_date_ansi) "%Y-%m-%d"] + set row(end_date) [lc_time_fmt $row(end_date_ansi) "%Y-%m-%d"] - set cal_item_id [db_exec_plsql cal_item_add { - begin - :1 := cal_item.new( - on_which_calendar => :on_which_calendar, - activity_id => :activity_id, - timespan_id => :timespan_id, - item_type_id => :item_type_id, - creation_user => :creation_user, - creation_ip => :creation_ip, - context_id => :on_which_calendar - ); - end; - } - ] + set row(day_of_week) [expr [lc_time_fmt $row(start_date_ansi) "%w"] + 1] + set row(pretty_day_of_week) [lc_time_fmt $row(start_date_ansi) "%A"] + set row(day_of_month) [lc_time_fmt $row(start_date_ansi) "%d"] + set row(pretty_short_start_date) [lc_time_fmt $row(start_date_ansi) "%x"] + set row(full_start_date) [lc_time_fmt $row(start_date_ansi) "%x"] + set row(full_end_date) [lc_time_fmt $row(end_date_ansi) "%x"] - cal_assign_item_permission $cal_item_id $creation_user read - cal_assign_item_permission $cal_item_id $creation_user write - cal_assign_item_permission $cal_item_id $creation_user delete - cal_assign_item_permission $cal_item_id $creation_user admin + set row(end_time) [lc_time_fmt $row(end_date_ansi) "%X"] +} - return $cal_item_id - +ad_proc -public calendar::item::add_recurrence { + {-cal_item_id:required} + {-interval_type:required} + {-every_n:required} + {-days_of_week ""} + {-recur_until ""} +} { + Adds a recurrence for a calendar item +} { + db_transaction { + set recurrence_id [db_exec_plsql create_recurrence {}] + + db_dml update_event {} + + db_exec_plsql insert_instances {} + + # Make sure they're all in the calendar! + db_dml insert_cal_items {} + } } -#------------------------------------------------ -# update an existing calendar item -ad_proc cal_item_update { - cal_item_id - start_date - end_date - name - description - {item_type_id ""} - {edit_all_p 0} - {calendar_id ""} +ad_proc -public calendar::item::edit { + {-cal_item_id:required} + {-start_date:required} + {-end_date:required} + {-name:required} + {-description:required} + {-item_type_id ""} + {-edit_all_p 0} + {-calendar_id ""} } { - Updating a cal_item + Edit the item + } { - - if {$edit_all_p} { - set recurrence_id [db_string select_recurrence_id {}] + if {[dates_valid_p -start_date $start_date -end_date $end_date]} { + if {$edit_all_p} { + set recurrence_id [db_string select_recurrence_id {}] - # If the recurrence id is NULL, then we stop here and just do the normal update - if {![empty_string_p $recurrence_id]} { - cal_item_edit_recurrence \ - -event_id $cal_item_id \ - -start_date $start_date \ - -end_date $end_date \ - -name $name \ - -description $description \ - -item_type_id $item_type_id \ - -calendar_id $calendar_id + # If the recurrence id is NULL, then we stop here and just do the normal update + if {![empty_string_p $recurrence_id]} { + calendar::item::edit_recurrence \ + -event_id $cal_item_id \ + -start_date $start_date \ + -end_date $end_date \ + -name $name \ + -description $description \ + -item_type_id $item_type_id \ + -calendar_id $calendar_id - return + return + } } - } - # set the date_format - set date_format "YYYY-MM-DD HH24:MI" + # Convert from user timezone to system timezone + set start_date [lc_time_conn_to_system $start_date] + set end_date [lc_time_conn_to_system $end_date] - # Convert from user timezone to system timezone - set start_date [lc_time_conn_to_system $start_date] - set end_date [lc_time_conn_to_system $end_date] + db_dml update_event {} - # update the events - db_dml update_event "" + # update the time interval based on the timespan id - # update the time interval based on the timespan id + db_1row get_interval_id {} - db_1row get_interval_id "" + db_transaction { + # call edit procedure + db_exec_plsql update_interval {} + + # Update the item_type_id and calendar_id + set colspecs [list] + lappend colspecs "item_type_id = :item_type_id" + if { ![empty_string_p $calendar_id] } { + lappend colspecs "on_which_calendar = :calendar_id" - db_transaction { - # call edit procedure - db_exec_plsql update_interval " - begin - time_interval.edit ( - interval_id => :interval_id, - start_date => to_date(:start_date,:date_format), - end_date => to_date(:end_date,:date_format) - ); - end; - " - - # Update the item_type_id and calendar_id - set colspecs [list] - lappend colspecs "item_type_id = :item_type_id" - if { ![empty_string_p $calendar_id] } { - lappend colspecs "on_which_calendar = :calendar_id" - - db_dml update_context_id { - update acs_objects - set context_id = :calendar_id - where object_id = :cal_item_id + db_dml update_context_id { + update acs_objects + set context_id = :calendar_id + where object_id = :cal_item_id + } } - } - - db_dml update_item_type_id " + + db_dml update_item_type_id " update cal_items set [join $colspecs ", "] where cal_item_id= :cal_item_id " - } + } + } else { + ad_return_complaint 1 "Start Time must be before End Time" + ad_script_abort + } } +ad_proc -public calendar::item::delete { + {-cal_item_id:required} +} { + Delete the calendar item +} { + db_exec_plsql delete_cal_item {} +} -#------------------------------------------------ -# delete an exiting cal_item -ad_proc cal_item_delete { cal_item_id } { - - delete an existing cal_item given a cal_item_id - +ad_proc calendar::item::assign_permission { cal_item_id + party_id + permission + {revoke ""} } { + update the permission of the specific cal_item + if revoke is set to revoke, then we revoke all permissions +} { + if { ![string equal $revoke "revoke"] } { + if { ![string equal $permission "cal_item_read"] } { + permission::grant -object_id $cal_item_id -party_id $party_id -privilege cal_item_read + } + permission::grant -object_id $cal_item_id -party_id $party_id -privilege $permission + } elseif { [string equal $revoke "revoke"] } { + permission::revoke -object_id $cal_item_id -party_id $party_id -privilege $permission - # call delete procedure - db_exec_plsql delete_cal_item " - begin - cal_item.delete ( - cal_item_id => :cal_item_id - ); - end; - " + } } - -# Recurrences -ad_proc -public cal_item_delete_recurrence { +ad_proc -public calendar::item::delete_recurrence { {-recurrence_id:required} } { - - # call delete procedure - db_exec_plsql delete_cal_item_recurrence " - begin - cal_item.delete_all ( - recurrence_id => :recurrence_id - ); - end; - " + delete a recurrence +} { + db_exec_plsql delete_cal_item_recurrence {} } -ad_proc -public cal_item_edit_recurrence { +ad_proc -public calendar::item::edit_recurrence { {-event_id:required} {-start_date:required} {-end_date:required} @@ -262,14 +257,8 @@ set recurrence_id [db_string select_recurrence_id {}] db_transaction { - # Update the recurrence start and end dates db_exec_plsql recurrence_timespan_update {} - # Update the activities table - # We shouldn't update activities, I don't think - # db_dml recurrence_activities_update {} - - # Update the events table db_dml recurrence_events_update {} set colspecs [list] @@ -299,30 +288,15 @@ } { # We do things in a transaction db_transaction { - # Create the recurrence - set recurrence_id [db_exec_plsql create_recurrence " - begin - :1 := recurrence.new(interval_type => :interval_type, - every_nth_interval => :every_n, - days_of_week => :days_of_week, - recur_until => :recur_until); - end; - "] + set recurrence_id [db_exec_plsql create_recurrence {}] - # Update the events table db_dml update_event "update acs_events set recurrence_id= :recurrence_id where event_id= :cal_item_id" - # Insert instances - db_exec_plsql insert_instances " - begin - acs_event.insert_instances(event_id => :cal_item_id); - end; - " + db_exec_plsql insert_instances {} # Make sure they're all in the calendar! db_dml insert_cal_items " insert into cal_items (cal_item_id, on_which_calendar) select event_id, (select on_which_calendar as calendar_id from cal_items where cal_item_id = :cal_item_id) from acs_events where recurrence_id= :recurrence_id and event_id <> :cal_item_id" } } -