dolibarr 20.0.0
dav.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2018 Destailleur Laurent <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
30{
34 private $db;
35
39 private $user;
40
44 private $langs; // @phpstan-ignore-line
45
53 public function __construct($user, $db, $langs)
54 {
55 $this->user = $user;
56 $this->db = $db;
57 $this->langs = $langs;
58 }
59
68 public function getSqlCalEvents($calid, $oid = false, $ouri = false)
69 {
70 // TODO : replace GROUP_CONCAT by
71 $sql = 'SELECT
72 a.tms AS lastupd,
73 a.*,
74 sp.firstname,
75 sp.lastname,
76 sp.address,
77 sp.zip,
78 sp.town,
79 co.label country_label,
80 sp.phone,
81 sp.phone_perso,
82 sp.phone_mobile,
83 s.nom AS soc_nom,
84 s.address soc_address,
85 s.zip soc_zip,
86 s.town soc_town,
87 cos.label soc_country_label,
88 s.phone soc_phone,
89 ac.sourceuid,
90 (SELECT GROUP_CONCAT(u.login) FROM '.MAIN_DB_PREFIX.'actioncomm_resources ar
91 LEFT OUTER JOIN '.MAIN_DB_PREFIX.'user AS u ON (u.rowid=fk_element)
92 WHERE ar.element_type=\'user\' AND fk_actioncomm=a.id) AS other_users
93 FROM '.MAIN_DB_PREFIX.'actioncomm AS a';
94 $sql .= " LEFT JOIN '.MAIN_DB_PREFIX.'c_country as co ON co.rowid = sp.fk_pays
95 LEFT JOIN '.MAIN_DB_PREFIX.'c_country as cos ON cos.rowid = s.fk_pays
96 WHERE a.id IN (SELECT ar.fk_actioncomm FROM '.MAIN_DB_PREFIX.'actioncomm_resources ar WHERE ar.element_type='user' AND ar.fk_element=".((int) $calid).")
97 AND a.code IN (SELECT cac.code FROM '.MAIN_DB_PREFIX.'c_actioncomm cac WHERE cac.type <> 'systemauto')
98 AND a.entity IN (".getEntity('societe', 1).")";
99 // TODO Restrict on external users
100 if ($oid !== false) {
101 if ($ouri === false) {
102 $sql .= ' AND a.id = '.((int) $oid);
103 } else {
104 $sql .= ' AND (a.id = '.((int) $oid)." OR ac.uuidext = '".$this->db->escape($ouri)."')";
105 }
106 }
107
108 return $sql;
109 }
110
118 public function toVCalendar($calid, $obj)
119 {
120 /*$categ = array();
121 if($obj->soc_client)
122 {
123 $nick[] = $obj->soc_code_client;
124 $categ[] = $this->langs->transnoentitiesnoconv('Customer');
125 }*/
126
127 $location = $obj->location;
128
129 // contact address
130 if (empty($location) && !empty($obj->address)) {
131 $location = trim(str_replace(array("\r", "\t", "\n"), ' ', $obj->address));
132 $location = trim($location.', '.$obj->zip);
133 $location = trim($location.' '.$obj->town);
134 $location = trim($location.', '.$obj->country_label);
135 }
136
137 // contact address
138 if (empty($location) && !empty($obj->soc_address)) {
139 $location = trim(str_replace(array("\r", "\t", "\n"), ' ', $obj->soc_address));
140 $location = trim($location.', '.$obj->soc_zip);
141 $location = trim($location.' '.$obj->soc_town);
142 $location = trim($location.', '.$obj->soc_country_label);
143 }
144
145 /*
146 $address = explode("\n", $obj->address, 2);
147 foreach ($address as $kAddr => $vAddr) {
148 $address[$kAddr] = trim(str_replace(array("\r", "\t"), ' ', str_replace("\n", ' | ', trim($vAddr))));
149 }
150 $address[] = '';
151 $address[] = '';
152 */
153
154 if ($obj->percent == -1 && trim($obj->datep) != '') {
155 $type = 'VEVENT';
156 } else {
157 $type = 'VTODO';
158 }
159
160 $timezone = date_default_timezone_get();
161
162 $caldata = "BEGIN:VCALENDAR\n";
163 $caldata .= "VERSION:2.0\n";
164 $caldata .= "METHOD:PUBLISH\n";
165 $caldata .= "PRODID:-//Dolibarr CDav//FR\n";
166 $caldata .= "BEGIN:".$type."\n";
167 $caldata .= "CREATED:".gmdate('Ymd\THis', strtotime($obj->datec))."Z\n";
168 $caldata .= "LAST-MODIFIED:".gmdate('Ymd\THis', strtotime($obj->lastupd))."Z\n";
169 $caldata .= "DTSTAMP:".gmdate('Ymd\THis', strtotime($obj->lastupd))."Z\n";
170 if ($obj->sourceuid == '') {
171 $caldata .= "UID:".$obj->id.'-ev-'.$calid.'-cal-'.constant('CDAV_URI_KEY')."\n";
172 } else {
173 $caldata .= "UID:".$obj->sourceuid."\n";
174 }
175 $caldata .= "SUMMARY:".$obj->label."\n";
176 $caldata .= "LOCATION:".$location."\n";
177 $caldata .= "PRIORITY:".$obj->priority."\n";
178 if ($obj->fulldayevent) {
179 $caldata .= "DTSTART;VALUE=DATE:".date('Ymd', strtotime($obj->datep))."\n";
180 if ($type == 'VEVENT') {
181 if (trim($obj->datep2) != '') {
182 $caldata .= "DTEND;VALUE=DATE:".date('Ymd', strtotime($obj->datep2) + 1)."\n";
183 } else {
184 $caldata .= "DTEND;VALUE=DATE:".date('Ymd', strtotime($obj->datep) + (25 * 3600))."\n";
185 }
186 } elseif (trim($obj->datep2) != '') {
187 $caldata .= "DUE;VALUE=DATE:".date('Ymd', strtotime($obj->datep2) + 1)."\n";
188 }
189 } else {
190 $caldata .= "DTSTART;TZID=".$timezone.":".strtr($obj->datep, array(" "=>"T", ":"=>"", "-"=>""))."\n";
191 if ($type == 'VEVENT') {
192 if (trim($obj->datep2) != '') {
193 $caldata .= "DTEND;TZID=".$timezone.":".strtr($obj->datep2, array(" "=>"T", ":"=>"", "-"=>""))."\n";
194 } else {
195 $caldata .= "DTEND;TZID=".$timezone.":".strtr($obj->datep, array(" "=>"T", ":"=>"", "-"=>""))."\n";
196 }
197 } elseif (trim($obj->datep2) != '') {
198 $caldata .= "DUE;TZID=".$timezone.":".strtr($obj->datep2, array(" "=>"T", ":"=>"", "-"=>""))."\n";
199 }
200 }
201 $caldata .= "CLASS:PUBLIC\n";
202 if ($obj->transparency == 1) {
203 $caldata .= "TRANSP:TRANSPARENT\n";
204 } else {
205 $caldata .= "TRANSP:OPAQUE\n";
206 }
207
208 if ($type == 'VEVENT') {
209 $caldata .= "STATUS:CONFIRMED\n";
210 } elseif ($obj->percent == 0) {
211 $caldata .= "STATUS:NEEDS-ACTION\n";
212 } elseif ($obj->percent == 100) {
213 $caldata .= "STATUS:COMPLETED\n";
214 } else {
215 $caldata .= "STATUS:IN-PROCESS\n";
216 $caldata .= "PERCENT-COMPLETE:".$obj->percent."\n";
217 }
218
219 $caldata .= "DESCRIPTION:";
220 $caldata .= strtr($obj->note, array("\n"=>"\\n", "\r"=>""));
221 if (!empty($obj->soc_nom)) {
222 $caldata .= "\\n*DOLIBARR-SOC: ".$obj->soc_nom;
223 }
224 if (!empty($obj->soc_phone)) {
225 $caldata .= "\\n*DOLIBARR-SOC-TEL: ".$obj->soc_phone;
226 }
227 if (!empty($obj->firstname) || !empty($obj->lastname)) {
228 $caldata .= "\\n*DOLIBARR-CTC: ".trim($obj->firstname.' '.$obj->lastname);
229 }
230 if (!empty($obj->phone) || !empty($obj->phone_perso) || !empty($obj->phone_mobile)) {
231 $caldata .= "\\n*DOLIBARR-CTC-TEL: ".trim($obj->phone.' '.$obj->phone_perso.' '.$obj->phone_mobile);
232 }
233 if (strpos($obj->other_users, ',')) { // several
234 $caldata .= "\\n*DOLIBARR-USR: ".$obj->other_users;
235 }
236 $caldata .= "\n";
237
238 $caldata .= "END:".$type."\n";
239 $caldata .= "END:VCALENDAR\n";
240
241 return $caldata;
242 }
243
251 public function getFullCalendarObjects($calendarId, $bCalendarData)
252 {
253 $calid = (int) $calendarId;
254 $calevents = array();
255
256 if (!$this->user->rights->agenda->myactions->read) {
257 return $calevents;
258 }
259
260 if ($calid != $this->user->id && (!isset($this->user->rights->agenda->allactions->read) || !$this->user->rights->agenda->allactions->read)) {
261 return $calevents;
262 }
263
264 $sql = $this->getSqlCalEvents($calid);
265
266 $result = $this->db->query($sql);
267
268 if ($result) {
269 while ($obj = $this->db->fetch_object($result)) {
270 $calendardata = $this->toVCalendar($calid, $obj);
271
272 if ($bCalendarData) {
273 $calevents[] = array(
274 'calendardata' => $calendardata,
275 'uri' => $obj->id.'-ev-'.constant('CDAV_URI_KEY'),
276 'lastmodified' => strtotime($obj->lastupd),
277 'etag' => '"'.md5($calendardata).'"',
278 'calendarid' => $calendarId,
279 'size' => strlen($calendardata),
280 'component' => strpos($calendardata, 'BEGIN:VEVENT') > 0 ? 'vevent' : 'vtodo',
281 );
282 } else {
283 $calevents[] = array(
284 // 'calendardata' => $calendardata, not necessary because etag+size are present
285 'uri' => $obj->id.'-ev-'.constant('CDAV_URI_KEY'),
286 'lastmodified' => strtotime($obj->lastupd),
287 'etag' => '"'.md5($calendardata).'"',
288 'calendarid' => $calendarId,
289 'size' => strlen($calendardata),
290 'component' => strpos($calendardata, 'BEGIN:VEVENT') > 0 ? 'vevent' : 'vtodo',
291 );
292 }
293 }
294 }
295 return $calevents;
296 }
297}
Define Common function to access calendar items and format it in vCalendar.
Definition dav.class.php:30
getFullCalendarObjects($calendarId, $bCalendarData)
getFullCalendarObjects
toVCalendar($calid, $obj)
Convert calendar row to VCalendar string.
getSqlCalEvents($calid, $oid=false, $ouri=false)
Base sql request for calendar events.
Definition dav.class.php:68
__construct($user, $db, $langs)
Constructor.
Definition dav.class.php:53
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
$conf db user
Active Directory does not allow anonymous connections.
Definition repair.php:143