dolibarr  17.0.4
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 
28 class CdavLib
29 {
30 
31  private $db;
32 
33  private $user;
34 
35  private $langs;
36 
44  public function __construct($user, $db, $langs)
45  {
46  $this->user = $user;
47  $this->db = $db;
48  $this->langs = $langs;
49  }
50 
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  $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).')
87  LEFT JOIN '.MAIN_DB_PREFIX.'societe AS s ON (s.rowid = sc.fk_soc)
88  LEFT JOIN '.MAIN_DB_PREFIX.'socpeople AS sp ON (sp.fk_soc = sc.fk_soc AND sp.rowid = a.fk_contact)
89  LEFT JOIN '.MAIN_DB_PREFIX.'actioncomm_cdav AS ac ON (a.id = ac.fk_object)';
90  } else {
91  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe AS s ON (s.rowid = a.fk_soc)
92  LEFT JOIN '.MAIN_DB_PREFIX.'socpeople AS sp ON (sp.rowid = a.fk_contact)
93  LEFT JOIN '.MAIN_DB_PREFIX.'actioncomm_cdav AS ac ON (a.id = ac.fk_object)';
94  }
95 
96  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as co ON co.rowid = sp.fk_pays
97  LEFT JOIN '.MAIN_DB_PREFIX.'c_country as cos ON cos.rowid = s.fk_pays
98  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).')
99  AND a.code IN (SELECT cac.code FROM '.MAIN_DB_PREFIX.'c_actioncomm cac WHERE cac.type<>\'systemauto\')
100  AND a.entity IN ('.getEntity('societe', 1).')';
101  if ($oid !== false) {
102  if ($ouri === false) {
103  $sql .= ' AND a.id = '.intval($oid);
104  } else {
105  $sql .= ' AND (a.id = '.intval($oid).' OR ac.uuidext = \''.$this->db->escape($ouri).'\')';
106  }
107  }
108 
109  return $sql;
110  }
111 
119  public function toVCalendar($calid, $obj)
120  {
121  /*$categ = array();
122  if($obj->soc_client)
123  {
124  $nick[] = $obj->soc_code_client;
125  $categ[] = $this->langs->transnoentitiesnoconv('Customer');
126  }*/
127 
128  $location = $obj->location;
129 
130  // contact address
131  if (empty($location) && !empty($obj->address)) {
132  $location = trim(str_replace(array("\r", "\t", "\n"), ' ', $obj->address));
133  $location = trim($location.', '.$obj->zip);
134  $location = trim($location.' '.$obj->town);
135  $location = trim($location.', '.$obj->country_label);
136  }
137 
138  // contact address
139  if (empty($location) && !empty($obj->soc_address)) {
140  $location = trim(str_replace(array("\r", "\t", "\n"), ' ', $obj->soc_address));
141  $location = trim($location.', '.$obj->soc_zip);
142  $location = trim($location.' '.$obj->soc_town);
143  $location = trim($location.', '.$obj->soc_country_label);
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  if ($obj->percent == -1 && trim($obj->datep) != '') {
154  $type = 'VEVENT';
155  } else {
156  $type = 'VTODO';
157  }
158 
159  $timezone = date_default_timezone_get();
160 
161  $caldata = "BEGIN:VCALENDAR\n";
162  $caldata .= "VERSION:2.0\n";
163  $caldata .= "METHOD:PUBLISH\n";
164  $caldata .= "PRODID:-//Dolibarr CDav//FR\n";
165  $caldata .= "BEGIN:".$type."\n";
166  $caldata .= "CREATED:".gmdate('Ymd\THis', strtotime($obj->datec))."Z\n";
167  $caldata .= "LAST-MODIFIED:".gmdate('Ymd\THis', strtotime($obj->lastupd))."Z\n";
168  $caldata .= "DTSTAMP:".gmdate('Ymd\THis', strtotime($obj->lastupd))."Z\n";
169  if ($obj->sourceuid == '') {
170  $caldata .= "UID:".$obj->id.'-ev-'.$calid.'-cal-'.constant('CDAV_URI_KEY')."\n";
171  } else {
172  $caldata .= "UID:".$obj->sourceuid."\n";
173  }
174  $caldata .= "SUMMARY:".$obj->label."\n";
175  $caldata .= "LOCATION:".$location."\n";
176  $caldata .= "PRIORITY:".$obj->priority."\n";
177  if ($obj->fulldayevent) {
178  $caldata .= "DTSTART;VALUE=DATE:".date('Ymd', strtotime($obj->datep))."\n";
179  if ($type == 'VEVENT') {
180  if (trim($obj->datep2) != '') {
181  $caldata .= "DTEND;VALUE=DATE:".date('Ymd', strtotime($obj->datep2) + 1)."\n";
182  } else {
183  $caldata .= "DTEND;VALUE=DATE:".date('Ymd', strtotime($obj->datep) + (25 * 3600))."\n";
184  }
185  } elseif (trim($obj->datep2) != '') {
186  $caldata .= "DUE;VALUE=DATE:".date('Ymd', strtotime($obj->datep2) + 1)."\n";
187  }
188  } else {
189  $caldata .= "DTSTART;TZID=".$timezone.":".strtr($obj->datep, array(" "=>"T", ":"=>"", "-"=>""))."\n";
190  if ($type == 'VEVENT') {
191  if (trim($obj->datep2) != '') {
192  $caldata .= "DTEND;TZID=".$timezone.":".strtr($obj->datep2, array(" "=>"T", ":"=>"", "-"=>""))."\n";
193  } else {
194  $caldata .= "DTEND;TZID=".$timezone.":".strtr($obj->datep, array(" "=>"T", ":"=>"", "-"=>""))."\n";
195  }
196  } elseif (trim($obj->datep2) != '') {
197  $caldata .= "DUE;TZID=".$timezone.":".strtr($obj->datep2, array(" "=>"T", ":"=>"", "-"=>""))."\n";
198  }
199  }
200  $caldata .= "CLASS:PUBLIC\n";
201  if ($obj->transparency == 1) {
202  $caldata .= "TRANSP:TRANSPARENT\n";
203  } else {
204  $caldata .= "TRANSP:OPAQUE\n";
205  }
206 
207  if ($type == 'VEVENT') {
208  $caldata .= "STATUS:CONFIRMED\n";
209  } elseif ($obj->percent == 0) {
210  $caldata .= "STATUS:NEEDS-ACTION\n";
211  } elseif ($obj->percent == 100) {
212  $caldata .= "STATUS:COMPLETED\n";
213  } else {
214  $caldata .= "STATUS:IN-PROCESS\n";
215  $caldata .= "PERCENT-COMPLETE:".$obj->percent."\n";
216  }
217 
218  $caldata .= "DESCRIPTION:";
219  $caldata .= strtr($obj->note, array("\n"=>"\\n", "\r"=>""));
220  if (!empty($obj->soc_nom)) {
221  $caldata .= "\\n*DOLIBARR-SOC: ".$obj->soc_nom;
222  }
223  if (!empty($obj->soc_phone)) {
224  $caldata .= "\\n*DOLIBARR-SOC-TEL: ".$obj->soc_phone;
225  }
226  if (!empty($obj->firstname) || !empty($obj->lastname)) {
227  $caldata .= "\\n*DOLIBARR-CTC: ".trim($obj->firstname.' '.$obj->lastname);
228  }
229  if (!empty($obj->phone) || !empty($obj->phone_perso) || !empty($obj->phone_mobile)) {
230  $caldata .= "\\n*DOLIBARR-CTC-TEL: ".trim($obj->phone.' '.$obj->phone_perso.' '.$obj->phone_mobile);
231  }
232  if (strpos($obj->other_users, ',')) { // several
233  $caldata .= "\\n*DOLIBARR-USR: ".$obj->other_users;
234  }
235  $caldata .= "\n";
236 
237  $caldata .= "END:".$type."\n";
238  $caldata .= "END:VCALENDAR\n";
239 
240  return $caldata;
241  }
242 
250  public function getFullCalendarObjects($calendarId, $bCalendarData)
251  {
252  $calid = ($calendarId * 1);
253  $calevents = array();
254 
255  if (!$this->user->rights->agenda->myactions->read) {
256  return $calevents;
257  }
258 
259  if ($calid != $this->user->id && (!isset($this->user->rights->agenda->allactions->read) || !$this->user->rights->agenda->allactions->read)) {
260  return $calevents;
261  }
262 
263  $sql = $this->getSqlCalEvents($calid);
264 
265  $result = $this->db->query($sql);
266 
267  if ($result) {
268  while ($obj = $this->db->fetch_object($result)) {
269  $calendardata = $this->toVCalendar($calid, $obj);
270 
271  if ($bCalendarData) {
272  $calevents[] = array(
273  'calendardata' => $calendardata,
274  'uri' => $obj->id.'-ev-'.constant('CDAV_URI_KEY'),
275  'lastmodified' => strtotime($obj->lastupd),
276  'etag' => '"'.md5($calendardata).'"',
277  'calendarid' => $calendarId,
278  'size' => strlen($calendardata),
279  'component' => strpos($calendardata, 'BEGIN:VEVENT') > 0 ? 'vevent' : 'vtodo',
280  );
281  } else {
282  $calevents[] = array(
283  // 'calendardata' => $calendardata, not necessary because etag+size are present
284  'uri' => $obj->id.'-ev-'.constant('CDAV_URI_KEY'),
285  'lastmodified' => strtotime($obj->lastupd),
286  'etag' => '"'.md5($calendardata).'"',
287  'calendarid' => $calendarId,
288  'size' => strlen($calendardata),
289  'component' => strpos($calendardata, 'BEGIN:VEVENT') > 0 ? 'vevent' : 'vtodo',
290  );
291  }
292  }
293  }
294  return $calevents;
295  }
296 }
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:59
__construct($user, $db, $langs)
Constructor.
Definition: dav.class.php:44
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
$conf db user
Definition: repair.php:123
$conf db
API class for accounts.
Definition: inc.php:41