dav.class.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/dav/dav.class.php
  19. * \ingroup dav
  20. * \brief Server DAV
  21. */
  22. /**
  23. * Define Common function to access calendar items and format it in vCalendar
  24. */
  25. class CdavLib
  26. {
  27. private $db;
  28. private $user;
  29. private $langs;
  30. /**
  31. * Constructor
  32. *
  33. * @param User $user user
  34. * @param DoliDB $db Database handler
  35. * @param Translate $langs translation
  36. */
  37. function __construct($user, $db, $langs)
  38. {
  39. $this->user = $user;
  40. $this->db = $db;
  41. $this->langs = $langs;
  42. }
  43. /**
  44. * Base sql request for calendar events
  45. *
  46. * @param int $calid Calendard id
  47. * @param int|boolean $oid Oid
  48. * @param int|boolean $ouri Ouri
  49. * @return string
  50. */
  51. public function getSqlCalEvents($calid, $oid=false, $ouri=false)
  52. {
  53. // TODO : replace GROUP_CONCAT by
  54. $sql = 'SELECT
  55. a.tms AS lastupd,
  56. a.*,
  57. sp.firstname,
  58. sp.lastname,
  59. sp.address,
  60. sp.zip,
  61. sp.town,
  62. co.label country_label,
  63. sp.phone,
  64. sp.phone_perso,
  65. sp.phone_mobile,
  66. s.nom AS soc_nom,
  67. s.address soc_address,
  68. s.zip soc_zip,
  69. s.town soc_town,
  70. cos.label soc_country_label,
  71. s.phone soc_phone,
  72. ac.sourceuid,
  73. (SELECT GROUP_CONCAT(u.login) FROM '.MAIN_DB_PREFIX.'actioncomm_resources ar
  74. LEFT OUTER JOIN '.MAIN_DB_PREFIX.'user AS u ON (u.rowid=fk_element)
  75. WHERE ar.element_type=\'user\' AND fk_actioncomm=a.id) AS other_users
  76. FROM '.MAIN_DB_PREFIX.'actioncomm AS a';
  77. if (! $this->user->rights->societe->client->voir )//FIXME si 'voir' on voit plus de chose ?
  78. {
  79. $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.')
  80. LEFT JOIN '.MAIN_DB_PREFIX.'societe AS s ON (s.rowid = sc.fk_soc)
  81. LEFT JOIN '.MAIN_DB_PREFIX.'socpeople AS sp ON (sp.fk_soc = sc.fk_soc AND sp.rowid = a.fk_contact)
  82. LEFT JOIN '.MAIN_DB_PREFIX.'actioncomm_cdav AS ac ON (a.id = ac.fk_object)';
  83. }
  84. else
  85. {
  86. $sql.=' LEFT JOIN '.MAIN_DB_PREFIX.'societe AS s ON (s.rowid = a.fk_soc)
  87. LEFT JOIN '.MAIN_DB_PREFIX.'socpeople AS sp ON (sp.rowid = a.fk_contact)
  88. LEFT JOIN '.MAIN_DB_PREFIX.'actioncomm_cdav AS ac ON (a.id = ac.fk_object)';
  89. }
  90. $sql.=' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as co ON co.rowid = sp.fk_pays
  91. LEFT JOIN '.MAIN_DB_PREFIX.'c_country as cos ON cos.rowid = s.fk_pays
  92. 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).')
  93. AND a.code IN (SELECT cac.code FROM '.MAIN_DB_PREFIX.'c_actioncomm cac WHERE cac.type<>\'systemauto\')
  94. AND a.entity IN ('.getEntity('societe', 1).')';
  95. if($oid!==false) {
  96. if($ouri===false)
  97. {
  98. $sql.=' AND a.id = '.intval($oid);
  99. }
  100. else
  101. {
  102. $sql.=' AND (a.id = '.intval($oid).' OR ac.uuidext = \''.$this->db->escape($ouri).'\')';
  103. }
  104. }
  105. return $sql;
  106. }
  107. /**
  108. * Convert calendar row to VCalendar string
  109. *
  110. * @param int $calid Calendar id
  111. * @param Object $obj Object id
  112. * @return string
  113. */
  114. public function toVCalendar($calid, $obj)
  115. {
  116. /*$categ = array();
  117. if($obj->soc_client)
  118. {
  119. $nick[] = $obj->soc_code_client;
  120. $categ[] = $this->langs->transnoentitiesnoconv('Customer');
  121. }*/
  122. $location=$obj->location;
  123. // contact address
  124. if(empty($location) && !empty($obj->address))
  125. {
  126. $location = trim(str_replace(array("\r","\t","\n"),' ', $obj->address));
  127. $location = trim($location.', '.$obj->zip);
  128. $location = trim($location.' '.$obj->town);
  129. $location = trim($location.', '.$obj->country_label);
  130. }
  131. // contact address
  132. if(empty($location) && !empty($obj->soc_address))
  133. {
  134. $location = trim(str_replace(array("\r","\t","\n"),' ', $obj->soc_address));
  135. $location = trim($location.', '.$obj->soc_zip);
  136. $location = trim($location.' '.$obj->soc_town);
  137. $location = trim($location.', '.$obj->soc_country_label);
  138. }
  139. $address=explode("\n",$obj->address,2);
  140. foreach($address as $kAddr => $vAddr)
  141. {
  142. $address[$kAddr] = trim(str_replace(array("\r","\t"),' ', str_replace("\n",' | ', trim($vAddr))));
  143. }
  144. $address[]='';
  145. $address[]='';
  146. if($obj->percent==-1 && trim($obj->datep)!='')
  147. $type='VEVENT';
  148. else
  149. $type='VTODO';
  150. $timezone = date_default_timezone_get();
  151. $caldata ="BEGIN:VCALENDAR\n";
  152. $caldata.="VERSION:2.0\n";
  153. $caldata.="METHOD:PUBLISH\n";
  154. $caldata.="PRODID:-//Dolibarr CDav//FR\n";
  155. $caldata.="BEGIN:".$type."\n";
  156. $caldata.="CREATED:".gmdate('Ymd\THis', strtotime($obj->datec))."Z\n";
  157. $caldata.="LAST-MODIFIED:".gmdate('Ymd\THis', strtotime($obj->lastupd))."Z\n";
  158. $caldata.="DTSTAMP:".gmdate('Ymd\THis', strtotime($obj->lastupd))."Z\n";
  159. if($obj->sourceuid=='')
  160. $caldata.="UID:".$obj->id.'-ev-'.$calid.'-cal-'.CDAV_URI_KEY."\n";
  161. else
  162. $caldata.="UID:".$obj->sourceuid."\n";
  163. $caldata.="SUMMARY:".$obj->label."\n";
  164. $caldata.="LOCATION:".$location."\n";
  165. $caldata.="PRIORITY:".$obj->priority."\n";
  166. if($obj->fulldayevent)
  167. {
  168. $caldata.="DTSTART;VALUE=DATE:".date('Ymd', strtotime($obj->datep))."\n";
  169. if($type=='VEVENT')
  170. {
  171. if(trim($obj->datep2)!='')
  172. $caldata.="DTEND;VALUE=DATE:".date('Ymd', strtotime($obj->datep2)+1)."\n";
  173. else
  174. $caldata.="DTEND;VALUE=DATE:".date('Ymd', strtotime($obj->datep)+(25*3600))."\n";
  175. }
  176. elseif(trim($obj->datep2)!='')
  177. $caldata.="DUE;VALUE=DATE:".date('Ymd', strtotime($obj->datep2)+1)."\n";
  178. }
  179. else
  180. {
  181. $caldata.="DTSTART;TZID=".$timezone.":".strtr($obj->datep,array(" "=>"T", ":"=>"", "-"=>""))."\n";
  182. if($type=='VEVENT')
  183. {
  184. if(trim($obj->datep2)!='')
  185. $caldata.="DTEND;TZID=".$timezone.":".strtr($obj->datep2,array(" "=>"T", ":"=>"", "-"=>""))."\n";
  186. else
  187. $caldata.="DTEND;TZID=".$timezone.":".strtr($obj->datep,array(" "=>"T", ":"=>"", "-"=>""))."\n";
  188. }
  189. elseif(trim($obj->datep2)!='')
  190. $caldata.="DUE;TZID=".$timezone.":".strtr($obj->datep2,array(" "=>"T", ":"=>"", "-"=>""))."\n";
  191. }
  192. $caldata.="CLASS:PUBLIC\n";
  193. if($obj->transparency==1)
  194. $caldata.="TRANSP:TRANSPARENT\n";
  195. else
  196. $caldata.="TRANSP:OPAQUE\n";
  197. if($type=='VEVENT')
  198. $caldata.="STATUS:CONFIRMED\n";
  199. elseif($obj->percent==0)
  200. $caldata.="STATUS:NEEDS-ACTION\n";
  201. elseif($obj->percent==100)
  202. $caldata.="STATUS:COMPLETED\n";
  203. else
  204. {
  205. $caldata.="STATUS:IN-PROCESS\n";
  206. $caldata.="PERCENT-COMPLETE:".$obj->percent."\n";
  207. }
  208. $caldata.="DESCRIPTION:";
  209. $caldata.=strtr($obj->note, array("\n"=>"\\n", "\r"=>""));
  210. if(!empty($obj->soc_nom))
  211. $caldata.="\\n*DOLIBARR-SOC: ".$obj->soc_nom;
  212. if(!empty($obj->soc_phone))
  213. $caldata.="\\n*DOLIBARR-SOC-TEL: ".$obj->soc_phone;
  214. if(!empty($obj->firstname) || !empty($obj->lastname))
  215. $caldata.="\\n*DOLIBARR-CTC: ".trim($obj->firstname.' '.$obj->lastname);
  216. if(!empty($obj->phone) || !empty($obj->phone_perso) || !empty($obj->phone_mobile))
  217. $caldata.="\\n*DOLIBARR-CTC-TEL: ".trim($obj->phone.' '.$obj->phone_perso.' '.$obj->phone_mobile);
  218. if(strpos($obj->other_users,',')) // several
  219. $caldata.="\\n*DOLIBARR-USR: ".$obj->other_users;
  220. $caldata.="\n";
  221. $caldata.="END:".$type."\n";
  222. $caldata.="END:VCALENDAR\n";
  223. return $caldata;
  224. }
  225. /**
  226. * getFullCalendarObjects
  227. *
  228. * @param int $calendarId Calendar id
  229. * @param int $bCalendarData Add calendar data
  230. * @return array|string[][]
  231. */
  232. public function getFullCalendarObjects($calendarId, $bCalendarData)
  233. {
  234. $calid = ($calendarId*1);
  235. $calevents = array();
  236. if(! $this->user->rights->agenda->myactions->read)
  237. return $calevents;
  238. if($calid!=$this->user->id && (!isset($this->user->rights->agenda->allactions->read) || !$this->user->rights->agenda->allactions->read))
  239. return $calevents;
  240. $sql = $this->getSqlCalEvents($calid);
  241. $result = $this->db->query($sql);
  242. if ($result)
  243. {
  244. while ($obj = $this->db->fetch_object($result))
  245. {
  246. $calendardata = $this->toVCalendar($calid, $obj);
  247. if($bCalendarData)
  248. {
  249. $calevents[] = array(
  250. 'calendardata' => $calendardata,
  251. 'uri' => $obj->id.'-ev-'.CDAV_URI_KEY,
  252. 'lastmodified' => strtotime($obj->lastupd),
  253. 'etag' => '"'.md5($calendardata).'"',
  254. 'calendarid' => $calendarId,
  255. 'size' => strlen($calendardata),
  256. 'component' => strpos($calendardata, 'BEGIN:VEVENT')>0 ? 'vevent' : 'vtodo',
  257. );
  258. }
  259. else
  260. {
  261. $calevents[] = array(
  262. // 'calendardata' => $calendardata, not necessary because etag+size are present
  263. 'uri' => $obj->id.'-ev-'.CDAV_URI_KEY,
  264. 'lastmodified' => strtotime($obj->lastupd),
  265. 'etag' => '"'.md5($calendardata).'"',
  266. 'calendarid' => $calendarId,
  267. 'size' => strlen($calendardata),
  268. 'component' => strpos($calendardata, 'BEGIN:VEVENT')>0 ? 'vevent' : 'vtodo',
  269. );
  270. }
  271. }
  272. }
  273. return $calevents;
  274. }
  275. }