PageRenderTime 47ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/comm/action/class/actioncomm.class.php

https://github.com/zeert/dolibarr
PHP | 995 lines | 733 code | 101 blank | 161 comment | 223 complexity | 77966c8622d3c1312c0cc9b952c79d58 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis@dolibarr.fr>
  5. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/comm/action/class/actioncomm.class.php
  22. * \ingroup commercial
  23. * \brief File of class to manage agenda events (actions)
  24. */
  25. require_once(DOL_DOCUMENT_ROOT.'/comm/action/class/cactioncomm.class.php');
  26. require_once(DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php');
  27. /** \class ActionComm
  28. * \brief Class to manage agenda events (actions)
  29. */
  30. class ActionComm extends CommonObject
  31. {
  32. public $element='action';
  33. public $table_element = 'actioncomm';
  34. public $table_rowid = 'id';
  35. protected $ismultientitymanaged = 2; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  36. var $id;
  37. var $type_id;
  38. var $type_code;
  39. var $type;
  40. var $label;
  41. var $date;
  42. var $datec; // Date creation record (datec)
  43. var $datem; // Date modification record (tms)
  44. var $author; // Object user that create action
  45. var $usermod; // Object user that modified action
  46. var $datep; // Date action start (datep)
  47. var $datef; // Date action end (datep2)
  48. var $durationp = -1; // -1=Unkown duration
  49. var $fulldayevent = 0; // 1=Event on full day
  50. var $punctual = 1; // Milestone
  51. var $percentage; // Percentage
  52. var $location; // Location
  53. var $priority; // Free text ('' By default)
  54. var $note; // Description
  55. var $usertodo; // Object user that must do action
  56. var $userdone; // Object user that did action
  57. var $societe; // Company linked to action (optionnal)
  58. var $contact; // Contact linked tot action (optionnal)
  59. var $fk_project; // Id of project (optionnal)
  60. // Properties for links to other objects
  61. var $fk_element; // Id of record
  62. var $elementtype; // Type of record. This if property ->element of object linked to.
  63. // Ical
  64. var $icalname;
  65. var $icalcolor;
  66. var $actions=array();
  67. /**
  68. * Constructor
  69. *
  70. * @param DoliDB $db Database handler
  71. */
  72. function __construct($db)
  73. {
  74. $this->db = $db;
  75. $this->author = (object) array();
  76. $this->usermod = (object) array();
  77. $this->usertodo = (object) array();
  78. $this->userdone = (object) array();
  79. $this->societe = (object) array();
  80. $this->contact = (object) array();
  81. }
  82. /**
  83. * Add an action/event into database
  84. *
  85. * @param User $user Object user making action
  86. * @param int $notrigger 1 = disable triggers, 0 = enable triggers
  87. * @return int Id of created event, < 0 if KO
  88. */
  89. function add($user,$notrigger=0)
  90. {
  91. global $langs,$conf;
  92. $error=0;
  93. $now=dol_now();
  94. // Clean parameters
  95. $this->label=dol_trunc(trim($this->label),128);
  96. $this->location=dol_trunc(trim($this->location),128);
  97. $this->note=dol_htmlcleanlastbr(trim($this->note));
  98. if (empty($this->percentage)) $this->percentage = 0;
  99. if (empty($this->priority)) $this->priority = 0;
  100. if (empty($this->fulldayevent)) $this->fuldayevent = 0;
  101. if (empty($this->punctual)) $this->punctual = 0;
  102. if ($this->percentage > 100) $this->percentage = 100;
  103. if ($this->percentage == 100 && ! $this->dateend) $this->dateend = $this->date;
  104. if ($this->datep && $this->datef) $this->durationp=($this->datef - $this->datep);
  105. if ($this->date && $this->dateend) $this->durationa=($this->dateend - $this->date);
  106. if ($this->datep && $this->datef && $this->datep > $this->datef) $this->datef=$this->datep;
  107. if ($this->date && $this->dateend && $this->date > $this->dateend) $this->dateend=$this->date;
  108. if ($this->fk_project < 0) $this->fk_project = 0;
  109. if ($this->elementtype=='facture') $this->elementtype='invoice';
  110. if ($this->elementtype=='commande') $this->elementtype='order';
  111. if ($this->elementtype=='contrat') $this->elementtype='contract';
  112. if (! $this->type_id && $this->type_code)
  113. {
  114. // Get id from code
  115. $cactioncomm=new CActionComm($this->db);
  116. $result=$cactioncomm->fetch($this->type_code);
  117. if ($result > 0)
  118. {
  119. $this->type_id=$cactioncomm->id;
  120. }
  121. else if ($result == 0)
  122. {
  123. $this->error='Failed to get record with code '.$this->type_code.' from dictionnary "type of events"';
  124. return -1;
  125. }
  126. else
  127. {
  128. $this->error=$cactioncomm->error;
  129. return -1;
  130. }
  131. }
  132. // Check parameters
  133. if (! $this->type_id)
  134. {
  135. $this->error="ErrorWrongParameters";
  136. return -1;
  137. }
  138. $this->db->begin();
  139. $sql = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm";
  140. $sql.= "(datec,";
  141. $sql.= "datep,";
  142. $sql.= "datep2,";
  143. //$sql.= "datea,";
  144. //$sql.= "datea2,";
  145. $sql.= "durationp,";
  146. //$sql.= "durationa,";
  147. $sql.= "fk_action,";
  148. $sql.= "fk_soc,";
  149. $sql.= "fk_project,";
  150. $sql.= "note,";
  151. $sql.= "fk_contact,";
  152. $sql.= "fk_user_author,";
  153. $sql.= "fk_user_action,";
  154. $sql.= "fk_user_done,";
  155. $sql.= "label,percent,priority,fulldayevent,location,punctual,";
  156. $sql.= "fk_element,";
  157. $sql.= "elementtype,";
  158. $sql.= "entity";
  159. $sql.= ") VALUES (";
  160. $sql.= "'".$this->db->idate($now)."',";
  161. $sql.= (strval($this->datep)!=''?"'".$this->db->idate($this->datep)."'":"null").",";
  162. $sql.= (strval($this->datef)!=''?"'".$this->db->idate($this->datef)."'":"null").",";
  163. //$sql.= (strval($this->date)!=''?"'".$this->db->idate($this->date)."'":"null").",";
  164. //$sql.= (strval($this->dateend)!=''?"'".$this->db->idate($this->dateend)."'":"null").",";
  165. $sql.= ($this->durationp >= 0 && $this->durationp != ''?"'".$this->durationp."'":"null").",";
  166. //$sql.= ($this->durationa >= 0 && $this->durationa != ''?"'".$this->durationa."'":"null").",";
  167. $sql.= " '".$this->type_id."',";
  168. $sql.= ($this->societe->id>0?" '".$this->societe->id."'":"null").",";
  169. $sql.= ($this->fk_project>0?" '".$this->fk_project."'":"null").",";
  170. $sql.= " '".$this->db->escape($this->note)."',";
  171. $sql.= ($this->contact->id > 0?"'".$this->contact->id."'":"null").",";
  172. $sql.= ($user->id > 0 ? "'".$user->id."'":"null").",";
  173. $sql.= ($this->usertodo->id > 0?"'".$this->usertodo->id."'":"null").",";
  174. $sql.= ($this->userdone->id > 0?"'".$this->userdone->id."'":"null").",";
  175. $sql.= "'".$this->db->escape($this->label)."','".$this->percentage."','".$this->priority."','".$this->fulldayevent."','".$this->db->escape($this->location)."','".$this->punctual."',";
  176. $sql.= ($this->fk_element?$this->fk_element:"null").",";
  177. $sql.= ($this->elementtype?"'".$this->elementtype."'":"null").",";
  178. $sql.= $conf->entity;
  179. $sql.= ")";
  180. dol_syslog(get_class($this)."::add sql=".$sql);
  181. $resql=$this->db->query($sql);
  182. if ($resql)
  183. {
  184. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."actioncomm","id");
  185. if (! $notrigger)
  186. {
  187. // Appel des triggers
  188. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  189. $interface=new Interfaces($this->db);
  190. $result=$interface->run_triggers('ACTION_CREATE',$this,$user,$langs,$conf);
  191. if ($result < 0) {
  192. $error++; $this->errors=$interface->errors;
  193. }
  194. // Fin appel triggers
  195. }
  196. $this->db->commit();
  197. return $this->id;
  198. }
  199. else
  200. {
  201. $this->db->rollback();
  202. $this->error=$this->db->lasterror();
  203. dol_syslog(get_class($this)."::add ".$this->error,LOG_ERR);
  204. return -1;
  205. }
  206. }
  207. /**
  208. * Load object from database
  209. *
  210. * @param int $id Id of action to get
  211. * @return int <0 if KO, >0 if OK
  212. */
  213. function fetch($id)
  214. {
  215. global $langs;
  216. $sql = "SELECT a.id,";
  217. $sql.= " a.id as ref,";
  218. $sql.= " a.ref_ext,";
  219. $sql.= " a.datep,";
  220. $sql.= " a.datep2,";
  221. $sql.= " a.datec,";
  222. $sql.= " a.durationp,";
  223. $sql.= " a.tms as datem,";
  224. $sql.= " a.note, a.label,";
  225. $sql.= " a.fk_soc,";
  226. $sql.= " a.fk_project,";
  227. $sql.= " a.fk_user_author, a.fk_user_mod,";
  228. $sql.= " a.fk_user_action, a.fk_user_done,";
  229. $sql.= " a.fk_contact, a.percent as percentage,";
  230. $sql.= " a.fk_element, a.elementtype,";
  231. $sql.= " a.priority, a.fulldayevent, a.location,";
  232. $sql.= " c.id as type_id, c.code as type_code, c.libelle,";
  233. $sql.= " s.nom as socname,";
  234. $sql.= " u.firstname, u.name as lastname";
  235. $sql.= " FROM (".MAIN_DB_PREFIX."c_actioncomm as c, ".MAIN_DB_PREFIX."actioncomm as a)";
  236. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on u.rowid = a.fk_user_author";
  237. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on s.rowid = a.fk_soc";
  238. $sql.= " WHERE a.id=".$id." AND a.fk_action=c.id";
  239. dol_syslog(get_class($this)."::fetch sql=".$sql);
  240. $resql=$this->db->query($sql);
  241. if ($resql)
  242. {
  243. if ($this->db->num_rows($resql))
  244. {
  245. $obj = $this->db->fetch_object($resql);
  246. $this->id = $obj->id;
  247. $this->ref = $obj->ref;
  248. $this->ref_ext = $obj->ref_ext;
  249. $this->type_id = $obj->type_id;
  250. $this->type_code = $obj->type_code;
  251. $transcode=$langs->trans("Action".$obj->type_code);
  252. $type_libelle=($transcode!="Action".$obj->type_code?$transcode:$obj->libelle);
  253. $this->type = $type_libelle;
  254. $this->label = $obj->label;
  255. $this->datep = $this->db->jdate($obj->datep);
  256. $this->datef = $this->db->jdate($obj->datep2);
  257. $this->datec = $this->db->jdate($obj->datec);
  258. $this->datem = $this->db->jdate($obj->datem);
  259. $this->note = $obj->note;
  260. $this->percentage = $obj->percentage;
  261. $this->author->id = $obj->fk_user_author;
  262. $this->author->firstname = $obj->firstname;
  263. $this->author->lastname = $obj->lastname;
  264. $this->usermod->id = $obj->fk_user_mod;
  265. $this->usertodo->id = $obj->fk_user_action;
  266. $this->userdone->id = $obj->fk_user_done;
  267. $this->priority = $obj->priority;
  268. $this->fulldayevent = $obj->fulldayevent;
  269. $this->location = $obj->location;
  270. $this->socid = $obj->fk_soc; // To have fetch_thirdparty method working
  271. $this->societe->id = $obj->fk_soc;
  272. $this->contact->id = $obj->fk_contact;
  273. $this->fk_project = $obj->fk_project;
  274. $this->fk_element = $obj->fk_element;
  275. $this->elementtype = $obj->elementtype;
  276. }
  277. $this->db->free($resql);
  278. return 1;
  279. }
  280. else
  281. {
  282. $this->error=$this->db->lasterror();
  283. return -1;
  284. }
  285. }
  286. /**
  287. * Delete event from database
  288. *
  289. * @param int $notrigger 1 = disable triggers, 0 = enable triggers
  290. * @return int <0 if KO, >0 if OK
  291. */
  292. function delete($notrigger=0)
  293. {
  294. global $user,$langs,$conf;
  295. $error=0;
  296. $this->db->begin();
  297. $sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm";
  298. $sql.= " WHERE id=".$this->id;
  299. dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG);
  300. if ($this->db->query($sql))
  301. {
  302. if (! $notrigger)
  303. {
  304. // Appel des triggers
  305. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  306. $interface=new Interfaces($this->db);
  307. $result=$interface->run_triggers('ACTION_DELETE',$this,$user,$langs,$conf);
  308. if ($result < 0) {
  309. $error++; $this->errors=$interface->errors;
  310. }
  311. // Fin appel triggers
  312. }
  313. if (! $error)
  314. {
  315. $this->db->commit();
  316. return 1;
  317. }
  318. else
  319. {
  320. $this->db->rollback();
  321. return -2;
  322. }
  323. }
  324. else
  325. {
  326. $this->db->rollback();
  327. $this->error=$this->db->lasterror();
  328. dol_syslog(get_class($this)."::delete ".$this->error,LOG_ERR);
  329. return -1;
  330. }
  331. }
  332. /**
  333. * Update action into database
  334. * If percentage = 100, on met a jour date 100%
  335. *
  336. * @param User $user Object user making change
  337. * @param int $notrigger 1 = disable triggers, 0 = enable triggers
  338. * @return int <0 if KO, >0 if OK
  339. */
  340. function update($user,$notrigger=0)
  341. {
  342. global $langs,$conf;
  343. $error=0;
  344. // Clean parameters
  345. $this->label=trim($this->label);
  346. $this->note=trim($this->note);
  347. if (empty($this->percentage)) $this->percentage = 0;
  348. if (empty($this->priority)) $this->priority = 0;
  349. if (empty($this->fulldayevent)) $this->fulldayevent = 0;
  350. if ($this->percentage > 100) $this->percentage = 100;
  351. if ($this->percentage == 100 && ! $this->dateend) $this->dateend = $this->date;
  352. if ($this->datep && $this->datef) $this->durationp=($this->datef - $this->datep);
  353. if ($this->date && $this->dateend) $this->durationa=($this->dateend - $this->date);
  354. if ($this->datep && $this->datef && $this->datep > $this->datef) $this->datef=$this->datep;
  355. if ($this->date && $this->dateend && $this->date > $this->dateend) $this->dateend=$this->date;
  356. if ($this->fk_project < 0) $this->fk_project = 0;
  357. // Check parameters
  358. if ($this->percentage == 0 && $this->userdone->id > 0)
  359. {
  360. $this->error="ErrorCantSaveADoneUserWithZeroPercentage";
  361. return -1;
  362. }
  363. $this->db->begin();
  364. //print 'eeea'.$this->datep.'-'.(strval($this->datep) != '').'-'.$this->db->idate($this->datep);
  365. $sql = "UPDATE ".MAIN_DB_PREFIX."actioncomm ";
  366. $sql.= " SET percent='".$this->percentage."'";
  367. $sql.= ", label = ".($this->label ? "'".$this->db->escape($this->label)."'":"null");
  368. $sql.= ", datep = ".(strval($this->datep)!='' ? "'".$this->db->idate($this->datep)."'" : 'null');
  369. $sql.= ", datep2 = ".(strval($this->datef)!='' ? "'".$this->db->idate($this->datef)."'" : 'null');
  370. //$sql.= ", datea = ".(strval($this->date)!='' ? "'".$this->db->idate($this->date)."'" : 'null');
  371. //$sql.= ", datea2 = ".(strval($this->dateend)!='' ? "'".$this->db->idate($this->dateend)."'" : 'null');
  372. $sql.= ", note = ".($this->note ? "'".$this->db->escape($this->note)."'":"null");
  373. $sql.= ", fk_soc =". ($this->societe->id > 0 ? "'".$this->societe->id."'":"null");
  374. $sql.= ", fk_project =". ($this->fk_project > 0 ? "'".$this->fk_project."'":"null");
  375. $sql.= ", fk_contact =". ($this->contact->id > 0 ? "'".$this->contact->id."'":"null");
  376. $sql.= ", priority = '".$this->priority."'";
  377. $sql.= ", fulldayevent = '".$this->fulldayevent."'";
  378. $sql.= ", location = ".($this->location ? "'".$this->db->escape($this->location)."'":"null");
  379. $sql.= ", fk_user_mod = '".$user->id."'";
  380. $sql.= ", fk_user_action=".($this->usertodo->id > 0 ? "'".$this->usertodo->id."'":"null");
  381. $sql.= ", fk_user_done=".($this->userdone->id > 0 ? "'".$this->userdone->id."'":"null");
  382. $sql.= " WHERE id=".$this->id;
  383. dol_syslog(get_class($this)."::update sql=".$sql);
  384. if ($this->db->query($sql))
  385. {
  386. if (! $notrigger)
  387. {
  388. // Appel des triggers
  389. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  390. $interface=new Interfaces($this->db);
  391. $result=$interface->run_triggers('ACTION_MODIFY',$this,$user,$langs,$conf);
  392. if ($result < 0) {
  393. $error++; $this->errors=$interface->errors;
  394. }
  395. // Fin appel triggers
  396. }
  397. if (! $error)
  398. {
  399. $this->db->commit();
  400. return 1;
  401. }
  402. else
  403. {
  404. $this->db->rollback();
  405. dol_syslog(get_class($this)."::update ".join(',',$this->errors),LOG_ERR);
  406. return -2;
  407. }
  408. }
  409. else
  410. {
  411. $this->db->rollback();
  412. $this->error=$this->db->lasterror();
  413. dol_syslog(get_class($this)."::update ".$this->error,LOG_ERR);
  414. return -1;
  415. }
  416. }
  417. /**
  418. * Load all objects with filters
  419. *
  420. * @param int $socid Filter by thirdparty
  421. * @param int $fk_element Id of element action is linked to
  422. * @param string $elementtype Type of element action is linked to
  423. * @param string $filter Other filter
  424. * @return int <0 if KO, >0 if OK
  425. */
  426. function getActions($socid=0, $fk_element=0, $elementtype='', $filter='')
  427. {
  428. global $conf, $langs;
  429. $sql = "SELECT a.id";
  430. $sql.= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
  431. $sql.= " WHERE a.entity = ".$conf->entity;
  432. if (! empty($socid)) $sql.= " AND a.fk_soc = ".$socid;
  433. if (! empty($elementtype))
  434. {
  435. if ($elementtype == 'project') $sql.= ' AND a.fk_project = '.$fk_element;
  436. else $sql.= " AND a.fk_element = ".$fk_element." AND a.elementtype = '".$elementtype."'";
  437. }
  438. if (! empty($filter)) $sql.= $filter;
  439. dol_syslog(get_class($this)."::getActions sql=".$sql);
  440. $resql=$this->db->query($sql);
  441. if ($resql)
  442. {
  443. $num = $this->db->num_rows($resql);
  444. if ($num)
  445. {
  446. for($i=0;$i<$num;$i++)
  447. {
  448. $obj = $this->db->fetch_object($resql);
  449. $actioncommstatic = new ActionComm($this->db);
  450. $actioncommstatic->fetch($obj->id);
  451. $this->actions[$i] = $actioncommstatic;
  452. }
  453. }
  454. $this->db->free($resql);
  455. return 1;
  456. }
  457. else
  458. {
  459. $this->error=$this->db->lasterror();
  460. return -1;
  461. }
  462. }
  463. /**
  464. * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
  465. *
  466. * @param User $user Objet user
  467. * @return int <0 if KO, >0 if OK
  468. */
  469. function load_board($user)
  470. {
  471. global $conf, $user;
  472. $now=dol_now();
  473. $this->nbtodo=$this->nbtodolate=0;
  474. $sql = "SELECT a.id, a.datep as dp";
  475. $sql.= " FROM (".MAIN_DB_PREFIX."actioncomm as a";
  476. if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  477. $sql.= ")";
  478. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON a.fk_soc = s.rowid";
  479. $sql.= " WHERE a.percent >= 0 AND a.percent < 100";
  480. $sql.= " AND a.entity = ".$conf->entity;
  481. if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= " AND a.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
  482. if ($user->societe_id) $sql.=" AND a.fk_soc = ".$user->societe_id;
  483. //print $sql;
  484. $resql=$this->db->query($sql);
  485. if ($resql)
  486. {
  487. while ($obj=$this->db->fetch_object($resql))
  488. {
  489. $this->nbtodo++;
  490. if (isset($obj->dp) && $this->db->jdate($obj->dp) < ($now - $conf->actions->warning_delay)) $this->nbtodolate++;
  491. }
  492. return 1;
  493. }
  494. else
  495. {
  496. $this->error=$this->db->error();
  497. return -1;
  498. }
  499. }
  500. /**
  501. * Charge les informations d'ordre info dans l'objet facture
  502. *
  503. * @param int $id Id de la facture a charger
  504. * @return void
  505. */
  506. function info($id)
  507. {
  508. $sql = 'SELECT ';
  509. $sql.= ' a.id,';
  510. $sql.= ' datec,';
  511. $sql.= ' tms as datem,';
  512. $sql.= ' fk_user_author,';
  513. $sql.= ' fk_user_mod';
  514. $sql.= ' FROM '.MAIN_DB_PREFIX.'actioncomm as a';
  515. $sql.= ' WHERE a.id = '.$id;
  516. dol_syslog(get_class($this)."::info sql=".$sql);
  517. $result=$this->db->query($sql);
  518. if ($result)
  519. {
  520. if ($this->db->num_rows($result))
  521. {
  522. $obj = $this->db->fetch_object($result);
  523. $this->id = $obj->id;
  524. if ($obj->fk_user_author)
  525. {
  526. $cuser = new User($this->db);
  527. $cuser->fetch($obj->fk_user_author);
  528. $this->user_creation = $cuser;
  529. }
  530. if ($obj->fk_user_mod)
  531. {
  532. $muser = new User($this->db);
  533. $muser->fetch($obj->fk_user_mod);
  534. $this->user_modification = $muser;
  535. }
  536. $this->date_creation = $this->db->jdate($obj->datec);
  537. $this->date_modification = $this->db->jdate($obj->datem);
  538. }
  539. $this->db->free($result);
  540. }
  541. else
  542. {
  543. dol_print_error($this->db);
  544. }
  545. }
  546. /**
  547. * Return label of status
  548. *
  549. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  550. * @param int $hidenastatus 1=Show nothing if status is "Not applicable"
  551. * @return string String with status
  552. */
  553. function getLibStatut($mode,$hidenastatus=0)
  554. {
  555. return $this->LibStatut($this->percentage,$mode,$hidenastatus);
  556. }
  557. /**
  558. * Return label of action status
  559. *
  560. * @param int $percent Percent
  561. * @param int $mode 0=Long label, 1=Short label, 2=Picto+Short label, 3=Picto, 4=Picto+Short label, 5=Short label+Picto, 6=Very short label+Picto
  562. * @param int $hidenastatus 1=Show nothing if status is "Not applicable"
  563. * @return string Label
  564. */
  565. function LibStatut($percent,$mode,$hidenastatus=0)
  566. {
  567. global $langs;
  568. if ($mode == 0)
  569. {
  570. if ($percent==-1 && ! $hidenastatus) return $langs->trans('StatusNotApplicable');
  571. else if ($percent==0) return $langs->trans('StatusActionToDo').' (0%)';
  572. else if ($percent > 0 && $percent < 100) return $langs->trans('StatusActionInProcess').' ('.$percent.'%)';
  573. else if ($percent >= 100) return $langs->trans('StatusActionDone').' (100%)';
  574. }
  575. else if ($mode == 1)
  576. {
  577. if ($percent==-1 && ! $hidenastatus) return $langs->trans('StatusNotApplicable');
  578. else if ($percent==0) return $langs->trans('StatusActionToDo');
  579. else if ($percent > 0 && $percent < 100) return $percent.'%';
  580. else if ($percent >= 100) return $langs->trans('StatusActionDone');
  581. }
  582. else if ($mode == 2)
  583. {
  584. if ($percent==-1 && ! $hidenastatus) return img_picto($langs->trans('StatusNotApplicable'),'statut9').' '.$langs->trans('StatusNotApplicable');
  585. else if ($percent==0) return img_picto($langs->trans('StatusActionToDo'),'statut1').' '.$langs->trans('StatusActionToDo');
  586. else if ($percent > 0 && $percent < 100) return img_picto($langs->trans('StatusActionInProcess'),'statut3').' '. $percent.'%';
  587. else if ($percent >= 100) return img_picto($langs->trans('StatusActionDone'),'statut6').' '.$langs->trans('StatusActionDone');
  588. }
  589. else if ($mode == 3)
  590. {
  591. if ($percent==-1 && ! $hidenastatus) return img_picto($langs->trans("Status").': '.$langs->trans('StatusNotApplicable'),'statut9');
  592. else if ($percent==0) return img_picto($langs->trans("Status").': '.$langs->trans('StatusActionToDo').' (0%)','statut1');
  593. else if ($percent > 0 && $percent < 100) return img_picto($langs->trans("Status").': '.$langs->trans('StatusActionInProcess').' ('.$percent.'%)','statut3');
  594. else if ($percent >= 100) return img_picto($langs->trans("Status").': '.$langs->trans('StatusActionDone').' (100%)','statut6');
  595. }
  596. else if ($mode == 4)
  597. {
  598. if ($percent==-1 && ! $hidenastatus) return img_picto($langs->trans('StatusNotApplicable'),'statut9').' '.$langs->trans('StatusNotApplicable');
  599. else if ($percent==0) return img_picto($langs->trans('StatusActionToDo'),'statut1').' '.$langs->trans('StatusActionToDo').' (0%)';
  600. else if ($percent > 0 && $percent < 100) return img_picto($langs->trans('StatusActionInProcess'),'statut3').' '.$langs->trans('StatusActionInProcess').' ('.$percent.'%)';
  601. else if ($percent >= 100) return img_picto($langs->trans('StatusActionDone'),'statut6').' '.$langs->trans('StatusActionDone').' (100%)';
  602. }
  603. else if ($mode == 5)
  604. {
  605. if ($percent==-1 && ! $hidenastatus) return img_picto($langs->trans('StatusNotApplicable'),'statut9');
  606. else if ($percent==0) return '0% '.img_picto($langs->trans('StatusActionToDo'),'statut1');
  607. else if ($percent > 0 && $percent < 100) return $percent.'% '.img_picto($langs->trans('StatusActionInProcess').' - '.$percent.'%','statut3');
  608. else if ($percent >= 100) return $langs->trans('StatusActionDone').' '.img_picto($langs->trans('StatusActionDone'),'statut6');
  609. }
  610. else if ($mode == 6)
  611. {
  612. if ($percent==-1 && ! $hidenastatus) return img_picto($langs->trans('StatusNotApplicable'),'statut9');
  613. else if ($percent==0) return '0% '.img_picto($langs->trans('StatusActionToDo'),'statut1');
  614. else if ($percent > 0 && $percent < 100) return $percent.'% '.img_picto($langs->trans('StatusActionInProcess').' - '.$percent.'%','statut3');
  615. else if ($percent >= 100) return img_picto($langs->trans('StatusActionDone'),'statut6');
  616. }
  617. return '';
  618. }
  619. /**
  620. * Renvoie nom clicable (avec eventuellement le picto)
  621. * Utilise $this->id, $this->code et $this->label
  622. *
  623. * @param int $withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
  624. * @param int $maxlength Nombre de caracteres max dans libelle
  625. * @param string $classname Force style class on a link
  626. * @param string $option ''=Link to action,'birthday'=Link to contact
  627. * @param int $overwritepicto 1=Overwrite picto
  628. * @return string Chaine avec URL
  629. */
  630. function getNomUrl($withpicto=0,$maxlength=0,$classname='',$option='',$overwritepicto='')
  631. {
  632. global $langs;
  633. $result='';
  634. if ($option=='birthday') $lien = '<a '.($classname?'class="'.$classname.'" ':'').'href="'.DOL_URL_ROOT.'/contact/perso.php?id='.$this->id.'">';
  635. else $lien = '<a '.($classname?'class="'.$classname.'" ':'').'href="'.DOL_URL_ROOT.'/comm/action/fiche.php?id='.$this->id.'">';
  636. $lienfin='</a>';
  637. //print $this->libelle;
  638. if ($withpicto == 2)
  639. {
  640. $libelle=$langs->trans("Action".$this->type_code);
  641. $libelleshort='';
  642. }
  643. else if (empty($this->libelle))
  644. {
  645. $libelle=$langs->trans("Action".$this->type_code);
  646. $libelleshort=$langs->trans("Action".$this->type_code,'','','','',$maxlength);
  647. }
  648. else
  649. {
  650. $libelle=$this->libelle;
  651. $libelleshort=dol_trunc($this->libelle,$maxlength);
  652. }
  653. if ($withpicto)
  654. {
  655. $libelle.=(($this->type_code && $libelle!=$langs->trans("Action".$this->type_code) && $langs->trans("Action".$this->type_code)!="Action".$this->type_code)?' ('.$langs->trans("Action".$this->type_code).')':'');
  656. $result.=$lien.img_object($langs->trans("ShowAction").': '.$libelle,($overwritepicto?$overwritepicto:'action')).$lienfin;
  657. }
  658. if ($withpicto==1) $result.=' ';
  659. $result.=$lien.$libelleshort.$lienfin;
  660. return $result;
  661. }
  662. /**
  663. * Export events from database into a cal file.
  664. *
  665. * @param string $format 'vcal', 'ical/ics', 'rss'
  666. * @param string $type 'event' or 'journal'
  667. * @param int $cachedelay Do not rebuild file if date older than cachedelay seconds
  668. * @param string $filename Force filename
  669. * @param array $filters Array of filters
  670. * @return int <0 if error, nb of events in new file if ok
  671. */
  672. function build_exportfile($format,$type,$cachedelay,$filename,$filters)
  673. {
  674. global $conf,$langs,$dolibarr_main_url_root,$mysoc;
  675. require_once (DOL_DOCUMENT_ROOT ."/core/lib/xcal.lib.php");
  676. require_once (DOL_DOCUMENT_ROOT ."/core/lib/date.lib.php");
  677. dol_syslog(get_class($this)."::build_exportfile Build export file format=".$format.", type=".$type.", cachedelay=".$cachedelay.", filename=".$filename.", filters size=".count($filters), LOG_DEBUG);
  678. // Check parameters
  679. if (empty($format)) return -1;
  680. // Clean parameters
  681. if (! $filename)
  682. {
  683. $extension='vcs';
  684. if ($format == 'ical') $extension='ics';
  685. $filename=$format.'.'.$extension;
  686. }
  687. // Create dir and define output file (definitive and temporary)
  688. $result=dol_mkdir($conf->agenda->dir_temp);
  689. $outputfile=$conf->agenda->dir_temp.'/'.$filename;
  690. $result=0;
  691. $buildfile=true;
  692. $login='';$logina='';$logind='';$logint='';
  693. $now = dol_now();
  694. if ($cachedelay)
  695. {
  696. $nowgmt = dol_now();
  697. include_once(DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php');
  698. if (dol_filemtime($outputfile) > ($nowgmt - $cachedelay))
  699. {
  700. dol_syslog(get_class($this)."::build_exportfile file ".$outputfile." is not older than now - cachedelay (".$nowgmt." - ".$cachedelay."). Build is canceled");
  701. $buildfile = false;
  702. }
  703. }
  704. if ($buildfile)
  705. {
  706. // Build event array
  707. $eventarray=array();
  708. $sql = "SELECT a.id,";
  709. $sql.= " a.datep,"; // Start
  710. $sql.= " a.datep2,"; // End
  711. $sql.= " a.durationp,";
  712. $sql.= " a.datec, a.tms as datem,";
  713. $sql.= " a.note, a.label, a.fk_action as type_id,";
  714. $sql.= " a.fk_soc,";
  715. $sql.= " a.fk_user_author, a.fk_user_mod,";
  716. $sql.= " a.fk_user_action, a.fk_user_done,";
  717. $sql.= " a.fk_contact, a.percent as percentage,";
  718. $sql.= " a.fk_element, a.elementtype,";
  719. $sql.= " a.priority, a.fulldayevent, a.location,";
  720. $sql.= " u.firstname, u.name,";
  721. $sql.= " s.nom as socname,";
  722. $sql.= " c.id as type_id, c.code as type_code, c.libelle";
  723. $sql.= " FROM (".MAIN_DB_PREFIX."c_actioncomm as c, ".MAIN_DB_PREFIX."actioncomm as a)";
  724. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on u.rowid = a.fk_user_author";
  725. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on s.rowid = a.fk_soc";
  726. $sql.= " WHERE a.fk_action=c.id";
  727. $sql.= " AND a.entity = ".$conf->entity;
  728. foreach ($filters as $key => $value)
  729. {
  730. if ($key == 'notolderthan') $sql.=" AND a.datep >= '".$this->db->idate($now-($value*24*60*60))."'";
  731. if ($key == 'year') $sql.=" AND a.datep BETWEEN '".$this->db->idate(dol_get_first_day($value,1))."' AND '".$this->db->idate(dol_get_last_day($value,12))."'";
  732. if ($key == 'id') $sql.=" AND a.id=".(is_numeric($value)?$value:0);
  733. if ($key == 'idfrom') $sql.=" AND a.id >= ".(is_numeric($value)?$value:0);
  734. if ($key == 'idto') $sql.=" AND a.id <= ".(is_numeric($value)?$value:0);
  735. if ($key == 'login')
  736. {
  737. $login=$value;
  738. $userforfilter=new User($this->db);
  739. $result=$userforfilter->fetch('',$value);
  740. $sql.= " AND (";
  741. $sql.= " a.fk_user_author = ".$userforfilter->id;
  742. $sql.= " OR a.fk_user_action = ".$userforfilter->id;
  743. $sql.= " OR a.fk_user_done = ".$userforfilter->id;
  744. $sql.= ")";
  745. }
  746. if ($key == 'logina')
  747. {
  748. $logina=$value;
  749. $userforfilter=new User($this->db);
  750. $result=$userforfilter->fetch('',$value);
  751. $sql.= " AND a.fk_user_author = ".$userforfilter->id;
  752. }
  753. if ($key == 'logint')
  754. {
  755. $logint=$value;
  756. $userforfilter=new User($this->db);
  757. $result=$userforfilter->fetch('',$value);
  758. $sql.= " AND a.fk_user_action = ".$userforfilter->id;
  759. }
  760. if ($key == 'logind')
  761. {
  762. $logind=$value;
  763. $userforfilter=new User($this->db);
  764. $result=$userforfilter->fetch('',$value);
  765. $sql.= " AND a.fk_user_done = ".$userforfilter->id;
  766. }
  767. }
  768. $sql.= " AND a.datep IS NOT NULL"; // To exclude corrupted events and avoid errors in lightning/sunbird import
  769. $sql.= " ORDER by datep";
  770. //print $sql;exit;
  771. dol_syslog(get_class($this)."::build_exportfile select events sql=".$sql);
  772. $resql=$this->db->query($sql);
  773. if ($resql)
  774. {
  775. // Note: Output of sql request is encoded in $conf->file->character_set_client
  776. while ($obj=$this->db->fetch_object($resql))
  777. {
  778. $qualified=true;
  779. // 'eid','startdate','duration','enddate','title','summary','category','email','url','desc','author'
  780. $event=array();
  781. $event['uid']='dolibarragenda-'.$this->db->database_name.'-'.$obj->id."@".$_SERVER["SERVER_NAME"];
  782. $event['type']=$type;
  783. //$datestart=$obj->datea?$obj->datea:$obj->datep;
  784. //$dateend=$obj->datea2?$obj->datea2:$obj->datep2;
  785. //$duration=$obj->durationa?$obj->durationa:$obj->durationp;
  786. $datestart=$this->db->jdate($obj->datep);
  787. //print $datestart.'x'; exit;
  788. $dateend=$this->db->jdate($obj->datep2);
  789. $duration=$obj->durationp;
  790. $event['summary']=$obj->label.($obj->socname?" (".$obj->socname.")":"");
  791. $event['desc']=$obj->note;
  792. $event['startdate']=$datestart;
  793. $event['duration']=$duration; // Not required with type 'journal'
  794. $event['enddate']=$dateend; // Not required with type 'journal'
  795. $event['author']=$obj->firstname.($obj->name?" ".$obj->name:"");
  796. $event['priority']=$obj->priority;
  797. $event['fulldayevent']=$obj->fulldayevent;
  798. $event['location']=$obj->location;
  799. $event['transparency']='TRANSPARENT'; // OPAQUE (busy) or TRANSPARENT (not busy)
  800. $event['category']=$obj->libelle; // libelle type action
  801. $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',$dolibarr_main_url_root);
  802. $url=$urlwithouturlroot.DOL_URL_ROOT.'/comm/action/fiche.php?id='.$obj->id;
  803. $event['url']=$url;
  804. $event['created']=$this->db->jdate($obj->datec);
  805. $event['modified']=$this->db->jdate($obj->datem);
  806. if ($qualified && $datestart)
  807. {
  808. $eventarray[$datestart]=$event;
  809. }
  810. }
  811. }
  812. else
  813. {
  814. $this->error=$this->db->lasterror();
  815. dol_syslog(get_class($this)."::build_exportfile ".$this->db->lasterror(), LOG_ERR);
  816. return -1;
  817. }
  818. $langs->load("agenda");
  819. // Define title and desc
  820. $more='';
  821. if ($login) $more=$langs->transnoentities("User").' '.$login;
  822. if ($logina) $more=$langs->transnoentities("ActionsAskedBy").' '.$logina;
  823. if ($logint) $more=$langs->transnoentities("ActionsToDoBy").' '.$logint;
  824. if ($logind) $more=$langs->transnoentities("ActionsDoneBy").' '.$logind;
  825. if ($more)
  826. {
  827. $title='Dolibarr actions '.$mysoc->name.' - '.$more;
  828. $desc=$more;
  829. $desc.=' ('.$mysoc->name.' - built by Dolibarr)';
  830. }
  831. else
  832. {
  833. $title='Dolibarr actions '.$mysoc->name;
  834. $desc=$langs->transnoentities('ListOfActions');
  835. $desc.=' ('.$mysoc->name.' - built by Dolibarr)';
  836. }
  837. // Create temp file
  838. $outputfiletmp=tempnam($conf->agenda->dir_temp,'tmp'); // Temporary file (allow call of function by different threads
  839. @chmod($outputfiletmp, octdec($conf->global->MAIN_UMASK));
  840. // Write file
  841. if ($format == 'vcal') $result=build_calfile($format,$title,$desc,$eventarray,$outputfiletmp);
  842. if ($format == 'ical') $result=build_calfile($format,$title,$desc,$eventarray,$outputfiletmp);
  843. if ($format == 'rss') $result=build_rssfile($format,$title,$desc,$eventarray,$outputfiletmp);
  844. if ($result >= 0)
  845. {
  846. if (rename($outputfiletmp,$outputfile)) $result=1;
  847. else
  848. {
  849. dol_syslog(get_class($this)."::build_exportfile failed to rename ".$outputfiletmp." to ".$outputfile, LOG_ERR);
  850. dol_delete_file($outputfiletmp,0,1);
  851. $result=-1;
  852. }
  853. }
  854. else
  855. {
  856. dol_syslog(get_class($this)."::build_exportfile build_xxxfile function fails to for format=".$format." outputfiletmp=".$outputfile, LOG_ERR);
  857. dol_delete_file($outputfiletmp,0,1);
  858. $langs->load("errors");
  859. $this->error=$langs->trans("ErrorFailToCreateFile",$outputfile);
  860. }
  861. }
  862. return $result;
  863. }
  864. /**
  865. * Initialise an instance with random values.
  866. * Used to build previews or test instances.
  867. * id must be 0 if object instance is a specimen.
  868. *
  869. * @return void
  870. */
  871. function initAsSpecimen()
  872. {
  873. global $user,$langs,$conf;
  874. $now=dol_now();
  875. // Initialise parametres
  876. $this->id=0;
  877. $this->specimen=1;
  878. $this->type_code='AC_OTH';
  879. $this->label='Label of event Specimen';
  880. $this->datec=$now;
  881. $this->datem=$now;
  882. $this->datep=$now;
  883. $this->datef=$now;
  884. $this->author=$user;
  885. $this->usermod=$user;
  886. $this->fulldayevent=0;
  887. $this->punctual=0;
  888. $this->percentage=0;
  889. $this->location='Location';
  890. $this->priority='Priority X';
  891. $this->note = 'Note';
  892. }
  893. }
  894. ?>