2001-07-16 13:38:40 +02:00
< ? php
/************************************************************************** \
* phpGroupWare - Calendar *
* http :// www . phpgroupware . org *
* Based on Webcalendar by Craig Knudsen < cknudsen @ radix . net > *
* http :// www . radix . net /~ cknudsen *
* Modified by Mark Peters < skeeter @ phpgroupware . org > *
* -------------------------------------------- *
* This program is free software ; you can redistribute it and / or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation ; either version 2 of the License , or ( at your *
* option ) any later version . *
\ **************************************************************************/
/* $Id$ */
2001-08-15 04:27:06 +02:00
if ( @ $GLOBALS [ 'phpgw_info' ][ 'flags' ][ 'included_classes' ][ 'socalendar_' ])
2001-07-16 13:38:40 +02:00
{
return ;
}
2001-08-15 04:27:06 +02:00
$GLOBALS [ 'phpgw_info' ][ 'flags' ][ 'included_classes' ][ 'socalendar_' ] = True ;
2001-07-16 13:38:40 +02:00
class socalendar_ extends socalendar__
{
var $deleted_events = Array ();
var $cal_event ;
var $today = Array ( 'raw' , 'day' , 'month' , 'year' , 'full' , 'dow' , 'dm' , 'bd' );
2001-09-04 06:01:12 +02:00
function socalendar_ ()
{
$this -> socalendar__ ();
2003-08-28 16:31:11 +02:00
if ( ! is_object ( $GLOBALS [ 'phpgw' ] -> asyncservice ))
{
$GLOBALS [ 'phpgw' ] -> asyncservice = CreateObject ( 'phpgwapi.asyncservice' );
}
$this -> async = & $GLOBALS [ 'phpgw' ] -> asyncservice ;
2001-09-04 06:01:12 +02:00
}
2001-07-16 13:38:40 +02:00
function open ( $calendar = '' , $user = '' , $passwd = '' , $options = '' )
{
if ( $user == '' )
{
2001-09-07 18:37:11 +02:00
// settype($user,'integer');
2001-08-15 04:27:06 +02:00
$this -> user = $GLOBALS [ 'phpgw_info' ][ 'user' ][ 'account_id' ];
2001-07-16 13:38:40 +02:00
}
elseif ( is_int ( $user ))
{
$this -> user = $user ;
}
elseif ( is_string ( $user ))
{
2001-08-15 04:27:06 +02:00
$this -> user = $GLOBALS [ 'phpgw' ] -> accounts -> name2id ( $user );
2001-07-16 13:38:40 +02:00
}
2001-08-15 04:27:06 +02:00
$this -> stream = $GLOBALS [ 'phpgw' ] -> db ;
2001-07-16 13:38:40 +02:00
return $this -> stream ;
}
function popen ( $calendar = '' , $user = '' , $passwd = '' , $options = '' )
{
return $this -> open ( $calendar , $user , $passwd , $options );
}
function reopen ( $calendar , $options = '' )
{
return $this -> stream ;
}
function close ( $options = '' )
{
return True ;
}
function create_calendar ( $calendar = '' )
{
return $calendar ;
}
function rename_calendar ( $old_name = '' , $new_name = '' )
{
return $new_name ;
}
function delete_calendar ( $calendar = '' )
{
$this -> stream -> query ( 'SELECT cal_id FROM phpgw_cal WHERE owner=' . intval ( $calendar ), __LINE__ , __FILE__ );
if ( $this -> stream -> num_rows ())
{
while ( $this -> stream -> next_record ())
{
$this -> delete_event ( intval ( $this -> stream -> f ( 'cal_id' )));
}
$this -> expunge ();
}
$this -> stream -> lock ( array ( 'phpgw_cal_user' ));
$this -> stream -> query ( 'DELETE FROM phpgw_cal_user WHERE cal_login=' . intval ( $calendar ), __LINE__ , __FILE__ );
$this -> stream -> unlock ();
return $calendar ;
}
2003-08-28 16:31:11 +02:00
/*!
@ function read_alarms
@ abstract read the alarms of a calendar - event specified by $cal_id
@ returns array of alarms with alarm - id as key
@ note the alarm - id is a string of 'cal:' . $cal_id . ':' . $alarm_nr , it is used as the job - id too
*/
function read_alarms ( $cal_id )
{
$alarms = array ();
if ( $jobs = $this -> async -> read ( 'cal:' . intval ( $cal_id ) . ':%' ))
{
foreach ( $jobs as $id => $job )
{
$alarm = $job [ 'data' ]; // text, enabled
$alarm [ 'id' ] = $id ;
$alarm [ 'time' ] = $job [ 'next' ];
$alarms [ $id ] = $alarm ;
}
}
return $alarms ;
}
/*!
@ function read_alarm
@ abstract read a single alarm specified by it ' s $id
@ returns array with data of the alarm
@ note the alarm - id is a string of 'cal:' . $cal_id . ':' . $alarm_nr , it is used as the job - id too
*/
function read_alarm ( $id )
{
if ( ! ( $jobs = $this -> async -> read ( $id )))
{
return False ;
}
list ( $id , $job ) = each ( $jobs );
$alarm = $job [ 'data' ]; // text, enabled
$alarm [ 'id' ] = $id ;
$alarm [ 'time' ] = $job [ 'next' ];
//echo "<p>read_alarm('$id')="; print_r($alarm); echo "</p>\n";
return $alarm ;
}
/*!
@ function save_alarm
@ abstract saves a new or updated alarm
@ syntax save_alarm ( $cal_id , $alarm , $id = False )
@ param $cal_id Id of the calendar - entry
@ param $alarm array with fields : text , owner , enabled , ..
*/
function save_alarm ( $cal_id , $alarm )
{
//echo "<p>save_alarm(cal_id=$cal_id, alarm="; print_r($alarm); echo ")</p>\n";
if ( ! ( $id = $alarm [ 'id' ]))
{
$alarms = $this -> read_alarms ( $cal_id ); // find a free alarm#
$n = count ( $alarms );
do
{
$id = 'cal:' . intval ( $cal_id ) . ':' . $n ;
++ $n ;
}
while ( @ isset ( $alarms [ $id ]));
}
else
{
$this -> async -> cancel_timer ( $id );
}
$alarm [ 'cal_id' ] = $cal_id ; // we need the back-reference
if ( ! $this -> async -> set_timer ( $alarm [ 'time' ], $id , 'calendar.bocalendar.send_alarm' , $alarm ))
{
return False ;
}
return $id ;
}
/*!
@ function delete_alarms ( $cal_id )
@ abstract delete all alarms of a calendar - entry
@ returns the number of alarms deleted
*/
function delete_alarms ( $cal_id )
{
$alarms = $this -> read_alarms ( $cal_id );
foreach ( $alarms as $id => $alarm )
{
$this -> async -> cancel_timer ( $id );
}
return count ( $alarms );
}
/*!
@ function delete_alarm ( $id )
@ abstract delete one alarms identified by its id
@ returns the number of alarms deleted
*/
function delete_alarm ( $id )
{
return $this -> async -> cancel_timer ( $id );
}
2001-07-16 13:38:40 +02:00
function fetch_event ( $event_id , $options = '' )
{
if ( ! isset ( $this -> stream ))
{
return False ;
}
2003-08-28 16:31:11 +02:00
$event_id = intval ( $event_id );
$this -> stream -> lock ( array ( 'phpgw_cal' , 'phpgw_cal_user' , 'phpgw_cal_repeats' , 'phpgw_cal_extra' /* OLD-ALARM,'phpgw_cal_alarm'*/ ));
2001-07-16 13:38:40 +02:00
$this -> stream -> query ( 'SELECT * FROM phpgw_cal WHERE cal_id=' . $event_id , __LINE__ , __FILE__ );
if ( $this -> stream -> num_rows () > 0 )
{
$this -> event_init ();
$this -> stream -> next_record ();
// Load the calendar event data from the db into $event structure
// Use http://www.php.net/manual/en/function.mcal-fetch-event.php as the reference
$this -> add_attribute ( 'owner' , intval ( $this -> stream -> f ( 'owner' )));
$this -> add_attribute ( 'id' , intval ( $this -> stream -> f ( 'cal_id' )));
$this -> set_class ( intval ( $this -> stream -> f ( 'is_public' )));
2001-09-17 04:33:19 +02:00
$this -> set_category ( $this -> stream -> f ( 'category' ));
2002-11-23 15:19:56 +01:00
$this -> set_title ( stripslashes ( $GLOBALS [ 'phpgw' ] -> strip_html ( $this -> stream -> f ( 'title' ))));
$this -> set_description ( stripslashes ( $GLOBALS [ 'phpgw' ] -> strip_html ( $this -> stream -> f ( 'description' ))));
2001-09-07 18:37:11 +02:00
$this -> add_attribute ( 'uid' , $GLOBALS [ 'phpgw' ] -> strip_html ( $this -> stream -> f ( 'uid' )));
2002-11-23 15:19:56 +01:00
$this -> add_attribute ( 'location' , stripslashes ( $GLOBALS [ 'phpgw' ] -> strip_html ( $this -> stream -> f ( 'location' ))));
2001-08-27 03:30:40 +02:00
$this -> add_attribute ( 'reference' , intval ( $this -> stream -> f ( 'reference' )));
2001-07-16 13:38:40 +02:00
// This is the preferred method once everything is normalized...
//$this->event->alarm = intval($this->stream->f('alarm'));
// But until then, do it this way...
//Legacy Support (New)
2002-06-25 01:24:24 +02:00
$datetime = $GLOBALS [ 'phpgw' ] -> datetime -> localdates ( $this -> stream -> f ( 'datetime' ));
2001-07-16 13:38:40 +02:00
$this -> set_start ( $datetime [ 'year' ], $datetime [ 'month' ], $datetime [ 'day' ], $datetime [ 'hour' ], $datetime [ 'minute' ], $datetime [ 'second' ]);
2002-06-25 01:24:24 +02:00
$datetime = $GLOBALS [ 'phpgw' ] -> datetime -> localdates ( $this -> stream -> f ( 'mdatetime' ));
2001-07-30 00:09:24 +02:00
$this -> set_date ( 'modtime' , $datetime [ 'year' ], $datetime [ 'month' ], $datetime [ 'day' ], $datetime [ 'hour' ], $datetime [ 'minute' ], $datetime [ 'second' ]);
2002-06-25 01:24:24 +02:00
$datetime = $GLOBALS [ 'phpgw' ] -> datetime -> localdates ( $this -> stream -> f ( 'edatetime' ));
2001-07-16 13:38:40 +02:00
$this -> set_end ( $datetime [ 'year' ], $datetime [ 'month' ], $datetime [ 'day' ], $datetime [ 'hour' ], $datetime [ 'minute' ], $datetime [ 'second' ]);
//Legacy Support
$this -> add_attribute ( 'priority' , intval ( $this -> stream -> f ( 'priority' )));
if ( $this -> stream -> f ( 'cal_group' ) || $this -> stream -> f ( 'groups' ) != 'NULL' )
{
$groups = explode ( ',' , $this -> stream -> f ( 'groups' ));
for ( $j = 1 ; $j < count ( $groups ) - 1 ; $j ++ )
{
2001-07-30 00:09:24 +02:00
$this -> add_attribute ( 'groups' , $groups [ $j ], $j - 1 );
2001-07-16 13:38:40 +02:00
}
}
$this -> stream -> query ( 'SELECT * FROM phpgw_cal_repeats WHERE cal_id=' . $event_id , __LINE__ , __FILE__ );
if ( $this -> stream -> num_rows ())
{
$this -> stream -> next_record ();
2001-07-30 00:09:24 +02:00
$this -> add_attribute ( 'recur_type' , intval ( $this -> stream -> f ( 'recur_type' )));
$this -> add_attribute ( 'recur_interval' , intval ( $this -> stream -> f ( 'recur_interval' )));
2001-07-16 13:38:40 +02:00
$enddate = $this -> stream -> f ( 'recur_enddate' );
if ( $enddate != 0 && $enddate != Null )
{
2002-06-25 01:24:24 +02:00
$datetime = $GLOBALS [ 'phpgw' ] -> datetime -> localdates ( $enddate );
2001-07-30 00:09:24 +02:00
$this -> add_attribute ( 'recur_enddate' , $datetime [ 'year' ], 'year' );
$this -> add_attribute ( 'recur_enddate' , $datetime [ 'month' ], 'month' );
$this -> add_attribute ( 'recur_enddate' , $datetime [ 'day' ], 'mday' );
$this -> add_attribute ( 'recur_enddate' , $datetime [ 'hour' ], 'hour' );
$this -> add_attribute ( 'recur_enddate' , $datetime [ 'minute' ], 'min' );
$this -> add_attribute ( 'recur_enddate' , $datetime [ 'second' ], 'sec' );
2001-07-16 13:38:40 +02:00
}
else
{
2001-07-30 00:09:24 +02:00
$this -> add_attribute ( 'recur_enddate' , 0 , 'year' );
$this -> add_attribute ( 'recur_enddate' , 0 , 'month' );
$this -> add_attribute ( 'recur_enddate' , 0 , 'mday' );
$this -> add_attribute ( 'recur_enddate' , 0 , 'hour' );
$this -> add_attribute ( 'recur_enddate' , 0 , 'min' );
$this -> add_attribute ( 'recur_enddate' , 0 , 'sec' );
2001-07-16 13:38:40 +02:00
}
2001-07-30 00:09:24 +02:00
$this -> add_attribute ( 'recur_enddate' , 0 , 'alarm' );
2001-08-12 16:47:23 +02:00
if ( $this -> debug )
{
echo 'Event ID#' . $this -> event [ 'id' ] . ' : Enddate = ' . $enddate . " <br> \n " ;
}
2001-07-30 00:09:24 +02:00
$this -> add_attribute ( 'recur_data' , $this -> stream -> f ( 'recur_data' ));
2001-11-05 03:08:31 +01:00
$exception_list = $this -> stream -> f ( 'recur_exception' );
$exceptions = Array ();
if ( strpos ( ' ' . $exception_list , ',' ))
{
$exceptions = explode ( ',' , $exception_list );
}
elseif ( $exception_list != '' )
{
$exceptions [] = $exception_list ;
}
$this -> add_attribute ( 'recur_exception' , $exceptions );
2001-07-16 13:38:40 +02:00
}
//Legacy Support
$this -> stream -> query ( 'SELECT * FROM phpgw_cal_user WHERE cal_id=' . $event_id , __LINE__ , __FILE__ );
if ( $this -> stream -> num_rows ())
{
while ( $this -> stream -> next_record ())
{
if ( intval ( $this -> stream -> f ( 'cal_login' )) == intval ( $this -> user ))
{
2001-07-30 00:09:24 +02:00
$this -> add_attribute ( 'users_status' , $this -> stream -> f ( 'cal_status' ));
2001-07-16 13:38:40 +02:00
}
2001-07-30 00:09:24 +02:00
$this -> add_attribute ( 'participants' , $this -> stream -> f ( 'cal_status' ), intval ( $this -> stream -> f ( 'cal_login' )));
2001-07-16 13:38:40 +02:00
}
}
2001-09-07 18:37:11 +02:00
2003-08-28 16:31:11 +02:00
// Custom fields
$this -> stream -> query ( 'SELECT * FROM phpgw_cal_extra WHERE cal_id=' . $event_id , __LINE__ , __FILE__ );
if ( $this -> stream -> num_rows ())
{
while ( $this -> stream -> next_record ())
{
$this -> add_attribute ( '#' . $this -> stream -> f ( 'cal_extra_name' ), $this -> stream -> f ( 'cal_extra_value' ));
}
}
/* OLD - ALARM
2001-10-27 14:59:06 +02:00
if ( $this -> event [ 'reference' ])
{
2003-08-28 16:31:11 +02:00
// What is event['reference']???
2001-10-27 14:59:06 +02:00
$alarm_cal_id = $event_id . ',' . $this -> event [ 'reference' ];
}
else
{
$alarm_cal_id = $event_id ;
}
2003-08-28 16:31:11 +02:00
//echo '<!-- cal_id='.$alarm_cal_id.' -->'."\n";
//$this->stream->query('SELECT * FROM phpgw_cal_alarm WHERE cal_id in ('.$alarm_cal_id.') AND cal_owner='.$this->user,__LINE__,__FILE__);
$this -> stream -> query ( 'SELECT * FROM phpgw_cal_alarm WHERE cal_id=' . $event_id . ' AND cal_owner=' . $this -> user , __LINE__ , __FILE__ );
2001-09-07 18:37:11 +02:00
if ( $this -> stream -> num_rows ())
{
while ( $this -> stream -> next_record ())
{
2001-09-08 10:48:38 +02:00
$this -> event [ 'alarm' ][] = Array (
2001-10-26 01:52:15 +02:00
'id' => intval ( $this -> stream -> f ( 'alarm_id' )),
2001-09-08 10:48:38 +02:00
'time' => intval ( $this -> stream -> f ( 'cal_time' )),
'text' => $this -> stream -> f ( 'cal_text' ),
'enabled' => intval ( $this -> stream -> f ( 'alarm_enabled' ))
);
2001-09-07 18:37:11 +02:00
}
}
2003-08-28 16:31:11 +02:00
*/
2001-07-16 13:38:40 +02:00
}
else
{
$this -> event = False ;
}
$this -> stream -> unlock ();
2003-08-28 16:31:11 +02:00
if ( $this -> event )
{
$this -> event [ 'alarm' ] = $this -> read_alarms ( $event_id );
if ( $this -> event [ 'reference' ])
{
$this -> event [ 'alarm' ] += $this -> read_alarms ( $event_id );
}
}
2001-07-16 13:38:40 +02:00
return $this -> event ;
}
2001-10-23 12:58:53 +02:00
function list_events ( $startYear , $startMonth , $startDay , $endYear = 0 , $endMonth = 0 , $endDay = 0 , $extra = '' , $tz_offset = 0 , $owner_id = 0 )
2001-07-16 13:38:40 +02:00
{
if ( ! isset ( $this -> stream ))
{
return False ;
}
$datetime = mktime ( 0 , 0 , 0 , $startMonth , $startDay , $startYear ) - $tz_offset ;
2001-10-26 01:52:15 +02:00
$user_where = ' AND (phpgw_cal_user.cal_login in (' ;
2001-10-23 12:58:53 +02:00
if ( $owner_id )
{
2001-10-26 01:52:15 +02:00
$user_where .= implode ( ',' , $owner_id );
2001-10-23 12:58:53 +02:00
}
else
{
2001-10-26 01:52:15 +02:00
$user_where .= $this -> user ;
}
$member_groups = $GLOBALS [ 'phpgw' ] -> accounts -> membership ( $this -> user );
@ reset ( $member_groups );
2002-01-10 03:15:33 +01:00
while ( $member_groups != False && list ( $key , $group_info ) = each ( $member_groups ))
2001-10-26 01:52:15 +02:00
{
$member [] = $group_info [ 'account_id' ];
2001-10-23 12:58:53 +02:00
}
2001-10-26 01:52:15 +02:00
@ reset ( $member );
2001-11-29 04:12:01 +01:00
// $user_where .= ','.implode(',',$member);
2001-10-26 01:52:15 +02:00
$user_where .= ')) ' ;
2001-11-03 02:52:26 +01:00
if ( $this -> debug )
{
echo '<!-- ' . $user_where . ' -->' . " \n " ;
}
2001-10-26 01:52:15 +02:00
2001-08-12 16:47:23 +02:00
$startDate = 'AND ( ( (phpgw_cal.datetime >= ' . $datetime . ') ' ;
$enddate = '' ;
2001-07-22 01:35:22 +02:00
if ( $endYear != 0 && $endMonth != 0 && $endDay != 0 )
2001-07-16 13:38:40 +02:00
{
$edatetime = mktime ( 23 , 59 , 59 , intval ( $endMonth ), intval ( $endDay ), intval ( $endYear )) - $tz_offset ;
2001-08-12 16:47:23 +02:00
$endDate .= 'AND (phpgw_cal.edatetime <= ' . $edatetime . ') ) '
. 'OR ( (phpgw_cal.datetime <= ' . $datetime . ') '
. 'AND (phpgw_cal.edatetime >= ' . $edatetime . ') ) '
. 'OR ( (phpgw_cal.datetime >= ' . $datetime . ') '
. 'AND (phpgw_cal.datetime <= ' . $edatetime . ') '
. 'AND (phpgw_cal.edatetime >= ' . $edatetime . ') ) '
. 'OR ( (phpgw_cal.datetime <= ' . $datetime . ') '
. 'AND (phpgw_cal.edatetime >= ' . $datetime . ') '
. 'AND (phpgw_cal.edatetime <= ' . $edatetime . ') ' ;
2001-07-16 13:38:40 +02:00
}
2001-08-12 16:47:23 +02:00
$endDate .= ') ) ' ;
2001-07-16 13:38:40 +02:00
$order_by = 'ORDER BY phpgw_cal.datetime ASC, phpgw_cal.edatetime ASC, phpgw_cal.priority ASC' ;
2001-08-12 16:47:23 +02:00
if ( $this -> debug )
{
echo " SQL : " . $user_where . $startDate . $endDate . $extra . " <br> \n " ;
}
2001-07-18 19:32:10 +02:00
return $this -> get_event_ids ( False , $user_where . $startDate . $endDate . $extra . $order_by );
2001-07-16 13:38:40 +02:00
}
function append_event ()
{
$this -> save_event ( $this -> event );
$this -> send_update ( MSG_ADDED , $this -> event -> participants , '' , $this -> event );
2001-07-30 00:09:24 +02:00
return $this -> event [ 'id' ];
2001-07-16 13:38:40 +02:00
}
function store_event ()
{
2001-09-07 18:37:11 +02:00
return $this -> save_event ( & $this -> event );
2001-07-16 13:38:40 +02:00
}
function delete_event ( $event_id )
{
$this -> deleted_events [] = $event_id ;
}
function snooze ( $event_id )
{
//Turn off an alarm for an event
//Returns true.
}
function list_alarms ( $begin_year = '' , $begin_month = '' , $begin_day = '' , $end_year = '' , $end_month = '' , $end_day = '' )
{
//Return a list of events that has an alarm triggered at the given datetime
//Returns an array of event ID's
}
// The function definition doesn't look correct...
// Need more information for this function
function next_recurrence ( $weekstart , $next )
{
// return next_recurrence (int stream, int weekstart, array next);
}
function expunge ()
{
if ( count ( $this -> deleted_events ) <= 0 )
{
return 1 ;
}
$this_event = $this -> event ;
$locks = Array (
'phpgw_cal' ,
'phpgw_cal_user' ,
2001-09-11 05:08:04 +02:00
'phpgw_cal_repeats' ,
2003-08-28 16:31:11 +02:00
'phpgw_cal_extra'
// OLD-ALARM 'phpgw_cal_alarm'
2001-07-16 13:38:40 +02:00
);
$this -> stream -> lock ( $locks );
2003-08-28 16:31:11 +02:00
foreach ( $this -> deleted_events as $cal_id )
2001-07-16 13:38:40 +02:00
{
2003-08-28 16:31:11 +02:00
foreach ( $locks as $table )
2001-07-16 13:38:40 +02:00
{
2003-08-28 16:31:11 +02:00
$this -> stream -> query ( 'DELETE FROM ' . $table . ' WHERE cal_id=' . $cal_id , __LINE__ , __FILE__ );
2001-07-16 13:38:40 +02:00
}
}
$this -> stream -> unlock ();
2003-08-28 16:31:11 +02:00
foreach ( $this -> deleted_events as $cal_id )
{
$this -> delete_alarms ( $cal_id );
}
$this -> deleted_events = array ();
2001-07-16 13:38:40 +02:00
$this -> event = $this_event ;
return 1 ;
}
/***************** Local functions for SQL based Calendar *****************/
2003-08-28 16:31:11 +02:00
function get_event_ids ( $search_repeats = False , $extra = '' , $search_extra = False )
2001-07-16 13:38:40 +02:00
{
2003-08-28 16:31:11 +02:00
$from = $where = ' ' ;
if ( $search_repeats )
2001-07-16 13:38:40 +02:00
{
2003-08-28 16:31:11 +02:00
$from = ', phpgw_cal_repeats ' ;
$where = 'AND (phpgw_cal_repeats.cal_id = phpgw_cal.cal_id) ' ;
2001-07-16 13:38:40 +02:00
}
2003-08-28 16:31:11 +02:00
if ( $search_extra )
2001-07-16 13:38:40 +02:00
{
2003-08-28 16:31:11 +02:00
$from .= 'LEFT JOIN phpgw_cal_extra ON phpgw_cal_extra.cal_id = phpgw_cal.cal_id ' ;
2001-07-16 13:38:40 +02:00
}
2003-08-28 16:31:11 +02:00
2001-07-16 13:38:40 +02:00
$sql = 'SELECT DISTINCT phpgw_cal.cal_id,'
. 'phpgw_cal.datetime,phpgw_cal.edatetime,'
. 'phpgw_cal.priority '
. 'FROM phpgw_cal, phpgw_cal_user'
2003-08-28 16:31:11 +02:00
. $from
2001-07-16 13:38:40 +02:00
. 'WHERE (phpgw_cal_user.cal_id = phpgw_cal.cal_id) '
2003-08-28 16:31:11 +02:00
. $where . $extra ;
2001-08-12 16:47:23 +02:00
if ( $this -> debug )
{
echo " FULL SQL : " . $sql . " <br> \n " ;
}
2001-07-16 13:38:40 +02:00
$this -> stream -> query ( $sql , __LINE__ , __FILE__ );
$retval = Array ();
if ( $this -> stream -> num_rows () == 0 )
{
2001-08-12 16:47:23 +02:00
if ( $this -> debug )
{
echo " No records found!<br> \n " ;
}
2001-07-16 13:38:40 +02:00
return $retval ;
}
while ( $this -> stream -> next_record ())
{
$retval [] = intval ( $this -> stream -> f ( 'cal_id' ));
}
2001-08-12 16:47:23 +02:00
if ( $this -> debug )
{
echo " Records found!<br> \n " ;
}
2001-07-16 13:38:40 +02:00
return $retval ;
}
function save_event ( & $event )
{
$locks = Array (
'phpgw_cal' ,
'phpgw_cal_user' ,
2003-08-28 16:31:11 +02:00
'phpgw_cal_repeats' ,
'phpgw_cal_extra'
// OLD-ALARM 'phpgw_cal_alarm'
2001-07-16 13:38:40 +02:00
);
$this -> stream -> lock ( $locks );
2001-07-30 00:09:24 +02:00
if ( $event [ 'id' ] == 0 )
2001-07-16 13:38:40 +02:00
{
2001-09-17 04:33:19 +02:00
if ( ! $event [ 'uid' ])
2001-09-07 18:37:11 +02:00
{
2001-09-17 04:33:19 +02:00
if ( $GLOBALS [ 'phpgw_info' ][ 'server' ][ 'hostname' ] != '' )
2001-09-07 19:10:23 +02:00
{
2001-09-17 04:33:19 +02:00
$id_suffix = $GLOBALS [ 'phpgw_info' ][ 'server' ][ 'hostname' ];
}
else
{
$id_suffix = $GLOBALS [ 'phpgw' ] -> common -> randomstring ( 3 ) . 'local' ;
}
$parts = Array (
0 => 'title' ,
1 => 'description'
);
@ reset ( $parts );
while ( list ( $key , $field ) = each ( $parts ))
{
$part [ $key ] = substr ( $GLOBALS [ 'phpgw' ] -> crypto -> encrypt ( $event [ $field ]), 0 , 20 );
if ( ! $GLOBALS [ 'phpgw' ] -> crypto -> enabled )
{
$part [ $key ] = bin2hex ( unserialize ( $part [ $key ]));
}
2001-09-07 19:10:23 +02:00
}
2001-09-17 04:33:19 +02:00
$event [ 'uid' ] = $part [ 0 ] . '-' . $part [ 1 ] . '@' . $id_suffix ;
2001-09-07 18:37:11 +02:00
}
2003-04-14 23:35:50 +02:00
$this -> stream -> query ( 'INSERT INTO phpgw_cal(uid,title,owner,priority,is_public,category) '
. " values(' " . $event [ 'uid' ] . " ',' " . $this -> stream -> db_addslashes ( $event [ 'title' ])
. " ', " . $event [ 'owner' ] . ',' . $event [ 'priority' ] . ',' . $event [ 'public' ] . " ,' "
. $event [ 'category' ] . " ') " , __LINE__ , __FILE__ );
$event [ 'id' ] = $this -> stream -> get_last_insert_id ( 'phpgw_cal' , 'cal_id' );
2001-07-16 13:38:40 +02:00
}
2002-06-25 01:24:24 +02:00
$date = $this -> maketime ( $event [ 'start' ]) - $GLOBALS [ 'phpgw' ] -> datetime -> tz_offset ;
$enddate = $this -> maketime ( $event [ 'end' ]) - $GLOBALS [ 'phpgw' ] -> datetime -> tz_offset ;
$today = time () - $GLOBALS [ 'phpgw' ] -> datetime -> tz_offset ;
2001-07-16 13:38:40 +02:00
2001-07-30 00:09:24 +02:00
if ( $event [ 'recur_type' ] != MCAL_RECUR_NONE )
2001-07-16 13:38:40 +02:00
{
$type = 'M' ;
}
else
{
$type = 'E' ;
}
$sql = 'UPDATE phpgw_cal SET '
2001-07-30 00:09:24 +02:00
. 'owner=' . $event [ 'owner' ] . ', '
2001-07-16 13:38:40 +02:00
. 'datetime=' . $date . ', '
. 'mdatetime=' . $today . ', '
. 'edatetime=' . $enddate . ', '
2001-07-30 00:09:24 +02:00
. 'priority=' . $event [ 'priority' ] . ', '
2001-09-17 04:33:19 +02:00
. " category=' " . $event [ 'category' ] . " ', "
2001-07-16 13:38:40 +02:00
. " cal_type=' " . $type . " ', "
2001-07-30 00:09:24 +02:00
. 'is_public=' . $event [ 'public' ] . ', '
2001-09-17 04:33:19 +02:00
. " title=' " . $this -> stream -> db_addslashes ( $event [ 'title' ]) . " ', "
. " description=' " . $this -> stream -> db_addslashes ( $event [ 'description' ]) . " ', "
2003-08-28 16:31:11 +02:00
. " location=' " . $this -> stream -> db_addslashes ( $event [ 'location' ]) . " ', "
2001-10-26 01:52:15 +02:00
. ( $event [ 'groups' ] ? " groups=' " . ( count ( $event [ 'groups' ]) > 1 ? implode ( ',' , $event [ 'groups' ]) : ',' . $event [ 'groups' ][ 0 ] . ',' ) . " ', " : '' )
2001-08-27 03:30:40 +02:00
. 'reference=' . $event [ 'reference' ] . ' '
2001-07-30 00:09:24 +02:00
. 'WHERE cal_id=' . $event [ 'id' ];
2001-07-16 13:38:40 +02:00
$this -> stream -> query ( $sql , __LINE__ , __FILE__ );
2001-07-30 00:09:24 +02:00
$this -> stream -> query ( 'DELETE FROM phpgw_cal_user WHERE cal_id=' . $event [ 'id' ], __LINE__ , __FILE__ );
2001-07-16 13:38:40 +02:00
2001-09-07 18:37:11 +02:00
@ reset ( $event [ 'participants' ]);
while ( list ( $key , $value ) = @ each ( $event [ 'participants' ]))
2001-07-16 13:38:40 +02:00
{
2002-08-25 06:07:21 +02:00
if ( intval ( $key ) == $event [ 'owner' ])
2001-07-16 13:38:40 +02:00
{
$value = 'A' ;
}
$this -> stream -> query ( 'INSERT INTO phpgw_cal_user(cal_id,cal_login,cal_status) '
2001-07-30 00:09:24 +02:00
. 'VALUES(' . $event [ 'id' ] . ',' . intval ( $key ) . " ,' " . $value . " ') " , __LINE__ , __FILE__ );
2001-07-16 13:38:40 +02:00
}
2001-07-30 00:09:24 +02:00
if ( $event [ 'recur_type' ] != MCAL_RECUR_NONE )
2001-07-16 13:38:40 +02:00
{
2001-07-30 00:09:24 +02:00
if ( $event [ 'recur_enddate' ][ 'month' ] != 0 && $event [ 'recur_enddate' ][ 'mday' ] != 0 && $event [ 'recur_enddate' ][ 'year' ] != 0 )
2001-07-16 13:38:40 +02:00
{
2002-06-25 01:24:24 +02:00
$end = $this -> maketime ( $event [ 'recur_enddate' ]) - $GLOBALS [ 'phpgw' ] -> datetime -> tz_offset ;
2001-07-16 13:38:40 +02:00
}
else
{
$end = 0 ;
}
2001-07-30 00:09:24 +02:00
$this -> stream -> query ( 'SELECT count(cal_id) FROM phpgw_cal_repeats WHERE cal_id=' . $event [ 'id' ], __LINE__ , __FILE__ );
2001-07-16 13:38:40 +02:00
$this -> stream -> next_record ();
$num_rows = $this -> stream -> f ( 0 );
if ( $num_rows == 0 )
{
$this -> stream -> query ( 'INSERT INTO phpgw_cal_repeats(cal_id,recur_type,recur_enddate,recur_data,recur_interval) '
2001-07-30 00:09:24 +02:00
. 'VALUES(' . $event [ 'id' ] . ',' . $event [ 'recur_type' ] . ',' . $end . ',' . $event [ 'recur_data' ] . ',' . $event [ 'recur_interval' ] . ')' , __LINE__ , __FILE__ );
2001-07-16 13:38:40 +02:00
}
else
{
$this -> stream -> query ( 'UPDATE phpgw_cal_repeats '
2001-11-05 03:08:31 +01:00
. 'SET recur_type=' . $event [ 'recur_type' ] . ', '
. 'recur_enddate=' . $end . ', '
. 'recur_data=' . $event [ 'recur_data' ] . ', '
. 'recur_interval=' . $event [ 'recur_interval' ] . ', '
. " recur_exception=' " . ( count ( $event [ 'recur_exception' ]) > 1 ? implode ( ',' , $event [ 'recur_exception' ]) : ( count ( $event [ 'recur_exception' ]) == 1 ? $event [ 'recur_exception' ][ 0 ] : '' )) . " ' "
. 'WHERE cal_id=' . $event [ 'id' ], __LINE__ , __FILE__ );
2001-07-16 13:38:40 +02:00
}
}
else
{
2001-07-30 00:09:24 +02:00
$this -> stream -> query ( 'DELETE FROM phpgw_cal_repeats WHERE cal_id=' . $event [ 'id' ], __LINE__ , __FILE__ );
2001-07-16 13:38:40 +02:00
}
2003-08-28 16:31:11 +02:00
// Custom fields
$this -> stream -> query ( 'DELETE FROM phpgw_cal_extra WHERE cal_id=' . $event [ 'id' ], __LINE__ , __FILE__ );
2001-11-19 00:01:09 +01:00
2003-08-28 16:31:11 +02:00
foreach ( $event as $name => $value )
{
if ( $name [ 0 ] == '#' && strlen ( $value ))
{
$this -> stream -> query ( 'INSERT INTO phpgw_cal_extra (cal_id,cal_extra_name,cal_extra_value) '
. 'VALUES(' . $event [ 'id' ] . " ,' " . addslashes ( substr ( $name , 1 )) . " ',' " . addslashes ( $value ) . " ') " , __LINE__ , __FILE__ );
}
}
/*
$alarmcount = count ( $event [ 'alarm' ]);
if ( $alarmcount > 1 )
{
// this should never happen, $event['alarm'] should only be set
// if creating a new event and uicalendar only sets up 1 alarm
// the user must use "Alarm Management" to create/establish multiple
// alarms or to edit/change an alarm
echo '<!-- how did this happen, too many alarms -->' . " \n " ;
$this -> stream -> unlock ();
return True ;
}
if ( $alarmcount == 1 )
{
list ( $key , $alarm ) = @ each ( $event [ 'alarm' ]);
$this -> stream -> query ( 'INSERT INTO phpgw_cal_alarm(cal_id,cal_owner,cal_time,cal_text,alarm_enabled) VALUES(' . $event [ 'id' ] . ',' . $event [ 'owner' ] . ',' . $alarm [ 'time' ] . " ,' " . $alarm [ 'text' ] . " ', " . $alarm [ 'enabled' ] . ')' , __LINE__ , __FILE__ );
$this -> stream -> query ( 'SELECT LAST_INSERT_ID()' );
$this -> stream -> next_record ();
$alarm [ 'id' ] = $this -> stream -> f ( 0 );
}
*/
2002-11-13 04:30:15 +01:00
print_debug ( 'Event Saved: ID #' , $event [ 'id' ]);
2001-07-16 13:38:40 +02:00
$this -> stream -> unlock ();
2003-08-28 16:31:11 +02:00
if ( is_array ( $event [ 'alarm' ]))
{
foreach ( $event [ 'alarm' ] as $alarm ) // this are all new alarms
{
$this -> save_alarm ( $event [ 'id' ], $alarm );
}
}
$GLOBALS [ 'phpgw_info' ][ 'cal_new_event_id' ] = $event [ 'id' ];
2001-07-16 13:38:40 +02:00
return True ;
}
2003-08-28 16:31:11 +02:00
function get_alarm ( $cal_id )
2001-09-04 05:17:49 +02:00
{
2003-08-28 16:31:11 +02:00
/* OLD - ALARM
2001-09-07 18:37:11 +02:00
$this -> stream -> query ( 'SELECT cal_time, cal_text FROM phpgw_cal_alarm WHERE cal_id=' . $id . ' AND cal_owner=' . $this -> user , __LINE__ , __FILE__ );
2001-09-04 05:17:49 +02:00
if ( $this -> stream -> num_rows ())
{
while ( $this -> stream -> next_record ())
{
$alarm [ $this -> stream -> f ( 'cal_time' )] = $this -> stream -> f ( 'cal_text' );
}
@ reset ( $alarm );
return $alarm ;
}
else
{
return False ;
}
2003-08-28 16:31:11 +02:00
*/
$alarms = $this -> read_alarms ( $cal_id );
$ret = False ;
foreach ( $alarms as $alarm )
{
if ( $alarm [ 'owner' ] == $this -> user || ! $alarm [ 'owner' ])
{
$ret [ $alarm [ 'time' ]] = $alarm [ 'text' ];
}
}
return $ret ;
2001-09-04 05:17:49 +02:00
}
2001-07-16 13:38:40 +02:00
function set_status ( $id , $owner , $status )
{
$status_code_short = Array (
REJECTED => 'R' ,
NO_RESPONSE => 'U' ,
TENTATIVE => 'T' ,
ACCEPTED => 'A'
);
2001-07-22 01:35:22 +02:00
$this -> stream -> query ( " UPDATE phpgw_cal_user SET cal_status=' " . $status_code_short [ $status ] . " ' WHERE cal_id= " . $id . " AND cal_login= " . $owner , __LINE__ , __FILE__ );
2003-08-28 16:31:11 +02:00
/* OLD - ALARM
if ( $status == 'R' )
{
$this -> stream -> query ( 'UPDATE phpgw_cal_alarm set alarm_enabled=0 where cal_id=' . $id . ' and cal_owner=' . $owner , __LINE__ , __FILE__ );
}
*/
2001-07-16 13:38:40 +02:00
return True ;
}
// End of ICal style support.......
function group_search ( $owner = 0 )
{
2001-08-15 04:27:06 +02:00
$owner = ( $owner == $GLOBALS [ 'phpgw_info' ][ 'user' ][ 'account_id' ] ? 0 : $owner );
$groups = substr ( $GLOBALS [ 'phpgw' ] -> common -> sql_search ( 'phpgw_cal.groups' , intval ( $owner )), 4 );
2001-07-16 13:38:40 +02:00
if ( ! $groups )
{
return '' ;
}
else
{
return " (phpgw_cal.is_public=2 AND ( " . $groups . ')) ' ;
}
}
function splittime_ ( $time )
{
$temp = array ( 'hour' , 'minute' , 'second' , 'ampm' );
$time = strrev ( $time );
$second = ( int ) strrev ( substr ( $time , 0 , 2 ));
$minute = ( int ) strrev ( substr ( $time , 2 , 2 ));
$hour = ( int ) strrev ( substr ( $time , 4 ));
$temp [ 'second' ] = ( int ) $second ;
$temp [ 'minute' ] = ( int ) $minute ;
$temp [ 'hour' ] = ( int ) $hour ;
$temp [ 'ampm' ] = ' ' ;
return $temp ;
}
function date_to_epoch ( $d )
{
return $this -> localdates ( mktime ( 0 , 0 , 0 , intval ( substr ( $d , 4 , 2 )), intval ( substr ( $d , 6 , 2 )), intval ( substr ( $d , 0 , 4 ))));
}
2003-08-28 16:31:11 +02:00
function list_dirty_events ( $lastmod =- 1 , $repeats = false )
{
if ( ! isset ( $this -> stream ))
{
return False ;
}
$lastmod = intval ( $lastmod );
$repeats = ( bool ) $repeats ;
$user_where = " AND phpgw_cal_user.cal_login = $this->user " ;
$member_groups = $GLOBALS [ 'phpgw' ] -> accounts -> membership ( $this -> user );
@ reset ( $member_groups );
while ( $member_groups != False && list ( $key , $group_info ) = each ( $member_groups ))
{
$member [] = $group_info [ 'account_id' ];
}
@ reset ( $member );
// $user_where .= ','.implode(',',$member);
//$user_where .= ')) ';
if ( $this -> debug )
{
echo '<!-- ' . $user_where . ' -->' . " \n " ;
}
if ( $lastmod > 0 )
{
$wheremod = " AND mdatetime = $lastmod " ;
}
$order_by = ' ORDER BY phpgw_cal.cal_id ASC' ;
if ( $this -> debug )
{
echo " SQL : " . $user_where . $wheremod . $extra . " <br> \n " ;
}
return $this -> get_event_ids ( $repeats , $user_where . $wheremod . $extra . $order_by );
}
/* OLD - ALARM
function add_alarm ( $eventid , $alarm , $owner )
{
$this -> stream -> query ( 'INSERT INTO phpgw_cal_alarm(cal_id,cal_owner,cal_time,cal_text,alarm_enabled) VALUES(' . $eventid . ',' . $owner . ',' . $alarm [ 'time' ] . " ,' " . $alarm [ 'text' ] . " ',1) " , __LINE__ , __FILE__ );
$this -> stream -> query ( 'SELECT LAST_INSERT_ID()' );
$this -> stream -> next_record ();
return ( $this -> stream -> f ( 0 ));
}
function delete_alarm ( $alarmid )
{
$this -> stream -> query ( 'DELETE FROM phpgw_cal_alarm WHERE alarm_id=' . $alarmid , __LINE__ , __FILE__ );
}
*/
2001-07-16 13:38:40 +02:00
}