Passed
Branch develop (5cbde9)
by
unknown
26:38
created

CdavLib   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 275
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 144
c 0
b 0
f 0
dl 0
loc 275
rs 8.96
wmc 43

4 Methods

Rating   Name   Duplication   Size   Complexity  
F toVCalendar() 0 121 28
A __construct() 0 5 1
A getSqlCalEvents() 0 57 4
B getFullCalendarObjects() 0 48 10

How to fix   Complexity   

Complex Class

Complex classes like CdavLib often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CdavLib, and based on these observations, apply Extract Interface, too.

1
<?php
2
/* Copyright (C) 2018	Destailleur Laurent	<[email protected]>
3
 *
4
 * This program 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 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * 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, see <http://www.gnu.org/licenses/>.
16
 */
17
18
/**
19
 *      \file       htdocs/dav/dav.class.php
20
 *      \ingroup    dav
21
 *      \brief      Server DAV
22
 */
23
24
25
/**
26
 * Define Common function to access calendar items and format it in vCalendar
27
 */
28
class CdavLib
29
{
30
31
	private $db;
32
33
	private $user;
34
35
	private $langs;
36
37
    /**
38
     * Constructor
39
     *
40
     * @param   User        $user   user
41
     * @param   DoliDB      $db     Database handler
42
     * @param   Translate   $langs  translation
43
     */
44
    public function __construct($user, $db, $langs)
45
	{
46
		$this->user = $user;
47
		$this->db = $db;
48
		$this->langs = $langs;
49
	}
50
51
	/**
52
	 * Base sql request for calendar events
53
	 *
54
	 * @param 	int 		$calid 			Calendard id
55
	 * @param 	int|boolean	$oid			Oid
56
	 * @param	int|boolean	$ouri			Ouri
57
	 * @return string
58
	 */
59
	public function getSqlCalEvents($calid, $oid = false, $ouri = false)
60
	{
61
		// TODO : replace GROUP_CONCAT by
62
		$sql = 'SELECT
63
					a.tms AS lastupd,
64
					a.*,
65
					sp.firstname,
66
					sp.lastname,
67
					sp.address,
68
					sp.zip,
69
					sp.town,
70
					co.label country_label,
71
					sp.phone,
72
					sp.phone_perso,
73
					sp.phone_mobile,
74
					s.nom AS soc_nom,
75
					s.address soc_address,
76
					s.zip soc_zip,
77
					s.town soc_town,
78
					cos.label soc_country_label,
79
					s.phone soc_phone,
80
					ac.sourceuid,
81
					(SELECT GROUP_CONCAT(u.login) FROM '.MAIN_DB_PREFIX.'actioncomm_resources ar
82
						LEFT OUTER JOIN '.MAIN_DB_PREFIX.'user AS u ON (u.rowid=fk_element)
83
						WHERE ar.element_type=\'user\' AND fk_actioncomm=a.id) AS other_users
84
				FROM '.MAIN_DB_PREFIX.'actioncomm AS a';
85
		if (! $this->user->rights->societe->client->voir )//FIXME si 'voir' on voit plus de chose ?
86
		{
87
			$sql.=' LEFT OUTER JOIN '.MAIN_DB_PREFIX.'societe_commerciaux AS sc ON (a.fk_soc = sc.fk_soc AND sc.fk_user='.$this->user->id.')
88
					LEFT JOIN '.MAIN_DB_PREFIX.'societe AS s ON (s.rowid = sc.fk_soc)
89
					LEFT JOIN '.MAIN_DB_PREFIX.'socpeople AS sp ON (sp.fk_soc = sc.fk_soc AND sp.rowid = a.fk_contact)
90
					LEFT JOIN '.MAIN_DB_PREFIX.'actioncomm_cdav AS ac ON (a.id = ac.fk_object)';
91
		}
92
		else
93
		{
94
			$sql.=' LEFT JOIN '.MAIN_DB_PREFIX.'societe AS s ON (s.rowid = a.fk_soc)
95
					LEFT JOIN '.MAIN_DB_PREFIX.'socpeople AS sp ON (sp.rowid = a.fk_contact)
96
					LEFT JOIN '.MAIN_DB_PREFIX.'actioncomm_cdav AS ac ON (a.id = ac.fk_object)';
97
		}
98
99
		$sql.=' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as co ON co.rowid = sp.fk_pays
100
				LEFT JOIN '.MAIN_DB_PREFIX.'c_country as cos ON cos.rowid = s.fk_pays
101
				WHERE 	a.id IN (SELECT ar.fk_actioncomm FROM '.MAIN_DB_PREFIX.'actioncomm_resources ar WHERE ar.element_type=\'user\' AND ar.fk_element='.intval($calid).')
102
						AND a.code IN (SELECT cac.code FROM '.MAIN_DB_PREFIX.'c_actioncomm cac WHERE cac.type<>\'systemauto\')
103
						AND a.entity IN ('.getEntity('societe', 1).')';
104
		if($oid!==false) {
105
			if($ouri===false)
106
			{
107
				$sql.=' AND a.id = '.intval($oid);
108
			}
109
			else
110
			{
111
				$sql.=' AND (a.id = '.intval($oid).' OR ac.uuidext = \''.$this->db->escape($ouri).'\')';
112
			}
113
		}
114
115
		return $sql;
116
	}
117
118
	/**
119
	 * Convert calendar row to VCalendar string
120
	 *
121
	 * @param 	int		$calid		Calendar id
122
	 * @param	Object	$obj		Object id
123
	 * @return string
124
	 */
125
	public function toVCalendar($calid, $obj)
126
	{
127
		/*$categ = array();
128
		if($obj->soc_client)
129
		{
130
			$nick[] = $obj->soc_code_client;
131
			$categ[] = $this->langs->transnoentitiesnoconv('Customer');
132
		}*/
133
134
		$location=$obj->location;
135
136
		// contact address
137
		if(empty($location) && !empty($obj->address))
138
		{
139
			$location = trim(str_replace(array("\r","\t","\n"), ' ', $obj->address));
140
			$location = trim($location.', '.$obj->zip);
141
			$location = trim($location.' '.$obj->town);
142
			$location = trim($location.', '.$obj->country_label);
143
		}
144
145
		// contact address
146
		if(empty($location) && !empty($obj->soc_address))
147
		{
148
			$location = trim(str_replace(array("\r","\t","\n"), ' ', $obj->soc_address));
149
			$location = trim($location.', '.$obj->soc_zip);
150
			$location = trim($location.' '.$obj->soc_town);
151
			$location = trim($location.', '.$obj->soc_country_label);
152
		}
153
154
		$address=explode("\n", $obj->address, 2);
155
		foreach($address as $kAddr => $vAddr)
156
		{
157
			$address[$kAddr] = trim(str_replace(array("\r","\t"), ' ', str_replace("\n", ' | ', trim($vAddr))));
158
		}
159
		$address[]='';
160
		$address[]='';
161
162
		if($obj->percent==-1 && trim($obj->datep)!='')
163
			$type='VEVENT';
164
		else
165
			$type='VTODO';
166
167
		$timezone = date_default_timezone_get();
168
169
		$caldata ="BEGIN:VCALENDAR\n";
170
		$caldata.="VERSION:2.0\n";
171
		$caldata.="METHOD:PUBLISH\n";
172
		$caldata.="PRODID:-//Dolibarr CDav//FR\n";
173
		$caldata.="BEGIN:".$type."\n";
174
		$caldata.="CREATED:".gmdate('Ymd\THis', strtotime($obj->datec))."Z\n";
175
		$caldata.="LAST-MODIFIED:".gmdate('Ymd\THis', strtotime($obj->lastupd))."Z\n";
176
		$caldata.="DTSTAMP:".gmdate('Ymd\THis', strtotime($obj->lastupd))."Z\n";
177
		if($obj->sourceuid=='')
178
			$caldata.="UID:".$obj->id.'-ev-'.$calid.'-cal-'.CDAV_URI_KEY."\n";
179
		else
180
			$caldata.="UID:".$obj->sourceuid."\n";
181
		$caldata.="SUMMARY:".$obj->label."\n";
182
		$caldata.="LOCATION:".$location."\n";
183
		$caldata.="PRIORITY:".$obj->priority."\n";
184
		if($obj->fulldayevent)
185
		{
186
			$caldata.="DTSTART;VALUE=DATE:".date('Ymd', strtotime($obj->datep))."\n";
187
			if($type=='VEVENT')
188
			{
189
				if(trim($obj->datep2)!='')
190
					$caldata.="DTEND;VALUE=DATE:".date('Ymd', strtotime($obj->datep2)+1)."\n";
191
				else
192
					$caldata.="DTEND;VALUE=DATE:".date('Ymd', strtotime($obj->datep)+(25*3600))."\n";
193
			}
194
			elseif(trim($obj->datep2)!='')
195
				$caldata.="DUE;VALUE=DATE:".date('Ymd', strtotime($obj->datep2)+1)."\n";
196
		}
197
		else
198
		{
199
			$caldata.="DTSTART;TZID=".$timezone.":".strtr($obj->datep, array(" "=>"T", ":"=>"", "-"=>""))."\n";
200
			if($type=='VEVENT')
201
			{
202
				if(trim($obj->datep2)!='')
203
					$caldata.="DTEND;TZID=".$timezone.":".strtr($obj->datep2, array(" "=>"T", ":"=>"", "-"=>""))."\n";
204
				else
205
					$caldata.="DTEND;TZID=".$timezone.":".strtr($obj->datep, array(" "=>"T", ":"=>"", "-"=>""))."\n";
206
			}
207
			elseif(trim($obj->datep2)!='')
208
				$caldata.="DUE;TZID=".$timezone.":".strtr($obj->datep2, array(" "=>"T", ":"=>"", "-"=>""))."\n";
209
		}
210
		$caldata.="CLASS:PUBLIC\n";
211
		if($obj->transparency==1)
212
			$caldata.="TRANSP:TRANSPARENT\n";
213
		else
214
			$caldata.="TRANSP:OPAQUE\n";
215
216
		if($type=='VEVENT')
217
			$caldata.="STATUS:CONFIRMED\n";
218
		elseif($obj->percent==0)
219
			$caldata.="STATUS:NEEDS-ACTION\n";
220
		elseif($obj->percent==100)
221
			$caldata.="STATUS:COMPLETED\n";
222
		else
223
		{
224
			$caldata.="STATUS:IN-PROCESS\n";
225
			$caldata.="PERCENT-COMPLETE:".$obj->percent."\n";
226
		}
227
228
		$caldata.="DESCRIPTION:";
229
		$caldata.=strtr($obj->note, array("\n"=>"\\n", "\r"=>""));
230
		if(!empty($obj->soc_nom))
231
			$caldata.="\\n*DOLIBARR-SOC: ".$obj->soc_nom;
232
		if(!empty($obj->soc_phone))
233
			$caldata.="\\n*DOLIBARR-SOC-TEL: ".$obj->soc_phone;
234
		if(!empty($obj->firstname) || !empty($obj->lastname))
235
			$caldata.="\\n*DOLIBARR-CTC: ".trim($obj->firstname.' '.$obj->lastname);
236
		if(!empty($obj->phone) || !empty($obj->phone_perso) || !empty($obj->phone_mobile))
237
			$caldata.="\\n*DOLIBARR-CTC-TEL: ".trim($obj->phone.' '.$obj->phone_perso.' '.$obj->phone_mobile);
238
		if(strpos($obj->other_users, ',')) // several
239
			$caldata.="\\n*DOLIBARR-USR: ".$obj->other_users;
240
		$caldata.="\n";
241
242
		$caldata.="END:".$type."\n";
243
		$caldata.="END:VCALENDAR\n";
244
245
		return $caldata;
246
	}
247
248
    /**
249
     * getFullCalendarObjects
250
     *
251
     * @param int	 	$calendarId			Calendar id
252
     * @param int		$bCalendarData		Add calendar data
253
     * @return array|string[][]
254
     */
255
    public function getFullCalendarObjects($calendarId, $bCalendarData)
256
    {
257
		$calid = ($calendarId*1);
258
		$calevents = array();
259
260
		if(! $this->user->rights->agenda->myactions->read)
261
			return $calevents;
262
263
		if($calid!=$this->user->id && (!isset($this->user->rights->agenda->allactions->read) || !$this->user->rights->agenda->allactions->read))
264
			return $calevents;
265
266
		$sql = $this->getSqlCalEvents($calid);
267
268
		$result = $this->db->query($sql);
269
270
		if ($result)
271
		{
272
			while ($obj = $this->db->fetch_object($result))
273
			{
274
				$calendardata = $this->toVCalendar($calid, $obj);
275
276
				if($bCalendarData)
277
				{
278
					$calevents[] = array(
279
						'calendardata' => $calendardata,
280
						'uri' => $obj->id.'-ev-'.CDAV_URI_KEY,
281
						'lastmodified' => strtotime($obj->lastupd),
282
						'etag' => '"'.md5($calendardata).'"',
283
						'calendarid'   => $calendarId,
284
						'size' => strlen($calendardata),
285
						'component' => strpos($calendardata, 'BEGIN:VEVENT')>0 ? 'vevent' : 'vtodo',
286
					);
287
				}
288
				else
289
				{
290
					$calevents[] = array(
291
						// 'calendardata' => $calendardata,  not necessary because etag+size are present
292
						'uri' => $obj->id.'-ev-'.CDAV_URI_KEY,
293
						'lastmodified' => strtotime($obj->lastupd),
294
						'etag' => '"'.md5($calendardata).'"',
295
						'calendarid'   => $calendarId,
296
						'size' => strlen($calendardata),
297
						'component' => strpos($calendardata, 'BEGIN:VEVENT')>0 ? 'vevent' : 'vtodo',
298
					);
299
				}
300
			}
301
		}
302
		return $calevents;
303
	}
304
}
305