dolibarr  19.0.0-dev
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  /*
147  $address = explode("\n", $obj->address, 2);
148  foreach ($address as $kAddr => $vAddr) {
149  $address[$kAddr] = trim(str_replace(array("\r", "\t"), ' ', str_replace("\n", ' | ', trim($vAddr))));
150  }
151  $address[] = '';
152  $address[] = '';
153  */
154 
155  if ($obj->percent == -1 && trim($obj->datep) != '') {
156  $type = 'VEVENT';
157  } else {
158  $type = 'VTODO';
159  }
160 
161  $timezone = date_default_timezone_get();
162 
163  $caldata = "BEGIN:VCALENDAR\n";
164  $caldata .= "VERSION:2.0\n";
165  $caldata .= "METHOD:PUBLISH\n";
166  $caldata .= "PRODID:-//Dolibarr CDav//FR\n";
167  $caldata .= "BEGIN:".$type."\n";
168  $caldata .= "CREATED:".gmdate('Ymd\THis', strtotime($obj->datec))."Z\n";
169  $caldata .= "LAST-MODIFIED:".gmdate('Ymd\THis', strtotime($obj->lastupd))."Z\n";
170  $caldata .= "DTSTAMP:".gmdate('Ymd\THis', strtotime($obj->lastupd))."Z\n";
171  if ($obj->sourceuid == '') {
172  $caldata .= "UID:".$obj->id.'-ev-'.$calid.'-cal-'.constant('CDAV_URI_KEY')."\n";
173  } else {
174  $caldata .= "UID:".$obj->sourceuid."\n";
175  }
176  $caldata .= "SUMMARY:".$obj->label."\n";
177  $caldata .= "LOCATION:".$location."\n";
178  $caldata .= "PRIORITY:".$obj->priority."\n";
179  if ($obj->fulldayevent) {
180  $caldata .= "DTSTART;VALUE=DATE:".date('Ymd', strtotime($obj->datep))."\n";
181  if ($type == 'VEVENT') {
182  if (trim($obj->datep2) != '') {
183  $caldata .= "DTEND;VALUE=DATE:".date('Ymd', strtotime($obj->datep2) + 1)."\n";
184  } else {
185  $caldata .= "DTEND;VALUE=DATE:".date('Ymd', strtotime($obj->datep) + (25 * 3600))."\n";
186  }
187  } elseif (trim($obj->datep2) != '') {
188  $caldata .= "DUE;VALUE=DATE:".date('Ymd', strtotime($obj->datep2) + 1)."\n";
189  }
190  } else {
191  $caldata .= "DTSTART;TZID=".$timezone.":".strtr($obj->datep, array(" "=>"T", ":"=>"", "-"=>""))."\n";
192  if ($type == 'VEVENT') {
193  if (trim($obj->datep2) != '') {
194  $caldata .= "DTEND;TZID=".$timezone.":".strtr($obj->datep2, array(" "=>"T", ":"=>"", "-"=>""))."\n";
195  } else {
196  $caldata .= "DTEND;TZID=".$timezone.":".strtr($obj->datep, array(" "=>"T", ":"=>"", "-"=>""))."\n";
197  }
198  } elseif (trim($obj->datep2) != '') {
199  $caldata .= "DUE;TZID=".$timezone.":".strtr($obj->datep2, array(" "=>"T", ":"=>"", "-"=>""))."\n";
200  }
201  }
202  $caldata .= "CLASS:PUBLIC\n";
203  if ($obj->transparency == 1) {
204  $caldata .= "TRANSP:TRANSPARENT\n";
205  } else {
206  $caldata .= "TRANSP:OPAQUE\n";
207  }
208 
209  if ($type == 'VEVENT') {
210  $caldata .= "STATUS:CONFIRMED\n";
211  } elseif ($obj->percent == 0) {
212  $caldata .= "STATUS:NEEDS-ACTION\n";
213  } elseif ($obj->percent == 100) {
214  $caldata .= "STATUS:COMPLETED\n";
215  } else {
216  $caldata .= "STATUS:IN-PROCESS\n";
217  $caldata .= "PERCENT-COMPLETE:".$obj->percent."\n";
218  }
219 
220  $caldata .= "DESCRIPTION:";
221  $caldata .= strtr($obj->note, array("\n"=>"\\n", "\r"=>""));
222  if (!empty($obj->soc_nom)) {
223  $caldata .= "\\n*DOLIBARR-SOC: ".$obj->soc_nom;
224  }
225  if (!empty($obj->soc_phone)) {
226  $caldata .= "\\n*DOLIBARR-SOC-TEL: ".$obj->soc_phone;
227  }
228  if (!empty($obj->firstname) || !empty($obj->lastname)) {
229  $caldata .= "\\n*DOLIBARR-CTC: ".trim($obj->firstname.' '.$obj->lastname);
230  }
231  if (!empty($obj->phone) || !empty($obj->phone_perso) || !empty($obj->phone_mobile)) {
232  $caldata .= "\\n*DOLIBARR-CTC-TEL: ".trim($obj->phone.' '.$obj->phone_perso.' '.$obj->phone_mobile);
233  }
234  if (strpos($obj->other_users, ',')) { // several
235  $caldata .= "\\n*DOLIBARR-USR: ".$obj->other_users;
236  }
237  $caldata .= "\n";
238 
239  $caldata .= "END:".$type."\n";
240  $caldata .= "END:VCALENDAR\n";
241 
242  return $caldata;
243  }
244 
252  public function getFullCalendarObjects($calendarId, $bCalendarData)
253  {
254  $calid = (int) $calendarId;
255  $calevents = array();
256 
257  if (!$this->user->rights->agenda->myactions->read) {
258  return $calevents;
259  }
260 
261  if ($calid != $this->user->id && (!isset($this->user->rights->agenda->allactions->read) || !$this->user->rights->agenda->allactions->read)) {
262  return $calevents;
263  }
264 
265  $sql = $this->getSqlCalEvents($calid);
266 
267  $result = $this->db->query($sql);
268 
269  if ($result) {
270  while ($obj = $this->db->fetch_object($result)) {
271  $calendardata = $this->toVCalendar($calid, $obj);
272 
273  if ($bCalendarData) {
274  $calevents[] = array(
275  'calendardata' => $calendardata,
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  } else {
284  $calevents[] = array(
285  // 'calendardata' => $calendardata, not necessary because etag+size are present
286  'uri' => $obj->id.'-ev-'.constant('CDAV_URI_KEY'),
287  'lastmodified' => strtotime($obj->lastupd),
288  'etag' => '"'.md5($calendardata).'"',
289  'calendarid' => $calendarId,
290  'size' => strlen($calendardata),
291  'component' => strpos($calendardata, 'BEGIN:VEVENT') > 0 ? 'vevent' : 'vtodo',
292  );
293  }
294  }
295  }
296  return $calevents;
297  }
298 }
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
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
$conf db user
Definition: repair.php:124