Completed
Push — 14.2 ( 5702a1...375f73 )
by Nathan
38:22
created

calendar_merge::calendar_replacements()   F

Complexity

Conditions 36
Paths > 20000

Size

Total Lines 87
Code Lines 47

Duplication

Lines 10
Ratio 11.49 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 36
eloc 47
nc 62208
nop 3
dl 10
loc 87
rs 2.2431
c 2
b 0
f 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Calendar - document merge
4
 *
5
 * @link http://www.egroupware.org
6
 * @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
7
 * @author Nathan Gray
8
 * @package calendar
9
 * @copyright (c) 2007-14 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
10
 * @copyright 2011 Nathan Gray
11
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
12
 * @version $Id$
13
 */
14
15
/**
16
 * Calendar - document merge object
17
 */
18
class calendar_merge extends bo_merge
19
{
20
	/**
21
	 * Functions that can be called via menuaction
22
	 *
23
	 * @var array
24
	 */
25
	var $public_functions = array(
26
		'download_by_request'	=> true,
27
		'show_replacements'		=> true,
28
	);
29
30
	// Object for getting calendar info
31
	protected $bo;
32
33
	// Object used for getting resource info
34
	protected static $resources;
35
36
	/**
37
	 * Recognised relative days - used as a day table, like day_<n>
38
	 */
39
	protected static $relative = array(
40
		'today',
41
		'tomorrow',
42
		'yesterday',
43
		'selected',
44
	);
45
46
	/**
47
	 * If you use a range, these extra tags are available
48
	 */
49
	protected static $range_tags = array(
50
		'start'	=> 'Y-m-d',
51
		'end'	=> 'Y-m-d',
52
		'month'	=> 'F',
53
		'year'	=> 'Y'
54
	);
55
56
	/**
57
	 * Base query for all event searches
58
	 */
59
	protected $query = array();
60
61
	/**
62
	 * Stored IDs, if user passed in ID / events instead of date range
63
	 */
64
	protected $ids = array();
65
66
	/**
67
	 * Constructor
68
	 */
69
	function __construct()
70
	{
71
		parent::__construct();
72
73
		// overwrite global export-limit, if one is set for calendar/appointments
74
		$this->export_limit = bo_merge::getExportLimit('calendar');
75
76
		// switch of handling of html formated content, if html is not used
77
		$this->parse_html_styles = egw_customfields::use_html('calendar');
78
		$this->bo = new calendar_boupdate();
79
80
		self::$range_tags['start'] = $GLOBALS['egw_info']['user']['preferences']['common']['dateformat'];
81
		self::$range_tags['end'] = $GLOBALS['egw_info']['user']['preferences']['common']['dateformat'];
82
83
		// Register table plugins
84
		$this->table_plugins['participant'] = 'participant';
85
		for($i = 0; $i < 7; $i++)
86
		{
87
			$this->table_plugins[date('l', strtotime("+$i days"))] = 'day_plugin';
88
		}
89
		for($i = 1; $i <= 31; $i++) {
90
			$this->table_plugins['day_'.$i] = 'day'; // Numerically by day number (1-31)
91
		}
92
		foreach(self::$relative as $day) {
93
			$this->table_plugins[$day] = 'day'; // Current day
94
		}
95
		$this->query = $GLOBALS['egw']->session->appsession('session_data','calendar');
96
		$this->query['users'] = explode(',', $this->query['owner']);
97
		$this->query['num_rows'] = -1;
98
	}
99
100
	/**
101
	 * Merges a given document with contact data
102
	 *
103
	 * Overridden from parent to be able to change a list of events into a range,
104
	 * if the target document has no pagerepeat tag.  Otherwise, parent::merge_string()
105
	 * would fail because we're trying to merge multiple records with no pagerepeat tag.
106
	 *
107
	 *
108
	 * @param string $content
109
	 * @param array $ids array with contact id(s)
110
	 * @param string &$err error-message on error
111
	 * @param string $mimetype mimetype of complete document, eg. text/*, application/vnd.oasis.opendocument.text, application/rtf
112
	 * @param array $fix=null regular expression => replacement pairs eg. to fix garbled placeholders
113
	 * @param string $charset=null charset to override default set by mimetype or export charset
114
	 * @return string|boolean merged document or false on error
115
	 */
116
	function merge_string($content,$ids,$err,$mimetype,$fix)
117
	{
118
		// Handle merging a list of events into a document with range instead of pagerepeat
119
		if(strpos($content, '$$pagerepeat') === false && strpos($content, '{{pagerepeat') === false && count($ids) > 1)
120
		{
121
			// Merging more than one something will fail without pagerepeat
122
			if (is_array($ids) && $ids[0]['id'])
123
			{
124
				// Passed an array of events, to be handled like a date range
125
				$events = $ids;
126
				$ids = array('start' => PHP_INT_MAX, 'end' => 0);
127
				$this->ids = array();
128
				foreach($events as $event) {
129 View Code Duplication
					if($event['start'] && egw_time::to($event['start'],'ts') < $ids['start']) $ids['start'] = egw_time::to($event['start'],'ts');
130 View Code Duplication
					if($event['end'] && egw_time::to($event['end'],'ts') > $ids['end']) $ids['end'] = egw_time::to($event['end'],'ts');
131
					// Keep ids for future use
132
					$this->ids[] = $event['id'];
133
				}
134
				$ids = array($ids);
135
			}
136
		}
137
		return parent::merge_string($content, $ids, $err, $mimetype,$fix);
138
	}
139
140
	/**
141
	 * Get replacements
142
	 *
143
	 * @param int|array $id event-id array with id,recur_date, or array with search parameters
144
	 * @param string &$content=null content to create some replacements only if they are used
145
	 * @return array|boolean
146
	 */
147
	protected function get_replacements($id,&$content=null)
148
	{
149
		$prefix = '';
150
		// List events ?
151
		if(is_array($id) && !$id['id'] && !$id[0]['id'])
152
		{
153
			$events = $this->bo->search($this->query + $id + array(
154
				'offset' => 0,
155
				'order' => 'cal_start',
156
				'cfs' => strpos($content, '#') !== false ? array_keys(config::get_customfields('calendar')) : null
157
			));
158
			if(strpos($content,'$$calendar/') !== false || strpos($content, '$$table/day') !== false)
159
			{
160
				array_unshift($events,false); unset($events[0]);	// renumber the array to start with key 1, instead of 0
161
				$prefix = 'calendar/%d';
162
			}
163
		}
164
		elseif (is_array($id) && $id[0]['id'])
165
		{
166
			// Passed an array of events, to be handled like a date range
167
			$events = $id;
168
			$id = array('start' => PHP_INT_MAX, 'end' => 0);
169
			$this->ids = array();
170 View Code Duplication
			foreach($events as $event) {
171
				if($event['start'] && $event['start'] < $id['start']) $id['start'] = $event['start'];
172
				if($event['end'] && $event['end'] > $id['end']) $id['end'] = $event['end'];
173
				// Keep ids for future use
174
				$this->ids[]  = $event['id'];
175
			}
176
			$id = array($id);
177
		}
178
		else
179
		{
180
			$events = array($id);
181
			$this->ids = $events;
182
		}
183
		// as this function allows to pass query- parameters, we need to check the result of the query against export_limit restrictions
184 View Code Duplication
		if (bo_merge::hasExportLimit($this->export_limit) && !bo_merge::is_export_limit_excepted() && count($events) > (int)$this->export_limit)
185
		{
186
			$err = lang('No rights to export more than %1 entries!',(int)$this->export_limit);
187
			throw new egw_exception_wrong_userinput($err);
188
		}
189
		$replacements = array();
190
		$n = 0;
191
		foreach($events as $event)
192
		{
193
			$values = $this->calendar_replacements($event,sprintf($prefix,++$n), $content);
194
			if(is_array($id) && $id['start'])
195
			{
196
				foreach(self::$range_tags as $key => $format)
197
				{
198
					$value = date($format, $key == 'end' ? $id['end'] : $id['start']);
199
					if($key == 'month') $value = lang($value);
200
					$values["$\$range/$key$$"] = $value;
1 ignored issue
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '$'
Loading history...
201
				}
202
			}
203
			$replacements += $values;
204
		}
205
		return $replacements;
206
	}
207
208
	/**
209
	 * Return replacements for the calendar
210
	 *
211
	 * @param int|array $id event-id or array with id/recur_date, or array with event info
212
	 * @param boolean $last_event_too=false also include information about the last event
213
	 * @return array
214
	 */
215
	public function calendar_replacements($id,$prefix = '', &$content = '')
216
	{
217
		$replacements = array();
218 View Code Duplication
		if(!is_array($id) || !$id['start']) {
219
			$event = $this->bo->read(is_array($id) ? $id['id'] : $id, is_array($id) ? $id['recur_date'] : null);
220
		} else {
221
			$event = $id;
222
		}
223
224
		$record = new calendar_egw_record($event['id']);
225
226
		// Convert to human friendly values
227
		$types = calendar_egw_record::$types;
228
		importexport_export_csv::convert($record, $types, 'calendar');
229
230
		$array = $record->get_record_array();
231
		foreach($array as $key => $value)
232
		{
233
			$replacements['$$'.($prefix?$prefix.'/':'').$key.'$$'] = $value;
234
		}
235
236
		$replacements['$$' . ($prefix ? $prefix . '/' : '') . 'calendar_id'. '$$'] = $event['id'];
237 View Code Duplication
		foreach($this->bo->event2array($event) as $name => $data)
238
		{
239
			if (substr($name,-4) == 'date') $name = substr($name,0,-4);
240
			$replacements['$$' . ($prefix ? $prefix . '/' : '') . 'calendar_'.$name . '$$'] = is_array($data['data']) ? implode(', ',$data['data']) : $data['data'];
241
		}
242
		// Add seperate lists of participants by type
243
		if(strpos($content, 'calendar_participants/')!== false)
244
		{
245
			$types = array();
246
			foreach($this->bo->resources as $resource)
247
			{
248
				$types[$resource['app']] = array();
249
			}
250
			foreach($event['participants'] as $uid => $status)
251
			{
252
				$type = $this->bo->resources[$uid[0]]['app'];
253
				if($type == 'home-accounts')
254
				{
255
					$type = ($GLOBALS['egw']->accounts->get_type($uid) == 'g' ? 'group' : 'account');
256
				}
257
				$types[$type][] = $this->bo->participant_name($uid);
258
			}
259
			foreach($types as $t_id => $type)
260
			{
261
				$replacements['$$'.($prefix ? $prefix . '/' : '') . "calendar_participants/{$t_id}$$"] = implode(', ',$type);
262
			}
263
		}
264
		if(!$replacements['$$'.($prefix ? $prefix . '/' : '') . 'calendar_recur_type$$'])
265
		{
266
			// Need to set it to '' if not set or previous record may be used
267
			$replacements['$$'.($prefix ? $prefix . '/' : '') . 'calendar_recur_type$$'] = '';
268
		}
269
		foreach(array('start','end') as $what)
270
		{
271
			foreach(array(
272
				'date' => $GLOBALS['egw_info']['user']['preferences']['common']['dateformat'],
273
				'day'  => 'l',
274
				'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'),
275
			) as $name => $format)
276
			{
277
				$value = egw_time::to($event[$what],$format);
278
				if ($format == 'l') $value = lang($value);
279
				$replacements['$$' .($prefix ? $prefix.'/':'').'calendar_'.$what.$name.'$$'] = $value;
280
			}
281
		}
282
		$duration = ($event['end'] - $event['start'])/60;
283
		$replacements['$$'.($prefix?$prefix.'/':'').'calendar_duration$$'] = floor($duration/60).lang('h').($duration%60 ? $duration%60 : '');
284
285
		// Add in contact stuff for owner
286
		if (strpos($content,'$$calendar_owner/') !== null && ($user = $GLOBALS['egw']->accounts->id2name($event['owner'],'person_id')))
287
		{
288
			$replacements += $this->contact_replacements($user,($prefix ? $prefix.'/':'').'calendar_owner');
289
			$replacements['$$'.($prefix?$prefix.'/':'').'calendar_owner/primary_group$$'] = $GLOBALS['egw']->accounts->id2name($GLOBALS['egw']->accounts->id2name($event['owner'],'account_primary_group'));
290
		}
291
292
		if($content && strpos($content, '$$#') !== FALSE)
293
		{
294
			$this->cf_link_to_expand($event, $content, $replacements);
295
		}
296
297
		// Links
298
		$replacements += $this->get_all_links('calendar', $event['id'], $prefix, $content);
299
300
		return $replacements;
301
	}
302
303
	/**
304
	* Table plugin for event
305
	* Lists events for a certain day of the week.  Only works for one week at a time, so for multiple weeks,
306
	* use multiple date ranges.
307
	*
308
	* Use:
309
	* $$table/Monday$$ $$starttime$$ $$title$$ $$endtable$$
310
	* The day of the week may be language specific (date('l')).
311
	*
312
	* @param string $plugin (Monday-Sunday)
313
	* @param int/array date or date range
314
	* @param int $n Row number
315
	* @param string $repeat Text being repeated for each entry
316
	* @return array
317
	*/
318
	public function day_plugin($plugin,$date,$n,$repeat)
319
	{
320
		static $days = null;
321
		if(is_array($date) && !$date['start']) {
322
			// List of IDs
323
			if($date[0]['start']) {
324
				$id = array('start' => PHP_INT_MAX, 'end' => 0);
325 View Code Duplication
				foreach($date as $event) {
326
					if($event['start'] && $event['start'] < $id['start']) $id['start'] = $event['start'];
327
					if($event['end'] && $event['end'] > $id['end']) $id['end'] = $event['end'];
328
				}
329
				$date = $id;
330
			} else {
331
				$event = $this->bo->read(is_array($date) ? $date['id'] : $date, is_array($date) ? $date['recur_date'] : null);
332
				if(date('l',$event['start']) != $plugin) return array();
333
				$date = $event['start'];
334
			}
335
		}
336
337
		$_date = $date['start'] ? $date['start'] : $date;
338 View Code Duplication
		if($days[date('Ymd',$_date)][$plugin]) return $days[date('Ymd',$_date)][$plugin][$n];
339
340
		$events = $this->bo->search($this->query + array(
341
			'start' => $date['end'] ? $date['start'] : mktime(0,0,0,date('m',$_date),date('d',$_date),date('Y',$_date)),
342
			'end' => $date['end'] ? $date['end'] : mktime(23,59,59,date('m',$_date),date('d',$_date),date('Y',$_date)),
343
			'offset' => 0,
344
			'num_rows' => 20,
345
			'order' => 'cal_start',
346
			'daywise' => true,
347
			'cfs' => array(),	// read all custom-fields
348
		));
349
350
		if (true) $days = array();
351
		$replacements = array();
352
		$time_format = $GLOBALS['egw_info']['user']['preferences']['common']['timeformat'] == 12 ? 'h:i a' : 'H:i';
353
		foreach($events as $day => $list)
354
		{
355
			foreach($list as $event)
356
			{
357
				if($this->ids && !in_array($event['id'], $this->ids)) continue;
358
				$start = egw_time::to($event['start'], 'array');
359
				$end = egw_time::to($event['end'], 'array');
360
				$replacements = $this->calendar_replacements($event);
361
				if($start['year'] == $end['year'] && $start['month'] == $end['month'] && $start['day'] == $end['day']) {
362
					$dow = date('l',$event['start']);
363
				} else {
364
					$dow = date('l', strtotime($day));
365
					// Fancy date+time formatting for multi-day events
366
					$replacements['$$calendar_starttime$$'] = date($time_format, $day == date('Ymd', $event['start']) ? $event['start'] : mktime(0,0,0,0,0,1));
367
					$replacements['$$calendar_endtime$$'] = date($time_format, $day == date('Ymd', $event['end']) ? $event['end'] : mktime(23,59,59,0,0,0));
368
				}
369
370
				$days[date('Ymd',$_date)][$dow][] = $replacements;
371
			}
372
			if(strpos($repeat, 'day/date') !== false || strpos($repeat, 'day/name') !== false) {
373
				$date_marker = array(
374
					'$$day/date$$' => date($GLOBALS['egw_info']['user']['preferences']['common']['dateformat'], strtotime($day)),
375
					'$$day/name$$' => lang(date('l', strtotime($day)))
376
				);
377
				if(!is_array($days[date('Ymd',$_date)][date('l',strtotime($day))])) {
378
					$blank = $this->calendar_replacements(array());
379
					foreach($blank as &$value)
380
					{
381
						$value = '';
382
					}
383
					$days[date('Ymd',$_date)][date('l',strtotime($day))][] = $blank;
384
				}
385
				$days[date('Ymd',$_date)][date('l',strtotime($day))][0] += $date_marker;
386
			}
387
		}
388
		return $days[date('Ymd',$_date)][$plugin][0];
389
	}
390
391
	/**
392
	* Table plugin for a certain date
393
	*
394
	* Can be either a particular date (2011-02-15) or a day of the month (15)
395
	*
396
	* @param string $plugin
397
	* @param int $id ID for this record
398
	* @param int $n Repeated row number
399
	* @param string $repeat Text being repeated for each entry
400
	* @return array
401
	*/
402
	public function day($plugin,$id,$n,$repeat)
403
	{
404
		static $days = null;
405
406
		// Figure out which day
407
		list($type, $which) = explode('_',$plugin);
408
		if($type == 'day' && $which)
409
		{
410
			if($id[0]['start'])
411
			{
412
				$dates = array('start' => PHP_INT_MAX, 'end' => 0);
413 View Code Duplication
				foreach($id as $event) {
414
					if($event['start'] && $event['start'] < $dates['start']) $dates['start'] = $event['start'];
415
					if($event['end'] && $event['end'] > $dates['end']) $dates['end'] = $event['end'];
416
				}
417
				$id = $dates;
418
			}
419
			$arr = $this->bo->date2array($id['start']);
420
			$arr['day'] = $which;
421
			$date = $this->bo->date2ts($arr);
422
			if(is_array($id) && $id['start'] && ($date < $id['start'] || $date > $id['end'])) return array();
423
		}
424
		elseif ($plugin == 'selected')
425
		{
426
			$date = $id['start'];
427
		}
428
		else
429
		{
430
			$date = strtotime($plugin);
431
		}
432
		if($type == 'day' && is_array($id) && !$id['start']) {
433
			$event = $this->bo->read(is_array($id) ? $id['id'] : $id, is_array($id) ? $id['recur_date'] : null);
434
			if($which && date('d',$event['start']) != $which) return array();
435
			if(date('Ymd',$date) != date('Ymd', $event['start'])) return array();
436
			return $n == 0 ? $this->calendar_replacements($event) : array();
437
		}
438
439
		// Use start for cache, in case of multiple months
440
		$_date = $id['start'] ? $id['start'] : $date;
441 View Code Duplication
		if($days[date('Ymd',$_date)][$plugin]) return $days[date('Ymd',$_date)][$plugin][$n];
442
443
		$events = $this->bo->search($this->query + array(
444
			'start' => $date,
445
			'end' => mktime(23,59,59,date('m',$date),date('d',$date),date('Y',$date)),
446
			'offset' => 0,
447
			'num_rows' => 20,
448
			'order' => 'cal_start',
449
			'daywise' => true,
450
			'cfs' => array(),	// read all custom-fields
451
		));
452
453
		$replacements = array();
454
		if (true) $days = array();
455
		$time_format = $GLOBALS['egw_info']['user']['preferences']['common']['timeformat'] == 12 ? 'h:i a' : 'H:i';
456
		foreach($events as $day => $list)
457
		{
458
			foreach($list as $event)
459
			{
460
				if($this->ids && !in_array($event['id'], $this->ids)) continue;
461
				$start = egw_time::to($event['start'], 'array');
462
				$end = egw_time::to($event['end'], 'array');
463
				$replacements = $this->calendar_replacements($event);
464
				if($start['year'] == $end['year'] && $start['month'] == $end['month'] && $start['day'] == $end['day']) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
465
					//$dow = date('l',$event['start']);
466
				} else {
467
					// Fancy date+time formatting for multi-day events
468
					$replacements['$$calendar_starttime$$'] = date($time_format, $day == date('Ymd', $event['start']) ? $event['start'] : mktime(0,0,0,0,0,1));
469
					$replacements['$$calendar_endtime$$'] = date($time_format, $day == date('Ymd', $event['end']) ? $event['end'] : mktime(23,59,59,0,0,0));
470
				}
471
				$days[date('Ymd',$_date)][$plugin][] = $replacements;
472
			}
473
			if(strpos($repeat, 'day/date') !== false || strpos($repeat, 'day/name') !== false) {
474
				$date_marker = array(
475
					'$$day/date$$' => date($GLOBALS['egw_info']['user']['preferences']['common']['dateformat'], strtotime($day)),
476
					'$$day/name$$' => lang(date('l', strtotime($day)))
477
				);
478
				if(!is_array($days[date('Ymd',$_date)][$plugin])) {
479
					$blank = $this->calendar_replacements(array());
480
					foreach($blank as &$value)
481
					{
482
						$value = '';
483
					}
484
					$days[date('Ymd',$_date)][$plugin][] = $blank;
485
				}
486
				$days[date('Ymd',$_date)][$plugin][0] += $date_marker;
487
			}
488
		}
489
		return $days[date('Ymd',$_date)][$plugin][0];
490
	}
491
492
	/**
493
	* Table plugin for participants
494
	*
495
	* Copied from eventmgr resources
496
	*
497
	* @param string $plugin
498
	* @param int $id
499
	* @param int $n
500
	* @return array
501
	*/
502
	public function participant($plugin,$id,$n)
503
	{
504
		unset($plugin);	// not used, but required by function signature
505
506 View Code Duplication
		if(!is_array($id) || !$id['start']) {
507
			$event = $this->bo->read(is_array($id) ? $id['id'] : $id, is_array($id) ? $id['recur_date'] : null);
508
		} else {
509
			$event = $id;
510
		}
511
512
		if(!is_array($event['participants']) || $n >= count($event['participants'])) return array();
513
514
		$participant = null;
515
		$status = null;
516
		$i = -1;
517
		foreach($event['participants'] as $participant => $status) {
518
			if(++$i == $n) break;
519
		}
520
521
		if(!$participant) return array();
522
523
		// Add some common information
524
		$quantity = $role = null;
525
		calendar_so::split_status($status,$quantity,$role);
526 View Code Duplication
		if ($role != 'REQ-PARTICIPANT')
527
		{
528
			if (isset($this->bo->roles[$role]))
529
			{
530
				$role = lang($this->bo->roles[$role]);
531
			}
532
			// allow to use cats as roles (beside regular iCal ones)
533
			elseif (substr($role,0,6) == 'X-CAT-' && ($cat_id = (int)substr($role,6)) > 0)
534
			{
535
				$role = $GLOBALS['egw']->categories->id2name($cat_id);
536
			}
537
			else
538
			{
539
				$role = lang(str_replace('X-','',$role));
540
			}
541
		}
542
		$info = array(
543
			'name'		=> $this->bo->participant_name($participant),
544
			'status'	=> lang($this->bo->verbose_status[$status]),
545
			'quantity'	=> $quantity,
546
			'role'		=> $role
547
		);
548
549
		switch ($participant[0])
550
		{
551
			case 'c':
552
				$replacements = $this->contact_replacements(substr($participant,1),'');
553
				break;
554
			case 'r':
555
				if (is_null(self::$resources)) self::$resources = CreateObject('resources.resources_bo');
556
				if (($resource = self::$resources->read(substr($participant,1))))
557
				{
558
					foreach($resource as $name => $value)
559
					{
560
					    $replacements['$$'.$name.'$$'] = $value;
561
					}
562
				}
563
				break;
564
			default:
565
				if (is_numeric($participant) && ($contact = $GLOBALS['egw']->accounts->id2name($participant,'person_id')))
566
				{
567
					$replacements = $this->contact_replacements($contact,'');
568
				}
569
				break;
570
		}
571
		foreach($info as $name => $value)
572
		{
573
			$replacements['$$'.$name.'$$'] = $value;
574
		}
575
		return $replacements;
576
	}
577
578
	/**
579
	 * Generate table with replacements for the preferences
580
	 *
581
	 */
582
	public function show_replacements()
583
	{
584
		$GLOBALS['egw']->translation->add_app('calendar');
585
		$GLOBALS['egw_info']['flags']['app_header'] = lang('calendar').' - '.lang('Replacements for inserting events into documents');
586
		$GLOBALS['egw_info']['flags']['nonavbar'] = false;
587
		common::egw_header();
588
589
		echo "<table width='90%' align='center'>\n";
590
		echo '<tr><td colspan="4"><h3>'.lang('Calendar fields:')."</h3></td></tr>";
591
592
		$n = 0;
593 View Code Duplication
		foreach(array(
594
			'calendar_id' => lang('Calendar ID'),
595
			'calendar_title' => lang('Title'),
596
			'calendar_description' => lang('Description'),
597
			'calendar_participants' => lang('Participants'),
598
			'calendar_location' => lang('Location'),
599
			'calendar_start'    => lang('Start').': '.lang('Date').'+'.lang('Time'),
600
			'calendar_startday' => lang('Start').': '.lang('Weekday'),
601
			'calendar_startdate'=> lang('Start').': '.lang('Date'),
602
			'calendar_starttime'=> lang('Start').': '.lang('Time'),
603
			'calendar_end'      => lang('End').': '.lang('Date').'+'.lang('Time'),
604
			'calendar_endday'   => lang('End').': '.lang('Weekday'),
605
			'calendar_enddate'  => lang('End').': '.lang('Date'),
606
			'calendar_endtime'  => lang('End').': '.lang('Time'),
607
			'calendar_duration' => lang('Duration'),
608
			'calendar_category' => lang('Category'),
609
			'calendar_priority' => lang('Priority'),
610
			'calendar_updated'  => lang('Updated'),
611
			'calendar_recur_type' => lang('Repetition'),
612
			'calendar_access'   => lang('Access').': '.lang('public').', '.lang('private'),
613
			'calendar_owner'    => lang('Owner'),
614
		) as $name => $label)
615
		{
616
			if (in_array($name,array('start','end')) && $n&1)		// main values, which should be in the first column
617
			{
618
				echo "</tr>\n";
619
				$n++;
620
			}
621
			if (!($n&1)) echo '<tr>';
622
			echo '<td>{{'.$name.'}}</td><td>'.$label.'</td>';
623
			if ($n&1) echo "</tr>\n";
624
			$n++;
625
		}
626
627
		echo '<tr><td colspan="4"><h3>'.lang('Range fields').":</h3></td></tr>";
628
		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>';
629
		foreach(array_keys(self::$range_tags) as $name)
630
		{
631
			echo '<tr><td>{{range/'.$name.'}}</td><td>'.lang($name)."</td></tr>\n";
632
		}
633
		echo '<tr><td colspan="4"><h3>'.lang('Custom fields').":</h3></td></tr>";
634
		$custom = config::get_customfields('calendar');
635
		foreach($custom as $name => $field)
636
		{
637
			echo '<tr><td>{{#'.$name.'}}</td><td colspan="3">'.$field['label']."</td></tr>\n";
638
		}
639
640
641
		echo '<tr><td colspan="4"><h3>'.lang('Participants').":</h3></td></tr>";
642
		echo '<tr><td>{{calendar_participants/account}}</td><td colspan="3">'.lang('Accounts')."</td></tr>\n";
643
		echo '<tr><td>{{calendar_participants/group}}</td><td colspan="3">'.lang('Groups')."</td></tr>\n";
644
		foreach($this->bo->resources as $resource)
645
		{
646
			if($resource['type'])
647
			{
648
				echo '<tr><td>{{calendar_participants/'.$resource['app'].'}}</td><td colspan="3">'.lang($resource['app'])."</td></tr>\n";
649
			}
650
		}
651
652
		echo '<tr><td colspan="4"><h3>'.lang('Participant table').":</h3></td></tr>";
653
		echo '<tr><td colspan="4">{{table/participant}} ... </td></tr>';
654
		echo '<tr><td>{{name}}</td><td>'.lang('name').'</td></tr>';
655
		echo '<tr><td>{{role}}</td><td>'.lang('role').'</td></tr>';
656
		echo '<tr><td>{{quantity}}</td><td>'.lang('quantity').'</td></tr>';
657
		echo '<tr><td>{{status}}</td><td>'.lang('status').'</td></tr>';
658
		echo '<tr><td colspan="4">{{endtable}}</td></tr>';
659
660
		echo '<tr style="vertical-align:top"><td colspan="2"><table >';
661
		echo '<tr><td><h3>'.lang('Day of week tables').":</h3></td></tr>";
662
		$days = array();
663
		for($i = 0; $i < 7; $i++)
664
		{
665
			$days[date('N',strtotime("+$i days"))] = date('l',strtotime("+$i days"));
666
		}
667
		ksort($days);
668
		foreach($days as $day)
669
		{
670
			echo '<tr><td>{{table/'.$day. '}} ... {{endtable}}</td></tr>';
671
		}
672
		echo '</table></td><td colspan="2"><table >';
673
		echo '<tr><td><h3>'.lang('Daily tables').":</h3></td></tr>";
674
		foreach(self::$relative as $value) {
675
			echo '<tr><td>{{table/'.$value. '}} ... {{endtable}}</td></tr>';
676
		}
677
		echo '<tr><td>{{table/day_n}} ... {{endtable}}</td><td>1 <= n <= 31</td></tr>';
678
		echo '</table></td></tr>';
679
		echo '<tr><td>{{day/date}}</td><td colspan="3">'.lang('Date for the day of the week, available for the first entry inside each day of week or daily table inside the selected range.').'</td></tr>';
680
		echo '<tr><td>{{day/name}}</td><td colspan="3">'.lang('Name of the week (ex: Monday), available for the first entry inside each day of week or daily table inside the selected range.').'</td></tr>';
681
682
		echo '<tr><td colspan="4"><h3>'.lang('General fields:')."</h3></td></tr>";
683 View Code Duplication
		foreach(array(
684
			'link' => lang('HTML link to the current record'),
685
			'links' => lang('Titles of any entries linked to the current record, excluding attached files'),
686
			'attachments' => lang('List of files linked to the current record'),
687
			'links_attachments' => lang('Links and attached files'),
688
			'links/[appname]' => lang('Links to specified application.  Example: {{links/infolog}}'),
689
			'date' => lang('Date'),
690
			'user/n_fn' => lang('Name of current user, all other contact fields are valid too'),
691
			'user/account_lid' => lang('Username'),
692
			'pagerepeat' => lang('For serial letter use this tag. Put the content, you want to repeat between two Tags.'),
693
			'label' => lang('Use this tag for addresslabels. Put the content, you want to repeat, between two tags.'),
694
			'labelplacement' => lang('Tag to mark positions for address labels'),
695
			'IF fieldname' => lang('Example {{IF n_prefix~Mr~Hello Mr.~Hello Ms.}} - search the field "n_prefix", for "Mr", if found, write Hello Mr., else write Hello Ms.'),
696
			'NELF' => lang('Example {{NELF role}} - if field role is not empty, you will get a new line with the value of field role'),
697
			'NENVLF' => lang('Example {{NELFNV role}} - if field role is not empty, set a LF without any value of the field'),
698
			'LETTERPREFIX' => lang('Example {{LETTERPREFIX}} - Gives a letter prefix without double spaces, if the title is emty for  example'),
699
			'LETTERPREFIXCUSTOM' => lang('Example {{LETTERPREFIXCUSTOM n_prefix title n_family}} - Example: Mr Dr. James Miller'),
700
			) as $name => $label)
701
		{
702
			echo '<tr><td>{{'.$name.'}}</td><td colspan="3">'.$label."</td></tr>\n";
703
		}
704
705
		echo "</table>\n";
706
707
		common::egw_footer();
708
	}
709
}
710