2004-10-08 00:11:18 +02:00
< ? php
2007-03-09 12:39:47 +01:00
/**
* eGroupWare - Calendar ' s forms of the UserInterface
*
* @ link http :// www . egroupware . org
* @ package calendar
* @ author Ralf Becker < RalfBecker - AT - outdoor - training . de >
* @ copyright ( c ) 2004 - 7 by RalfBecker - At - outdoor - training . de
* @ license http :// opensource . org / licenses / gpl - license . php GPL - GNU General Public License
* @ version $Id $
*/
2004-10-08 00:11:18 +02:00
2005-11-09 00:15:14 +01:00
include_once ( EGW_INCLUDE_ROOT . '/calendar/inc/class.uical.inc.php' );
2004-10-08 00:11:18 +02:00
/**
2005-11-09 00:15:14 +01:00
* calendar UserInterface forms : view and edit events , freetime search
*
* The new UI , BO and SO classes have a strikt definition , in which time - zone they operate :
* UI only operates in user - time , so there have to be no conversation at all !!!
* BO ' s functions take and return user - time only ( ! ), they convert internaly everything to servertime , because
* SO operates only on server - time
*
* The state of the UI elements is managed in the uical class , which all UI classes extend .
*
* All permanent debug messages of the calendar - code should done via the debug - message method of the bocal class ! ! !
2004-10-08 00:11:18 +02:00
*/
class uiforms extends uical
{
var $public_functions = array (
'freetimesearch' => True ,
2005-11-09 00:15:14 +01:00
'edit' => true ,
'export' => true ,
'import' => true ,
2004-10-08 00:11:18 +02:00
);
2006-08-22 18:50:45 +02:00
/**
* Reference to link - class of bocal
*
* @ var bolink
*/
var $link ;
2008-05-08 00:12:25 +02:00
2006-06-03 01:36:43 +02:00
/**
* Standard durations used in edit and freetime search
*
* @ var array
*/
var $durations = array ();
2008-05-08 00:12:25 +02:00
2006-12-22 20:51:56 +01:00
/**
* Name of the tabs used in edit
*
* @ var string
*/
var $tabs = 'general|description|participants|recurrence|custom|links|alarms' ;
2008-05-08 00:12:25 +02:00
2008-01-15 09:21:25 +01:00
/**
* default timelock for entries , that are opened by another user
*
* @ var time in secomdas
*/
var $locktime_default = 1 ;
2004-10-08 00:11:18 +02:00
/**
* Constructor
*/
function uiforms ()
{
2005-11-09 00:15:14 +01:00
$this -> uical ( true ); // call the parent's constructor
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
$this -> link =& $this -> bo -> link ;
2008-05-08 00:12:25 +02:00
2006-06-03 01:36:43 +02:00
for ( $n = 15 ; $n <= 8 * 60 ; $n += ( $n < 60 ? 15 : ( $n < 240 ? 30 : 60 )))
{
$this -> durations [ $n * 60 ] = sprintf ( '%d:%02d' , $n / 60 , $n % 60 );
}
2005-11-09 00:15:14 +01:00
}
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
/**
* Create a default event ( adding a new event ) by evaluating certain _GET vars
*
* @ return array event - array
2008-05-08 00:12:25 +02:00
*/
2005-11-09 00:15:14 +01:00
function & default_add_event ()
{
2005-11-23 15:21:20 +01:00
$extra_participants = $_GET [ 'participants' ] ? explode ( ',' , $_GET [ 'participants' ]) : array ();
2006-03-03 19:56:01 +01:00
if ( isset ( $_GET [ 'owner' ]))
{
$owner = $_GET [ 'owner' ];
}
// dont set the planner start group as owner/participants if called from planner
elseif ( $this -> view != 'planner' || $this -> owner != $this -> cal_prefs [ 'planner_start_with_group' ])
{
$owner = $this -> owner ;
}
2008-05-08 00:12:25 +02:00
if ( ! $owner || ! is_numeric ( $owner ) || $GLOBALS [ 'egw' ] -> accounts -> get_type ( $owner ) != 'u' ||
2005-11-23 15:21:20 +01:00
! $this -> bo -> check_perms ( EGW_ACL_ADD , 0 , $owner ))
{
if ( $owner ) // make an owner who is no user or we have no add-rights a participant
{
2005-11-23 22:39:37 +01:00
$extra_participants += explode ( ',' , $owner );
2005-11-23 15:21:20 +01:00
}
$owner = $this -> user ;
}
2006-03-03 19:56:01 +01:00
//echo "<p>this->owner=$this->owner, _GET[owner]=$_GET[owner], user=$this->user => owner=$owner, extra_participants=".implode(',',$extra_participants)."</p>\n";
2005-11-09 00:15:14 +01:00
2005-11-23 15:21:20 +01:00
// by default include the owner as participant (the user can remove him)
$extra_participants [] = $owner ;
2005-11-09 00:15:14 +01:00
$start = $this -> bo -> date2ts ( array (
'full' => isset ( $_GET [ 'date' ]) && ( int ) $_GET [ 'date' ] ? ( int ) $_GET [ 'date' ] : $this -> date ,
'hour' => ( int ) ( isset ( $_GET [ 'hour' ]) && ( int ) $_GET [ 'hour' ] ? $_GET [ 'hour' ] : $this -> bo -> cal_prefs [ 'workdaystarts' ]),
'minute' => ( int ) $_GET [ 'minute' ],
));
//echo "<p>_GET[date]=$_GET[date], _GET[hour]=$_GET[hour], _GET[minute]=$_GET[minute], this->date=$this->date ==> start=$start=".date('Y-m-d H:i',$start)."</p>\n";
2008-05-08 00:12:25 +02:00
2005-11-23 15:21:20 +01:00
$participant_types [ 'u' ] = $participant_types = $participants = array ();
foreach ( $extra_participants as $uid )
2005-11-09 00:15:14 +01:00
{
2005-11-23 15:21:20 +01:00
if ( isset ( $participants [ $uid ])) continue ; // already included
if ( is_numeric ( $uid ))
2005-11-09 00:15:14 +01:00
{
2005-11-23 15:21:20 +01:00
$participants [ $uid ] = $participant_types [ 'u' ][ $uid ] = $uid == $this -> user ? 'A' : 'U' ;
}
2008-05-08 17:02:35 +02:00
elseif ( is_array ( $this -> bo -> resources [ $uid [ 0 ]]))
2005-11-23 15:21:20 +01:00
{
2008-05-08 17:02:35 +02:00
$res_data = $this -> bo -> resources [ $uid [ 0 ]];
2005-11-23 15:21:20 +01:00
list ( $id , $quantity ) = explode ( ':' , substr ( $uid , 1 ));
2008-05-08 17:02:35 +02:00
$participants [ $uid ] = $participant_types [ $uid [ 0 ]][ $id ] = ( $res_data [ 'new_status' ] ? ExecMethod ( $res_data [ 'new_status' ], $id ) : 'U' ) .
2005-11-23 15:21:20 +01:00
(( int ) $quantity > 1 ? ( int ) $quantity : '' );
// if new_status == 'x', resource is not bookable
2008-05-08 17:02:35 +02:00
if ( strpos ( $participant_types [ $uid [ 0 ]][ $id ], 'x' ) !== false )
2005-11-09 00:15:14 +01:00
{
2008-05-08 17:02:35 +02:00
unset ( $participant_types [ $uid [ 0 ]][ $id ]);
2005-11-23 15:21:20 +01:00
unset ( $participants [ $uid ]);
2005-11-09 00:15:14 +01:00
}
}
}
return array (
'participant_types' => $participant_types ,
'participants' => $participants ,
'owner' => $owner ,
'start' => $start ,
'end' => $start + ( int ) $this -> bo -> cal_prefs [ 'defaultlength' ] * 60 ,
'priority' => 2 , // normal
'public' => $this -> cal_prefs [ 'default_private' ] ? 0 : 1 ,
'alarm' => array (),
);
}
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
/**
* Process the edited event and evtl . call edit to redisplay it
*
* @ param array $content posted eTemplate content
*/
function process_edit ( $content )
{
2008-01-16 15:49:02 +01:00
$referer = $this -> view_menuaction ;
2005-11-09 00:15:14 +01:00
list ( $button ) = @ each ( $content [ 'button' ]);
2006-12-22 20:51:56 +01:00
if ( ! $button && $content [ 'action' ]) $button = $content [ 'action' ]; // action selectbox
unset ( $content [ 'button' ]); unset ( $content [ 'action' ]);
2005-11-09 00:15:14 +01:00
2006-12-22 20:51:56 +01:00
$view = $content [ 'view' ];
if ( $button == 'ical' )
2005-11-09 00:15:14 +01:00
{
$msg = $this -> export ( $content [ 'id' ], true );
}
// delete a recur-exception
if ( $content [ 'recur_exception' ][ 'delete_exception' ])
{
list ( $date ) = each ( $content [ 'recur_exception' ][ 'delete_exception' ]);
unset ( $content [ 'recur_exception' ][ 'delete_exception' ]);
if (( $key = array_search ( $date , $content [ 'recur_exception' ])) !== false )
{
unset ( $content [ 'recur_exception' ][ $key ]);
$content [ 'recur_exception' ] = array_values ( $content [ 'recur_exception' ]);
}
}
// delete an alarm
if ( $content [ 'alarm' ][ 'delete_alarm' ])
{
list ( $id ) = each ( $content [ 'alarm' ][ 'delete_alarm' ]);
//echo "delete alarm $id"; _debug_array($content['alarm']['delete_alarm']);
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
if ( $content [ 'id' ])
{
if ( $this -> bo -> delete_alarm ( $id ))
{
$msg = lang ( 'Alarm deleted' );
unset ( $content [ 'alarm' ][ $id ]);
}
else
{
2008-05-08 00:12:25 +02:00
$msg = lang ( 'Permission denied' );
2005-11-09 00:15:14 +01:00
}
}
else
{
unset ( $content [ 'alarm' ][ $id ]);
}
}
2006-06-03 01:36:43 +02:00
if ( $content [ 'duration' ])
{
$content [ 'end' ] = $content [ 'start' ] + $content [ 'duration' ];
}
2005-11-09 00:15:14 +01:00
$event = $content ;
unset ( $event [ 'new_alarm' ]);
unset ( $event [ 'alarm' ][ 'delete_alarm' ]);
2006-09-28 06:32:38 +02:00
unset ( $event [ 'duration' ]);
2005-11-09 00:15:14 +01:00
2006-03-07 21:30:29 +01:00
if ( in_array ( $button , array ( 'ignore' , 'freetime' , 'reedit' ))) // called from conflict display
{
// no conversation necessary, event is already in the right format
}
2006-12-22 20:51:56 +01:00
elseif ( isset ( $content [ 'participants' ])) // convert content => event
2005-11-09 00:15:14 +01:00
{
if ( $content [ 'whole_day' ])
{
$event [ 'start' ] = $this -> bo -> date2array ( $event [ 'start' ]);
$event [ 'start' ][ 'hour' ] = $event [ 'start' ][ 'minute' ] = 0 ; unset ( $event [ 'start' ][ 'raw' ]);
$event [ 'start' ] = $this -> bo -> date2ts ( $event [ 'start' ]);
$event [ 'end' ] = $this -> bo -> date2array ( $event [ 'end' ]);
$event [ 'end' ][ 'hour' ] = 23 ; $event [ 'end' ][ 'minute' ] = $event [ 'end' ][ 'second' ] = 59 ; unset ( $event [ 'end' ][ 'raw' ]);
$event [ 'end' ] = $this -> bo -> date2ts ( $event [ 'end' ]);
}
// some checks for recurances, if you give a date, make it a weekly repeating event and visa versa
if ( $event [ 'recur_type' ] == MCAL_RECUR_NONE && $event [ 'recur_data' ]) $event [ 'recur_type' ] = MCAL_RECUR_WEEKLY ;
if ( $event [ 'recur_type' ] == MCAL_RECUR_WEEKLY && ! $event [ 'recur_data' ])
{
$event [ 'recur_data' ] = 1 << ( int ) date ( 'w' , $event [ 'start' ]);
}
2006-12-22 20:51:56 +01:00
$event [ 'participants' ] = $event [ 'participant_types' ] = array ();
foreach ( $content [ 'participants' ] as $key => $data )
2005-11-09 00:15:14 +01:00
{
2006-12-22 20:51:56 +01:00
switch ( $key )
2005-11-09 00:15:14 +01:00
{
2008-05-08 17:02:35 +02:00
case 'delete' : // handled in default
case 'quantity' : // handled in new_resource
case 'cal_resources' :
break ;
2006-12-22 20:51:56 +01:00
case 'add' :
2008-05-08 17:02:35 +02:00
// email or rfc822 addresse (eg. "Ralf Becker <ralf@domain.com>") in the search field
// ToDo: get eTemplate to return that field
if (( $email = $_POST [ 'exec' ][ 'participants' ][ 'resource' ][ 'query' ]) &&
( preg_match ( '/^(.*<)?([a-z0-9_.@-]{8,})>?$/i' , $email , $matches )))
{
// check if email belongs to account or contact --> prefer them over just emails
if (( $data = $GLOBALS [ 'egw' ] -> accounts -> name2id ( $matches [ 2 ], 'account_email' )))
{
$event [ 'participants' ][ $data ] = $event [ 'participant_types' ][ 'u' ][ $data ] = 'U' ;
}
elseif (( list ( $data ) = ExecMethod2 ( 'addressbook.bocontacts.search' , array (
'email' => $matches [ 2 ],
'email_home' => $matches [ 2 ],
), true , '' , '' , '' , false , 'OR' )))
{
$event [ 'participants' ][ 'c' . $data [ 'id' ]] = $event [ 'participant_types' ][ 'c' ][ $data [ 'id' ]] = 'U' ;
}
else
{
$event [ 'participants' ][ 'e' . $email ] = $event [ 'participant_types' ][ 'e' ][ $email ] = 'U' ;
}
}
elseif ( ! $content [ 'participants' ][ 'account' ] && ! $content [ 'participants' ][ 'resource' ])
2006-12-22 20:51:56 +01:00
{
$msg = lang ( 'You need to select an account, contact or resource first!' );
}
2007-05-07 20:59:05 +02:00
break ;
2006-12-22 20:51:56 +01:00
case 'resource' :
list ( $app , $id ) = explode ( ':' , $data );
foreach ( $this -> bo -> resources as $type => $data ) if ( $data [ 'app' ] == $app ) break ;
$uid = $this -> bo -> resources [ $type ][ 'app' ] == $app ? $type . $id : false ;
// check if new entry is no contact or no account
if ( $app != 'addressbook' || ! ( $data = $GLOBALS [ 'egw' ] -> accounts -> name2id ( $id , 'person_id' )))
{
$status = isset ( $this -> bo -> resources [ $type ][ 'new_status' ]) ? ExecMethod ( $this -> bo -> resources [ $type ][ 'new_status' ], $id ) : 'U' ;
$quantity = $content [ 'participants' ][ 'quantity' ] ? $content [ 'participants' ][ 'quantity' ] : 1 ;
2008-05-08 00:12:25 +02:00
if ( $uid ) $event [ 'participants' ][ $uid ] = $event [ 'participant_types' ][ $type ][ $id ] =
2007-05-08 09:54:42 +02:00
$status . (( int ) $quantity > 1 ? ( int ) $quantity : '' );
2006-12-22 20:51:56 +01:00
break ;
}
// fall-through for accounts entered as contact
case 'account' :
2007-05-07 20:59:05 +02:00
foreach ( is_array ( $data ) ? $data : explode ( ',' , $data ) as $uid )
{
2008-05-08 00:12:25 +02:00
if ( $uid ) $event [ 'participants' ][ $uid ] = $event [ 'participant_types' ][ 'u' ][ $uid ] =
2007-05-07 20:59:05 +02:00
$uid == $this -> bo -> user ? 'A' : 'U' ;
}
2006-12-22 20:51:56 +01:00
break ;
2007-05-07 20:59:05 +02:00
2006-12-22 20:51:56 +01:00
default : // existing participant row
2007-02-26 23:09:17 +01:00
foreach ( array ( 'uid' , 'status' , 'status_recurrence' , 'quantity' ) as $name )
2006-12-22 20:51:56 +01:00
{
$$name = $data [ $name ];
}
if ( $content [ 'participants' ][ 'delete' ][ $uid ])
2005-11-09 00:15:14 +01:00
{
2006-12-22 20:51:56 +01:00
$uid = false ; // entry has been deleted
}
else
{
if ( is_numeric ( $uid ))
2005-11-09 00:15:14 +01:00
{
2008-05-08 00:12:25 +02:00
$id = $uid ;
2006-12-22 20:51:56 +01:00
$type = 'u' ;
}
else
{
2008-05-08 00:12:25 +02:00
$id = substr ( $uid , 1 );
2008-05-08 17:02:35 +02:00
$type = $uid [ 0 ];
2006-12-22 20:51:56 +01:00
}
if ( $data [ 'old_status' ] != $status )
{
2007-05-07 20:59:05 +02:00
if ( $this -> bo -> set_status ( $event [ 'id' ], $uid , $status , $event [ 'recur_type' ] != MCAL_RECUR_NONE && ! $status_recurrence ? $content [ 'participants' ][ 'status_date' ] : 0 ))
2006-12-22 20:51:56 +01:00
{
// refreshing the calendar-view with the changed participant-status
$msg = lang ( 'Status changed' );
if ( ! $preserv [ 'no_popup' ])
{
$js = 'opener.location.href=\'' . addslashes ( $GLOBALS [ 'egw' ] -> link ( '/index.php' , array (
2008-01-16 15:49:02 +01:00
'menuaction' => $referer ,
2006-12-22 20:51:56 +01:00
'msg' => $msg ,
))) . '\';' ;
}
}
2005-11-09 00:15:14 +01:00
}
2007-05-07 20:59:05 +02:00
if ( $uid && $status != 'G' )
{
2008-05-08 00:12:25 +02:00
$event [ 'participants' ][ $uid ] = $event [ 'participant_types' ][ $type ][ $id ] =
2007-05-07 20:59:05 +02:00
$status . (( int ) $quantity > 1 ? ( int ) $quantity : '' );
}
2005-11-09 00:15:14 +01:00
}
2006-12-22 20:51:56 +01:00
break ;
2005-11-09 00:15:14 +01:00
}
}
}
$preserv = array (
'view' => $view ,
'edit_single' => $content [ 'edit_single' ],
2006-12-22 20:51:56 +01:00
'actual_date' => $content [ 'actual_date' ],
2008-01-16 15:49:02 +01:00
'referer' => $referer ,
2005-11-09 00:15:14 +01:00
'no_popup' => $content [ 'no_popup' ],
2006-12-22 20:51:56 +01:00
$this -> tabs => $content [ $this -> tabs ],
2005-11-09 00:15:14 +01:00
);
2008-01-15 09:21:25 +01:00
$noerror = true ;
2006-12-22 20:51:56 +01:00
switch (( string ) $button )
2005-11-09 00:15:14 +01:00
{
2005-11-23 22:39:37 +01:00
case 'exception' : // create an exception in a recuring event
2008-02-07 03:38:55 +01:00
$msg = $this -> _create_exception ( $event , $preserv );
2005-11-23 22:39:37 +01:00
break ;
2005-11-09 00:15:14 +01:00
case 'copy' : // create new event with copied content, some content need to be unset to make a "new" event
unset ( $event [ 'id' ]);
unset ( $event [ 'uid' ]);
unset ( $event [ 'alarm' ]);
unset ( $event [ 'reference' ]);
unset ( $event [ 'recur_exception' ]);
unset ( $event [ 'edit_single' ]); // in case it has been set
unset ( $event [ 'modified' ]);
unset ( $event [ 'modifier' ]);
$event [ 'owner' ] = ! ( int ) $this -> owner || ! $this -> bo -> check_perms ( EGW_ACL_ADD , 0 , $this -> owner ) ? $this -> user : $this -> owner ;
$preserv [ 'view' ] = $preserv [ 'edit_single' ] = false ;
$msg = lang ( 'Event copied - the copy can now be edited' );
$event [ 'title' ] = lang ( 'Copy of:' ) . ' ' . $event [ 'title' ];
break ;
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
case 'ignore' :
$ignore_conflicts = true ;
$button = $event [ 'button_was' ]; // save or apply
unset ( $event [ 'button_was' ]);
// fall through
2006-12-22 20:51:56 +01:00
case 'mail' :
2005-11-09 00:15:14 +01:00
case 'save' :
case 'apply' :
2008-05-08 00:12:25 +02:00
if ( $event [ 'id' ] && ! $this -> bo -> check_perms ( EGW_ACL_EDIT , $event ))
{
2006-12-22 20:51:56 +01:00
if ( $button == 'mail' ) // just mail without edit-rights is ok
{
$js = $this -> custom_mail ( $event , false );
break ;
}
2008-05-08 00:12:25 +02:00
$msg = lang ( 'Permission denied' );
break ;
2005-11-09 00:15:14 +01:00
}
2005-11-23 15:21:20 +01:00
if ( $event [ 'start' ] > $event [ 'end' ])
{
$msg = lang ( 'Error: Starttime has to be before the endtime !!!' );
$button = '' ;
break ;
}
if ( ! $event [ 'participants' ])
{
$msg = lang ( 'Error: no participants selected !!!' );
$button = '' ;
break ;
}
2005-11-09 00:15:14 +01:00
if ( $content [ 'edit_single' ]) // we edited a single event from a series
{
$event [ 'reference' ] = $event [ 'id' ];
unset ( $event [ 'id' ]);
unset ( $event [ 'uid' ]);
$conflicts = $this -> bo -> update ( $event , $ignore_conflicts );
if ( ! is_array ( $conflicts ) && $conflicts )
{
// now we need to add the original start as recur-execption to the series
$recur_event = $this -> bo -> read ( $event [ 'reference' ]);
$recur_event [ 'recur_exception' ][] = $content [ 'edit_single' ];
2005-11-23 22:39:37 +01:00
unset ( $recur_event [ 'start' ]); unset ( $recur_event [ 'end' ]); // no update necessary
2005-11-09 00:15:14 +01:00
$this -> bo -> update ( $recur_event , true ); // no conflict check here
unset ( $recur_event );
unset ( $event [ 'edit_single' ]); // if we further edit it, it's just a single event
2005-11-23 22:39:37 +01:00
unset ( $preserv [ 'edit_single' ]);
2005-11-09 00:15:14 +01:00
}
else // conflict or error, we need to reset everything to the state befor we tried to save it
{
$event [ 'id' ] = $event [ 'reference' ];
unset ( $event [ 'reference' ]);
$event [ 'uid' ] = $content [ 'uid' ];
}
}
else // we edited a non-reccuring event or the whole series
{
$conflicts = $this -> bo -> update ( $event , $ignore_conflicts );
unset ( $event [ 'ignore' ]);
}
if ( is_array ( $conflicts ))
{
$event [ 'button_was' ] = $button ; // remember for ignore
return $this -> conflicts ( $event , $conflicts , $preserv );
}
2008-05-08 00:12:25 +02:00
elseif ( $conflicts === 0 )
2008-01-15 09:21:25 +01:00
{
$msg .= ( $msg ? ', ' : '' ) . lang ( 'Error: the entry has been updated since you opened it for editing!' ) . '<br />' .
lang ( 'Copy your changes to the clipboard, %1reload the entry%2 and merge them.' , '<a href="' .
htmlspecialchars ( $GLOBALS [ 'egw' ] -> link ( '/index.php' , array (
'menuaction' => 'calendar.uiforms.edit' ,
'cal_id' => $content [ 'id' ],
2008-01-16 15:49:02 +01:00
'referer' => $referer ,
2008-01-15 09:21:25 +01:00
))) . '">' , '</a>' );
$noerror = false ;
2008-05-08 00:12:25 +02:00
2008-01-15 09:21:25 +01:00
}
2008-05-08 00:12:25 +02:00
elseif ( $conflicts > 0 )
2005-11-09 00:15:14 +01:00
{
$msg .= ( $msg ? ', ' : '' ) . lang ( 'Event saved' );
// writing links for new entry, existing ones are handled by the widget itself
2008-05-08 00:12:25 +02:00
if ( ! $content [ 'id' ] && is_array ( $content [ 'link_to' ][ 'to_id' ]))
2005-11-09 00:15:14 +01:00
{
2008-03-26 13:23:26 +01:00
egw_link :: link ( 'calendar' , $event [ 'id' ], $content [ 'link_to' ][ 'to_id' ]);
2005-11-09 00:15:14 +01:00
}
$js = 'opener.location.href=\'' . addslashes ( $GLOBALS [ 'egw' ] -> link ( '/index.php' , array (
2008-01-16 15:49:02 +01:00
'menuaction' => $referer ,
2005-11-09 00:15:14 +01:00
'msg' => $msg ,
))) . '\';' ;
2006-12-22 20:51:56 +01:00
if ( $button == 'mail' )
2005-11-09 00:15:14 +01:00
{
$js = $this -> custom_mail ( $event , ! $content [ 'id' ]) . " \n " . $js ; // first open the new window and then update the view
}
}
else
{
$msg = lang ( 'Error: saving the event !!!' );
}
break ;
2008-05-08 00:12:25 +02:00
2007-01-15 23:58:32 +01:00
case 'cancel' :
if ( $content [ 'cancel_needs_refresh' ])
{
$js = 'opener.location.href=\'' . addslashes ( $GLOBALS [ 'egw' ] -> link ( '/index.php' , array (
2008-01-16 15:49:02 +01:00
'menuaction' => $referer ,
2007-01-15 23:58:32 +01:00
'msg' => $msg ,
))) . '\';' ;
}
break ;
2005-11-09 00:15:14 +01:00
case 'delete' :
2005-11-23 22:39:37 +01:00
if ( $this -> bo -> delete ( $event [ 'id' ],( int ) $content [ 'edit_single' ]))
2005-11-09 00:15:14 +01:00
{
2005-11-23 22:39:37 +01:00
$msg = lang ( 'Event deleted' );
$js = 'opener.location.href=\'' . addslashes ( $GLOBALS [ 'egw' ] -> link ( '/index.php' , array (
2008-01-16 15:49:02 +01:00
'menuaction' => $referer ,
2005-11-23 22:39:37 +01:00
'msg' => $msg ,
))) . '\';' ;
2005-11-09 00:15:14 +01:00
}
break ;
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
case 'freetime' :
// the "click" has to be in onload, to make sure the button is already created
$GLOBALS [ 'egw' ] -> js -> set_onload ( " document.getElementsByName('exec[freetime]')[0].click(); " );
break ;
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
case 'add_alarm' :
if ( $this -> bo -> check_perms ( EGW_ACL_EDIT , ! $content [ 'new_alarm' ][ 'owner' ] ? $event : 0 , $content [ 'new_alarm' ][ 'owner' ]))
{
$offset = DAY_s * $content [ 'new_alarm' ][ 'days' ] + HOUR_s * $content [ 'new_alarm' ][ 'hours' ] + 60 * $content [ 'new_alarm' ][ 'mins' ];
$alarm = array (
'offset' => $offset ,
2007-05-07 13:26:31 +02:00
'time' => ( $content [ 'actual_date' ] ? $content [ 'actual_date' ] : $content [ 'start' ]) - $offset ,
2005-11-09 00:15:14 +01:00
'all' => ! $content [ 'new_alarm' ][ 'owner' ],
'owner' => $content [ 'new_alarm' ][ 'owner' ] ? $content [ 'new_alarm' ][ 'owner' ] : $this -> user ,
);
if ( $alarm [ 'time' ] < $this -> bo -> now_su )
{
$msg = lang ( " Can't add alarms in the past !!! " );
}
elseif ( $event [ 'id' ]) // save the alarm immediatly
{
if (( $alarm_id = $this -> bo -> save_alarm ( $event [ 'id' ], $alarm )))
{
$alarm [ 'id' ] = $alarm_id ;
$event [ 'alarm' ][ $alarm_id ] = $alarm ;
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
$msg = lang ( 'Alarm added' );
}
else
{
$msg = lang ( 'Error adding the alarm' );
}
}
else
{
for ( $alarm [ 'id' ] = 1 ; isset ( $event [ 'alarm' ][ $alarm [ 'id' ]]); $alarm [ 'id' ] ++ ); // get a temporary non-conflicting, numeric id
$event [ 'alarm' ][ $alarm [ 'id' ]] = $alarm ;
}
}
else
{
$msg = lang ( 'Permission denied' );
}
break ;
}
2008-01-15 09:21:25 +01:00
if ( in_array ( $button , array ( 'cancel' , 'save' , 'delete' )) && $noerror )
2005-11-09 00:15:14 +01:00
{
2008-05-08 00:12:25 +02:00
if ( $content [ 'lock_token' ]) // remove an existing lock
{
egw_vfs :: unlock ( egw_vfs :: app_entry_lock_path ( 'calendar' , $content [ 'id' ]), $content [ 'lock_token' ], false );
}
2005-11-09 00:15:14 +01:00
if ( $content [ 'no_popup' ])
{
$GLOBALS [ 'egw' ] -> redirect_link ( '/index.php' , array (
2008-01-16 15:49:02 +01:00
'menuaction' => $referer ,
2005-11-09 00:15:14 +01:00
'msg' => $msg ,
));
}
$js .= 'window.close();' ;
echo " <html><body onload= \" $js\ " ></ body ></ html > \n " ;
$GLOBALS [ 'egw' ] -> common -> egw_exit ();
}
2005-11-14 20:56:51 +01:00
return $this -> edit ( $event , $preserv , $msg , $js , $event [ 'id' ] ? $event [ 'id' ] : $content [ 'link_to' ][ 'to_id' ]);
2005-11-09 00:15:14 +01:00
}
2008-05-08 00:12:25 +02:00
2008-02-07 03:38:55 +01:00
/**
* Create an exception from the clicked event
2008-05-08 00:12:25 +02:00
*
2008-02-07 03:38:55 +01:00
* It ' s not stored to the DB unless the user saves it !
*
* @ param array & $event
* @ param array & $preserv
* @ return string message that exception was created
*/
function _create_exception ( & $event , & $preserv )
{
$event [ 'end' ] += $preserv [ 'actual_date' ] - $event [ 'start' ];
$event [ 'start' ] = $preserv [ 'edit_single' ] = $preserv [ 'actual_date' ];
$event [ 'recur_type' ] = MCAL_RECUR_NONE ;
foreach ( array ( 'recur_enddate' , 'recur_interval' , 'recur_exception' , 'recur_data' ) as $name )
{
unset ( $event [ $name ]);
}
return lang ( 'Exception created - you can now edit or delete it' );
}
2005-11-09 00:15:14 +01:00
/**
* return javascript to open felamimail compose window with preset content to mail all participants
*
* @ param array $event
* @ param boolean $added
* @ return string javascript window . open command
*/
function custom_mail ( $event , $added )
{
$to = array ();
2008-05-08 17:02:35 +02:00
2005-11-09 00:15:14 +01:00
foreach ( $event [ 'participants' ] as $uid => $status )
{
2006-12-22 20:51:56 +01:00
if ( $status == 'R' || $uid == $this -> user ) continue ;
2008-05-08 00:12:25 +02:00
2006-12-22 20:51:56 +01:00
if ( is_numeric ( $uid ) && $GLOBALS [ 'egw' ] -> accounts -> get_type ( $uid ) == 'u' )
2005-11-09 00:15:14 +01:00
{
2007-12-11 08:13:08 +01:00
if ( ! ( $email = $GLOBALS [ 'egw' ] -> accounts -> id2name ( $uid , 'account_email' ))) continue ;
2005-11-09 00:15:14 +01:00
$GLOBALS [ 'egw' ] -> accounts -> get_account_name ( $uid , $lid , $firstname , $lastname );
2008-05-08 00:12:25 +02:00
2007-12-11 08:13:08 +01:00
$to [] = $firstname . ' ' . $lastname . ' <' . $email . '>' ;
}
elseif ( $uid < 0 )
{
foreach ( $GLOBALS [ 'egw' ] -> accounts -> members ( $uid , true ) as $uid )
{
if ( ! ( $email = $GLOBALS [ 'egw' ] -> accounts -> id2name ( $uid , 'account_email' ))) continue ;
$GLOBALS [ 'egw' ] -> accounts -> get_account_name ( $uid , $lid , $firstname , $lastname );
2008-05-08 00:12:25 +02:00
2007-12-11 08:13:08 +01:00
$to [] = $firstname . ' ' . $lastname . ' <' . $email . '>' ;
}
2005-11-09 00:15:14 +01:00
}
2008-05-08 17:02:35 +02:00
elseif (( $info = $this -> bo -> resource_info ( $uid )))
2006-12-22 20:51:56 +01:00
{
2008-05-08 17:02:35 +02:00
$to [] = $info [ 'email' ];
2006-12-22 20:51:56 +01:00
}
2005-11-09 00:15:14 +01:00
}
list ( $subject , $body ) = $this -> bo -> get_update_message ( $event , $added ? MSG_ADDED : MSG_MODIFIED ); // update-message is in TZ of the user
$boical =& CreateObject ( 'calendar.boical' );
2005-11-18 16:00:02 +01:00
$ics = $boical -> exportVCal ( array ( $event ), '2.0' , 'request' );
2005-11-09 00:15:14 +01:00
$ics_file = tempnam ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'temp_dir' ], 'ics' );
if (( $f = fopen ( $ics_file , 'w' )))
{
fwrite ( $f , $ics );
fclose ( $f );
}
$vars = array (
'menuaction' => 'felamimail.uicompose.compose' ,
2007-05-16 09:30:58 +02:00
'preset[to]' => $to ,
2005-11-09 00:15:14 +01:00
'preset[subject]' => $subject ,
'preset[body]' => $body ,
'preset[name]' => 'event.ics' ,
'preset[file]' => $ics_file ,
'preset[type]' => 'text/calendar; method=request' ,
'preset[size]' => filesize ( $ics_file ),
);
2006-04-26 23:38:47 +02:00
return " window.open(' " . $GLOBALS [ 'egw' ] -> link ( '/index.php' , $vars ) . " ','_blank','width=700,height=700,scrollbars=yes,status=no'); " ;
2005-11-09 00:15:14 +01:00
}
/**
* Edit a calendar event
*
* @ param array $event = null Event to edit , if not $_GET [ 'cal_id' ] contains the event - id
* @ param array $perserv = null following keys :
* view boolean view - mode , if no edit - access we automatic fallback to view - mode
* referer string menuaction of the referer
* no_popup boolean use a popup or not
* edit_single int timestamp of single event edited , unset / null otherwise
* @ param string $msg = '' msg to display
* @ param string $js = 'window.focus();' javascript to include in the page
* @ param mixed $link_to_id = '' $content from or for the link - widget
*/
function edit ( $event = null , $preserv = null , $msg = '' , $js = 'window.focus();' , $link_to_id = '' )
{
$etpl =& CreateObject ( 'etemplate.etemplate' , 'calendar.edit' );
2005-11-23 15:21:20 +01:00
$sel_options = array (
2006-12-22 20:51:56 +01:00
'recur_type' => & $this -> bo -> recur_types ,
'status' => $this -> bo -> verbose_status ,
'duration' => $this -> durations ,
'action' => array (
'copy' => array ( 'label' => 'Copy' , 'title' => 'Copy this event' ),
'ical' => array ( 'label' => 'Export' , 'title' => 'Download this event as iCal' ),
2007-05-07 11:35:05 +02:00
'mail' => array ( 'label' => 'Mail all participants' , 'title' => 'compose a mail to all participants after the event is saved' ),
2006-12-22 20:51:56 +01:00
),
2007-02-26 23:09:17 +01:00
'status_recurrence' => array ( '' => 'for this event' , 'A' => 'for all future events' ),
2005-11-23 15:21:20 +01:00
);
2006-12-22 20:51:56 +01:00
unset ( $sel_options [ 'status' ][ 'G' ]);
2005-11-09 00:15:14 +01:00
if ( ! is_array ( $event ))
{
$preserv = array (
'no_popup' => isset ( $_GET [ 'no_popup' ]),
'referer' => preg_match ( '/menuaction=([^&]+)/' , $_SERVER [ 'HTTP_REFERER' ], $matches ) ? $matches [ 1 ] : $this -> view_menuaction ,
);
$cal_id = ( int ) $_GET [ 'cal_id' ];
2006-12-22 20:51:56 +01:00
2005-11-09 00:15:14 +01:00
if ( ! $cal_id || $cal_id && ! ( $event = $this -> bo -> read ( $cal_id , $_GET [ 'date' ])) || ! $this -> bo -> check_perms ( EGW_ACL_READ , $event ))
{
2008-05-08 00:12:25 +02:00
if ( $cal_id )
2005-11-09 00:15:14 +01:00
{
if ( ! $preserv [ 'no_popup' ])
{
$js = " alert(' " . lang ( 'Permission denied' ) . " '); window.close(); " ;
}
else
{
2006-12-22 20:51:56 +01:00
$GLOBALS [ 'egw' ] -> framework -> render ( '<p class="redItalic" align="center">' . lang ( 'Permission denied' ) . " </p> \n " , null , true );
2005-11-09 00:15:14 +01:00
$GLOBALS [ 'egw' ] -> common -> egw_exit ();
}
}
$event =& $this -> default_add_event ();
}
2006-12-22 20:51:56 +01:00
else
{
$preserv [ 'actual_date' ] = $event [ 'start' ]; // remember the date clicked
if ( $event [ 'recur_type' ] != MCAL_RECUR_NONE )
{
2007-02-26 23:09:17 +01:00
$participants = array ( 'participants' => $event [ 'participants' ], 'participant_types' => $event [ 'participant_types' ]); // preserv participants of this event
$event = array_merge ( $this -> bo -> read ( $cal_id , 0 , true ), $participants ); // recuring event --> read the series + concatenate with participants of the selected recurrence
2008-02-07 03:38:55 +01:00
// check if we should create an exception
if ( $_GET [ 'exception' ])
{
$msg = $this -> _create_exception ( $event , $preserv );
}
2006-12-22 20:51:56 +01:00
}
}
2007-01-15 23:58:32 +01:00
// set new start and end if given by $_GET
if ( isset ( $_GET [ 'start' ])) { $event [ 'start' ] = $_GET [ 'start' ]; }
if ( isset ( $_GET [ 'end' ])) { $event [ 'end' ] = $_GET [ 'end' ]; }
2005-11-09 00:15:14 +01:00
// check if the event is the whole day
$start = $this -> bo -> date2array ( $event [ 'start' ]);
$end = $this -> bo -> date2array ( $event [ 'end' ]);
$event [ 'whole_day' ] = ! $start [ 'hour' ] && ! $start [ 'minute' ] && $end [ 'hour' ] == 23 && $end [ 'minute' ] == 59 ;
$link_to_id = $event [ 'id' ];
if ( ! $add_link && ! $event [ 'id' ] && isset ( $_GET [ 'link_app' ]) && isset ( $_GET [ 'link_id' ]) &&
preg_match ( '/^[a-z_0-9-]+:[:a-z_0-9-]+$/i' , $_GET [ 'link_app' ] . ':' . $_GET [ 'link_id' ])) // gard against XSS
{
2008-03-26 13:23:26 +01:00
egw_link :: link ( 'calendar' , $link_to_id , $_GET [ 'link_app' ], $_GET [ 'link_id' ]);
2005-11-09 00:15:14 +01:00
}
}
$view = $preserv [ 'view' ] = $preserv [ 'view' ] || $event [ 'id' ] && ! $this -> bo -> check_perms ( EGW_ACL_EDIT , $event );
//echo "view=$view, event="; _debug_array($event);
2008-05-08 00:12:25 +02:00
// shared locking of entries to edit
if ( ! $view && ( $locktime = $GLOBALS [ 'egw_info' ][ 'server' ][ 'Lock_Time_Calender' ]) && $event [ 'id' ])
{
$lock_path = egw_vfs :: app_entry_lock_path ( 'calendar' , $event [ 'id' ]);
$lock_owner = 'mailto:' . $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_email' ];
if (( $preserv [ 'lock_token' ] = $content [ 'lock_token' ])) // already locked --> refresh the lock
{
egw_vfs :: lock ( $lock_path , $preserv [ 'lock_token' ], $locktime , $lock_owner , $scope = 'shared' , $type = 'write' , true , false );
}
if (( $lock = egw_vfs :: checkLock ( $lock_path )) && $lock [ 'owner' ] != $lock_owner )
{
$msg .= ' ' . lang ( 'This entry is currently opened by %1!' ,
(( $lock_uid = $GLOBALS [ 'egw' ] -> accounts -> name2id ( substr ( $lock [ 'owner' ], 7 ), 'account_email' )) ?
$GLOBALS [ 'egw' ] -> common -> grab_owner_name ( $lock_uid ) : $lock [ 'owner' ]));
}
elseif ( $lock )
{
$preserv [ 'lock_token' ] = $lock [ 'token' ];
}
elseif ( egw_vfs :: lock ( $lock_path , $preserv [ 'lock_token' ], $locktime , $lock_owner , $scope = 'shared' , $type = 'write' , false , false ))
{
// install ajax handler to unlock the entry again, if the window get's closed by the user
$GLOBALS [ 'egw' ] -> js -> set_onunload ( " xajax_doXMLHTTP('calendar.uiforms.ajax_unlock', $event[id] ,' $preserv[lock_token] '); " );
}
else
{
$msg .= ' ' . lang ( " Can't aquire lock! " ); // eg. an exclusive lock via CalDAV ...
$view = true ;
}
//echo "<p>lock_path=$lock_path, lock_owner=$lock_owner, lock_token=$preserv[lock_token], msg=$msg</p>\n";
}
2005-11-09 00:15:14 +01:00
$content = array_merge ( $event , array (
'link_to' => array (
'to_id' => $link_to_id ,
'to_app' => 'calendar' ,
),
'edit_single' => $preserv [ 'edit_single' ], // need to be in content too, as it is used in the template
2006-12-22 20:51:56 +01:00
$this -> tabs => $preserv [ $this -> tabs ],
2005-11-09 00:15:14 +01:00
'view' => $view ,
2008-05-08 00:12:25 +02:00
'msg' => $msg ,
2005-11-09 00:15:14 +01:00
));
2006-06-03 01:36:43 +02:00
$content [ 'duration' ] = $content [ 'end' ] - $content [ 'start' ];
if ( isset ( $this -> durations [ $content [ 'duration' ]])) $content [ 'end' ] = '' ;
2006-12-22 20:51:56 +01:00
$row = 2 ;
$readonlys = $content [ 'participants' ] = $preserv [ 'participants' ] = array ();
2005-11-09 00:15:14 +01:00
foreach ( $event [ 'participant_types' ] as $type => $participants )
{
$name = 'accounts' ;
if ( isset ( $this -> bo -> resources [ $type ]))
{
$name = $this -> bo -> resources [ $type ][ 'app' ];
}
foreach ( $participants as $id => $status )
{
2006-12-22 20:51:56 +01:00
$uid = $type == 'u' ? $id : $type . $id ;
$preserv [ 'participants' ][ $row ] = $content [ 'participants' ][ $row ] = array (
'app' => $name == 'accounts' ? ( $GLOBALS [ 'egw' ] -> accounts -> get_type ( $id ) == 'g' ? 'Group' : 'User' ) : $name ,
'uid' => $uid ,
2008-05-08 17:02:35 +02:00
'status' => $status [ 0 ],
'old_status' => $status [ 0 ],
2006-12-22 20:51:56 +01:00
'quantity' => substr ( $status , 1 ),
);
$readonlys [ $row . '[quantity]' ] = $type == 'u' || ! isset ( $this -> bo -> resources [ $type ][ 'max_quantity' ]);
2007-02-26 23:09:17 +01:00
$readonlys [ $row . '[status]' ] = $readonlys [ $row . '[status_recurrence]' ] = ! $this -> bo -> check_status_perms ( $uid , $event );
2006-12-22 20:51:56 +01:00
$readonlys [ " delete[ $uid ] " ] = ! $this -> bo -> check_perms ( EGW_ACL_EDIT , $event );
2008-05-08 17:02:35 +02:00
// todo: make the participants available as links with email as title
if ( $name == 'accounts' )
{
$content [ 'participants' ][ $row ++ ][ 'title' ] = $GLOBALS [ 'egw' ] -> common -> grab_owner_name ( $id );
}
elseif (( $info = $this -> bo -> resource_info ( $uid )))
{
$content [ 'participants' ][ $row ++ ][ 'title' ] = $info [ 'name' ] ? $info [ 'name' ] : $info [ 'email' ];
}
else
{
$content [ 'participants' ][ $row ++ ][ 'title' ] = '#' . $uid ;
}
2005-11-23 15:21:20 +01:00
// enumerate group-invitations, so people can accept/reject them
2006-12-22 20:51:56 +01:00
if ( $name == 'accounts' && $GLOBALS [ 'egw' ] -> accounts -> get_type ( $id ) == 'g' &&
( $members = $GLOBALS [ 'egw' ] -> accounts -> members ( $id , true )))
2005-11-23 15:21:20 +01:00
{
2006-12-22 20:51:56 +01:00
$sel_options [ 'status' ][ 'G' ] = lang ( 'Select one' );
foreach ( $members as $member )
2005-11-23 15:21:20 +01:00
{
2006-12-22 20:51:56 +01:00
if ( ! isset ( $participants [ $member ]) && $this -> bo -> check_perms ( EGW_ACL_EDIT , 0 , $member ))
2005-11-23 15:21:20 +01:00
{
2006-12-22 20:51:56 +01:00
$preserv [ 'participants' ][ $row ] = $content [ 'participants' ][ $row ] = array (
'app' => 'Group invitation' ,
'uid' => $member ,
'status' => 'G' ,
);
$readonlys [ $row . '[quantity]' ] = $readonlys [ " delete[ $member ] " ] = true ;
$content [ 'participants' ][ $row ++ ][ 'title' ] = $GLOBALS [ 'egw' ] -> common -> grab_owner_name ( $member );
2005-11-23 15:21:20 +01:00
}
}
}
2005-11-09 00:15:14 +01:00
}
2006-12-22 20:51:56 +01:00
// resouces / apps we shedule, atm. resources and addressbook
$content [ 'participants' ][ 'cal_resources' ] = '' ;
foreach ( $this -> bo -> resources as $data ) $content [ 'participants' ][ 'cal_resources' ] .= ',' . $data [ 'app' ];
2005-11-09 00:15:14 +01:00
}
2006-12-22 20:51:56 +01:00
$content [ 'participants' ][ 'status_date' ] = $preserv [ 'actual_date' ];
2007-02-26 23:09:17 +01:00
$content [ 'participants' ][ 'hide_status_recurrence' ] = $event [ 'recur_type' ] == MCAL_RECUR_NONE ;
2006-12-22 20:51:56 +01:00
$preserv = array_merge ( $preserv , $content );
2005-11-09 00:15:14 +01:00
if ( $event [ 'alarm' ])
{
// makes keys of the alarm-array starting with 1
$content [ 'alarm' ] = array ( false );
foreach ( array_values ( $event [ 'alarm' ]) as $id => $alarm )
{
if ( ! $alarm [ 'all' ] && ! $this -> bo -> check_perms ( EGW_ACL_READALARM , 0 , $alarm [ 'owner' ]))
{
continue ; // no read rights to the calendar of the alarm-owner, dont show the alarm
}
$alarm [ 'all' ] = ( int ) $alarm [ 'all' ];
$days = ( int ) ( $alarm [ 'offset' ] / DAY_s );
$hours = ( int ) (( $alarm [ 'offset' ] % DAY_s ) / HOUR_s );
$minutes = ( int ) (( $alarm [ 'offset' ] % HOUR_s ) / 60 );
$label = array ();
if ( $days ) $label [] = $days . ' ' . lang ( 'days' );
if ( $hours ) $label [] = $hours . ' ' . lang ( 'hours' );
if ( $minutes ) $label [] = $minutes . ' ' . lang ( 'Minutes' );
$alarm [ 'offset' ] = implode ( ', ' , $label );
$content [ 'alarm' ][] = $alarm ;
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
$readonlys [ 'delete_alarm[' . $id . ']' ] = ! $this -> bo -> check_perms ( EGW_ACL_EDIT , $alarm [ 'all' ] ? $event : 0 , $alarm [ 'owner' ]);
}
}
else
{
$content [ 'alarm' ] = false ;
}
$content [ 'msg' ] = $msg ;
if ( $view )
{
foreach ( $event as $key => $val )
{
if ( $key != 'alarm' ) $readonlys [ $key ] = true ;
}
// we need to unset the tab itself, as this would make all content (incl. the change-status selects) readonly
2006-12-22 20:51:56 +01:00
unset ( $readonlys [ $this -> tabs ]);
// participants are handled individual
unset ( $readonlys [ 'participants' ]);
2005-11-09 00:15:14 +01:00
$readonlys [ 'button[save]' ] = $readonlys [ 'button[apply]' ] = $readonlys [ 'freetime' ] = true ;
2005-11-23 22:39:37 +01:00
$readonlys [ 'link_to' ] = $readonlys [ 'customfields' ] = true ;
2006-06-03 01:36:43 +02:00
$readonlys [ 'duration' ] = true ;
2008-05-08 00:12:25 +02:00
2005-11-23 22:39:37 +01:00
if ( $event [ 'recur_type' ] != MCAL_RECUR_NONE )
{
$onclick =& $etpl -> get_cell_attribute ( 'button[delete]' , 'onclick' );
$onclick = str_replace ( 'Delete this event' , 'Delete this series of recuring events' , $onclick );
}
2006-12-22 20:51:56 +01:00
$content [ 'participants' ][ 'no_add' ] = true ;
2005-11-09 00:15:14 +01:00
}
else
{
2006-06-03 01:36:43 +02:00
// We hide the enddate if one of our predefined durations fits
// the call to set_style_by_class has to be in onload, to make sure the function and the element is already created
$GLOBALS [ 'egw' ] -> js -> set_onload ( " set_style_by_class('table','end_hide','visibility',' " . ( $content [ 'duration' ] && isset ( $sel_options [ 'duration' ][ $content [ 'duration' ]]) ? 'hidden' : 'visible' ) . " '); " );
2005-11-09 00:15:14 +01:00
$readonlys [ 'recur_exception' ] = ! count ( $content [ 'recur_exception' ]); // otherwise we get a delete button
}
// disabling the custom fields tab, if there are none
2006-12-22 20:51:56 +01:00
$readonlys [ $this -> tabs ] = array (
2006-08-22 18:50:45 +02:00
'custom' => ! count ( $this -> bo -> config [ 'customfields' ]),
'participants' => $this -> accountsel -> account_selection == 'none' ,
2005-11-09 00:15:14 +01:00
);
2006-12-22 20:51:56 +01:00
if ( ! isset ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'apps' ][ 'felamimail' ])) // no mail without mail-app
{
unset ( $sel_options [ 'action' ][ 'mail' ]);
}
if ( ! $event [ 'id' ]) // no ical export for new (not saved) events
2005-11-09 00:15:14 +01:00
{
2006-12-22 20:51:56 +01:00
$readonlys [ 'action' ] = true ;
2005-11-09 00:15:14 +01:00
}
2006-12-22 20:51:56 +01:00
if ( ! ( $readonlys [ 'button[exception]' ] = ! $this -> bo -> check_perms ( EGW_ACL_EDIT , $event ) || $event [ 'recur_type' ] == MCAL_RECUR_NONE ))
2005-11-09 00:15:14 +01:00
{
2006-12-22 20:51:56 +01:00
$content [ 'exception_label' ] = $this -> bo -> long_date ( $preserv [ 'actual_date' ]);
2005-11-09 00:15:14 +01:00
}
2005-11-23 22:39:37 +01:00
$readonlys [ 'button[delete]' ] = ! $event [ 'id' ] || ! $this -> bo -> check_perms ( EGW_ACL_DELETE , $event );
2007-05-07 11:35:05 +02:00
if ( ! $event [ 'id' ] || $this -> bo -> check_perms ( EGW_ACL_EDIT , $event )) // new event or edit rights to the event ==> allow to add alarm for all users
2005-11-09 00:15:14 +01:00
{
$sel_options [ 'owner' ][ 0 ] = lang ( 'All participants' );
}
2007-05-07 11:35:05 +02:00
if ( isset ( $event [ 'participant_types' ][ 'u' ][ $this -> user ]))
{
$sel_options [ 'owner' ][ $this -> user ] = $this -> bo -> participant_name ( $this -> user );
}
2005-11-09 00:15:14 +01:00
foreach (( array ) $event [ 'participant_types' ][ 'u' ] as $uid => $status )
{
2007-05-07 11:35:05 +02:00
if ( $uid != $this -> user && $status != 'R' && $this -> bo -> check_perms ( EGW_ACL_EDIT , 0 , $uid ))
2005-11-09 00:15:14 +01:00
{
$sel_options [ 'owner' ][ $uid ] = $this -> bo -> participant_name ( $uid );
}
}
$content [ 'no_add_alarm' ] = ! count ( $sel_options [ 'owner' ]); // no rights to set any alarm
if ( ! $event [ 'id' ])
{
2008-05-08 00:12:25 +02:00
$etpl -> set_cell_attribute ( 'button[new_alarm]' , 'type' , 'checkbox' );
2005-11-09 00:15:14 +01:00
}
if ( $preserv [ 'no_popup' ])
{
$etpl -> set_cell_attribute ( 'button[cancel]' , 'onclick' , '' );
}
//echo "content="; _debug_array($content);
//echo "preserv="; _debug_array($preserv);
//echo "readonlys="; _debug_array($readonlys);
//echo "sel_options="; _debug_array($sel_options);
2008-05-08 00:12:25 +02:00
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'app_header' ] = lang ( 'calendar' ) . ' - ' . ( ! $event [ 'id' ] ? lang ( 'Add' ) : ( $view ? lang ( 'View' ) :
2006-12-22 20:51:56 +01:00
( $content [ 'edit_single' ] ? lang ( 'Create exception' ) : ( $content [ 'recur_type' ] ? lang ( 'Edit series' ) : lang ( 'Edit' )))));
2005-11-09 00:15:14 +01:00
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'java_script' ] .= " <script> \n $js\n </script> \n " ;
2008-05-08 00:12:25 +02:00
2007-01-15 23:58:32 +01:00
$content [ 'cancel_needs_refresh' ] = ( bool ) $_GET [ 'cancel_needs_refresh' ];
2008-01-17 16:39:22 +01:00
2007-01-15 23:58:32 +01:00
// non_interactive==true from $_GET calls immediate save action without displaying the edit form
if ( isset ( $_GET [ 'non_interactive' ]) && ( bool ) $_GET [ 'non_interactive' ] === true )
{
unset ( $_GET [ 'non_interactive' ]); // prevent process_exec <--> edit loops
$content [ 'button' ][ 'save' ] = true ;
2007-01-22 19:47:59 +01:00
$this -> process_edit ( array_merge ( $content , $preserv ));
2007-01-15 23:58:32 +01:00
}
else
{
$etpl -> exec ( 'calendar.uiforms.process_edit' , $content , $sel_options , $readonlys , $preserv , $preserv [ 'no_popup' ] ? 0 : 2 );
}
2005-11-09 00:15:14 +01:00
}
2008-05-08 00:12:25 +02:00
/**
* Remove ( shared ) lock via ajax , when edit popup get ' s closed
*
* @ param int $id
* @ param string $token
*/
function ajax_unlock ( $id , $token )
{
$lock_path = egw_vfs :: app_entry_lock_path ( 'calendar' , $id );
$lock_owner = 'mailto:' . $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_email' ];
if (( $lock = egw_vfs :: checkLock ( $lock_path )) && $lock [ 'owner' ] == $lock_owner || $lock [ 'token' ] == $token )
{
egw_vfs :: unlock ( $lock_path , $token , false );
}
$response = new xajaxResponse ();
$response -> addScript ( 'window.close();' );
return $response -> getXML ();
}
2005-11-09 00:15:14 +01:00
/**
* displays a sheduling conflict
*
2008-05-08 00:12:25 +02:00
* @ param array $event
2005-11-09 00:15:14 +01:00
* @ param array $conflicts array with conflicting events , the events are not garantied to be readable by the user !
* @ param array $preserv data to preserv
*/
function conflicts ( $event , $conflicts , $preserv )
{
$etpl =& CreateObject ( 'etemplate.etemplate' , 'calendar.conflicts' );
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
foreach ( $conflicts as $k => $conflict )
{
$is_readable = $this -> bo -> check_perms ( EGW_ACL_READ , $conflict );
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
$conflicts [ $k ] += array (
'icon_participants' => $is_readable ? ( count ( $conflict [ 'participants' ]) > 1 ? 'users' : 'single' ) : 'private' ,
'tooltip_participants' => $is_readable ? implode ( ', ' , $this -> bo -> participants ( $conflict )) : '' ,
'time' => $this -> bo -> long_date ( $conflict [ 'start' ], $conflict [ 'end' ], true ),
'conflicting_participants' => implode ( " , \n " , $this -> bo -> participants ( array (
'participants' => array_intersect_key ( $conflict [ 'participants' ], $event [ 'participants' ]),
2006-03-16 19:03:43 +01:00
), true , true )), // show group invitations too
2005-11-09 00:15:14 +01:00
'icon_recur' => $conflict [ 'recur_type' ] != MCAL_RECUR_NONE ? 'recur' : '' ,
'text_recur' => $conflict [ 'recur_type' ] != MCAL_RECUR_NONE ? lang ( 'Recurring event' ) : ' ' ,
2008-05-08 00:12:25 +02:00
);
2005-11-09 00:15:14 +01:00
}
$content = $event + array (
'conflicts' => array_values ( $conflicts ), // conflicts have id-start as key
);
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'app_header' ] = lang ( 'calendar' ) . ' - ' . lang ( 'Scheduling conflict' );
$etpl -> exec ( 'calendar.uiforms.process_edit' , $content , false , false , array_merge ( $event , $preserv ), $preserv [ 'no_popup' ] ? 0 : 2 );
2004-10-08 00:11:18 +02:00
}
/**
* Freetime search
*
* As the function is called in a popup via javascript , parametes get initialy transfered via the url
2005-11-09 00:15:14 +01:00
* @ param array $content = null array with parameters or false ( default ) to use the get - params
* @ param string start [ str ] start - date
* @ param string start [ hour ] start - hour
* @ param string start [ min ] start - minutes
* @ param string end [ str ] end - date
* @ param string end [ hour ] end - hour
* @ param string end [ min ] end - minutes
* @ param string participants ':' delimited string of user - id ' s
2004-10-08 00:11:18 +02:00
*/
2005-11-09 00:15:14 +01:00
function freetimesearch ( $content = null )
2004-10-08 00:11:18 +02:00
{
2005-11-09 00:15:14 +01:00
$etpl =& CreateObject ( 'etemplate.etemplate' , 'calendar.freetimesearch' );
2004-12-12 13:31:41 +01:00
$sel_options [ 'search_window' ] = array (
7 * DAY_s => lang ( 'one week' ),
14 * DAY_s => lang ( 'two weeks' ),
31 * DAY_s => lang ( 'one month' ),
92 * DAY_s => lang ( 'three month' ),
365 * DAY_s => lang ( 'one year' ),
);
2004-10-08 00:11:18 +02:00
if ( ! is_array ( $content ))
{
2005-11-09 00:15:14 +01:00
$edit_content = $etpl -> process_values2url ();
2006-06-03 01:36:43 +02:00
if ( $edit_content [ 'duration' ])
{
$edit_content [ 'end' ] = $edit_content [ 'start' ] + $edit_content [ 'duration' ];
}
2005-11-09 00:15:14 +01:00
if ( $edit_content [ 'whole_day' ])
2004-10-08 00:11:18 +02:00
{
2005-11-09 00:15:14 +01:00
$arr = $this -> bo -> date2array ( $edit_content [ 'start' ]);
$arr [ 'hour' ] = $arr [ 'minute' ] = $arr [ 'second' ] = 0 ; unset ( $arr [ 'raw' ]);
$edit_content [ 'start' ] = $this -> bo -> date2ts ( $arr );
$arr = $this -> bo -> date2array ( $edit_content [ 'end' ]);
$arr [ 'hour' ] = 23 ; $arr [ 'minute' ] = $arr [ 'second' ] = 59 ; unset ( $arr [ 'raw' ]);
$edit_content [ 'end' ] = $this -> bo -> date2ts ( $arr );
2004-10-08 00:11:18 +02:00
}
2005-11-09 00:15:14 +01:00
$content = array (
'start' => $edit_content [ 'start' ],
'duration' => $edit_content [ 'end' ] - $edit_content [ 'start' ],
'end' => $edit_content [ 'end' ],
'cal_id' => $edit_content [ 'id' ],
'recur_type' => $edit_content [ 'recur_type' ],
'participants' => array (),
);
2006-12-27 18:11:47 +01:00
foreach ( $edit_content [ 'participants' ] as $key => $data )
2004-10-08 00:11:18 +02:00
{
2006-12-27 18:11:47 +01:00
if ( is_numeric ( $key ) && ! $edit_content [ 'participants' ][ 'delete' ][ $data [ 'uid' ]])
2005-11-09 00:15:14 +01:00
{
2006-12-27 18:11:47 +01:00
$content [ 'participants' ][] = $data [ 'uid' ];
2005-11-09 00:15:14 +01:00
}
2006-12-27 18:11:47 +01:00
elseif ( $key == 'account' && $data )
2005-11-09 00:15:14 +01:00
{
2006-12-27 18:11:47 +01:00
$content [ 'participants' ][] = $data ;
2005-11-09 00:15:14 +01:00
}
2004-10-08 00:11:18 +02:00
}
// default search parameters
2005-11-09 00:15:14 +01:00
$content [ 'start_time' ] = $edit_content [ 'whole_day' ] ? 0 : $this -> cal_prefs [ 'workdaystarts' ];
2004-10-08 00:11:18 +02:00
$content [ 'end_time' ] = $this -> cal_prefs [ 'workdayends' ];
2004-12-12 13:31:41 +01:00
if ( $this -> cal_prefs [ 'workdayends' ] * HOUR_s < $this -> cal_prefs [ 'workdaystarts' ] * HOUR_s + $content [ 'duration' ])
2004-10-08 00:11:18 +02:00
{
$content [ 'end_time' ] = 0 ; // no end-time limit, as duration would never fit
}
$content [ 'weekdays' ] = MCAL_M_WEEKDAYS ;
2008-05-08 00:12:25 +02:00
2004-10-08 00:11:18 +02:00
$content [ 'search_window' ] = 7 * DAY_s ;
2004-12-12 13:31:41 +01:00
// pick a searchwindow fitting the duration (search for a 10 day slot in a one week window never succeeds)
foreach ( $sel_options [ 'search_window' ] as $window => $label )
{
2008-05-08 00:12:25 +02:00
if ( $window > $content [ 'duration' ])
2004-12-12 13:31:41 +01:00
{
$content [ 'search_window' ] = $window ;
break ;
}
}
2004-10-08 00:11:18 +02:00
}
else
{
2008-05-08 00:12:25 +02:00
if ( ! $content [ 'duration' ]) $content [ 'duration' ] = $content [ 'end' ] - $content [ 'start' ];
2004-10-08 00:11:18 +02:00
if ( is_array ( $content [ 'freetime' ][ 'select' ]))
{
list ( $selected ) = each ( $content [ 'freetime' ][ 'select' ]);
2004-10-11 12:43:26 +02:00
//echo "$selected = ".date('D d.m.Y H:i',$content['freetime'][$selected]['start']);
2004-10-08 00:11:18 +02:00
$start = ( int ) $content [ 'freetime' ][ $selected ][ 'start' ];
2004-12-12 13:31:41 +01:00
$end = $start + $content [ 'duration' ];
2005-11-09 00:15:14 +01:00
/**
* ToDo : make this an eTemplate function to transmit content back to the opener
*/
2004-10-08 00:11:18 +02:00
$fields_to_set = array (
2005-11-09 00:15:14 +01:00
'exec[start][str]' => date ( $this -> common_prefs [ 'dateformat' ], $start ),
'exec[start][i]' => ( int ) date ( 'i' , $start ),
'exec[end][str]' => date ( $this -> common_prefs [ 'dateformat' ], $end ),
'exec[end][i]' => ( int ) date ( 'i' , $end ),
2006-06-03 01:36:43 +02:00
'exec[duration]' => $content [ 'duration' ],
2004-10-08 00:11:18 +02:00
);
if ( $this -> common_prefs [ 'timeformat' ] == 12 )
{
$fields_to_set += array (
2005-11-09 00:15:14 +01:00
'exec[start][H]' => date ( 'h' , $start ),
'exec[start][a]' => date ( 'a' , $start ),
'exec[end][H]' => date ( 'h' , $end ),
'exec[end][a]' => date ( 'a' , $end ),
2004-10-08 00:11:18 +02:00
);
}
2008-05-08 00:12:25 +02:00
else
2004-10-08 00:11:18 +02:00
{
$fields_to_set += array (
2005-11-09 00:15:14 +01:00
'exec[start][H]' => ( int ) date ( 'H' , $start ),
'exec[end][H]' => ( int ) date ( 'H' , $end ),
2004-10-08 00:11:18 +02:00
);
}
echo " <html>
< script >
var fields = Array ( '".implode("' , '",array_keys($fields_to_set))."' );
var values = Array ( '".implode("' , '",$fields_to_set)."' );
for ( i = 0 ; i < fields . length ; ++ i ) {
elements = opener . document . getElementsByName ( fields [ i ]);
if ( elements ) {
if ( elements . length == 1 )
elements [ 0 ] . value = values [ i ];
else
for ( n = 0 ; n < elements . length ; ++ n ) {
if ( elements [ n ] . value == values [ i ]) elements [ n ] . checked = true ;
}
}
}
2008-05-08 00:12:25 +02:00
window . close ();
2004-10-08 00:11:18 +02:00
</ script >
</ html > \n " ;
exit ;
}
2004-10-08 18:04:50 +02:00
}
if ( $content [ 'recur_type' ])
{
$content [ 'msg' ] .= lang ( 'Only the initial date of that recuring event is checked!' );
}
2004-12-12 13:31:41 +01:00
$content [ 'freetime' ] = $this -> freetime ( $content [ 'participants' ], $content [ 'start' ], $content [ 'start' ] + $content [ 'search_window' ], $content [ 'duration' ], $content [ 'cal_id' ]);
$content [ 'freetime' ] = $this -> split_freetime_daywise ( $content [ 'freetime' ], $content [ 'duration' ], $content [ 'weekdays' ], $content [ 'start_time' ], $content [ 'end_time' ], $sel_options );
2004-10-08 00:11:18 +02:00
//echo "<pre>".print_r($content,true)."</pre>\n";
2005-11-09 00:15:14 +01:00
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'app_header' ] = lang ( 'calendar' ) . ' - ' . lang ( 'freetime search' );
2004-10-08 18:04:50 +02:00
// let the window popup, if its already there
2005-11-09 00:15:14 +01:00
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'java_script' ] .= " <script> \n window.focus(); \n </script> \n " ;
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
if ( ! is_object ( $GLOBALS [ 'egw' ] -> js ))
{
$GLOBALS [ 'egw' ] -> js = CreateObject ( 'phpgwapi.javascript' );
}
2006-06-03 01:36:43 +02:00
$sel_options [ 'duration' ] = $this -> durations ;
if ( $content [ 'duration' ] && isset ( $sel_options [ 'duration' ][ $content [ 'duration' ]])) $content [ 'end' ] = '' ;
2005-11-09 00:15:14 +01:00
// We hide the enddate if one of our predefined durations fits
// the call to set_style_by_class has to be in onload, to make sure the function and the element is already created
2006-06-03 01:36:43 +02:00
$GLOBALS [ 'egw' ] -> js -> set_onload ( " set_style_by_class('table','end_hide','visibility',' " . ( $content [ 'duration' ] && isset ( $sel_options [ 'duration' ][ $content [ 'duration' ]]) ? 'hidden' : 'visible' ) . " '); " );
2004-10-08 00:11:18 +02:00
$etpl -> exec ( 'calendar.uiforms.freetimesearch' , $content , $sel_options , '' , array (
'participants' => $content [ 'participants' ],
2004-10-08 18:04:50 +02:00
'cal_id' => $content [ 'cal_id' ],
'recur_type' => $content [ 'recur_type' ],
2008-05-08 00:12:25 +02:00
), 2 );
2004-10-08 00:11:18 +02:00
}
2008-05-08 00:12:25 +02:00
2004-10-08 00:11:18 +02:00
/**
* calculate the freetime of given $participants in a certain time - span
*
2005-11-09 00:15:14 +01:00
* @ param array $participants user - id ' s
* @ param int $start start - time timestamp in user - time
* @ param int $end end - time timestamp in user - time
* @ param int $duration min . duration in sec , default 1
* @ param int $cal_id own id for existing events , to exclude them from being busy - time , default 0
2004-10-08 00:11:18 +02:00
* @ return array of free time - slots : array with start and end values
*/
2004-10-08 18:04:50 +02:00
function freetime ( $participants , $start , $end , $duration = 1 , $cal_id = 0 )
2004-10-08 00:11:18 +02:00
{
2004-12-12 13:31:41 +01:00
if ( $this -> debug > 2 ) $this -> bo -> debug_message ( 'uiforms::freetime(participants=%1, start=%2, end=%3, duration=%4, cal_id=%5)' , true , $participants , $start , $end , $duration , $cal_id );
2004-10-08 00:11:18 +02:00
$busy = $this -> bo -> search ( array (
'start' => $start ,
'end' => $end ,
'users' => $participants ,
2006-03-16 19:03:43 +01:00
'ignore_acl' => true , // otherwise we get only events readable by the user
2004-10-08 00:11:18 +02:00
));
$busy [] = array ( // add end-of-search-date as event, to cope with empty search and get freetime til that date
2005-11-09 00:15:14 +01:00
'start' => $end ,
'end' => $end ,
2008-05-08 00:12:25 +02:00
);
2004-10-08 00:11:18 +02:00
$ft_start = $start ;
$freetime = array ();
$n = 0 ;
foreach ( $busy as $event )
{
2004-10-08 18:04:50 +02:00
if (( int ) $cal_id && $event [ 'id' ] == ( int ) $cal_id ) continue ; // ignore our own event
2006-03-08 11:23:44 +01:00
if ( $event [ 'non_blocking' ]) continue ; // ignore non_blocking events
2004-10-08 00:11:18 +02:00
if ( $this -> debug )
{
echo " <p>ft_start= " . date ( 'D d.m.Y H:i' , $ft_start ) . " <br> \n " ;
echo " event[title]= $event[title] <br> \n " ;
echo " event[start]= " . date ( 'D d.m.Y H:i' , $event [ 'start' ][ 'raw' ]) . " <br> \n " ;
echo " event[end]= " . date ( 'D d.m.Y H:i' , $event [ 'end' ][ 'raw' ]) . " <br> \n " ;
}
// $events ends before our actual position ==> ignore it
2005-11-09 00:15:14 +01:00
if ( $event [ 'end' ] < $ft_start )
2004-10-08 00:11:18 +02:00
{
//echo "==> event ends before ft_start ==> continue<br>\n";
continue ;
}
// $events starts before our actual position ==> set start to it's end and go to next event
2005-11-09 00:15:14 +01:00
if ( $event [ 'start' ] < $ft_start )
2004-10-08 00:11:18 +02:00
{
//echo "==> event starts before ft_start ==> set ft_start to it's end & continue<br>\n";
2005-11-09 00:15:14 +01:00
$ft_start = $event [ 'end' ];
2004-10-08 00:11:18 +02:00
continue ;
}
2005-11-09 00:15:14 +01:00
$ft_end = $event [ 'start' ];
2004-10-08 00:11:18 +02:00
// only show slots equal or bigger to min_length
if ( $ft_end - $ft_start >= $duration )
{
$freetime [ ++ $n ] = array (
'start' => $ft_start ,
'end' => $ft_end ,
);
2004-12-12 13:31:41 +01:00
if ( $this -> debug > 1 ) echo " <p>freetime: " . date ( 'D d.m.Y H:i' , $ft_start ) . " - " . date ( 'D d.m.Y H:i' , $ft_end ) . " </p> \n " ;
2008-05-08 00:12:25 +02:00
}
2005-11-09 00:15:14 +01:00
$ft_start = $event [ 'end' ];
2004-10-08 00:11:18 +02:00
}
2004-12-12 13:31:41 +01:00
if ( $this -> debug > 0 ) $this -> bo -> debug_message ( 'uiforms::freetime(participants=%1, start=%2, end=%3, duration=%4, cal_id=%5) freetime=%6' , true , $participants , $start , $end , $duration , $cal_id , $freetime );
2008-05-08 00:12:25 +02:00
2004-10-08 00:11:18 +02:00
return $freetime ;
}
2008-05-08 00:12:25 +02:00
2004-10-08 00:11:18 +02:00
/**
* split the freetime in daywise slot , taking into account weekdays , start - and stop - times
*
2004-12-12 13:31:41 +01:00
* If the duration is bigger then the difference of start - and end_time , the end_time is ignored
*
2005-11-09 00:15:14 +01:00
* @ param array $freetime free time - slots : array with start and end values
* @ param int $duration min . duration in sec
* @ param int $weekdays allowed weekdays , bitfield of MCAL_M_ ...
* @ param int $start_time minimum start - hour 0 - 23
* @ param int $end_time maximum end - hour 0 - 23 , or 0 for none
* @ param array $sel_options on return options for start - time selectbox
2004-10-08 00:11:18 +02:00
* @ return array of free time - slots : array with start and end values
*/
function split_freetime_daywise ( $freetime , $duration , $weekdays , $start_time , $end_time , & $sel_options )
{
2004-12-12 13:31:41 +01:00
if ( $this -> debug > 1 ) $this -> bo -> debug_message ( 'uiforms::split_freetime_daywise(freetime=%1, duration=%2, start_time=%3, end_time=%4)' , true , $freetime , $duration , $start_time , $end_time );
2008-05-08 00:12:25 +02:00
2004-12-12 13:31:41 +01:00
$freetime_daywise = array ();
if ( ! is_array ( $sel_options )) $sel_options = array ();
2004-10-08 00:11:18 +02:00
$time_format = $this -> common_prefs [ 'timeformat' ] == 12 ? 'h:i a' : 'H:i' ;
2008-05-08 00:12:25 +02:00
2004-12-12 13:31:41 +01:00
$start_time = ( int ) $start_time ; // ignore leading zeros
$end_time = ( int ) $end_time ;
2004-10-08 00:11:18 +02:00
2004-12-12 13:31:41 +01:00
// ignore the end_time, if duration would never fit
2008-05-08 00:12:25 +02:00
if (( $end_time - $start_time ) * HOUR_s < $duration )
2004-12-12 13:31:41 +01:00
{
2008-05-08 00:12:25 +02:00
$end_time = 0 ;
2004-12-12 13:31:41 +01:00
if ( $this -> debug > 1 ) $this -> bo -> debug_message ( 'uiforms::split_freetime_daywise(, duration=%2, start_time=%3,..) end_time set to 0, it never fits durationn otherwise' , true , $duration , $start_time );
}
2004-10-08 00:11:18 +02:00
$n = 0 ;
foreach ( $freetime as $ft )
{
$daybegin = $this -> bo -> date2array ( $ft [ 'start' ]);
$daybegin [ 'hour' ] = $daybegin [ 'minute' ] = $daybegin [ 'second' ] = 0 ;
unset ( $daybegin [ 'raw' ]);
$daybegin = $this -> bo -> date2ts ( $daybegin );
2008-05-08 00:12:25 +02:00
2004-10-08 00:11:18 +02:00
for ( $t = $daybegin ; $t < $ft [ 'end' ]; $t += DAY_s , $daybegin += DAY_s )
{
$dow = date ( 'w' , $daybegin + DAY_s / 2 ); // 0=Sun, .., 6=Sat
$mcal_dow = pow ( 2 , $dow );
if ( ! ( $weekdays & $mcal_dow ))
{
2004-12-12 13:31:41 +01:00
//echo "wrong day of week $dow<br>\n";
2004-10-08 00:11:18 +02:00
continue ; // wrong day of week
}
$start = $t < $ft [ 'start' ] ? $ft [ 'start' ] : $t ;
2008-05-08 00:12:25 +02:00
2004-10-08 00:11:18 +02:00
if ( $start - $daybegin < $start_time * HOUR_s ) // start earlier then start_time
{
$start = $daybegin + $start_time * HOUR_s ;
}
// if end_time given use it, else the original slot's end
$end = $end_time ? $daybegin + $end_time * HOUR_s : $ft [ 'end' ];
if ( $end > $ft [ 'end' ]) $end = $ft [ 'end' ];
// slot to small for duration
if ( $end - $start < $duration )
{
2004-12-12 13:31:41 +01:00
//echo "slot to small for duration=$duration<br>\n";
2004-10-08 00:11:18 +02:00
continue ;
}
$freetime_daywise [ ++ $n ] = array (
'start' => $start ,
'end' => $end ,
);
$times = array ();
2004-12-12 13:31:41 +01:00
for ( $s = $start ; $s + $duration <= $end && $s < $daybegin + DAY_s ; $s += 60 * $this -> cal_prefs [ 'interval' ])
2004-10-08 00:11:18 +02:00
{
$e = $s + $duration ;
$end_date = $e - $daybegin > DAY_s ? lang ( date ( 'l' , $e )) . ' ' . date ( $this -> common_prefs [ 'dateformat' ], $e ) . ' ' : '' ;
$times [ $s ] = date ( $time_format , $s ) . ' - ' . $end_date . date ( $time_format , $e );
}
$sel_options [ $n . '[start]' ] = $times ;
}
}
return $freetime_daywise ;
}
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
/**
* Export events as vCalendar version 2.0 files ( iCal )
*
* @ param int / array $content = 0 numeric cal_id or submitted content from etempalte :: exec
* @ param boolean $return_error = false should an error - msg be returned or a regular page with it generated ( default )
* @ return string error - msg if $return_error
*/
function export ( $content = 0 , $return_error = false )
{
if ( is_numeric ( $cal_id = $content ? $content : $_REQUEST [ 'cal_id' ]))
{
2005-11-18 16:00:02 +01:00
if ( ! ( $ical =& ExecMethod2 ( 'calendar.boical.exportVCal' , $cal_id , '2.0' )))
2005-11-09 00:15:14 +01:00
{
$msg = lang ( 'Permission denied' );
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
if ( $return_error ) return $msg ;
}
else
{
2007-09-29 11:17:42 +02:00
$GLOBALS [ 'egw' ] -> browser -> content_header ( 'event.ics' , 'text/calendar' , bytes ( $ical ));
2005-11-09 00:15:14 +01:00
echo $ical ;
$GLOBALS [ 'egw' ] -> common -> egw_exit ();
}
}
if ( is_array ( $content ))
{
$events =& $this -> bo -> search ( array (
'start' => $content [ 'start' ],
'end' => $content [ 'end' ],
'enum_recuring' => false ,
'daywise' => false ,
'owner' => $this -> owner ,
'date_format' => 'server' , // timestamp in server time for boical class
));
if ( ! $events )
{
$msg = lang ( 'No events found' );
}
else
{
2006-04-10 08:33:20 +02:00
$ical =& ExecMethod2 ( 'calendar.boical.exportVCal' , $events , '2.0' /*$content['version']*/ );
2007-09-29 11:17:42 +02:00
$GLOBALS [ 'egw' ] -> browser -> content_header ( $content [ 'file' ] ? $content [ 'file' ] : 'event.ics' , 'text/calendar' , bytes ( $ical ));
2005-11-09 00:15:14 +01:00
echo $ical ;
$GLOBALS [ 'egw' ] -> common -> egw_exit ();
}
}
if ( ! is_array ( $content ))
{
$view = $GLOBALS [ 'egw' ] -> session -> appsession ( 'view' , 'calendar' );
$content = array (
'start' => $this -> bo -> date2ts ( $_REQUEST [ 'start' ] ? $_REQUEST [ 'start' ] : $this -> date ),
'end' => $this -> bo -> date2ts ( $_REQUEST [ 'end' ] ? $_REQUEST [ 'end' ] : $this -> date ),
'file' => 'event.ics' ,
'version' => '2.0' ,
);
}
$content [ 'msg' ] = $msg ;
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'app_header' ] = lang ( 'calendar' ) . ' - ' . lang ( 'iCal Export' );
$etpl =& CreateObject ( 'etemplate.etemplate' , 'calendar.export' );
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
$etpl -> exec ( 'calendar.uiforms.export' , $content );
}
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
/**
* Import events as vCalendar version 2.0 files ( iCal )
*
* @ param array $content = null submitted content from etempalte :: exec
*/
function import ( $content = null )
{
if ( is_array ( $content ))
{
if ( is_array ( $content [ 'ical_file' ]) && is_uploaded_file ( $content [ 'ical_file' ][ 'tmp_name' ]))
{
if ( ! ExecMethod ( 'calendar.boical.importVCal' , file_get_contents ( $content [ 'ical_file' ][ 'tmp_name' ])))
{
$msg = lang ( 'Error: importing the iCal' );
}
else
{
$msg = lang ( 'iCal successful imported' );
}
}
else
{
$msg = lang ( 'You need to select an iCal file first' );
}
}
$content = array (
'msg' => $msg ,
);
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'app_header' ] = lang ( 'calendar' ) . ' - ' . lang ( 'iCal Import' );
$etpl =& CreateObject ( 'etemplate.etemplate' , 'calendar.import' );
2008-05-08 00:12:25 +02:00
2005-11-09 00:15:14 +01:00
$etpl -> exec ( 'calendar.uiforms.import' , $content );
}
2004-10-08 00:11:18 +02:00
}