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