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.20.2.1 -r1.20.2.2
--- openacs-4/packages/calendar/tcl/cal-item-procs.tcl	19 Jun 2005 07:49:35 -0000	1.20.2.1
+++ openacs-4/packages/calendar/tcl/cal-item-procs.tcl	23 Jun 2005 02:49:44 -0000	1.20.2.2
@@ -58,7 +58,7 @@
         # 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 {}]
-	db_dml set_item_type_id "update cal_items set item_type_id=:item_type_id where cal_item_id=:cal_item_id"
+
         assign_permission  $cal_item_id  $creation_user read
         assign_permission  $cal_item_id  $creation_user write
         assign_permission  $cal_item_id  $creation_user delete
Index: openacs-4/packages/calendar/tcl/calendar-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/calendar/tcl/calendar-procs.tcl,v
diff -u -r1.25.2.2 -r1.25.2.3
--- openacs-4/packages/calendar/tcl/calendar-procs.tcl	19 Jun 2005 07:49:35 -0000	1.25.2.2
+++ openacs-4/packages/calendar/tcl/calendar-procs.tcl	23 Jun 2005 02:51:47 -0000	1.25.2.3
@@ -15,7 +15,84 @@
 namespace eval calendar {}
 namespace eval calendar::notification {}
 
+namespace eval calendar::merge {
+    ad_proc -callback MergeShowUserInfo -impl calendar {
+	-user_id:required
+    } {
+	Shows the calendar tables user info
+    } {
+	set msg "Calendars of user_id: $user_id"
+	set return_msg [list $msg]
+	
+	set cals [db_list get_calendars {*SQL*} ]
+	if { [empty_string_p $cals ] } {
+	    lappend return_msg "none"
+	} else {
+	    lappend return_msg $cals
+	}
+	
+	return $return_msg
+    }
 
+    ad_proc -callback MergePackageUser -impl calendar {
+	-from_user_id:required
+	-to_user_id:required
+    } {
+	Merge the calendars of two users.
+	The from_user_id is the user_id of the user
+	that will be deleted and all the calendar elements
+	of this user will be mapped to the to_user_id.
+    } {
+	set msg "Merging calendar"
+	ns_log Notice $msg
+	set return_msg [list $msg]
+
+	set from_calendars [db_list_of_lists get_from_calendars {*SQL*} ]
+	db_transaction {
+	    ns_log Notice "  Entering to calendar transaction"
+	    foreach calendar $from_calendars {
+		# l_* vars will represent
+		# each item of the from_user_id list of lists
+		set l_cal_id [lindex $calendar 0]
+		set l_pkg_id [lindex $calendar 1]
+
+		# if the pkg_id of this cal_id is the
+		# the same for some to_user_id cal
+		# we have to delete it, else we must 
+		# change the items from one cal to the other one
+		if { [db_string get_repeated_pkgs {*SQL*} ] } {
+		    # We will move the cal items if the 
+		    # calendars are of the same type (package_id)
+		    set to_cal_id [db_string gettocalid {*SQL*} ]
+		    
+		    db_dml calendar_items_upd { *SQL* }
+		    
+		    # ns_log Notice "  Deleting calendar"
+		    # TODO: calendar::delete -calendar_id $l_cal_i is broken
+		    # so, we will delete directly from the calendars table
+		    db_dml del_from_cal { *SQL* }
+		    
+		} else {
+		    ns_log Notice "  Change owner of $calendar"
+		    # change the owner
+		    db_dml calendars_upd { *SQL* }
+		}
+	    }
+	    set msg "  Calendar merge is done"
+	    ns_log Notice $msg
+	    lappend return_msg $msg
+	}
+	# I commented this section to avoid partial merges
+	# If something is wrong the merge should be stopped.
+	#        on_error {
+	# 	    set msg "  I couldn't merge calendar. The error was $errmsg"
+	# 	    ns_log Notice $msg
+	# 	    lappend return_msg $msg
+	# 	}
+	return $return_msg
+    }
+}
+
 ad_proc calendar::make_datetime {
     event_date
     {event_time ""}
@@ -401,7 +478,6 @@
 } {
     return the item types
 } {
-
     return [concat [list [list {--} {}]] \
             [db_list_of_lists select_item_types {}]]
 }