module_calendar_planner::get_user_interface()   F
last analyzed

Complexity

Conditions 23
Paths 5616

Size

Total Lines 110
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
eloc 59
nc 5616
nop 0
dl 0
loc 110
rs 0
c 0
b 0
f 0

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
 * eGroupWare - Calendar planner block for sitemgr
4
 *
5
 * @link http://www.egroupware.org
6
 * @package calendar
7
 * @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
8
 * @copyright (c) 2010 by RalfBecker-At-outdoor-training.de
9
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
10
 * @version $Id$
11
 */
12
13
use EGroupware\Api;
14
use EGroupware\Api\Link;
15
use EGroupware\Api\Framework;
16
use EGroupware\Api\Acl;
17
use EGroupware\Api\Etemplate;
18
19
/**
20
 * Calendar planner block for sitemgr
21
 */
22
class module_calendar_planner extends Module
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
	 * Constructor
33
	 */
34
	function __construct()
35
	{
36
		parent::__construct();
37
38
		$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...
39
			'sortby' => array(
40
				'type' => 'select',
41
				'label' => lang('Type of planner'),
42
				'options' => array(
43
					0 => lang('Planner by category'),
44
					'user' => lang('Planner by user'),
45
					'month' => lang('Yearly Planner')
46
				),
47
			),
48
			'cat_id' => array(
49
				'type' => 'select',
50
				'label' => lang('Choose a category'),
51
				'options' => array(),	// done by get_user_interface()
52
				'multiple' => true,
53
			),
54
			'owner' => array(
55
				'type' => 'select',
56
				'label' => lang('Group(s) or user(s) to show'),
57
				'options' => array(),
58
			),
59
			'resources' => array(
60
				'type' => 'select',
61
				'label' => lang('Resources'),
62
				'options' => array(),
63
			),
64
			'filter' => array(
65
				'type' => 'select',
66
				'label' => lang('Filter'),
67
				'options' => array(
68
					'default'     => lang('Not rejected'),
69
					'accepted'    => lang('Accepted'),
70
					'unknown'     => lang('Invitations'),
71
					'tentative'   => lang('Tentative'),
72
					'rejected'    => lang('Rejected'),
73
					'owner'       => lang('Owner too'),
74
					'all'         => lang('All incl. rejected'),
75
					'hideprivate' => lang('Hide private infos'),
76
					'no-enum-groups' => lang('only group-events'),
77
				),
78
				'default' => 'default',
79
			),
80
			'date' => array(
81
				'type' => 'textfield',
82
				'label' => 'Startdate as YYYYmmdd (empty for current date)',
83
				'default' => '',
84
				'params' => array('size' => 10),
85
			),
86
		);
87
		$this->title = lang('Calendar - Planner');
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...
88
		$this->description = lang('This module displays a planner calendar.');
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...
89
	}
90
91
	/**
92
	 * Reimplemented to fetch the cats, users/groups and resources
93
	 */
94
	function get_user_interface()
95
	{
96
		$cats = new Api\Categories('','calendar');
97
		foreach($cats->return_array('all',0,False,'','cat_name','',True) as $cat)
98
		{
99
			$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...
100
		}
101
		if (count($this->arguments['cat_id']['options']) > 5)
102
		{
103
			$this->arguments['cat_id']['multiple'] = 5;
104
		}
105
106
		if (!isset($GLOBALS['egw']->accounts))
107
		{
108
			$GLOBALS['egw']->accounts = new Api\Accounts();
109
		}
110
		$this->accounts =& $GLOBALS['egw']->accounts;
0 ignored issues
show
Bug Best Practice introduced by
The property accounts does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
111
		$search_params = array(
112
			'type' => 'both',
113
			'app' => 'calendar',
114
		);
115
		$accounts = $this->accounts->search($search_params);
116
		$calendar_bo = new calendar_bo();
117
		$users = array();
118
		$groups = array();
119
		// sort users and groups separately.
120
		$anon_user = $this->accounts->name2id($GLOBALS['Common_BO']->sites->current_site['anonymous_user'],'account_lid','u');
121
		$anon_groups = $this->accounts->memberships($anon_user,true);
122
		foreach ($accounts as $entry)
123
		{
124
			$is_group = false;
125
			$has_read_permissions = false;
126
			$acl = new Acl($entry['account_id']);
127
			$acl->read_repository();
128
			// get the rights for each account to check whether the anon user has read permissions.
129
			$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...
130
			// also add the anon user if it's his own calendar.
131
			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))
132
			{
133
				$has_read_permissions = true;
134
			}
135
			else
136
			{
137
				// scan the groups which pass on permissions to the anon user group member
138
				// or ass permissions if this is the anon group's calendar.
139
				foreach ($anon_groups as $parent_group)
140
				{
141
					$rights = $acl->get_rights($parent_group,'calendar');
142
					if (($rights & Acl::READ) || ($entry['account_id'] == $parent_group))
143
					{
144
						$has_read_permissions = true;
145
						break;
146
					}
147
				}
148
			}
149
			if ($has_read_permissions)
150
			{
151
				// Separate groups from users for improved readability.
152
				if ($is_group)
153
				{
154
					$groups[$entry['account_id']] = $entry['account_lid'];
155
				}
156
				else
157
				{
158
					$users[$entry['account_id']] = Api\Accounts::format_username($entry['account_lid'],$entry['account_firstname'],$entry['account_lastname']);
159
				}
160
			}
161
		}
162
		asort($groups);
163
		asort($users);
164
		// concat users and groups to the option array.
165
		$this->arguments['owner']['options'] = array_unique($groups + $users);
166
		if (count($this->arguments['owner']['options']) > 10)
167
		{
168
			$this->arguments['owner']['multiple'] = 10;
169
		}
170
		else if (count($this->arguments['owner']['options']) > 0)
171
		{
172
			$this->arguments['owner']['multiple'] = true;
173
		}
174
175
		$query = '';
176
		$options = array('start' => 0);
177
178
		$acl = new Acl($anon_user);
179
		$acl->read_repository();
180
 		foreach ($calendar_bo->resources as $type => $data)
181
		{
182
			// Check anon user's permissions - must have at least run for the hook to be available
183
			if($acl->check('run',EGW_ACL_READ, $data['app']) &&
184
				$type != '' && $data['app'] && Link::get_registry($data['app'], 'query')
185
			)
186
			{
187
				$_results = Link::query($data['app'], $query,$options);
188
			}
189
			if(!$_results) continue;
190
			$_results = array_unique($_results);
191
			foreach ($_results as $key => $value)
192
			{
193
				if($calendar_bo->check_perms(Acl::READ,0,$type.$key,'ts',null,$anon_user))
194
				{
195
					$this->arguments['resources']['options'][$type.$key] = $value;
196
				}
197
			}
198
		}
199
		$this->arguments['resources']['options'] = array_unique($this->arguments['resources']['options']);
200
		$this->arguments['resources']['multiple'] = count($this->arguments['resources']['options']) ? 4 : 0;
201
202
203
		return parent::get_user_interface();
204
	}
205
206
	/**
207
	 * Get block content
208
	 *
209
	 * @param $arguments
210
	 * @param $properties
211
	 */
212
	function get_content(&$arguments,$properties)
213
	{
214
		$GLOBALS['egw_info']['flags']['currentapp'] = 'calendar';
215
216
		//error_log(array2string($arguments));
217
		if (empty($arguments['date']))
218
		{
219
			$arguments['date'] = date('Ymd');
220
		}
221
		if ($arguments['sortby'] == 'yearly')
222
		{
223
			$arguments['sortby'] = 'month';
224
			$arguments['date'] = substr($arguments['date'],0,4).'0101';
225
		}
226
		if (isset($_GET['date'])) $arguments['date'] = $_GET['date'];
227
		if (empty($arguments['cat_id'])) $arguments['cat_id'] = '';
228
		if(isset($arguments['resources']) && in_array('r0', $arguments['resources']))
229
		{
230
			foreach($arguments['resources'] as $index => $value)
231
			{
232
				if($value == 'r0')
233
				{
234
					unset($arguments['resources'][$index]);
235
				}
236
			}
237
		}
238
239
		$params = $arguments;
240
		if (isset($params['resources']) && count($params['resources']))
241
		{
242
			$params['owner'] = array_merge((array)$params['owner'], (array)$params['resources']);
243
			unset($params['resources']);
244
		}
245
246
		$html = '<style type="text/css">'."\n";
247
		$html .= '@import url('.$GLOBALS['egw_info']['server']['webserver_url'].self::CALENDAR_CSS.");\n";
248
		$html .= '@import url('.$GLOBALS['egw_info']['server']['webserver_url'].self::ETEMPLATE_CSS.");\n";
249
		$html .= '@import url('.$GLOBALS['egw_info']['server']['webserver_url'].Api\Categories::css(Api\Categories::GLOBAL_APPNAME).");\n";
250
		$html .= '@import url('.$GLOBALS['egw_info']['server']['webserver_url'].Api\Categories::css('calendar').");\n";
251
		$html .= '.popupMainDiv #calendar-planner { position: static;}
252
		#calendar-planner .calendar_plannerWidget, #calendar-planner div.calendar_plannerRows {
253
    height: auto !important;
254
}
255
	</style>'."\n";
256
		$html .= Api\Html::image('sitemgr', 'left', lang('Previous'), 'onclick=\'app.calendar.toolbar_action({id:"previous"});\'')
257
		. Api\Html::image('sitemgr', 'right', lang('Next'), 'style="float: right;" onclick=\'app.calendar.toolbar_action({id:"next"});\'');
258
259
		if (is_array($params['owner']))
260
		{
261
			// Buffer, and add anything that gets cleared to the content
262
			ob_start(function($buffer) use(&$html) {
263
				$html .= $buffer;
264
				return '';
265
			});
266
			Framework::$header_done = true;
267
			$ui = new calendar_uiviews();
268
			$ui->sortby = $arguments['sortby'];
269
			$ui->owner = $params['owner'];
270
271
			if (!$ui->planner_view || $ui->planner_view == 'month')	// planner monthview
0 ignored issues
show
Bug introduced by
The property planner_view does not seem to exist on calendar_uiviews.
Loading history...
272
			{
273
				if ($ui->day < 15)	// show one complete month
274
				{
275
					$ui->_week_align_month($ui->first,$ui->last);
276
				}
277
				else	// show 2 half month
278
				{
279
					$ui->_week_align_month($ui->first,$ui->last,15);
280
				}
281
			}
282
			elseif ($ui->planner_view == 'week' || $ui->planner_view == 'weekN')	// weeekview
283
			{
284
				$start = new Api\DateTime($ui->date);
285
				$start->setWeekstart();
286
				$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...
287
				$ui->last = $ui->bo->date2array($this->first);
0 ignored issues
show
Documentation Bug introduced by
It seems like $ui->bo->date2array($this->first) of type array is incompatible with the declared type integer of property $last.

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...
288
				$ui->last['day'] += ($ui->planner_view == 'week' ? 7 : 7 * $ui->cal_prefs['multiple_weeks'])-1;
289
				$ui->last['hour'] = 23; $ui->last['minute'] = $ui->last['sec'] = 59;
290
				unset($ui->last['raw']);
291
				$ui->last = $ui->bo->date2ts($ui->last);
292
			}
293
			else // dayview
294
			{
295
				$ui->first = $ui->bo->date2ts($ui->date);
296
				$ui->last = $ui->bo->date2array($ui->first);
297
				$ui->last['day'] += 0;
298
				$ui->last['hour'] = 23; $ui->last['minute'] = $ui->last['sec'] = 59;
299
				unset($ui->last['raw']);
300
				$ui->last = $ui->bo->date2ts($ui->last);
301
			}
302
303
			$search_params = $ui->search_params;
304
			$search_params['daywise'] = false;
305
			$search_params['start'] = $ui->first;
306
			$search_params['end'] = $ui->last;
307
			$search_params['owner'] = $ui->owner;
308
			$search_params['enum_groups'] = $ui->sortby == 'user';
309
310
			$content = array();
311
			$sel_options = array();
312
			$content['planner'] = $ui->bo->search($search_params);
313
			foreach($content['planner'] as &$event)
314
			{
315
				$ui->to_client($event);
316
			}
317
318
			$tmpl = new Etemplate('calendar.planner');
319
320
			$tmpl->setElementAttribute('planner','start_date', Api\DateTime::to($ui->first, Api\DateTime::ET2));
321
			$tmpl->setElementAttribute('planner','end_date', Api\DateTime::to($ui->last, Api\DateTime::ET2));
322
			$tmpl->setElementAttribute('planner','owner', $search_params['owner']);
323
			$tmpl->setElementAttribute('planner','group_by', $ui->sortby);
324
325
			// Make sure all used owners are there, faking
326
			// calendar_owner_etemplate_widget::beforeSendToClient() since the
327
			// rest of the calendar app is probably missing.
328
			foreach($search_params['owner'] as $owner)
329
			{
330
				$sel_options['owner'][] = Array(
331
					'id' => $owner,
332
					'value' => $owner,
333
					'label' => calendar_owner_etemplate_widget::get_owner_label($owner)
334
				);
335
			}
336
			$tmpl->exec(__METHOD__, $content,$sel_options, array('__ALL__' => true),array(),2);
337
			$html .= ob_get_contents();
338
			$html .= '<script>'
339
			. '(function() {jQuery("#calendar-planner").on("load",function() {'
340
			. 'app.calendar.update_state(' . json_encode(array(
341
					'view'   => 'planner',
342
					'planner_view' => 'month',
343
					'date'   => Api\DateTime::to($ui->first, Api\DateTime::ET2),
344
					'owner'  => $search_params['owner'],
345
					'sortby' => $ui->sortby,
346
					'filter' => $arguments['filter']
347
				)).');'
348
			. '});})();'
349
			. '</script>';
350
351
			ob_end_clean();
352
		}
353
		else
354
		{
355
			$html .= '<div class="message" align="center">'.lang('No owner selected').'</div>';
356
		}
357
358
		return $html;
359
	}
360
}
361