2011-02-09 16:58:06 +01:00
< ? php
/**
2016-05-01 19:47:59 +02:00
* EGroupware Calendar - document merge
2011-02-09 16:58:06 +01:00
*
* @ link http :// www . egroupware . org
* @ author Ralf Becker < RalfBecker - AT - outdoor - training . de >
* @ author Nathan Gray
* @ package calendar
2016-05-01 19:47:59 +02:00
* @ copyright ( c ) 2007 - 16 by Ralf Becker < RalfBecker - AT - outdoor - training . de >
2011-02-09 16:58:06 +01:00
* @ copyright 2011 Nathan Gray
* @ license http :// opensource . org / licenses / gpl - license . php GPL - GNU General Public License
* @ version $Id $
*/
2016-05-01 19:47:59 +02:00
use EGroupware\Api ;
2011-02-09 16:58:06 +01:00
/**
* Calendar - document merge object
*/
2016-05-01 19:47:59 +02:00
class calendar_merge extends Api\Storage\Merge
2011-02-09 16:58:06 +01:00
{
/**
* Functions that can be called via menuaction
*
* @ var array
*/
var $public_functions = array (
2021-10-07 21:45:49 +02:00
'download_by_request' => true ,
'show_replacements' => true ,
'merge_entries' => true
2011-02-09 16:58:06 +01:00
);
// Object for getting calendar info
protected $bo ;
// Object used for getting resource info
protected static $resources ;
2011-02-15 21:35:46 +01:00
/**
* Recognised relative days - used as a day table , like day_ < n >
*/
protected static $relative = array (
'today' ,
'tomorrow' ,
'yesterday' ,
2012-02-16 17:55:42 +01:00
'selected' ,
2011-02-15 21:35:46 +01:00
);
/**
* If you use a range , these extra tags are available
*/
protected static $range_tags = array (
2021-10-07 21:45:49 +02:00
'start' => 'Y-m-d' ,
'end' => 'Y-m-d' ,
'month' => 'F' ,
'year' => 'Y'
2011-02-15 21:35:46 +01:00
);
2011-02-16 18:19:19 +01:00
/**
* Base query for all event searches
*/
protected $query = array ();
2011-06-22 19:23:04 +02:00
/**
* Stored IDs , if user passed in ID / events instead of date range
2017-12-18 22:28:46 +01:00
* We keep the IDs then filter the events in the range to only the selected
* IDs
2011-06-22 19:23:04 +02:00
*/
protected $ids = array ();
2011-02-09 16:58:06 +01:00
/**
* Constructor
*/
function __construct ()
{
parent :: __construct ();
2011-09-13 11:05:33 +02:00
// overwrite global export-limit, if one is set for calendar/appointments
2016-05-01 19:47:59 +02:00
$this -> export_limit = Api\Storage\Merge :: getExportLimit ( 'calendar' );
2011-09-13 11:05:33 +02:00
2016-05-01 19:47:59 +02:00
// switch of handling of Api\Html formated content, if Api\Html is not used
$this -> parse_html_styles = Api\Storage\Customfields :: use_html ( 'calendar' );
2011-02-09 16:58:06 +01:00
$this -> bo = new calendar_boupdate ();
2011-02-17 17:50:46 +01:00
self :: $range_tags [ 'start' ] = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'dateformat' ];
self :: $range_tags [ 'end' ] = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'dateformat' ];
2011-02-15 21:35:46 +01:00
2011-02-09 16:58:06 +01:00
// Register table plugins
$this -> table_plugins [ 'participant' ] = 'participant' ;
for ( $i = 0 ; $i < 7 ; $i ++ )
{
$this -> table_plugins [ date ( 'l' , strtotime ( " + $i days " ))] = 'day_plugin' ;
}
2021-10-07 21:45:49 +02:00
for ( $i = 1 ; $i <= 31 ; $i ++ )
{
$this -> table_plugins [ 'day_' . $i ] = 'day' ; // Numerically by day number (1-31)
2011-02-15 21:35:46 +01:00
}
2021-10-07 21:45:49 +02:00
foreach ( self :: $relative as $day )
{
2011-02-15 21:35:46 +01:00
$this -> table_plugins [ $day ] = 'day' ; // Current day
}
2015-10-09 21:48:28 +02:00
$this -> query = is_array ( $this -> bo -> cal_prefs [ 'saved_states' ]) ?
$this -> bo -> cal_prefs [ 'saved_states' ] : unserialize ( $this -> bo -> cal_prefs [ 'saved_states' ]);
$this -> query [ 'users' ] = is_array ( $this -> query [ 'owner' ]) ? $this -> query [ 'owner' ] : explode ( ',' , $this -> query [ 'owner' ]);
2011-02-18 17:09:42 +01:00
$this -> query [ 'num_rows' ] = - 1 ;
2011-02-09 16:58:06 +01:00
}
2015-10-09 21:48:28 +02:00
/**
* Merges a given document with contact data
*
* Overridden from parent to be able to change a list of events into a range ,
* if the target document has no pagerepeat tag . Otherwise , parent :: merge_string ()
* would fail because we ' re trying to merge multiple records with no pagerepeat tag .
*
*
* @ param string $content
* @ param array $ids array with contact id ( s )
* @ param string & $err error - message on error
* @ param string $mimetype mimetype of complete document , eg . text /* , application / vnd . oasis . opendocument . text , application / rtf
2021-10-07 21:45:49 +02:00
* @ param array $fix = null regular expression => replacement pairs eg . to fix garbled placeholders
* @ param string $charset = null charset to override default set by mimetype or export charset
2015-10-09 21:48:28 +02:00
* @ return string | boolean merged document or false on error
*/
2021-10-07 21:45:49 +02:00
function & merge_string ( $content , $ids , & $err , $mimetype , array $fix = null , $charset = null )
2015-10-09 21:48:28 +02:00
{
2018-02-14 18:16:06 +01:00
$ids = $this -> validate_ids (( array ) $ids , $content );
2015-12-04 21:58:37 +01:00
2021-03-28 20:48:55 +02:00
return parent :: merge_string ( $content , $ids , $err , $mimetype , $fix , $charset );
2015-10-09 21:48:28 +02:00
}
2021-11-23 23:50:10 +01:00
public static function merge_entries ( array $ids = null , \EGroupware\Api\Storage\Merge & $document_merge = null , $pdf = null )
{
$document_merge = new calendar_merge ();
if ( is_null (( $ids )))
{
$ids = json_decode ( $_REQUEST [ 'id' ], true );
}
// Try to make time span into appropriate ranges to match
$template = $ids [ 'view' ] ? : '' ;
if ( stripos ( $_REQUEST [ 'document' ], 'month' ) !== false || stripos ( $_REQUEST [ 'document' ], lang ( 'month' )) !== false )
{
$template = 'month' ;
}
if ( stripos ( $_REQUEST [ 'document' ], 'week' ) !== false || stripos ( $_REQUEST [ 'document' ], lang ( 'week' )) !== false )
{
$template = 'week' ;
}
//error_log("Detected template $template");
$date = $ids [ 'date' ];
$first = $ids [ 'first' ];
$last = $ids [ 'last' ];
// Pull dates from session if they're not in the request
if ( ! array_key_exists ( 'first' , $ids ))
{
$ui = new calendar_ui ();
$date = $ui -> date ;
$first = $ui -> first ;
$last = $ui -> last ;
}
switch ( $template )
{
case 'month' :
// Trim to _only_ the month, do not pad to week start / end
$time = new Api\DateTime ( $date );
$timespan = array ( array (
'start' => Api\DateTime :: to ( $time -> format ( 'Y-m-01 00:00:00' ), 'ts' ),
'end' => Api\DateTime :: to ( $time -> format ( 'Y-m-t 23:59:59' ), 'ts' )
));
break ;
case 'week' :
$timespan = array ();
$start = new Api\DateTime ( $first );
$end = new Api\DateTime ( $last );
$t = clone $start ;
$t -> modify ( '+1 week' ) -> modify ( '-1 second' );
if ( $t < $end )
{
do
{
$timespan [] = array (
'start' => $start -> format ( 'ts' ),
'end' => $t -> format ( 'ts' )
);
$start -> modify ( '+1 week' );
$t -> modify ( '+1 week' );
}
while ( $start < $end );
break ;
}
// Fall through
default :
$timespan = array ( array (
'start' => $first ,
'end' => $last
));
}
// Add path into document
static :: check_document ( $_REQUEST [ 'document' ], $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'calendar' ][ 'document_dir' ]);
return \EGroupware\Api\Storage\Merge :: merge_entries ( array_key_exists ( '0' , $ids ) ? $ids : $timespan , $document_merge );
}
public function get_filename_placeholders ( $document , $ids )
{
$placeholders = parent :: get_filename_placeholders ( $document , $ids );
$request = json_decode ( $_REQUEST [ 'id' ], true ) ? : [];
$template = $ids [ 'view' ] ? : '' ;
if ( stripos ( $document , 'month' ) !== false || stripos ( $document , lang ( 'month' )) !== false )
{
$template = 'month' ;
}
if ( stripos ( $document , 'week' ) !== false || stripos ( $document , lang ( 'week' )) !== false )
{
$template = 'week' ;
}
$placeholders [ '$$span$$' ] = lang ( $template );
$placeholders [ '$$first$$' ] = Api\DateTime :: to ( $ids [ 'first' ] ? : $request [ 'first' ], true );
$placeholders [ '$$last$$' ] = Api\DateTime :: to ( $ids [ 'last' ] ? : $request [ 'last' ], true );
$placeholders [ '$$date$$' ] = Api\DateTime :: to ( $ids [ 'date' ] ? : $request [ 'date' ], true );
return $placeholders ;
}
2011-02-09 16:58:06 +01:00
/**
* Get replacements
*
* @ param int | array $id event - id array with id , recur_date , or array with search parameters
2021-10-07 21:45:49 +02:00
* @ param string & $content = null content to create some replacements only if they are used
2011-02-09 16:58:06 +01:00
* @ return array | boolean
*/
2021-10-07 21:45:49 +02:00
protected function get_replacements ( $id , & $content = null )
2011-02-09 16:58:06 +01:00
{
$prefix = '' ;
// List events ?
2011-06-22 19:23:04 +02:00
if ( is_array ( $id ) && ! $id [ 'id' ] && ! $id [ 0 ][ 'id' ])
2011-02-09 16:58:06 +01:00
{
2011-02-16 18:19:19 +01:00
$events = $this -> bo -> search ( $this -> query + $id + array (
2021-10-07 21:45:49 +02:00
'offset' => 0 ,
'order' => 'cal_start' ,
'cfs' => strpos ( $content , '#' ) !== false ? array_keys ( Api\Storage\Customfields :: get ( 'calendar' )) : null
)
);
if ( strpos ( $content , '$$calendar/' ) !== false || strpos ( $content , '$$table/day' ) !== false )
2011-02-16 18:19:19 +01:00
{
2021-10-07 21:45:49 +02:00
array_unshift ( $events , false );
unset ( $events [ 0 ]); // renumber the array to start with key 1, instead of 0
2011-02-16 18:19:19 +01:00
$prefix = 'calendar/%d' ;
}
2011-02-09 16:58:06 +01:00
}
2021-10-07 21:45:49 +02:00
elseif ( is_array ( $id ) && $id [ 0 ][ 'id' ])
2011-06-22 19:23:04 +02:00
{
// Passed an array of events, to be handled like a date range
$events = $id ;
2017-12-18 22:28:46 +01:00
$id = array ( $this -> events_to_range ( $id ));
2011-06-22 19:23:04 +02:00
}
2011-02-09 16:58:06 +01:00
else
{
$events = array ( $id );
}
2011-09-13 17:41:31 +02:00
// as this function allows to pass query- parameters, we need to check the result of the query against export_limit restrictions
2021-10-07 21:45:49 +02:00
if ( Api\Storage\Merge :: hasExportLimit ( $this -> export_limit ) && ! Api\Storage\Merge :: is_export_limit_excepted () && count ( $events ) > ( int ) $this -> export_limit )
2011-09-13 17:41:31 +02:00
{
2021-10-07 21:45:49 +02:00
$err = lang ( 'No rights to export more than %1 entries!' , ( int ) $this -> export_limit );
2016-05-01 19:47:59 +02:00
throw new Api\Exception\WrongUserinput ( $err );
2011-09-13 17:41:31 +02:00
}
2011-02-09 16:58:06 +01:00
$replacements = array ();
2011-02-10 20:17:26 +01:00
$n = 0 ;
foreach ( $events as $event )
2011-02-09 16:58:06 +01:00
{
2021-10-07 21:45:49 +02:00
$event_id = $event [ 'id' ] . ( $event [ 'recur_date' ] ? ':' . $event [ 'recur_date' ] : '' );
2017-12-18 22:28:46 +01:00
if ( $this -> ids && ! in_array ( $event_id , $this -> ids )) continue ;
2021-10-07 21:45:49 +02:00
$values = $this -> calendar_replacements ( $event , sprintf ( $prefix , ++ $n ), $content );
2011-02-15 21:35:46 +01:00
if ( is_array ( $id ) && $id [ 'start' ])
{
foreach ( self :: $range_tags as $key => $format )
{
2021-11-23 23:50:10 +01:00
$value = Api\DateTime :: to ( $key == 'end' ? $id [ 'end' ] : $id [ 'start' ], $format );
2011-02-22 19:45:33 +01:00
if ( $key == 'month' ) $value = lang ( $value );
$values [ " $\ $range / $key $ $ " ] = $value ;
2011-02-15 21:35:46 +01:00
}
}
2011-02-09 16:58:06 +01:00
$replacements += $values ;
}
return $replacements ;
}
/**
* Return replacements for the calendar
*
* @ param int | array $id event - id or array with id / recur_date , or array with event info
2021-10-07 21:45:49 +02:00
* @ param boolean $last_event_too = false also include information about the last event
2011-02-09 16:58:06 +01:00
* @ return array
*/
2021-10-07 21:45:49 +02:00
public function calendar_replacements ( $id , $prefix = '' , & $content = '' )
2011-02-09 16:58:06 +01:00
{
$replacements = array ();
2021-10-07 21:45:49 +02:00
if ( ! is_array ( $id ) || ! $id [ 'start' ])
{
2017-12-18 22:28:46 +01:00
if ( is_string ( $id ) && strpos ( $id , ':' ))
2017-12-15 22:27:19 +01:00
{
$_id = $id ;
$id = array ();
2021-10-07 21:45:49 +02:00
list ( $id [ 'id' ], $id [ 'recur_date' ]) = explode ( ':' , $_id );
2017-12-15 22:27:19 +01:00
}
2011-02-09 16:58:06 +01:00
$event = $this -> bo -> read ( is_array ( $id ) ? $id [ 'id' ] : $id , is_array ( $id ) ? $id [ 'recur_date' ] : null );
2021-10-07 21:45:49 +02:00
}
else
{
2011-02-09 16:58:06 +01:00
$event = $id ;
}
2016-05-17 01:26:15 +02:00
$record = new calendar_egw_record ( $event [ 'id' ]);
// Convert to human friendly values
$types = calendar_egw_record :: $types ;
importexport_export_csv :: convert ( $record , $types , 'calendar' );
$array = $record -> get_record_array ();
foreach ( $array as $key => $value )
{
2021-10-07 21:45:49 +02:00
$replacements [ '$$' . ( $prefix ? $prefix . '/' : '' ) . $key . '$$' ] = $value ;
2016-05-17 01:26:15 +02:00
}
2021-10-07 21:45:49 +02:00
$replacements [ '$$' . ( $prefix ? $prefix . '/' : '' ) . 'calendar_id' . '$$' ] = $event [ 'id' ];
2011-02-09 16:58:06 +01:00
foreach ( $this -> bo -> event2array ( $event ) as $name => $data )
{
if ( substr ( $name , - 4 ) == 'date' ) $name = substr ( $name , 0 , - 4 );
2021-10-07 21:45:49 +02:00
$replacements [ '$$' . ( $prefix ? $prefix . '/' : '' ) . 'calendar_' . $name . '$$' ] = is_array ( $data [ 'data' ]) ? implode ( ', ' , $data [ 'data' ]) : $data [ 'data' ];
2011-02-09 16:58:06 +01:00
}
2015-07-10 17:27:06 +02:00
// Add seperate lists of participants by type
2021-10-07 21:45:49 +02:00
if ( strpos ( $content , 'calendar_participants/' ) !== false )
2015-07-10 17:27:06 +02:00
{
$types = array ();
foreach ( $this -> bo -> resources as $resource )
{
$types [ $resource [ 'app' ]] = array ();
}
foreach ( $event [ 'participants' ] as $uid => $status )
{
$type = $this -> bo -> resources [ $uid [ 0 ]][ 'app' ];
2016-05-01 11:57:48 +02:00
if ( $type == 'api-accounts' )
2015-07-10 17:27:06 +02:00
{
$type = ( $GLOBALS [ 'egw' ] -> accounts -> get_type ( $uid ) == 'g' ? 'group' : 'account' );
}
$types [ $type ][] = $this -> bo -> participant_name ( $uid );
}
foreach ( $types as $t_id => $type )
{
2021-10-07 21:45:49 +02:00
$replacements [ '$$' . ( $prefix ? $prefix . '/' : '' ) . " calendar_participants/ { $t_id } $ $ " ] = implode ( ', ' , $type );
2015-07-10 17:27:06 +02:00
}
}
2018-02-15 19:50:55 +01:00
// Participant email list (not declined)
$this -> participant_emails ( $replacements , $record , $prefix , $content );
// Add participant summary
$this -> participant_summary ( $replacements , $record , $prefix , $content );
2021-10-07 21:45:49 +02:00
if ( ! $replacements [ '$$' . ( $prefix ? $prefix . '/' : '' ) . 'calendar_recur_type$$' ])
2011-03-15 00:25:51 +01:00
{
// Need to set it to '' if not set or previous record may be used
2021-10-07 21:45:49 +02:00
$replacements [ '$$' . ( $prefix ? $prefix . '/' : '' ) . 'calendar_recur_type$$' ] = '' ;
2011-03-15 00:25:51 +01:00
}
2021-10-07 21:45:49 +02:00
foreach ( array ( 'start' , 'end' ) as $what )
2011-02-09 16:58:06 +01:00
{
foreach ( array (
2021-10-07 21:45:49 +02:00
'date' => $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'dateformat' ],
'day' => 'l' ,
'time' => ( date ( 'Ymd' , $event [ 'start' ]) != date ( 'Ymd' , $event [ 'end' ]) ? $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'dateformat' ] . ' ' : '' ) . ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'timeformat' ] == 12 ? 'h:i a' : 'H:i' ),
) as $name => $format )
2011-02-09 16:58:06 +01:00
{
2021-10-07 21:45:49 +02:00
$value = Api\DateTime :: to ( $event [ $what ], $format );
if ( $format == 'l' )
{
$value = lang ( $value );
}
$replacements [ '$$' . ( $prefix ? $prefix . '/' : '' ) . 'calendar_' . $what . $name . '$$' ] = $value ;
2011-02-09 16:58:06 +01:00
}
}
2021-10-07 21:45:49 +02:00
$duration = ( $event [ 'end' ] - $event [ 'start' ]) / 60 ;
$replacements [ '$$' . ( $prefix ? $prefix . '/' : '' ) . 'calendar_duration$$' ] = floor ( $duration / 60 ) . lang ( 'h' ) . ( $duration % 60 ? $duration % 60 : '' );
2011-02-09 16:58:06 +01:00
2014-07-08 21:05:09 +02:00
// Add in contact stuff for owner
2021-10-07 21:45:49 +02:00
if ( strpos ( $content , '$$calendar_owner/' ) !== null && ( $user = $GLOBALS [ 'egw' ] -> accounts -> id2name ( $event [ 'owner' ], 'person_id' )))
2014-07-08 21:05:09 +02:00
{
2021-10-07 21:45:49 +02:00
$replacements += $this -> contact_replacements ( $user , ( $prefix ? $prefix . '/' : '' ) . 'calendar_owner' );
$replacements [ '$$' . ( $prefix ? $prefix . '/' : '' ) . 'calendar_owner/primary_group$$' ] = $GLOBALS [ 'egw' ] -> accounts -> id2name ( $GLOBALS [ 'egw' ] -> accounts -> id2name ( $event [ 'owner' ], 'account_primary_group' ));
2014-07-08 21:05:09 +02:00
}
2016-05-18 23:41:20 +02:00
if ( $content && strpos ( $content , '$$#' ) !== FALSE )
2012-04-11 22:03:11 +02:00
{
$this -> cf_link_to_expand ( $event , $content , $replacements );
}
2011-10-26 18:45:38 +02:00
// Links
2013-02-07 10:36:07 +01:00
$replacements += $this -> get_all_links ( 'calendar' , $event [ 'id' ], $prefix , $content );
2011-10-26 18:45:38 +02:00
2011-02-09 16:58:06 +01:00
return $replacements ;
}
2018-02-15 19:50:55 +01:00
/**
* Generate placeholder ( s ) for email addresses of all participants who have
* them .
*
* @ param Array $replacements Array of replacements
* @ param calendar_egw_record $record Event record
* @ param string $prefix Prefix of placeholder
* @ param string $content Content with placeholders in it
*/
public function participant_emails ( & $replacements , & $record , $prefix , & $content )
{
// Early exit if the placeholder is not used
2021-10-07 21:45:49 +02:00
if ( strpos ( $content , '$$' . ( $prefix ? $prefix . '/' : '' ) . 'participant_emails$$' ) === FALSE )
2018-02-15 19:50:55 +01:00
{
return false ;
}
$emails = array ();
2018-07-24 21:34:49 +02:00
$event = array (
'participants' => $record -> participants
);
$this -> bo -> enum_groups ( $event );
foreach ( $event [ 'participants' ] as $uid => $status )
2018-02-15 19:50:55 +01:00
{
// Skip rejected
2021-10-07 21:45:49 +02:00
if ( in_array ( substr ( $status , 0 , 1 ), array ( 'R' )))
2018-02-15 19:50:55 +01:00
{
continue ;
}
$info = $this -> bo -> resource_info ( $uid );
if ( $info [ 'email' ])
{
$emails [] = $info [ 'email' ];
}
}
2021-10-07 21:45:49 +02:00
$replacements [ '$$' . ( $prefix ? $prefix . '/' : '' ) . 'participant_emails$$' ] = implode ( ', ' , $emails );
2018-02-15 19:50:55 +01:00
}
/**
* Generate placeholder for a summary of participant status :
* 3 Participants : 1 Accepted , 2 Unknown
*
* Blank if only one participant , matches what ' s shown in UI event hover
*
* @ param Array $replacements Array of replacements
* @ param calendar_egw_record $record Event record
* @ param string $prefix Prefix of placeholder
* @ param string $content Content with placeholders in it
*/
public function participant_summary ( & $replacements , & $record , $prefix , & $content )
{
// Early exit if the placeholder is not used
2021-10-07 21:45:49 +02:00
if ( strpos ( $content , '$$' . ( $prefix ? $prefix . '/' : '' ) . 'participant_summary$$' ) === FALSE )
2018-02-15 19:50:55 +01:00
{
return false ;
}
2021-10-07 21:45:49 +02:00
$placeholder = '$$' . ( $prefix ? $prefix . '/' : '' ) . 'participant_summary$$' ;
2018-02-15 19:50:55 +01:00
// No summary for 1 participant
if ( count ( $record -> participants ) < 2 )
{
$replacements [ $placeholder ] = '' ;
}
$participant_status = array ( 'A' => 0 , 'R' => 0 , 'T' => 0 , 'U' => 0 , 'D' => 0 );
2021-10-07 21:45:49 +02:00
$status_label = array ( 'A' => 'accepted' , 'R' => 'rejected' , 'T' => 'tentative' , 'U' => 'unknown' ,
'D' => 'delegated' );
$participant_summary = count ( $record -> participants ) . ' ' . lang ( 'Participants' ) . ': ' ;
2018-02-15 19:50:55 +01:00
$status_totals = [];
foreach ( $record -> participants as $uid => $status )
{
2021-10-07 21:45:49 +02:00
$participant_status [ substr ( $status , 0 , 1 )] ++ ;
2018-02-15 19:50:55 +01:00
}
foreach ( $participant_status as $status => $count )
{
if ( $count > 0 )
{
$status_totals [] = $count . ' ' . lang ( $status_label [ $status ]);
}
}
2021-10-07 21:45:49 +02:00
$summary = $participant_summary . join ( ', ' , $status_totals );
2018-02-15 19:50:55 +01:00
$replacements [ $placeholder ] = $summary ;
}
2011-02-09 16:58:06 +01:00
/**
2021-10-07 21:45:49 +02:00
* Table plugin for event
* Lists events for a certain day of the week . Only works for one week at a time , so for multiple weeks ,
* use multiple date ranges .
*
* Use :
* $$table / Monday $ $ $$starttime $ $ $$title $ $ $$endtable $ $
* The day of the week may be language specific ( date ( 'l' )) .
*
* @ param string $plugin ( Monday - Sunday )
* @ param int / array date or date range
* @ param int $n Row number
* @ param string $repeat Text being repeated for each entry
* @ return array
*/
public function day_plugin ( $plugin , $date , $n , $repeat )
2011-02-09 16:58:06 +01:00
{
2014-11-19 13:20:59 +01:00
static $days = null ;
2021-10-07 21:45:49 +02:00
if ( is_array ( $date ) && ! $date [ 'start' ])
{
2011-06-22 19:23:04 +02:00
// List of IDs
2021-10-07 21:45:49 +02:00
if ( $date [ 0 ][ 'start' ])
{
2011-06-22 19:23:04 +02:00
$id = array ( 'start' => PHP_INT_MAX , 'end' => 0 );
2021-10-07 21:45:49 +02:00
foreach ( $date as $event )
{
if ( $event [ 'start' ] && $event [ 'start' ] < $id [ 'start' ])
{
$id [ 'start' ] = $event [ 'start' ];
}
if ( $event [ 'end' ] && $event [ 'end' ] > $id [ 'end' ])
{
$id [ 'end' ] = $event [ 'end' ];
}
2011-06-22 19:23:04 +02:00
}
$date = $id ;
2021-10-07 21:45:49 +02:00
}
else
{
2011-06-22 19:23:04 +02:00
$event = $this -> bo -> read ( is_array ( $date ) ? $date [ 'id' ] : $date , is_array ( $date ) ? $date [ 'recur_date' ] : null );
2021-10-07 21:45:49 +02:00
if ( date ( 'l' , $event [ 'start' ]) != $plugin )
{
return array ();
}
2011-06-22 19:23:04 +02:00
$date = $event [ 'start' ];
}
2011-02-09 16:58:06 +01:00
}
2021-11-23 23:50:10 +01:00
$_date = new Api\DateTime ([ 'start' ] ? $date [ 'start' ] : $date );
if ( $days [ $_date -> format ( 'Ymd' )][ $plugin ])
{
return $days [ $_date -> format ( 'Ymd' )][ $plugin ][ $n ];
}
2011-02-09 16:58:06 +01:00
2011-02-16 18:19:19 +01:00
$events = $this -> bo -> search ( $this -> query + array (
2021-10-07 21:45:49 +02:00
'start' => $date [ 'end' ] ? $date [ 'start' ] : mktime ( 0 , 0 , 0 , date ( 'm' , $_date ), date ( 'd' , $_date ), date ( 'Y' , $_date )),
'end' => $date [ 'end' ] ? $date [ 'end' ] : mktime ( 23 , 59 , 59 , date ( 'm' , $_date ), date ( 'd' , $_date ), date ( 'Y' , $_date )),
'offset' => 0 ,
'num_rows' => 20 ,
'order' => 'cal_start' ,
'daywise' => true ,
'cfs' => array (), // read all custom-fields
)
);
2011-06-22 19:23:04 +02:00
2014-11-19 13:20:59 +01:00
if ( true ) $days = array ();
2011-02-09 16:58:06 +01:00
$replacements = array ();
2011-03-28 18:55:24 +02:00
$time_format = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'timeformat' ] == 12 ? 'h:i a' : 'H:i' ;
2012-04-11 22:03:11 +02:00
foreach ( $events as $day => $list )
2011-02-09 16:58:06 +01:00
{
2014-11-19 13:20:59 +01:00
foreach ( $list as $event )
2011-02-09 16:58:06 +01:00
{
2021-10-07 21:45:49 +02:00
$event_id = $event [ 'id' ] . ( $event [ 'recur_date' ] ? ':' . $event [ 'recur_date' ] : '' );
if ( $this -> ids && ! in_array ( $event_id , $this -> ids ))
{
continue ;
}
2016-05-01 19:47:59 +02:00
$start = Api\DateTime :: to ( $event [ 'start' ], 'array' );
$end = Api\DateTime :: to ( $event [ 'end' ], 'array' );
2011-03-28 18:55:24 +02:00
$replacements = $this -> calendar_replacements ( $event );
2021-10-07 21:45:49 +02:00
if ( $start [ 'year' ] == $end [ 'year' ] && $start [ 'month' ] == $end [ 'month' ] && $start [ 'day' ] == $end [ 'day' ])
{
$dow = date ( 'l' , $event [ 'start' ]);
}
else
{
2011-02-18 17:09:42 +01:00
$dow = date ( 'l' , strtotime ( $day ));
2011-03-28 18:55:24 +02:00
// Fancy date+time formatting for multi-day events
2021-10-07 21:45:49 +02:00
$replacements [ '$$calendar_starttime$$' ] = date ( $time_format , $day == date ( 'Ymd' , $event [ 'start' ]) ? $event [ 'start' ] : mktime ( 0 , 0 , 0 , 0 , 0 , 1 ));
$replacements [ '$$calendar_endtime$$' ] = date ( $time_format , $day == date ( 'Ymd' , $event [ 'end' ]) ? $event [ 'end' ] : mktime ( 23 , 59 , 59 , 0 , 0 , 0 ));
2011-02-18 17:09:42 +01:00
}
2012-05-15 15:23:11 +02:00
2021-11-23 23:50:10 +01:00
$days [ $_date -> format ( 'Ymd' )][ $dow ][] = $replacements ;
2011-02-09 16:58:06 +01:00
}
2021-10-07 21:45:49 +02:00
if ( strpos ( $repeat , 'day/date' ) !== false || strpos ( $repeat , 'day/name' ) !== false )
{
2011-02-22 19:45:33 +01:00
$date_marker = array (
'$$day/date$$' => date ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'dateformat' ], strtotime ( $day )),
'$$day/name$$' => lang ( date ( 'l' , strtotime ( $day )))
);
2021-11-23 23:50:10 +01:00
if ( ! is_array ( $days [ $_date -> format ( 'Ymd' )][ date ( 'l' , strtotime ( $day ))]))
2021-10-07 21:45:49 +02:00
{
2011-02-18 16:30:21 +01:00
$blank = $this -> calendar_replacements ( array ());
2014-11-19 13:20:59 +01:00
foreach ( $blank as & $value )
{
$value = '' ;
}
2021-11-23 23:50:10 +01:00
$days [ $_date -> format ( 'Ymd' )][ date ( 'l' , strtotime ( $day ))][] = $blank ;
2011-02-17 19:18:06 +01:00
}
2021-11-23 23:50:10 +01:00
$days [ $_date -> format ( 'Ymd' )][ date ( 'l' , strtotime ( $day ))][ 0 ] += $date_marker ;
2011-02-17 19:18:06 +01:00
}
2017-07-27 22:41:16 +02:00
// Add in birthdays
if ( strpos ( $repeat , 'day/birthdays' ) !== false )
{
2021-11-23 23:50:10 +01:00
$days [ $_date -> format ( 'Ymd' )][ date ( 'l' , strtotime ( $day ))][ 0 ][ '$$day/birthdays$$' ] = $this -> get_birthdays ( $day );
2017-07-27 22:41:16 +02:00
}
2011-02-09 16:58:06 +01:00
}
2021-11-23 23:50:10 +01:00
return $days [ $_date -> format ( 'Ymd' )][ $plugin ][ 0 ];
2011-02-09 16:58:06 +01:00
}
2011-02-15 21:35:46 +01:00
/**
2021-10-07 21:45:49 +02:00
* Table plugin for a certain date
*
* Can be either a particular date ( 2011 - 02 - 15 ) or a day of the month ( 15 )
*
* @ param string $plugin
* @ param int $id ID for this record
* @ param int $n Repeated row number
* @ param string $repeat Text being repeated for each entry
* @ return array
*/
public function day ( $plugin , $id , $n , $repeat )
2011-02-15 21:35:46 +01:00
{
2014-11-19 13:20:59 +01:00
static $days = null ;
2011-02-15 21:35:46 +01:00
// Figure out which day
2021-10-07 21:45:49 +02:00
list ( $type , $which ) = explode ( '_' , $plugin );
2011-02-15 21:35:46 +01:00
if ( $type == 'day' && $which )
{
2014-11-19 13:20:59 +01:00
$arr = $this -> bo -> date2array ( $id [ 'start' ]);
$arr [ 'day' ] = $which ;
$date = $this -> bo -> date2ts ( $arr );
2021-10-07 21:45:49 +02:00
if ( is_array ( $id ) && $id [ 'start' ] && ( $date < $id [ 'start' ] || $date > $id [ 'end' ]))
{
return array ();
}
2011-02-15 21:35:46 +01:00
}
2021-10-07 21:45:49 +02:00
elseif ( $plugin == 'selected' )
2012-02-16 17:55:42 +01:00
{
$date = $id [ 'start' ];
}
2011-02-15 21:35:46 +01:00
else
{
$date = strtotime ( $plugin );
}
2021-10-07 21:45:49 +02:00
if ( $type == 'day' && is_array ( $id ) && ! $id [ 'start' ])
{
2011-02-15 21:35:46 +01:00
$event = $this -> bo -> read ( is_array ( $id ) ? $id [ 'id' ] : $id , is_array ( $id ) ? $id [ 'recur_date' ] : null );
2021-10-07 21:45:49 +02:00
if ( $which && date ( 'd' , $event [ 'start' ]) != $which )
{
return array ();
}
if ( date ( 'Ymd' , $date ) != date ( 'Ymd' , $event [ 'start' ]))
{
return array ();
}
2011-02-15 21:35:46 +01:00
return $n == 0 ? $this -> calendar_replacements ( $event ) : array ();
}
2012-05-15 15:23:11 +02:00
2011-02-15 21:35:46 +01:00
// Use start for cache, in case of multiple months
$_date = $id [ 'start' ] ? $id [ 'start' ] : $date ;
2021-10-07 21:45:49 +02:00
if ( $days [ date ( 'Ymd' , $_date )][ $plugin ]) return $days [ date ( 'Ymd' , $_date )][ $plugin ][ $n ];
2011-02-15 21:35:46 +01:00
2011-02-16 18:19:19 +01:00
$events = $this -> bo -> search ( $this -> query + array (
2021-10-07 21:45:49 +02:00
'start' => $date ,
'end' => mktime ( 23 , 59 , 59 , date ( 'm' , $date ), date ( 'd' , $date ), date ( 'Y' , $date )),
'offset' => 0 ,
'num_rows' => 20 ,
'order' => 'cal_start' ,
'daywise' => true ,
'cfs' => array (), // read all custom-fields
)
);
2011-02-15 21:35:46 +01:00
$replacements = array ();
2014-11-19 13:20:59 +01:00
if ( true ) $days = array ();
2011-03-29 17:10:55 +02:00
$time_format = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'timeformat' ] == 12 ? 'h:i a' : 'H:i' ;
2012-04-11 22:03:11 +02:00
foreach ( $events as $day => $list )
2011-02-15 21:35:46 +01:00
{
2014-11-19 13:20:59 +01:00
foreach ( $list as $event )
2011-02-15 21:35:46 +01:00
{
2021-10-07 21:45:49 +02:00
$event_id = $event [ 'id' ] . ( $event [ 'recur_date' ] ? ':' . $event [ 'recur_date' ] : '' );
if ( $this -> ids && ! in_array ( $event_id , $this -> ids ))
{
continue ;
}
2016-05-01 19:47:59 +02:00
$start = Api\DateTime :: to ( $event [ 'start' ], 'array' );
$end = Api\DateTime :: to ( $event [ 'end' ], 'array' );
2011-03-28 18:55:24 +02:00
$replacements = $this -> calendar_replacements ( $event );
2021-10-07 21:45:49 +02:00
if ( $start [ 'year' ] == $end [ 'year' ] && $start [ 'month' ] == $end [ 'month' ] && $start [ 'day' ] == $end [ 'day' ])
{
2014-11-19 13:20:59 +01:00
//$dow = date('l',$event['start']);
2021-10-07 21:45:49 +02:00
}
else
{
2011-03-28 18:55:24 +02:00
// Fancy date+time formatting for multi-day events
2021-10-07 21:45:49 +02:00
$replacements [ '$$calendar_starttime$$' ] = date ( $time_format , $day == date ( 'Ymd' , $event [ 'start' ]) ? $event [ 'start' ] : mktime ( 0 , 0 , 0 , 0 , 0 , 1 ));
$replacements [ '$$calendar_endtime$$' ] = date ( $time_format , $day == date ( 'Ymd' , $event [ 'end' ]) ? $event [ 'end' ] : mktime ( 23 , 59 , 59 , 0 , 0 , 0 ));
2011-03-28 18:55:24 +02:00
}
2021-10-07 21:45:49 +02:00
$days [ date ( 'Ymd' , $_date )][ $plugin ][] = $replacements ;
2011-02-15 21:35:46 +01:00
}
2021-10-07 21:45:49 +02:00
if ( strpos ( $repeat , 'day/date' ) !== false || strpos ( $repeat , 'day/name' ) !== false )
{
2011-02-22 19:45:33 +01:00
$date_marker = array (
'$$day/date$$' => date ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'dateformat' ], strtotime ( $day )),
'$$day/name$$' => lang ( date ( 'l' , strtotime ( $day )))
);
2021-10-07 21:45:49 +02:00
if ( ! is_array ( $days [ date ( 'Ymd' , $_date )][ $plugin ]))
{
2011-02-18 16:30:21 +01:00
$blank = $this -> calendar_replacements ( array ());
2014-11-19 13:20:59 +01:00
foreach ( $blank as & $value )
{
$value = '' ;
}
2021-10-07 21:45:49 +02:00
$days [ date ( 'Ymd' , $_date )][ $plugin ][] = $blank ;
2011-02-17 19:18:06 +01:00
}
2021-10-07 21:45:49 +02:00
$days [ date ( 'Ymd' , $_date )][ $plugin ][ 0 ] += $date_marker ;
2011-02-17 19:18:06 +01:00
}
2017-07-27 22:41:16 +02:00
// Add in birthdays
if ( strpos ( $repeat , 'day/birthdays' ) !== false )
{
2021-10-07 21:45:49 +02:00
$days [ date ( 'Ymd' , $_date )][ date ( 'l' , strtotime ( $day ))][ 0 ][ '$$day/birthdays$$' ] = $this -> get_birthdays ( $day );
2017-07-27 22:41:16 +02:00
}
2011-02-15 21:35:46 +01:00
}
2021-10-07 21:45:49 +02:00
return $days [ date ( 'Ymd' , $_date )][ $plugin ][ 0 ];
2011-02-15 21:35:46 +01:00
}
2011-02-09 16:58:06 +01:00
/**
2021-10-07 21:45:49 +02:00
* Table plugin for participants
*
* Copied from eventmgr resources
*
* @ param string $plugin
* @ param int $id
* @ param int $n
* @ return array
*/
public function participant ( $plugin , $id , $n )
2011-02-09 16:58:06 +01:00
{
2021-10-07 21:45:49 +02:00
unset ( $plugin ); // not used, but required by function signature
2014-11-19 13:20:59 +01:00
2021-10-07 21:45:49 +02:00
if ( ! is_array ( $id ) || ! $id [ 'start' ])
{
2011-02-09 16:58:06 +01:00
$event = $this -> bo -> read ( is_array ( $id ) ? $id [ 'id' ] : $id , is_array ( $id ) ? $id [ 'recur_date' ] : null );
2021-10-07 21:45:49 +02:00
}
else
{
2011-02-09 16:58:06 +01:00
$event = $id ;
}
2021-10-07 21:45:49 +02:00
if ( ! is_array ( $event [ 'participants' ]) || $n >= count ( $event [ 'participants' ]))
{
return array ();
}
2011-02-09 16:58:06 +01:00
$participant = null ;
$status = null ;
$i = - 1 ;
2021-10-07 21:45:49 +02:00
foreach ( $event [ 'participants' ] as $participant => $status )
{
if ( ++ $i == $n )
{
break ;
}
2011-02-09 16:58:06 +01:00
}
2021-10-07 21:45:49 +02:00
if ( ! $participant )
{
return array ();
}
2011-02-09 16:58:06 +01:00
// Add some common information
2014-11-19 13:20:59 +01:00
$quantity = $role = null ;
2021-10-07 21:45:49 +02:00
calendar_so :: split_status ( $status , $quantity , $role );
if ( $role != 'REQ-PARTICIPANT' )
2011-02-09 16:58:06 +01:00
{
2021-10-07 21:45:49 +02:00
if ( isset ( $this -> bo -> roles [ $role ]))
2011-02-09 16:58:06 +01:00
{
$role = lang ( $this -> bo -> roles [ $role ]);
}
// allow to use cats as roles (beside regular iCal ones)
2021-10-07 21:45:49 +02:00
elseif ( substr ( $role , 0 , 6 ) == 'X-CAT-' && ( $cat_id = ( int ) substr ( $role , 6 )) > 0 )
2011-02-09 16:58:06 +01:00
{
$role = $GLOBALS [ 'egw' ] -> categories -> id2name ( $cat_id );
}
else
{
2021-10-07 21:45:49 +02:00
$role = lang ( str_replace ( 'X-' , '' , $role ));
2011-02-09 16:58:06 +01:00
}
}
$info = array (
2021-10-07 21:45:49 +02:00
'name' => $this -> bo -> participant_name ( $participant ),
'status' => lang ( $this -> bo -> verbose_status [ $status ]),
'quantity' => $quantity ,
'role' => $role
2011-02-09 16:58:06 +01:00
);
2012-05-15 15:23:11 +02:00
2021-10-07 21:45:49 +02:00
switch ( $participant [ 0 ])
2011-02-09 16:58:06 +01:00
{
case 'c' :
2021-10-07 21:45:49 +02:00
$replacements = $this -> contact_replacements ( substr ( $participant , 1 ), '' );
2011-02-09 16:58:06 +01:00
break ;
case 'r' :
2021-10-07 21:45:49 +02:00
if ( is_null ( self :: $resources ))
{
self :: $resources = new resources_bo ();
}
if (( $resource = self :: $resources -> read ( substr ( $participant , 1 ))))
2011-02-09 16:58:06 +01:00
{
foreach ( $resource as $name => $value )
{
2021-10-07 21:45:49 +02:00
$replacements [ '$$' . $name . '$$' ] = $value ;
2011-02-09 16:58:06 +01:00
}
}
break ;
default :
2021-10-07 21:45:49 +02:00
if ( is_numeric ( $participant ) && ( $contact = $GLOBALS [ 'egw' ] -> accounts -> id2name ( $participant , 'person_id' )))
2011-02-09 16:58:06 +01:00
{
2021-10-07 21:45:49 +02:00
$replacements = $this -> contact_replacements ( $contact , '' );
2011-02-09 16:58:06 +01:00
}
break ;
}
foreach ( $info as $name => $value )
{
2021-10-07 21:45:49 +02:00
$replacements [ '$$' . $name . '$$' ] = $value ;
2011-02-09 16:58:06 +01:00
}
return $replacements ;
}
2017-07-27 22:41:16 +02:00
/**
* Get replacement for birthdays placeholder
2017-07-27 22:41:16 +02:00
* @ param String $day Date in Ymd format
2017-07-27 22:41:16 +02:00
*/
2017-07-27 22:41:16 +02:00
protected function get_birthdays ( $day )
2017-07-27 22:41:16 +02:00
{
$contacts = new Api\Contacts ();
2021-10-07 21:45:49 +02:00
$birthdays = array ();
2017-07-27 22:41:16 +02:00
foreach ( $contacts -> get_addressbooks () as $owner => $name )
{
2017-07-27 22:41:16 +02:00
$birthdays += $contacts -> read_birthdays ( $owner , substr ( $day , 0 , 4 ));
2017-07-27 22:41:16 +02:00
}
return $birthdays [ $day ] ? implode ( ', ' , array_column ( $birthdays [ $day ], 'name' )) : '' ;
}
2017-12-18 22:28:46 +01:00
/**
* Validate and properly format a list of 'ID' s into either a list of ranges
* or a list of IDs , depending on what the template needs . Templates using
* the range placeholder need a list of date ranges , templates using pagerepeat
2018-02-07 19:02:50 +01:00
* need a list of individual events . Templates using neither get just the
* first ID
2017-12-18 22:28:46 +01:00
*
* @ param Array [] | String [] $ids List of IDs , which can be a list of individual
2021-10-07 21:45:49 +02:00
* event IDs , entire events , a date range ( start & end ) or a list of date ranges .
2017-12-18 22:28:46 +01:00
* @ param String $content Template content , used to determine what style of
2021-10-07 21:45:49 +02:00
* ID is needed .
2017-12-18 22:28:46 +01:00
*/
2021-10-07 21:45:49 +02:00
protected function validate_ids ( array $ids , $content )
2017-12-18 22:28:46 +01:00
{
$validated_ids = array ();
if (( strpos ( $content , '$$range' ) !== false || strpos ( $content , '{{range' ) !== false ) && is_array ( $ids ))
{
// Merging into a template that uses range - need ranges, got events
2021-11-15 08:26:06 +01:00
if ( is_array ( $ids ) && ( is_array ( $ids [ 0 ]) && isset ( $ids [ 0 ][ 'id' ]) || is_string ( $ids [ 0 ])))
2017-12-18 22:28:46 +01:00
{
// Passed an array of events, to be handled like a date range
$events = $ids ;
$validated_ids = ( array ) $this -> events_to_range ( $ids );
}
2021-10-07 21:45:49 +02:00
else if ( is_array ( $ids ) && $ids [ 0 ][ 'start' ])
2017-12-18 22:28:46 +01:00
{
// Got a list of ranges
$validated_ids = $ids ;
}
}
// Handle merging a range of events into a document with pagerepeat instead of range
2021-10-07 21:45:49 +02:00
else if (( strpos ( $content , '$$pagerepeat' ) !== false || strpos ( $content , '{{pagerepeat' ) !== false )
2017-12-18 22:28:46 +01:00
&& (( strpos ( $content , '$$range' ) === false && strpos ( $content , '{{range' ) === false )))
{
2021-11-15 08:26:06 +01:00
if ( is_array ( $ids ) && ! ( is_array ( $ids [ 0 ]) && isset ( $ids [ 0 ][ 'id' ]) || is_string ( $ids [ 0 ])))
2017-12-18 22:28:46 +01:00
{
foreach ( $ids as $range )
{
// Passed a range, needs to be expanded into list of events
$events = $this -> bo -> search ( $this -> query + $range + array (
2021-10-07 21:45:49 +02:00
'offset' => 0 ,
'enum_recuring' => true ,
'order' => 'cal_start' ,
'cfs' => strpos ( $content , '#' ) !== false ? array_keys ( Api\Storage\Customfields :: get ( 'calendar' )) : null
)
);
foreach ( $events as $event )
{
2017-12-18 22:28:46 +01:00
$validated_ids [] = $event ;
}
}
}
else
{
foreach ( $ids as $id )
{
$validated_ids [] = $this -> normalize_event_id ( $id );
}
}
}
2018-02-07 19:02:50 +01:00
else
{
$validated_ids [] = $this -> normalize_event_id ( array_shift ( $ids ));
}
2017-12-18 22:28:46 +01:00
return $validated_ids ;
}
/**
* Convert a list of event IDs into a range
*
* @ param String [] | Array [] $ids Some event identifier , in either string or array form
*/
protected function events_to_range ( $ids )
{
$limits = array ( 'start' => PHP_INT_MAX , 'end' => 0 );
$this -> ids = array ();
2021-10-07 21:45:49 +02:00
foreach ( $ids as $event )
{
2017-12-18 22:28:46 +01:00
$event = $this -> normalize_event_id ( $event );
2021-10-07 21:45:49 +02:00
if ( $event [ 'start' ] && Api\DateTime :: to ( $event [ 'start' ], 'ts' ) < $limits [ 'start' ])
{
$limits [ 'start' ] = Api\DateTime :: to ( $event [ 'start' ], 'ts' );
}
if ( $event [ 'end' ] && Api\DateTime :: to ( $event [ 'end' ], 'ts' ) > $limits [ 'end' ])
{
$limits [ 'end' ] = Api\DateTime :: to ( $event [ 'end' ], 'ts' );
}
2017-12-18 22:28:46 +01:00
// Keep ids for future use
if ( $event [ 'id' ])
{
2021-10-07 21:45:49 +02:00
$this -> ids [] = $event [ 'id' ] . ( $event [ 'recur_date' ] ? ':' . $event [ 'recur_date' ] : '' );
2017-12-18 22:28:46 +01:00
}
}
// Check a start was found
if ( $limits [ 'start' ] == PHP_INT_MAX )
{
// Start of today
$limits [ 'start' ] = mktime ( 0 , 0 , 0 );
}
// Check an end was found
if ( $limits [ 'end' ] == 0 )
{
// End of today
$limits [ 'end' ] = mktime ( 25 , 59 , 59 );
}
$limits [ 'start' ] = new Api\DateTime ( $limits [ 'start' ]);
$limits [ 'end' ] = new Api\DateTime ( $limits [ 'end' ]);
// Align with user's week
2021-10-07 21:45:49 +02:00
$limits [ 'start' ] -> setTime ( 0 , 0 );
2017-12-18 22:28:46 +01:00
$limits [ 'start' ] -> setWeekstart ();
// Ranges should be at most a week, since that's what our templates expect
$rrule = new calendar_rrule ( $limits [ 'start' ], calendar_rrule :: WEEKLY , 1 , $limits [ 'end' ]);
$rrule -> rewind ();
do
{
$current = $rrule -> current ();
$rrule -> next_no_exception ();
$validated_ids [] = array (
'start' => Api\DateTime :: to ( $current , 'ts' ),
2021-10-07 21:45:49 +02:00
'end' => Api\DateTime :: to ( $rrule -> current (), 'ts' ) - 1
2017-12-18 22:28:46 +01:00
);
2021-10-07 21:45:49 +02:00
}
while ( $rrule -> valid ());
2017-12-18 22:28:46 +01:00
return $validated_ids ;
}
/**
* Normalize a calendar event ID into a standard array .
*
* Depending on where they come from , IDs can be passed in as colon separated ,
* an array with ID & recur_date , or be a full event . They can also be a
* date range with start and end , rather than a single event .
*
* @ param String | Array $id Some record identifier , in either string or array form
*
* @ param Array If an id for a single event is passed in , an array with id & recur_date ,
2021-10-07 21:45:49 +02:00
* otherwise a range with start & end .
2017-12-18 22:28:46 +01:00
*/
protected function normalize_event_id ( $id )
{
2021-11-15 14:53:35 +01:00
if ( is_string ( $id ) || is_array ( $id ) && ! empty ( $id [ 'id' ]) && empty ( $id [ 'start' ]))
2021-10-07 21:45:49 +02:00
{
2021-11-15 14:53:35 +01:00
if ( is_string ( $id ) && strpos ( $id , ':' ))
2017-12-18 22:28:46 +01:00
{
$_id = $id ;
$id = array ();
2021-10-07 21:45:49 +02:00
list ( $id [ 'id' ], $id [ 'recur_date' ]) = explode ( ':' , $_id );
2017-12-18 22:28:46 +01:00
}
$event = $this -> bo -> read ( is_array ( $id ) ? $id [ 'id' ] : $id , is_array ( $id ) ? $id [ 'recur_date' ] : null );
2021-10-07 21:45:49 +02:00
}
else
{
2017-12-18 22:28:46 +01:00
$event = $id ;
}
return $event ;
}
2011-02-09 16:58:06 +01:00
/**
* Generate table with replacements for the preferences
*
*/
public function show_replacements ()
{
2016-05-01 19:47:59 +02:00
Api\Translation :: add_app ( 'calendar' );
2021-10-07 21:45:49 +02:00
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'app_header' ] = lang ( 'calendar' ) . ' - ' . lang ( 'Replacements for inserting events into documents' );
2015-07-15 18:29:10 +02:00
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'nonavbar' ] = true ;
2016-05-14 21:26:36 +02:00
echo $GLOBALS [ 'egw' ] -> framework -> header ();
2011-02-09 16:58:06 +01:00
echo " <table width='90%' align='center'> \n " ;
2021-10-07 21:45:49 +02:00
echo '<tr><td colspan="4"><h3>' . lang ( 'Calendar fields:' ) . " </h3></td></tr> " ;
2011-02-09 16:58:06 +01:00
2014-11-19 13:20:59 +01:00
$n = 0 ;
2011-02-09 16:58:06 +01:00
foreach ( array (
2021-10-07 21:45:49 +02:00
'calendar_id' => lang ( 'Calendar ID' ),
'calendar_title' => lang ( 'Title' ),
'calendar_description' => lang ( 'Description' ),
'calendar_participants' => lang ( 'Participants' ),
'calendar_location' => lang ( 'Location' ),
'calendar_start' => lang ( 'Start' ) . ': ' . lang ( 'Date' ) . '+' . lang ( 'Time' ),
'calendar_startday' => lang ( 'Start' ) . ': ' . lang ( 'Weekday' ),
'calendar_startdate' => lang ( 'Start' ) . ': ' . lang ( 'Date' ),
'calendar_starttime' => lang ( 'Start' ) . ': ' . lang ( 'Time' ),
'calendar_end' => lang ( 'End' ) . ': ' . lang ( 'Date' ) . '+' . lang ( 'Time' ),
'calendar_endday' => lang ( 'End' ) . ': ' . lang ( 'Weekday' ),
'calendar_enddate' => lang ( 'End' ) . ': ' . lang ( 'Date' ),
'calendar_endtime' => lang ( 'End' ) . ': ' . lang ( 'Time' ),
'calendar_duration' => lang ( 'Duration' ),
'calendar_category' => lang ( 'Category' ),
'calendar_priority' => lang ( 'Priority' ),
'calendar_updated' => lang ( 'Updated' ),
'calendar_recur_type' => lang ( 'Repetition' ),
'calendar_access' => lang ( 'Access' ) . ': ' . lang ( 'public' ) . ', ' . lang ( 'private' ),
'calendar_owner' => lang ( 'Owner' ),
) as $name => $label )
{
if ( in_array ( $name , array ( 'start' ,
'end' )) && $n & 1 ) // main values, which should be in the first column
2011-02-09 16:58:06 +01:00
{
echo " </tr> \n " ;
$n ++ ;
}
2021-10-07 21:45:49 +02:00
if ( ! ( $n & 1 ))
{
echo '<tr>' ;
}
echo '<td>{{' . $name . '}}</td><td>' . $label . '</td>' ;
if ( $n & 1 )
{
echo " </tr> \n " ;
}
2011-02-09 16:58:06 +01:00
$n ++ ;
}
2021-10-07 21:45:49 +02:00
echo '<tr><td colspan="4"><h3>' . lang ( 'Range fields' ) . " :</h3></td></tr> " ;
echo '<tr><td colspan="4">' . lang ( 'If you select a range (month, week, etc) instead of a list of entries, these extra fields are available' ) . '</td></tr>' ;
2014-11-19 13:20:59 +01:00
foreach ( array_keys ( self :: $range_tags ) as $name )
2011-02-15 21:35:46 +01:00
{
2021-10-07 21:45:49 +02:00
echo '<tr><td>{{range/' . $name . '}}</td><td>' . lang ( $name ) . " </td></tr> \n " ;
2011-02-15 21:35:46 +01:00
}
2021-10-07 21:45:49 +02:00
echo '<tr><td colspan="4"><h3>' . lang ( 'Custom fields' ) . " :</h3></td></tr> " ;
2016-05-01 19:47:59 +02:00
$custom = Api\Storage\Customfields :: get ( 'calendar' );
2011-02-09 16:58:06 +01:00
foreach ( $custom as $name => $field )
{
2021-10-07 21:45:49 +02:00
echo '<tr><td>{{#' . $name . '}}</td><td colspan="3">' . $field [ 'label' ] . " </td></tr> \n " ;
2011-02-09 16:58:06 +01:00
}
2015-07-10 17:27:06 +02:00
2021-10-07 21:45:49 +02:00
echo '<tr><td colspan="4"><h3>' . lang ( 'Participants' ) . " :</h3></td></tr> " ;
echo '<tr><td>{{participant_emails}}</td><td colspan="3">' . lang ( 'A list of email addresses of all participants who have not declined' ) . " </td></tr> \n " ;
echo '<tr><td>{{participant_summary}}</td><td colspan="3">' . lang ( 'Summary of participant status: 3 Participants: 1 Accepted, 2 Unknown' ) . " </td></tr> \n " ;
echo '<tr><td colspan="4">' . lang ( 'Participant names by type' ) . '</td></tr>' ;
echo '<tr><td>{{calendar_participants/account}}</td><td colspan="3">' . lang ( 'Accounts' ) . " </td></tr> \n " ;
echo '<tr><td>{{calendar_participants/group}}</td><td colspan="3">' . lang ( 'Groups' ) . " </td></tr> \n " ;
2015-07-10 17:27:06 +02:00
foreach ( $this -> bo -> resources as $resource )
{
if ( $resource [ 'type' ])
{
2021-10-07 21:45:49 +02:00
echo '<tr><td>{{calendar_participants/' . $resource [ 'app' ] . '}}</td><td colspan="3">' . lang ( $resource [ 'app' ]) . " </td></tr> \n " ;
2015-07-10 17:27:06 +02:00
}
}
2021-10-07 21:45:49 +02:00
echo '<tr><td colspan="4"><h3>' . lang ( 'Participant table' ) . " :</h3></td></tr> " ;
2011-02-18 18:02:14 +01:00
echo '<tr><td colspan="4">{{table/participant}} ... </td></tr>' ;
2021-10-07 21:45:49 +02:00
echo '<tr><td>{{name}}</td><td>' . lang ( 'name' ) . '</td></tr>' ;
echo '<tr><td>{{role}}</td><td>' . lang ( 'role' ) . '</td></tr>' ;
echo '<tr><td>{{quantity}}</td><td>' . lang ( 'quantity' ) . '</td></tr>' ;
echo '<tr><td>{{status}}</td><td>' . lang ( 'status' ) . '</td></tr>' ;
2011-02-18 18:02:14 +01:00
echo '<tr><td colspan="4">{{endtable}}</td></tr>' ;
2012-05-15 15:23:11 +02:00
2011-02-15 21:35:46 +01:00
echo '<tr style="vertical-align:top"><td colspan="2"><table >' ;
2021-10-07 21:45:49 +02:00
echo '<tr><td><h3>' . lang ( 'Day of week tables' ) . " :</h3></td></tr> " ;
2011-02-09 16:58:06 +01:00
$days = array ();
for ( $i = 0 ; $i < 7 ; $i ++ )
{
2021-10-07 21:45:49 +02:00
$days [ date ( 'N' , strtotime ( " + $i days " ))] = date ( 'l' , strtotime ( " + $i days " ));
2011-02-09 16:58:06 +01:00
}
ksort ( $days );
foreach ( $days as $day )
{
2021-10-07 21:45:49 +02:00
echo '<tr><td>{{table/' . $day . '}} ... {{endtable}}</td></tr>' ;
2011-02-15 21:35:46 +01:00
}
2011-02-17 19:18:06 +01:00
echo '</table></td><td colspan="2"><table >' ;
2021-10-07 21:45:49 +02:00
echo '<tr><td><h3>' . lang ( 'Daily tables' ) . " :</h3></td></tr> " ;
foreach ( self :: $relative as $value )
{
echo '<tr><td>{{table/' . $value . '}} ... {{endtable}}</td></tr>' ;
2011-02-09 16:58:06 +01:00
}
2011-02-18 18:02:14 +01:00
echo '<tr><td>{{table/day_n}} ... {{endtable}}</td><td>1 <= n <= 31</td></tr>' ;
2011-02-15 21:35:46 +01:00
echo '</table></td></tr>' ;
2021-10-07 21:45:49 +02:00
echo '<tr><td colspan="2">' . lang ( 'Available for the first entry inside each day of week or daily table inside the selected range:' ) . '</td></tr>' ;
echo '<tr><td>{{day/date}}</td><td colspan="3">' . lang ( 'Date for the day of the week' ) . '</td></tr>' ;
echo '<tr><td>{{day/name}}</td><td colspan="3">' . lang ( 'Name of the day of the week (ex: Monday)' ) . '</td></tr>' ;
echo '<tr><td>{{day/birthdays}}</td><td colspan="3">' . lang ( 'Birthdays' ) . '</td></tr>' ;
2011-02-09 16:58:06 +01:00
2021-10-07 21:45:49 +02:00
echo '<tr><td colspan="4"><h3>' . lang ( 'General fields:' ) . " </h3></td></tr> " ;
2020-10-06 18:56:08 +02:00
foreach ( $this -> get_common_replacements () as $name => $label )
2011-02-09 16:58:06 +01:00
{
2021-10-07 21:45:49 +02:00
echo '<tr><td>{{' . $name . '}}</td><td colspan="3">' . $label . " </td></tr> \n " ;
2011-02-09 16:58:06 +01:00
}
echo " </table> \n " ;
2016-05-14 21:26:36 +02:00
echo $GLOBALS [ 'egw' ] -> framework -> footer ();
2011-02-09 16:58:06 +01:00
}
2021-10-07 21:45:49 +02:00
/**
* Get a list of placeholders provided .
*
* Placeholders are grouped logically . Group key should have a user - friendly translation .
*/
public function get_placeholder_list ( $prefix = '' )
{
$placeholders = array (
'event' => [],
'range' => [],
'participant' => [],
'customfields' => []
) + parent :: get_placeholder_list ( $prefix );
unset ( $placeholders [ 'placeholders' ]);
$fields = array (
'calendar_id' => lang ( 'Calendar ID' ),
'calendar_title' => lang ( 'Title' ),
'calendar_description' => lang ( 'Description' ),
'calendar_location' => lang ( 'Location' ),
'calendar_start' => lang ( 'Start' ) . ': ' . lang ( 'Date' ) . '+' . lang ( 'Time' ),
'calendar_startday' => lang ( 'Start' ) . ': ' . lang ( 'Weekday' ),
'calendar_startdate' => lang ( 'Start' ) . ': ' . lang ( 'Date' ),
'calendar_starttime' => lang ( 'Start' ) . ': ' . lang ( 'Time' ),
'calendar_end' => lang ( 'End' ) . ': ' . lang ( 'Date' ) . '+' . lang ( 'Time' ),
'calendar_endday' => lang ( 'End' ) . ': ' . lang ( 'Weekday' ),
'calendar_enddate' => lang ( 'End' ) . ': ' . lang ( 'Date' ),
'calendar_endtime' => lang ( 'End' ) . ': ' . lang ( 'Time' ),
'calendar_duration' => lang ( 'Duration' ),
'calendar_category' => lang ( 'Category' ),
'calendar_priority' => lang ( 'Priority' ),
'calendar_updated' => lang ( 'Updated' ),
'calendar_recur_type' => lang ( 'Repetition' ),
'calendar_access' => lang ( 'Access' ) . ': ' . lang ( 'public' ) . ', ' . lang ( 'private' ),
'calendar_owner' => lang ( 'Owner' ),
);
$group = 'event' ;
foreach ( $fields as $name => $label )
{
$marker = $this -> prefix ( $prefix , $name , '{' );
if ( ! array_filter ( $placeholders , function ( $a ) use ( $marker )
{
return array_key_exists ( $marker , $a );
}))
{
$placeholders [ $group ][] = [
'value' => $marker ,
'label' => $label
];
}
}
/**
* These ones only work if you select a range , not events
* $group = 'range' ;
* foreach ( array_keys ( self :: $range_tags ) as $name )
* {
* $marker = $this -> prefix ( $prefix , " range/ $name " , '{' );
* $placeholders [ $group ][] = [
* 'value' => $marker ,
* 'label' => lang ( $name )
* ];
* }
*/
$group = 'participant' ;
$placeholders [ $group ][] = array (
'value' => '{{calendar_participants}}' ,
'label' => lang ( 'Participants' )
);
$placeholders [ $group ][] = array (
'value' => '{{participant_emails}}' ,
'label' => 'Emails' ,
'title' => lang ( 'A list of email addresses of all participants who have not declined' )
);
$placeholders [ $group ][] = array (
'value' => '{{participant_summary}}' ,
'label' => 'participant summary' ,
'title' => lang ( 'Summary of participant status: 3 Participants: 1 Accepted, 2 Unknown' )
);
$placeholders [ $group ][] = array (
'value' => '{{calendar_participants/account}}' ,
'label' => lang ( 'Accounts' )
);
$placeholders [ $group ][] = array (
'value' => '{{calendar_participants/group}}' ,
'label' => lang ( 'Groups' )
);
foreach ( $this -> bo -> resources as $resource )
{
if ( $resource [ 'type' ])
{
$marker = $this -> prefix ( $prefix , 'calendar_participants/' . $resource [ 'app' ], '{' );
$placeholders [ $group ][] = array (
'value' => $marker ,
'label' => lang ( $resource [ 'app' ])
);
}
}
return $placeholders ;
}
2011-02-09 16:58:06 +01:00
}