dolibarr 19.0.3
dav.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2018 Destailleur Laurent <eldy@users.sourceforge.net>
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 <https://www.gnu.org/licenses/>.
16 */
17
29{
30 private $db;
31
32 private $user;
33
34 private $langs;
35
43 public function __construct($user, $db, $langs)
44 {
45 $this->user = $user;
46 $this->db = $db;
47 $this->langs = $langs;
48 }
49
58 public function getSqlCalEvents($calid, $oid = false, $ouri = false)
59 {
60 // TODO : replace GROUP_CONCAT by
61 $sql = 'SELECT
62 a.tms AS lastupd,
63 a.*,
64 sp.firstname,
65 sp.lastname,
66 sp.address,
67 sp.zip,
68 sp.town,
69 co.label country_label,
70 sp.phone,
71 sp.phone_perso,
72 sp.phone_mobile,
73 s.nom AS soc_nom,
74 s.address soc_address,
75 s.zip soc_zip,
76 s.town soc_town,
77 cos.label soc_country_label,
78 s.phone soc_phone,
79 ac.sourceuid,
80 (SELECT GROUP_CONCAT(u.login) FROM '.MAIN_DB_PREFIX.'actioncomm_resources ar
81 LEFT OUTER JOIN '.MAIN_DB_PREFIX.'user AS u ON (u.rowid=fk_element)
82 WHERE ar.element_type=\'user\' AND fk_actioncomm=a.id) AS other_users
83 FROM '.MAIN_DB_PREFIX.'actioncomm AS a';
84 if (!$this->user->rights->societe->client->voir) { //FIXME si 'voir' on voit plus de chose ?
85 $sql .= ' LEFT OUTER JOIN '.MAIN_DB_PREFIX.'societe_commerciaux AS sc ON (a.fk_soc = sc.fk_soc AND sc.fk_user='.((int) $this->user->id).')
86 LEFT JOIN '.MAIN_DB_PREFIX.'societe AS s ON (s.rowid = sc.fk_soc)
87 LEFT JOIN '.MAIN_DB_PREFIX.'socpeople AS sp ON (sp.fk_soc = sc.fk_soc AND sp.rowid = a.fk_contact)
88 LEFT JOIN '.MAIN_DB_PREFIX.'actioncomm_cdav AS ac ON (a.id = ac.fk_object)';
89 } else {
90 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe AS s ON (s.rowid = a.fk_soc)
91 LEFT JOIN '.MAIN_DB_PREFIX.'socpeople AS sp ON (sp.rowid = a.fk_contact)
92 LEFT JOIN '.MAIN_DB_PREFIX.'actioncomm_cdav AS ac ON (a.id = ac.fk_object)';
93 }
94
95 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as co ON co.rowid = sp.fk_pays
96 LEFT JOIN '.MAIN_DB_PREFIX.'c_country as cos ON cos.rowid = s.fk_pays
97 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).')
98 AND a.code IN (SELECT cac.code FROM '.MAIN_DB_PREFIX.'c_actioncomm cac WHERE cac.type<>\'systemauto\')
99 AND a.entity IN ('.getEntity('societe', 1).')';
100 if ($oid !== false) {
101 if ($ouri === false) {
102 $sql .= ' AND a.id = '.intval($oid);
103 } else {
104 $sql .= ' AND (a.id = '.intval($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:29
getSqlCalEvents($calid, $oid=false, $ouri=false)
Base sql request for calendar events.
Definition dav.class.php:58
__construct($user, $db, $langs)
Constructor.
Definition dav.class.php:43
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
$conf db user
Definition repair.php:125