eventperiod.php ➔ caldav_onCollectCalendarsBeforeDisplay()   C
last analyzed

Complexity

Conditions 14
Paths 61

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
nc 61
nop 1
dl 0
loc 65
rs 6.2666
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
// OVIDENTIA http://www.ovidentia.org
4
// Ovidentia is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 2, or (at your option)
7
// any later version.
8
//
9
// This program is distributed in the hope that it will be useful, but
10
// WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
// See the GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17
// USA.
18
//-------------------------------------------------------------------------
19
/**
20
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
21
 * @copyright Copyright (c) 2010 by CANTICO ({@link http://www.cantico.fr})
22
 */
23
24
/**
25
 * Function registered to collect events.
26
 *
27
 * @param bab_eventBeforePeriodsCreated $event
28
 */
29
function caldav_onBeforePeriodsCreated(bab_eventBeforePeriodsCreated $event)
30
{
31
32
33
	if (!is_array($event->periods->calendars)) {
34
		return;
35
	}
36
37
	if (!$event->periods->isPeriodCollection('bab_CalendarEventCollection')) {
38
		return;
39
	}
40
	
41
	/* @var $backend Func_CalendarBackend_Caldav */
42
	$backend = bab_functionality::get('CalendarBackend/Caldav');
43
	
44
	$oneCalendar = false;
45
	foreach($event->periods->calendars as $calendar)
46
	{
47
		if ($calendar->getBackend() instanceof $backend)
48
		{
49
			$oneCalendar= true;
50
			break;
51
		}
52
	}
53
54
	
55
	if (!$oneCalendar)
56
	{
57
		return;
58
	}
59
	
60
	
61
62
63
	$criteria = $event->getCriteria();
64
65
	$beginDate = $event->getBeginDate();
66
	if ($beginDate instanceof BAB_DateTime) {
67
		$criteria = $criteria->_AND_(bab_PeriodCriteriaFactory::Begin($event->getBeginDate()));
68
	}
69
	$endDate = $event->getEndDate();
70
	if ($endDate instanceof BAB_DateTime) {
71
		$criteria = $criteria->_AND_(bab_PeriodCriteriaFactory::End($event->getEndDate()));
72
	}
73
74
	$periods = $backend->selectPeriods($criteria);
75
76
	foreach ($periods as $period) {
77
	//	bab_debug($period->toHtml(), DBG_TRACE, 'caldav');
78
		$event->periods->addPeriod($period);
79
	}
80
}
81
82
83
84
85
86
/**
87
 * Function registered to collect calendars.
88
 *
89
 * Add all calendars from caldav to the bab_icalendars collection
90
 *
91
 * @param bab_eventCollectCalendarsBeforeDisplay $event
92
 */
93
function caldav_onCollectCalendarsBeforeDisplay(bab_eventCollectCalendarsBeforeDisplay $event)
94
{
95
	global $babBody;
96
97
	/* @var $backend Func_CalendarBackend_Caldav */
98
	$backend = bab_functionality::get('CalendarBackend/Caldav');
99
	if (!isset($backend) || $backend === false) {
100
		return;
101
	}
102
103
	$personal_calendar = null;
104
	$access_user = $event->getAccessUser();
105
	$calendar_backend = $event->getBackendName();  // bab_getICalendars()->calendar_backend;
106
107
	if (!empty($access_user) && 'Caldav' === $calendar_backend) {
108
109
		try {
110
			$personal_calendar = $backend->PersonalCalendar($access_user);
111
		}
112
		catch(Exception $e)
113
		{
114
			bab_debug($e->getMessage());
115
		}
116
117
		if ($personal_calendar) {
118
			$personal_calendar->setName(bab_getUserName($access_user, true) . ' (CalDAV)');
119
			$personal_calendar->setAccessUser($access_user);
120
			$event->addCalendar($personal_calendar);
121
		}
122
	}
123
124
125
	if ('Caldav' !== $calendar_backend || $personal_calendar || $babBody->babsite['iPersonalCalendarAccess'] == 'Y') {
126
		$arr = $backend->getAccessiblePersonalCalendars($access_user, 'caldav_personal');
127
128
		foreach ($arr as $id_user) {
129
			$calendar = $backend->PersonalCalendar($id_user);
130
			$calendar->setName(bab_getUserName($id_user, true) . ' (CalDAV)');
131
			$calendar->setAccessUser($access_user);
132
			$event->addCalendar($calendar);
133
		}
134
	}
135
	
136
	// add resources calendars
137
	
138
	if (isset($GLOBALS['BAB_SESS_USERID']))
139
	{
140
		$_idu = $GLOBALS['BAB_SESS_USERID'];
141
	} else {
142
		// new addon controller
143
		$_idu = bab_getUserId();
144
	}
145
	
146
	
147
	if ($access_user == $_idu)
148
	{
149
		$arr = $backend->getAccessibleResourceCalendars();
150
		
151
		foreach ($arr as $uid => $resource) {
152
			$calendar = $backend->ResourceCalendar($uid, $resource);
153
			$calendar->setName($resource['name'] . ' (CalDAV)');
154
			$event->addCalendar($calendar);
155
		}
156
	}
157
}
158
159
160