PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

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