module_calendar_week   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 338
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 182
dl 0
loc 338
rs 9.36
c 0
b 0
f 0
wmc 38

3 Methods

Rating   Name   Duplication   Size   Complexity  
F get_user_interface() 0 109 22
A __construct() 0 46 1
D get_content() 0 137 15
1
<?php
2
/**************************************************************************\
3
 * eGroupWare SiteMgr - Web Content Management                              *
4
 * http://www.egroupware.org                                                *
5
 * --------------------------------------------                             *
6
 *  This program is free software; you can redistribute it and/or modify it *
7
 *  under the terms of the GNU General Public License as published by the   *
8
 *  Free Software Foundation; either version 2 of the License, or (at your  *
9
 *  option) any later version.                                              *
10
 \**************************************************************************/
11
12
use EGroupware\Api;
13
use EGroupware\Api\Link;
14
use EGroupware\Api\Framework;
15
use EGroupware\Api\Acl;
16
use EGroupware\Api\Etemplate;
17
18
/* $Id$ */
19
20
class module_calendar_week extends Module
21
{
22
23
24
	/**
25
	 * Default calendar CSS file
26
	 */
27
	const CALENDAR_CSS = '/calendar/templates/default/app.css';
28
29
	const ETEMPLATE_CSS = '/api/templates/default/etemplate2.css';
30
31
	/**
32
	 * Instance of the business object of calendar
33
	 *
34
	 * @var bo
35
	 */
36
	var $bo;
37
	/**
38
	 * Instance of the user interface object of calendar
39
	 *
40
	 * @var ui
41
	 */
42
	var $ui;
43
	/**
44
	 * Instance of the user interface object of calendar
45
	 *
46
	 * @var ui
47
	 */
48
	var $uiviews;
49
	/**
50
	 * Instance of the Api\Accounts object
51
	 *
52
	 * @var Api\Accounts
53
	 */
54
	var $accounts;
55
	/**
56
	 * Default CSS style
57
	 *
58
	 * @var default_css
0 ignored issues
show
Bug introduced by
The type default_css was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
59
	 */
60
	var $default_css = '/calendar/templates/default/app.css';
61
62
	function __construct()
63
	{
64
		parent::__construct();
65
66
		$this->arguments = array(
0 ignored issues
show
Bug Best Practice introduced by
The property arguments does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
67
			'cat_id' => array(
68
				'type' => 'select',
69
				'label' => lang('Choose a category'),
70
				'options' => array(),	// specification of options is postponed into the get_user_interface function
71
				'multiple' => true,
72
			),
73
			'numWeeks' => array(
74
				'type' => 'textfield',
75
				'label' => lang('Number of weeks to show'),
76
				'default' => 2,
77
				'params' => array('size' => 1)
78
			),
79
			'search' => array(
80
				'type' => 'textfield',
81
				'label' => lang('Search string for the events'),
82
			),
83
			'owner' => array(
84
				'type' => 'select',
85
				'options' => array(),
86
				'label' => lang('Group(s) or user(s) whose calendars to show (if ACL exists)'),
87
				// 'multiple' => true, is set in the get_user_interface function.
88
			),
89
			'resources' => array(
90
				'type' => 'select',
91
				'label' => lang('Resources'),
92
				'options' => array(),
93
				'multiple' => true
94
			),
95
			'css' => array(
96
				'type' => 'textfield',
97
				'label' => lang('User selectable CSS file for the calendar setup'),
98
				'default' => $this->default_css,
99
			),
100
			'acceptDateParam' => array(
101
				'type' => 'checkbox',
102
				'label' => lang('Shall the date parameter be accepted (e.g. from calendar module)?'),
103
				'default' => false,
104
			),
105
		);
106
		$this->title = lang('Calendar - Multi-Weekly');
0 ignored issues
show
Bug Best Practice introduced by
The property title does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
107
		$this->description = lang("This module displays a user's calendar as multiple weeks. Don't give calendar application access to the anon user!");
0 ignored issues
show
Bug Best Practice introduced by
The property description does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
108
	}
109
110
	function get_user_interface()
111
	{
112
		// copied from bookmarks module.
113
		$cats = new Api\Categories('','calendar');
114
		foreach($cats->return_array('all',0,False,'','cat_name','',True) as $cat)
115
		{
116
			$this->arguments['cat_id']['options'][$cat['id']] = str_repeat('&nbsp; ',$cat['level']).$cat['name'];
0 ignored issues
show
Bug Best Practice introduced by
The property arguments does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
117
		}
118
		if (count($this->arguments['cat_id']['options']) > 5)
119
		{
120
			$this->arguments['cat_id']['multiple'] = 5;
121
		}
122
123
		if (! isset($GLOBALS['egw']->accounts))
124
		{
125
			$GLOBALS['egw']->accounts = new Api\Accounts();
126
		}
127
		$this->accounts =& $GLOBALS['egw']->accounts;
128
		$search_params=array(
129
			'type' => 'both',
130
			'app' => 'calendar',
131
		);
132
		$accounts = $this->accounts->search($search_params);
133
		$calendar_bo = new calendar_bo();
134
		$users = array();
135
		$groups = array();
136
		// sort users and groups separately.
137
		$anon_user = $this->accounts->name2id($GLOBALS['Common_BO']->sites->current_site['anonymous_user'],'account_lid','u');
138
		$anon_groups = $this->accounts->memberships($anon_user,true);
139
		foreach ($accounts as $entry)
140
		{
141
			$is_group = false;
142
			$has_read_permissions = false;
143
			$acl = new Acl($entry['account_id']);
144
			$acl->read_repository();
145
			// get the rights for each account to check whether the anon user has read permissions.
146
			$rights = $acl->get_rights($anon_user,'calendar');
0 ignored issues
show
Unused Code introduced by
The assignment to $rights is dead and can be removed.
Loading history...
147
			// also add the anon user if it's his own calendar.
148
			if ($calendar_bo->check_perms(Acl::READ|calendar_bo::ACL_READ_FOR_PARTICIPANTS|calendar_bo::ACL_FREEBUSY,0,$entry['account_id'],'ts',null,$anon_user) || ($entry['account_id'] == $anon_user))
149
			{
150
				$has_read_permissions = true;
151
			}
152
			else
153
			{
154
				// scan the groups which pass on permissions to the anon user group member
155
				// or ass permissions if this is the anon group's calendar.
156
				foreach ($anon_groups as $parent_group)
157
				{
158
					$rights = $acl->get_rights($parent_group,'calendar');
159
					if (($rights & Acl::READ) || ($entry['account_id'] == $parent_group))
160
					{
161
						$has_read_permissions = true;
162
						break;
163
					}
164
				}
165
			}
166
			if ($has_read_permissions)
167
			{
168
				// Separate groups from users for improved readability.
169
				if ($is_group)
170
				{
171
					$groups[$entry['account_id']] = $entry['account_lid'];
172
				}
173
				else
174
				{
175
					$users[$entry['account_id']] = Api\Accounts::format_username($entry['account_lid'],$entry['account_firstname'],$entry['account_lastname']);
176
				}
177
			}
178
		}
179
		asort($groups);
180
		asort($users);
181
		// concat users and groups to the option array.
182
		$this->arguments['owner']['options'] = array_unique($groups + $users);
183
		if (count($this->arguments['owner']['options']) > 10)
184
		{
185
			$this->arguments['owner']['multiple'] = 10;
186
		}
187
		else if (count($this->arguments['owner']['options']) > 0)
188
		{
189
			$this->arguments['owner']['multiple'] = true;
190
		}
191
192
		// Resources
193
		$query = '';
194
		$options = array('start' => 0);
195
196
		$acl = new Acl($anon_user);
197
		$acl->read_repository();
198
 		foreach ($calendar_bo->resources as $type => $data)
199
		{
200
			// Check anon user's permissions - must have at least run for the hook to be available
201
			if($acl->check('run',EGW_ACL_READ, $data['app']) &&
202
				$type != '' && $data['app'] && Link::get_registry($data['app'], 'query')
203
			)
204
			{
205
				$_results = Link::query($data['app'], $query,$options);
206
			}
207
			if(!$_results) continue;
208
			$_results = array_unique($_results);
209
			foreach ($_results as $key => $value)
210
			{
211
				if($calendar_bo->check_perms(Acl::READ,0,$type.$key,'ts',null,$anon_user))
212
				{
213
					$this->arguments['resources']['options'][$type.$key] = $value;
214
				}
215
			}
216
		}
217
218
		return parent::get_user_interface();
219
	}
220
221
	function get_content(&$arguments,$properties)
222
	{
223
		$GLOBALS['egw_info']['flags']['currentapp'] = 'calendar';
224
		Api\Translation::add_app('calendar');
225
226
		//error_log(array2string($arguments));
227
		if (empty($arguments['date']))
228
		{
229
			$arguments['date'] = date('Ymd');
230
		}
231
232
		if (isset($_GET['date'])) $arguments['date'] = $_GET['date'];
233
		if (empty($arguments['cat_id'])) $arguments['cat_id'] = '';
234
		if(isset($arguments['resources']) && in_array('r0', $arguments['resources']))
235
		{
236
			foreach($arguments['resources'] as $index => $value)
237
			{
238
				if($value == 'r0')
239
				{
240
					unset($arguments['resources'][$index]);
241
				}
242
			}
243
		}
244
245
		$params = $arguments;
246
		if (isset($params['resources']) && count($params['resources']))
247
		{
248
			$params['owner'] = array_merge((array)$params['owner'], (array)$params['resources']);
249
			unset($params['resources']);
250
		}
251
252
		$html = '<style type="text/css">'."\n";
253
		$html .= '@import url('.$GLOBALS['egw_info']['server']['webserver_url'].self::CALENDAR_CSS.");\n";
254
		$html .= '@import url('.$GLOBALS['egw_info']['server']['webserver_url'].self::ETEMPLATE_CSS.");\n";
255
		$html .= '@import url('.$GLOBALS['egw_info']['server']['webserver_url'].Api\Categories::css(Api\Categories::GLOBAL_APPNAME).");\n";
256
		$html .= '@import url('.$GLOBALS['egw_info']['server']['webserver_url'].Api\Categories::css('calendar').");\n";
257
		$html .= '.popupMainDiv #calendar-view {position: static; min-height: 250px; height: 99% !important;}
258
	</style>'."\n";
259
		$html .= Api\Html::image('sitemgr', 'left', lang('Previous'), 'onclick=\'app.calendar.toolbar_action({id:"previous",data:{state:{view:"weekN"}}});\'')
260
		. Api\Html::image('sitemgr', 'right', lang('Next'), 'style="float: right;" onclick=\'app.calendar.toolbar_action({id:"next",data:{state:{view:"weekN"}}});\'');
261
262
		if (is_array($params['owner']))
263
		{
264
			// Buffer, and add anything that gets cleared to the content
265
			ob_start(function($buffer) use(&$html) {
266
				$html .= $buffer;
267
				return '';
268
			});
269
			Framework::$header_done = true;
270
			$ui = new calendar_uiviews();
271
			$ui->owner = $params['owner'];
272
273
			$tmpl = new Etemplate('calendar.view');
274
275
			$start = new Api\DateTime($arguments['date']);
276
			$start->setWeekstart();
277
			$ui->first = $start->format('ts');
0 ignored issues
show
Documentation Bug introduced by
It seems like $start->format('ts') of type EGroupware\Api\DateTime is incompatible with the declared type integer of property $first.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
278
			$ui->last = strtotime("+{$params['numWeeks']} weeks",$ui->first) - 1;
279
280
			// Calendar uses user preferences for number of weeks, so set it
281
			if((int)$params['numWeeks'] != (int)$ui->cal_prefs['multiple_weeks'])
282
			{
283
				$anon_user = $GLOBALS['egw']->accounts->name2id($GLOBALS['Common_BO']->sites->current_site['anonymous_user'],'account_lid','u');
284
				$pref = new Api\Preferences($anon_user);
285
				$pref->add('calendar','multiple_weeks',(int)$params['numWeeks']);
286
				$pref->save_repository();
287
			}
288
289
			$navHeader = lang('Week').' '.$ui->week_number($ui->first).' - '.$ui->week_number($this->last).': '.
0 ignored issues
show
Unused Code introduced by
The assignment to $navHeader is dead and can be removed.
Loading history...
290
				$ui->bo->long_date($ui->first,$ui->last);
291
292
			$granularity = ($ui->cal_prefs['interval'] ? (int)$ui->cal_prefs['interval'] : 30);
293
			
294
			$content = array('view' => array());
295
296
			$sel_options = array();
297
298
			$ui->search_params['query'] = $params['search'];
299
			$ui->search_params['cat_id'] = $params['cat_id'];
300
301
			// Loop through, using Api\DateTime to handle DST
302
			$week = 0;
303
			$week_start = new EGroupware\Api\DateTime($ui->first);
304
			$week_start->setTime(0,0,0);
305
			$week_end = new Api\DateTime($week_start);
306
			$week_end->add(new DateInterval('P6DT23H59M59S'));
307
			$last = new EGroupware\Api\DateTime($ui->last);
308
			
309
			for ($week_start; $week_start < $last; $week_start->add('1 week'), $week_end->add('1 week'))
310
			{
311
				$search_params = $ui->search_params;
312
313
				$search_params['start'] = $week_start->format('ts');
314
				$search_params['end'] = $week_end->format('ts');
315
316
				$content['view'][] = (array)$ui->tagWholeDayOnTop($ui->bo->search($search_params)) +
317
				array(
318
					'id' => $week_start->format('Ymd')
319
				);
320
				$tmpl->setElementAttribute("view[$week]",'onchange',false);
321
				$tmpl->setElementAttribute("view[$week]",'granularity',$granularity);
322
				$tmpl->setElementAttribute("view[$week]",'height','250px');
323
				$week++;
324
			}
325
326
327
			// Make sure all used owners are there, faking
328
			// calendar_owner_etemplate_widget::beforeSendToClient() since the
329
			// rest of the calendar app is probably missing.
330
			foreach($params['owner'] as $owner)
331
			{
332
				$sel_options['owner'][] = Array(
333
					'id' => $owner,
334
					'value' => $owner,
335
					'label' => calendar_owner_etemplate_widget::get_owner_label($owner)
336
				);
337
			}
338
			$tmpl->exec(__METHOD__, $content,$sel_options, array('__ALL__' => true),array(),2);
339
			$html .= ob_get_contents();
340
			
341
			$html .= '<script>'
342
			. '	window.egw_LAB.wait(function() {jQuery(function() {'
343
			. 'app.calendar.set_state(' . json_encode(array(
344
					'owner' => $params['owner'],
345
					'date' => $start->format(EGroupware\Api\DateTime::ET2)
346
				)).'); '
347
			. '});});'
348
			. '</script>';
349
350
			ob_end_clean();
351
		}
352
		else
353
		{
354
			$html .= '<div class="message" align="center">'.lang('No owner selected').'</div>';
355
		}
356
357
		return $html;
358
	}
359
}
360