actioncomm.class.php 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  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.houssin@capnetworks.com>
  5. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/comm/action/class/actioncomm.class.php
  23. * \ingroup agenda
  24. * \brief File of class to manage agenda events (actions)
  25. */
  26. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/cactioncomm.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  28. /**
  29. * Class to manage agenda events (actions)
  30. */
  31. class ActionComm extends CommonObject
  32. {
  33. public $element='action';
  34. public $table_element = 'actioncomm';
  35. public $table_rowid = 'id';
  36. protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  37. /**
  38. * Id of the event
  39. * @var int
  40. */
  41. var $id;
  42. /**
  43. * Id of the event. Use $id as possible
  44. * @var int
  45. */
  46. public $ref;
  47. var $type_id; // Id into parent table llx_c_actioncomm (used only if option to use type is set)
  48. var $type_code; // Code into parent table llx_c_actioncomm (used only if option to use type is set). With default setup, should be AC_OTH_AUTO or AC_OTH.
  49. var $type; // Label into parent table llx_c_actioncomm (used only if option to use type is set)
  50. var $type_color; // Color into parent table llx_c_actioncomm (used only if option to use type is set)
  51. var $code; // Free code to identify action. Ie: Agenda trigger add here AC_TRIGGERNAME ('AC_COMPANY_CREATE', 'AC_PROPAL_VALIDATE', ...)
  52. var $label;
  53. /**
  54. * @var string
  55. * @deprecated Use $label
  56. * @see label
  57. */
  58. public $libelle;
  59. var $datec; // Date creation record (datec)
  60. var $datem; // Date modification record (tms)
  61. /**
  62. * Object user that create action
  63. * @var User
  64. * @deprecated
  65. * @see authorid
  66. */
  67. var $author;
  68. /**
  69. * Object user that modified action
  70. * @var User
  71. * @deprecated
  72. * @see usermodid
  73. */
  74. var $usermod;
  75. var $authorid; // Id user that create action
  76. var $usermodid; // Id user that modified action
  77. var $datep; // Date action start (datep)
  78. var $datef; // Date action end (datep2)
  79. /**
  80. * @var int -1=Unkown duration
  81. * @deprecated
  82. */
  83. var $durationp = -1;
  84. var $fulldayevent = 0; // 1=Event on full day
  85. /**
  86. * Milestone
  87. * @var int
  88. * @deprecated Milestone is already event with end date = start date
  89. */
  90. var $punctual = 1;
  91. var $percentage; // Percentage
  92. var $location; // Location
  93. var $transparency; // Transparency (ical standard). Used to say if people assigned to event are busy or not by event. 0=available, 1=busy, 2=busy (refused events)
  94. var $priority; // Small int (0 By default)
  95. var $userassigned = array(); // Array of user ids
  96. var $userownerid; // Id of user owner = fk_user_action into table
  97. var $userdoneid; // Id of user done (deprecated)
  98. /**
  99. * Object user of owner
  100. * @var User
  101. * @deprecated
  102. * @see userownerid
  103. */
  104. var $usertodo;
  105. /**
  106. * Object user that did action
  107. * @var User
  108. * @deprecated
  109. * @see userdoneid
  110. */
  111. var $userdone;
  112. var $socid;
  113. var $contactid;
  114. /**
  115. * Company linked to action (optional)
  116. * @var Societe|null
  117. * @deprecated
  118. * @see socid
  119. */
  120. var $societe;
  121. /**
  122. * Contact linked to action (optional)
  123. * @var Contact|null
  124. * @deprecated
  125. * @see contactid
  126. */
  127. var $contact;
  128. // Properties for links to other objects
  129. var $fk_element; // Id of record
  130. var $elementtype; // Type of record. This if property ->element of object linked to.
  131. // Ical
  132. var $icalname;
  133. var $icalcolor;
  134. var $actions=array();
  135. // Fields for emails
  136. var $email_msgid;
  137. var $email_from;
  138. var $email_sender;
  139. var $email_to;
  140. var $email_tocc;
  141. var $email_tobcc;
  142. var $email_subject;
  143. var $errors_to;
  144. /**
  145. * Constructor
  146. *
  147. * @param DoliDB $db Database handler
  148. */
  149. public function __construct(DoliDB $db)
  150. {
  151. $this->db = $db;
  152. $this->societe = new stdClass(); // deprecated
  153. $this->contact = new stdClass(); // deprecated
  154. }
  155. /**
  156. * Add an action/event into database.
  157. * $this->type_id OR $this->type_code must be set.
  158. *
  159. * @param User $user Object user making action
  160. * @param int $notrigger 1 = disable triggers, 0 = enable triggers
  161. * @return int Id of created event, < 0 if KO
  162. */
  163. public function create(User $user, $notrigger = 0)
  164. {
  165. global $langs,$conf,$hookmanager;
  166. $error=0;
  167. $now=dol_now();
  168. // Check parameters
  169. if (empty($this->userownerid))
  170. {
  171. dol_syslog("You tried to create an event but mandatory property ownerid was not defined", LOG_WARNING);
  172. $this->errors[]='ErrorPropertyUserowneridNotDefined';
  173. return -1;
  174. }
  175. // Clean parameters
  176. $this->label=dol_trunc(trim($this->label),128);
  177. $this->location=dol_trunc(trim($this->location),128);
  178. $this->note=dol_htmlcleanlastbr(trim($this->note));
  179. if (empty($this->percentage)) $this->percentage = 0;
  180. if (empty($this->priority) || ! is_numeric($this->priority)) $this->priority = 0;
  181. if (empty($this->fulldayevent)) $this->fulldayevent = 0;
  182. if (empty($this->punctual)) $this->punctual = 0;
  183. if (empty($this->transparency)) $this->transparency = 0;
  184. if ($this->percentage > 100) $this->percentage = 100;
  185. //if ($this->percentage == 100 && ! $this->dateend) $this->dateend = $this->date;
  186. if (! empty($this->datep) && ! empty($this->datef)) $this->durationp=($this->datef - $this->datep); // deprecated
  187. //if (! empty($this->date) && ! empty($this->dateend)) $this->durationa=($this->dateend - $this->date);
  188. if (! empty($this->datep) && ! empty($this->datef) && $this->datep > $this->datef) $this->datef=$this->datep;
  189. //if (! empty($this->date) && ! empty($this->dateend) && $this->date > $this->dateend) $this->dateend=$this->date;
  190. if (! isset($this->fk_project) || $this->fk_project < 0) $this->fk_project = 0;
  191. if ($this->elementtype=='facture') $this->elementtype='invoice';
  192. if ($this->elementtype=='commande') $this->elementtype='order';
  193. if ($this->elementtype=='contrat') $this->elementtype='contract';
  194. if (! is_array($this->userassigned) && ! empty($this->userassigned)) // For backward compatibility
  195. {
  196. $tmpid=$this->userassigned;
  197. $this->userassigned=array();
  198. $this->userassigned[$tmpid]=array('id'=>$tmpid);
  199. }
  200. if (is_object($this->contact) && isset($this->contact->id) && $this->contact->id > 0 && ! ($this->contactid > 0)) $this->contactid = $this->contact->id; // For backward compatibility. Using this->contact->xx is deprecated
  201. $userownerid=$this->userownerid;
  202. $userdoneid=$this->userdoneid;
  203. // Be sure assigned user is defined as an array of array('id'=>,'mandatory'=>,...).
  204. if (empty($this->userassigned) || count($this->userassigned) == 0 || ! is_array($this->userassigned))
  205. $this->userassigned = array($userownerid=>array('id'=>$userownerid));
  206. if (! $this->type_id || ! $this->type_code)
  207. {
  208. $key=empty($this->type_id)?$this->type_code:$this->type_id;
  209. // Get id from code
  210. $cactioncomm=new CActionComm($this->db);
  211. $result=$cactioncomm->fetch($key);
  212. if ($result > 0)
  213. {
  214. $this->type_id=$cactioncomm->id;
  215. $this->type_code=$cactioncomm->code;
  216. }
  217. else if ($result == 0)
  218. {
  219. $this->error='Failed to get record with id '.$this->type_id.' code '.$this->type_code.' from dictionary "type of events"';
  220. return -1;
  221. }
  222. else
  223. {
  224. $this->error=$cactioncomm->error;
  225. return -1;
  226. }
  227. }
  228. // Check parameters
  229. if (! $this->type_id)
  230. {
  231. $this->error="ErrorWrongParameters";
  232. return -1;
  233. }
  234. $this->db->begin();
  235. $sql = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm";
  236. $sql.= "(datec,";
  237. $sql.= "datep,";
  238. $sql.= "datep2,";
  239. $sql.= "durationp,"; // deprecated
  240. $sql.= "fk_action,";
  241. $sql.= "code,";
  242. $sql.= "fk_soc,";
  243. $sql.= "fk_project,";
  244. $sql.= "note,";
  245. $sql.= "fk_contact,";
  246. $sql.= "fk_user_author,";
  247. $sql.= "fk_user_action,";
  248. $sql.= "fk_user_done,";
  249. $sql.= "label,percent,priority,fulldayevent,location,punctual,";
  250. $sql.= "transparency,";
  251. $sql.= "fk_element,";
  252. $sql.= "elementtype,";
  253. $sql.= "entity";
  254. $sql.= ") VALUES (";
  255. $sql.= "'".$this->db->idate($now)."',";
  256. $sql.= (strval($this->datep)!=''?"'".$this->db->idate($this->datep)."'":"null").",";
  257. $sql.= (strval($this->datef)!=''?"'".$this->db->idate($this->datef)."'":"null").",";
  258. $sql.= ((isset($this->durationp) && $this->durationp >= 0 && $this->durationp != '')?"'".$this->durationp."'":"null").","; // deprecated
  259. $sql.= (isset($this->type_id)?$this->type_id:"null").",";
  260. $sql.= (isset($this->type_code)?" '".$this->type_code."'":"null").",";
  261. $sql.= ((isset($this->socid) && $this->socid > 0)?" '".$this->socid."'":"null").",";
  262. $sql.= ((isset($this->fk_project) && $this->fk_project > 0)?" '".$this->fk_project."'":"null").",";
  263. $sql.= " '".$this->db->escape($this->note)."',";
  264. $sql.= ((isset($this->contactid) && $this->contactid > 0)?"'".$this->contactid."'":"null").",";
  265. $sql.= (isset($user->id) && $user->id > 0 ? "'".$user->id."'":"null").",";
  266. $sql.= ($userownerid>0?"'".$userownerid."'":"null").",";
  267. $sql.= ($userdoneid>0?"'".$userdoneid."'":"null").",";
  268. $sql.= "'".$this->db->escape($this->label)."','".$this->percentage."','".$this->priority."','".$this->fulldayevent."','".$this->db->escape($this->location)."','".$this->punctual."',";
  269. $sql.= "'".$this->transparency."',";
  270. $sql.= (! empty($this->fk_element)?$this->fk_element:"null").",";
  271. $sql.= (! empty($this->elementtype)?"'".$this->elementtype."'":"null").",";
  272. $sql.= $conf->entity;
  273. $sql.= ")";
  274. dol_syslog(get_class($this)."::add", LOG_DEBUG);
  275. $resql=$this->db->query($sql);
  276. if ($resql)
  277. {
  278. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."actioncomm","id");
  279. // Now insert assignedusers
  280. if (! $error)
  281. {
  282. foreach($this->userassigned as $key => $val)
  283. {
  284. if (! is_array($val)) // For backward compatibility when val=id
  285. {
  286. $val=array('id'=>$val);
  287. }
  288. $sql ="INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element, mandatory, transparency, answer_status)";
  289. $sql.=" VALUES(".$this->id.", 'user', ".$val['id'].", ".(empty($val['mandatory'])?'0':$val['mandatory']).", ".(empty($val['transparency'])?'0':$val['transparency']).", ".(empty($val['answer_status'])?'0':$val['answer_status']).")";
  290. $resql = $this->db->query($sql);
  291. if (! $resql)
  292. {
  293. $error++;
  294. $this->errors[]=$this->db->lasterror();
  295. }
  296. //var_dump($sql);exit;
  297. }
  298. }
  299. if (! $error)
  300. {
  301. $action='create';
  302. // Actions on extra fields (by external module or standard code)
  303. // TODO le hook fait double emploi avec le trigger !!
  304. $hookmanager->initHooks(array('actioncommdao'));
  305. $parameters=array('actcomm'=>$this->id);
  306. $reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
  307. if (empty($reshook))
  308. {
  309. if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
  310. {
  311. $result=$this->insertExtraFields();
  312. if ($result < 0)
  313. {
  314. $error++;
  315. }
  316. }
  317. }
  318. else if ($reshook < 0) $error++;
  319. }
  320. if (! $error && ! $notrigger)
  321. {
  322. // Call trigger
  323. $result=$this->call_trigger('ACTION_CREATE',$user);
  324. if ($result < 0) { $error++; }
  325. // End call triggers
  326. }
  327. if (! $error)
  328. {
  329. $this->db->commit();
  330. return $this->id;
  331. }
  332. else
  333. {
  334. $this->db->rollback();
  335. return -1;
  336. }
  337. }
  338. else
  339. {
  340. $this->db->rollback();
  341. $this->error=$this->db->lasterror();
  342. return -1;
  343. }
  344. }
  345. /**
  346. * Add an action/event into database.
  347. * $this->type_id OR $this->type_code must be set.
  348. *
  349. * @param User $user Object user making action
  350. * @param int $notrigger 1 = disable triggers, 0 = enable triggers
  351. * @return int Id of created event, < 0 if KO
  352. * @deprecated Use create instead
  353. */
  354. public function add(User $user, $notrigger = 0)
  355. {
  356. return $this->create($user, $notrigger);
  357. }
  358. /**
  359. * Load an object from its id and create a new one in database
  360. *
  361. * @param user $fuser Object user making action
  362. * @param int $socid Id of thirdparty
  363. * @return int New id of clone
  364. */
  365. function createFromClone($fuser, $socid)
  366. {
  367. global $db, $user, $langs, $conf, $hookmanager;
  368. $this->context['createfromclone']='createfromclone';
  369. $error=0;
  370. $now=dol_now();
  371. $this->db->begin();
  372. // Load source object
  373. $objFrom = clone $this;
  374. $this->fetch_optionals();
  375. $this->fetch_userassigned();
  376. $this->id=0;
  377. if (!is_object($fuser))
  378. {
  379. if ($fuser > 0)
  380. {
  381. $u = new User($db);
  382. $u->fetch($fuser);
  383. $fuser = $u;
  384. }
  385. else
  386. {
  387. $fuser = $user;
  388. }
  389. }
  390. // Create clone
  391. $result=$this->create($fuser);
  392. if ($result < 0) $error++;
  393. if (! $error)
  394. {
  395. // Hook of thirdparty module
  396. if (is_object($hookmanager))
  397. {
  398. $parameters=array('objFrom'=>$objFrom);
  399. $action='';
  400. $reshook=$hookmanager->executeHooks('createFrom',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
  401. if ($reshook < 0) $error++;
  402. }
  403. // Call trigger
  404. $result=$this->call_trigger('ACTION_CLONE', $fuser);
  405. if ($result < 0) { $error++; }
  406. // End call triggers
  407. }
  408. unset($this->context['createfromclone']);
  409. // End
  410. if (! $error)
  411. {
  412. $this->db->commit();
  413. return $this->id;
  414. }
  415. else
  416. {
  417. $this->db->rollback();
  418. return -1;
  419. }
  420. }
  421. /**
  422. * Load object from database
  423. *
  424. * @param int $id Id of action to get
  425. * @param string $ref Ref of action to get
  426. * @param string $ref_ext Ref ext to get
  427. * @return int <0 if KO, >0 if OK
  428. */
  429. function fetch($id, $ref='',$ref_ext='')
  430. {
  431. global $langs;
  432. $sql = "SELECT a.id,";
  433. $sql.= " a.id as ref,";
  434. $sql.= " a.ref_ext,";
  435. $sql.= " a.datep,";
  436. $sql.= " a.datep2,";
  437. $sql.= " a.durationp,"; // deprecated
  438. $sql.= " a.datec,";
  439. $sql.= " a.tms as datem,";
  440. $sql.= " a.code, a.label, a.note,";
  441. $sql.= " a.fk_soc,";
  442. $sql.= " a.fk_project,";
  443. $sql.= " a.fk_user_author, a.fk_user_mod,";
  444. $sql.= " a.fk_user_action, a.fk_user_done,";
  445. $sql.= " a.fk_contact, a.percent as percentage,";
  446. $sql.= " a.fk_element, a.elementtype,";
  447. $sql.= " a.priority, a.fulldayevent, a.location, a.punctual, a.transparency,";
  448. $sql.= " c.id as type_id, c.code as type_code, c.libelle,";
  449. $sql.= " s.nom as socname,";
  450. $sql.= " u.firstname, u.lastname as lastname";
  451. $sql.= " FROM ".MAIN_DB_PREFIX."actioncomm as a ";
  452. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_actioncomm as c ON a.fk_action=c.id ";
  453. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on u.rowid = a.fk_user_author";
  454. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on s.rowid = a.fk_soc";
  455. $sql.= " WHERE ";
  456. if ($ref) $sql.= " a.id=".$ref; // No field ref, we use id
  457. elseif ($ref_ext) $sql.= " a.ref_ext='".$this->db->escape($ref_ext)."'";
  458. else $sql.= " a.id=".$id;
  459. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  460. $resql=$this->db->query($sql);
  461. if ($resql)
  462. {
  463. $num=$this->db->num_rows($resql);
  464. if ($num)
  465. {
  466. $obj = $this->db->fetch_object($resql);
  467. $this->id = $obj->id;
  468. $this->ref = $obj->ref;
  469. $this->ref_ext = $obj->ref_ext;
  470. // Properties of parent table llx_c_actioncomm (will be deprecated in future)
  471. $this->type_id = $obj->type_id;
  472. $this->type_code = $obj->type_code;
  473. $transcode=$langs->trans("Action".$obj->type_code);
  474. $type_libelle=($transcode!="Action".$obj->type_code?$transcode:$obj->libelle);
  475. $this->type = $type_libelle;
  476. $this->code = $obj->code;
  477. $this->label = $obj->label;
  478. $this->datep = $this->db->jdate($obj->datep);
  479. $this->datef = $this->db->jdate($obj->datep2);
  480. // $this->durationp = $this->durationp; // deprecated
  481. $this->datec = $this->db->jdate($obj->datec);
  482. $this->datem = $this->db->jdate($obj->datem);
  483. $this->note = $obj->note;
  484. $this->percentage = $obj->percentage;
  485. $this->authorid = $obj->fk_user_author;
  486. $this->usermodid = $obj->fk_user_mod;
  487. if (!is_object($this->author)) $this->author = new stdClass(); // For avoid warning
  488. $this->author->id = $obj->fk_user_author; // deprecated
  489. $this->author->firstname = $obj->firstname; // deprecated
  490. $this->author->lastname = $obj->lastname; // deprecated
  491. if (!is_object($this->usermod)) $this->usermod = new stdClass(); // For avoid warning
  492. $this->usermod->id = $obj->fk_user_mod; // deprecated
  493. $this->userownerid = $obj->fk_user_action;
  494. $this->userdoneid = $obj->fk_user_done;
  495. $this->priority = $obj->priority;
  496. $this->fulldayevent = $obj->fulldayevent;
  497. $this->location = $obj->location;
  498. $this->transparency = $obj->transparency;
  499. $this->punctual = $obj->punctual; // deprecated
  500. $this->socid = $obj->fk_soc; // To have fetch_thirdparty method working
  501. $this->contactid = $obj->fk_contact; // To have fetch_contact method working
  502. $this->fk_project = $obj->fk_project; // To have fetch_project method working
  503. $this->societe->id = $obj->fk_soc; // deprecated
  504. $this->contact->id = $obj->fk_contact; // deprecated
  505. $this->fk_element = $obj->fk_element;
  506. $this->elementtype = $obj->elementtype;
  507. }
  508. $this->db->free($resql);
  509. }
  510. else
  511. {
  512. $this->error=$this->db->lasterror();
  513. return -1;
  514. }
  515. return $num;
  516. }
  517. /**
  518. * Initialize this->userassigned array with list of id of user assigned to event
  519. *
  520. * @return int <0 if KO, >0 if OK
  521. */
  522. function fetch_userassigned()
  523. {
  524. $sql ="SELECT fk_actioncomm, element_type, fk_element, answer_status, mandatory, transparency";
  525. $sql.=" FROM ".MAIN_DB_PREFIX."actioncomm_resources";
  526. $sql.=" WHERE element_type = 'user' AND fk_actioncomm = ".$this->id;
  527. $resql2=$this->db->query($sql);
  528. if ($resql2)
  529. {
  530. $this->userassigned=array();
  531. // If owner is known, we must but id first into list
  532. if ($this->userownerid > 0) $this->userassigned[$this->userownerid]=array('id'=>$this->userownerid); // Set first so will be first into list.
  533. while ($obj = $this->db->fetch_object($resql2))
  534. {
  535. if ($obj->fk_element > 0) $this->userassigned[$obj->fk_element]=array('id'=>$obj->fk_element, 'mandatory'=>$obj->mandatory, 'answer_status'=>$obj->answer_status, 'transparency'=>$obj->transparency);
  536. if (empty($this->userownerid)) $this->userownerid=$obj->fk_element; // If not defined (should not happened, we fix this)
  537. }
  538. return 1;
  539. }
  540. else
  541. {
  542. dol_print_error($this->db);
  543. return -1;
  544. }
  545. }
  546. /**
  547. * Delete event from database
  548. *
  549. * @param int $notrigger 1 = disable triggers, 0 = enable triggers
  550. * @return int <0 if KO, >0 if OK
  551. */
  552. function delete($notrigger=0)
  553. {
  554. global $user,$langs,$conf;
  555. $error=0;
  556. $this->db->begin();
  557. $sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm";
  558. $sql.= " WHERE id=".$this->id;
  559. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  560. $res=$this->db->query($sql);
  561. if ($res < 0) {
  562. $this->error=$this->db->lasterror();
  563. $error++;
  564. }
  565. if (! $error) {
  566. $sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm_resources";
  567. $sql.= " WHERE fk_actioncomm=".$this->id;
  568. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  569. $res=$this->db->query($sql);
  570. if ($res < 0) {
  571. $this->error=$this->db->lasterror();
  572. $error++;
  573. }
  574. }
  575. // Removed extrafields
  576. if (! $error) {
  577. $result=$this->deleteExtraFields();
  578. if ($result < 0)
  579. {
  580. $error++;
  581. dol_syslog(get_class($this)."::delete error -3 ".$this->error, LOG_ERR);
  582. }
  583. }
  584. if (!$error)
  585. {
  586. if (! $notrigger)
  587. {
  588. // Call trigger
  589. $result=$this->call_trigger('ACTION_DELETE',$user);
  590. if ($result < 0) { $error++; }
  591. // End call triggers
  592. }
  593. if (! $error)
  594. {
  595. $this->db->commit();
  596. return 1;
  597. }
  598. else
  599. {
  600. $this->db->rollback();
  601. return -2;
  602. }
  603. }
  604. else
  605. {
  606. $this->db->rollback();
  607. $this->error=$this->db->lasterror();
  608. return -1;
  609. }
  610. }
  611. /**
  612. * Update action into database
  613. * If percentage = 100, on met a jour date 100%
  614. *
  615. * @param User $user Object user making change
  616. * @param int $notrigger 1 = disable triggers, 0 = enable triggers
  617. * @return int <0 if KO, >0 if OK
  618. */
  619. function update($user,$notrigger=0)
  620. {
  621. global $langs,$conf,$hookmanager;
  622. $error=0;
  623. // Clean parameters
  624. $this->label=trim($this->label);
  625. $this->note=trim($this->note);
  626. if (empty($this->percentage)) $this->percentage = 0;
  627. if (empty($this->priority) || ! is_numeric($this->priority)) $this->priority = 0;
  628. if (empty($this->transparency)) $this->transparency = 0;
  629. if (empty($this->fulldayevent)) $this->fulldayevent = 0;
  630. if ($this->percentage > 100) $this->percentage = 100;
  631. //if ($this->percentage == 100 && ! $this->dateend) $this->dateend = $this->date;
  632. if ($this->datep && $this->datef) $this->durationp=($this->datef - $this->datep); // deprecated
  633. //if ($this->date && $this->dateend) $this->durationa=($this->dateend - $this->date);
  634. if ($this->datep && $this->datef && $this->datep > $this->datef) $this->datef=$this->datep;
  635. //if ($this->date && $this->dateend && $this->date > $this->dateend) $this->dateend=$this->date;
  636. if ($this->fk_project < 0) $this->fk_project = 0;
  637. // Check parameters
  638. if ($this->percentage == 0 && $this->userdoneid > 0)
  639. {
  640. $this->error="ErrorCantSaveADoneUserWithZeroPercentage";
  641. return -1;
  642. }
  643. $socid=($this->socid?$this->socid:((isset($this->societe->id) && $this->societe->id > 0) ? $this->societe->id : 0));
  644. $contactid=($this->contactid?$this->contactid:((isset($this->contact->id) && $this->contact->id > 0) ? $this->contact->id : 0));
  645. $userownerid=($this->userownerid?$this->userownerid:0);
  646. $userdoneid=($this->userdoneid?$this->userdoneid:0);
  647. $this->db->begin();
  648. $sql = "UPDATE ".MAIN_DB_PREFIX."actioncomm ";
  649. $sql.= " SET percent = '".$this->percentage."'";
  650. if ($this->fk_action > 0) $sql.= ", fk_action = '".$this->fk_action."'";
  651. $sql.= ", label = ".($this->label ? "'".$this->db->escape($this->label)."'":"null");
  652. $sql.= ", datep = ".(strval($this->datep)!='' ? "'".$this->db->idate($this->datep)."'" : 'null');
  653. $sql.= ", datep2 = ".(strval($this->datef)!='' ? "'".$this->db->idate($this->datef)."'" : 'null');
  654. $sql.= ", durationp = ".(isset($this->durationp) && $this->durationp >= 0 && $this->durationp != ''?"'".$this->durationp."'":"null"); // deprecated
  655. $sql.= ", note = ".($this->note ? "'".$this->db->escape($this->note)."'":"null");
  656. $sql.= ", fk_project =". ($this->fk_project > 0 ? "'".$this->fk_project."'":"null");
  657. $sql.= ", fk_soc =". ($socid > 0 ? "'".$socid."'":"null");
  658. $sql.= ", fk_contact =". ($contactid > 0 ? "'".$contactid."'":"null");
  659. $sql.= ", priority = '".$this->priority."'";
  660. $sql.= ", fulldayevent = '".$this->fulldayevent."'";
  661. $sql.= ", location = ".($this->location ? "'".$this->db->escape($this->location)."'":"null");
  662. $sql.= ", transparency = '".$this->transparency."'";
  663. $sql.= ", fk_user_mod = '".$user->id."'";
  664. $sql.= ", fk_user_action=".($userownerid > 0 ? "'".$userownerid."'":"null");
  665. $sql.= ", fk_user_done=".($userdoneid > 0 ? "'".$userdoneid."'":"null");
  666. if (! empty($this->fk_element)) $sql.= ", fk_element=".($this->fk_element?$this->fk_element:"null");
  667. if (! empty($this->elementtype)) $sql.= ", elementtype=".($this->elementtype?"'".$this->elementtype."'":"null");
  668. $sql.= " WHERE id=".$this->id;
  669. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  670. if ($this->db->query($sql))
  671. {
  672. $action='update';
  673. // Actions on extra fields (by external module or standard code)
  674. // TODO le hook fait double emploi avec le trigger !!
  675. $hookmanager->initHooks(array('actioncommdao'));
  676. $parameters=array('actcomm'=>$this->id);
  677. $reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
  678. if (empty($reshook))
  679. {
  680. if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
  681. {
  682. $result=$this->insertExtraFields();
  683. if ($result < 0)
  684. {
  685. $error++;
  686. }
  687. }
  688. }
  689. else if ($reshook < 0) $error++;
  690. // Now insert assignedusers
  691. if (! $error)
  692. {
  693. $sql ="DELETE FROM ".MAIN_DB_PREFIX."actioncomm_resources where fk_actioncomm = ".$this->id." AND element_type = 'user'";
  694. $resql = $this->db->query($sql);
  695. foreach($this->userassigned as $key => $val)
  696. {
  697. if (! is_array($val)) // For backward compatibility when val=id
  698. {
  699. $val=array('id'=>$val);
  700. }
  701. $sql ="INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element, mandatory, transparency, answer_status)";
  702. $sql.=" VALUES(".$this->id.", 'user', ".$val['id'].", ".(empty($val['manadatory'])?'0':$val['manadatory']).", ".(empty($val['transparency'])?'0':$val['transparency']).", ".(empty($val['answer_status'])?'0':$val['answer_status']).")";
  703. $resql = $this->db->query($sql);
  704. if (! $resql)
  705. {
  706. $error++;
  707. $this->errors[]=$this->db->lasterror();
  708. }
  709. //var_dump($sql);exit;
  710. }
  711. }
  712. if (! $error && ! $notrigger)
  713. {
  714. // Call trigger
  715. $result=$this->call_trigger('ACTION_MODIFY',$user);
  716. if ($result < 0) { $error++; }
  717. // End call triggers
  718. }
  719. if (! $error)
  720. {
  721. $this->db->commit();
  722. return 1;
  723. }
  724. else
  725. {
  726. $this->db->rollback();
  727. dol_syslog(get_class($this)."::update ".join(',',$this->errors),LOG_ERR);
  728. return -2;
  729. }
  730. }
  731. else
  732. {
  733. $this->db->rollback();
  734. $this->error=$this->db->lasterror();
  735. return -1;
  736. }
  737. }
  738. /**
  739. * Load all objects with filters
  740. *
  741. * @param DoliDb $db Database handler
  742. * @param int $socid Filter by thirdparty
  743. * @param int $fk_element Id of element action is linked to
  744. * @param string $elementtype Type of element action is linked to
  745. * @param string $filter Other filter
  746. * @param string $sortfield Sort on this field
  747. * @param string $sortorder ASC or DESC
  748. * @return array or string Error string if KO, array with actions if OK
  749. */
  750. static function getActions($db, $socid=0, $fk_element=0, $elementtype='', $filter='', $sortfield='', $sortorder='')
  751. {
  752. global $conf, $langs;
  753. $resarray=array();
  754. $sql = "SELECT a.id";
  755. $sql.= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
  756. $sql.= " WHERE a.entity IN (".getEntity('actioncomm', 1).")";
  757. if (! empty($socid)) $sql.= " AND a.fk_soc = ".$socid;
  758. if (! empty($elementtype))
  759. {
  760. if ($elementtype == 'project') $sql.= ' AND a.fk_project = '.$fk_element;
  761. else $sql.= " AND a.fk_element = ".(int) $fk_element." AND a.elementtype = '".$elementtype."'";
  762. }
  763. if (! empty($filter)) $sql.= $filter;
  764. if ($sortorder && $sortfield) $sql.=$db->order($sortfield, $sortorder);
  765. dol_syslog(get_class()."::getActions", LOG_DEBUG);
  766. $resql=$db->query($sql);
  767. if ($resql)
  768. {
  769. $num = $db->num_rows($resql);
  770. if ($num)
  771. {
  772. for($i=0;$i<$num;$i++)
  773. {
  774. $obj = $db->fetch_object($resql);
  775. $actioncommstatic = new ActionComm($db);
  776. $actioncommstatic->fetch($obj->id);
  777. $resarray[$i] = $actioncommstatic;
  778. }
  779. }
  780. $db->free($resql);
  781. return $resarray;
  782. }
  783. else
  784. {
  785. return $db->lasterror();
  786. }
  787. }
  788. /**
  789. * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
  790. *
  791. * @param User $user Objet user
  792. * @return WorkboardResponse|int <0 if KO, WorkboardResponse if OK
  793. */
  794. function load_board($user)
  795. {
  796. global $conf, $langs;
  797. $sql = "SELECT a.id, a.datep as dp";
  798. $sql.= " FROM (".MAIN_DB_PREFIX."actioncomm as a";
  799. $sql.= ")";
  800. if (! $user->rights->societe->client->voir && ! $user->societe_id) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON a.fk_soc = sc.fk_soc";
  801. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON a.fk_soc = s.rowid";
  802. $sql.= " WHERE a.percent >= 0 AND a.percent < 100";
  803. $sql.= " AND a.entity IN (".getEntity('actioncomm', 1).")";
  804. if (! $user->rights->societe->client->voir && ! $user->societe_id) $sql.= " AND (a.fk_soc IS NULL OR sc.fk_user = " .$user->id . ")";
  805. if ($user->societe_id) $sql.=" AND a.fk_soc = ".$user->societe_id;
  806. if (! $user->rights->agenda->allactions->read) $sql.= " AND (a.fk_user_author = ".$user->id . " OR a.fk_user_action = ".$user->id . " OR a.fk_user_done = ".$user->id . ")";
  807. $resql=$this->db->query($sql);
  808. if ($resql)
  809. {
  810. $agenda_static = new ActionComm($this->db);
  811. $response = new WorkboardResponse();
  812. $response->warning_delay = $conf->agenda->warning_delay/60/60/24;
  813. $response->label = $langs->trans("ActionsToDo");
  814. $response->url = DOL_URL_ROOT.'/comm/action/listactions.php?status=todo&amp;mainmenu=agenda';
  815. if ($user->rights->agenda->allactions->read) $response->url.='&amp;filtert=-1';
  816. $response->img = img_object($langs->trans("Actions"),"action");
  817. // This assignment in condition is not a bug. It allows walking the results.
  818. while ($obj=$this->db->fetch_object($resql))
  819. {
  820. $response->nbtodo++;
  821. $agenda_static->datep = $this->db->jdate($obj->dp);
  822. if ($agenda_static->hasDelay()) {
  823. $response->nbtodolate++;
  824. }
  825. }
  826. return $response;
  827. }
  828. else
  829. {
  830. $this->error=$this->db->error();
  831. return -1;
  832. }
  833. }
  834. /**
  835. * Charge les informations d'ordre info dans l'objet facture
  836. *
  837. * @param int $id Id de la facture a charger
  838. * @return void
  839. */
  840. function info($id)
  841. {
  842. $sql = 'SELECT ';
  843. $sql.= ' a.id,';
  844. $sql.= ' datec,';
  845. $sql.= ' tms as datem,';
  846. $sql.= ' fk_user_author,';
  847. $sql.= ' fk_user_mod';
  848. $sql.= ' FROM '.MAIN_DB_PREFIX.'actioncomm as a';
  849. $sql.= ' WHERE a.id = '.$id;
  850. dol_syslog(get_class($this)."::info", LOG_DEBUG);
  851. $result=$this->db->query($sql);
  852. if ($result)
  853. {
  854. if ($this->db->num_rows($result))
  855. {
  856. $obj = $this->db->fetch_object($result);
  857. $this->id = $obj->id;
  858. if ($obj->fk_user_author)
  859. {
  860. $cuser = new User($this->db);
  861. $cuser->fetch($obj->fk_user_author);
  862. $this->user_creation = $cuser;
  863. }
  864. if ($obj->fk_user_mod)
  865. {
  866. $muser = new User($this->db);
  867. $muser->fetch($obj->fk_user_mod);
  868. $this->user_modification = $muser;
  869. }
  870. $this->date_creation = $this->db->jdate($obj->datec);
  871. if (! empty($obj->fk_user_mod)) $this->date_modification = $this->db->jdate($obj->datem);
  872. }
  873. $this->db->free($result);
  874. }
  875. else
  876. {
  877. dol_print_error($this->db);
  878. }
  879. }
  880. /**
  881. * Return label of status
  882. *
  883. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  884. * @param int $hidenastatus 1=Show nothing if status is "Not applicable"
  885. * @return string String with status
  886. */
  887. function getLibStatut($mode,$hidenastatus=0)
  888. {
  889. return $this->LibStatut($this->percentage,$mode,$hidenastatus);
  890. }
  891. /**
  892. * Return label of action status
  893. *
  894. * @param int $percent Percent
  895. * @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
  896. * @param int $hidenastatus 1=Show nothing if status is "Not applicable"
  897. * @return string Label
  898. */
  899. function LibStatut($percent,$mode,$hidenastatus=0)
  900. {
  901. global $langs;
  902. if ($mode == 0)
  903. {
  904. if ($percent==-1 && ! $hidenastatus) return $langs->trans('StatusNotApplicable');
  905. else if ($percent==0) return $langs->trans('StatusActionToDo').' (0%)';
  906. else if ($percent > 0 && $percent < 100) return $langs->trans('StatusActionInProcess').' ('.$percent.'%)';
  907. else if ($percent >= 100) return $langs->trans('StatusActionDone').' (100%)';
  908. }
  909. else if ($mode == 1)
  910. {
  911. if ($percent==-1 && ! $hidenastatus) return $langs->trans('StatusNotApplicable');
  912. else if ($percent==0) return $langs->trans('StatusActionToDo');
  913. else if ($percent > 0 && $percent < 100) return $percent.'%';
  914. else if ($percent >= 100) return $langs->trans('StatusActionDone');
  915. }
  916. else if ($mode == 2)
  917. {
  918. if ($percent==-1 && ! $hidenastatus) return img_picto($langs->trans('StatusNotApplicable'),'statut9').' '.$langs->trans('StatusNotApplicable');
  919. else if ($percent==0) return img_picto($langs->trans('StatusActionToDo'),'statut1').' '.$langs->trans('StatusActionToDo');
  920. else if ($percent > 0 && $percent < 100) return img_picto($langs->trans('StatusActionInProcess'),'statut3').' '. $percent.'%';
  921. else if ($percent >= 100) return img_picto($langs->trans('StatusActionDone'),'statut6').' '.$langs->trans('StatusActionDone');
  922. }
  923. else if ($mode == 3)
  924. {
  925. if ($percent==-1 && ! $hidenastatus) return img_picto($langs->trans("Status").': '.$langs->trans('StatusNotApplicable'),'statut9');
  926. else if ($percent==0) return img_picto($langs->trans("Status").': '.$langs->trans('StatusActionToDo').' (0%)','statut1');
  927. else if ($percent > 0 && $percent < 100) return img_picto($langs->trans("Status").': '.$langs->trans('StatusActionInProcess').' ('.$percent.'%)','statut3');
  928. else if ($percent >= 100) return img_picto($langs->trans("Status").': '.$langs->trans('StatusActionDone').' (100%)','statut6');
  929. }
  930. else if ($mode == 4)
  931. {
  932. if ($percent==-1 && ! $hidenastatus) return img_picto($langs->trans('StatusNotApplicable'),'statut9').' '.$langs->trans('StatusNotApplicable');
  933. else if ($percent==0) return img_picto($langs->trans('StatusActionToDo'),'statut1').' '.$langs->trans('StatusActionToDo').' (0%)';
  934. else if ($percent > 0 && $percent < 100) return img_picto($langs->trans('StatusActionInProcess'),'statut3').' '.$langs->trans('StatusActionInProcess').' ('.$percent.'%)';
  935. else if ($percent >= 100) return img_picto($langs->trans('StatusActionDone'),'statut6').' '.$langs->trans('StatusActionDone').' (100%)';
  936. }
  937. else if ($mode == 5)
  938. {
  939. if ($percent==-1 && ! $hidenastatus) return img_picto($langs->trans('StatusNotApplicable'),'statut9');
  940. else if ($percent==0) return '0% '.img_picto($langs->trans('StatusActionToDo'),'statut1');
  941. else if ($percent > 0 && $percent < 100) return $percent.'% '.img_picto($langs->trans('StatusActionInProcess').' - '.$percent.'%','statut3');
  942. else if ($percent >= 100) return $langs->trans('StatusActionDone').' '.img_picto($langs->trans('StatusActionDone'),'statut6');
  943. }
  944. else if ($mode == 6)
  945. {
  946. if ($percent==-1 && ! $hidenastatus) return img_picto($langs->trans('StatusNotApplicable'),'statut9');
  947. else if ($percent==0) return '0% '.img_picto($langs->trans('StatusActionToDo'),'statut1');
  948. else if ($percent > 0 && $percent < 100) return $percent.'% '.img_picto($langs->trans('StatusActionInProcess').' - '.$percent.'%','statut3');
  949. else if ($percent >= 100) return img_picto($langs->trans('StatusActionDone'),'statut6');
  950. }
  951. return '';
  952. }
  953. /**
  954. * Return URL of event
  955. * Use $this->id, $this->type_code, $this->label and $this->type_label
  956. *
  957. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  958. * @param int $maxlength Max number of charaters into label. If negative, use the ref as label.
  959. * @param string $classname Force style class on a link
  960. * @param string $option ''=Link to action, 'birthday'=Link to contact
  961. * @param int $overwritepicto 1=Overwrite picto
  962. * @return string Chaine avec URL
  963. */
  964. function getNomUrl($withpicto=0,$maxlength=0,$classname='',$option='',$overwritepicto=0)
  965. {
  966. global $conf,$langs, $hookmanager;
  967. $result='';
  968. $tooltip = '<u>' . $langs->trans('ShowAction'.$objp->code) . '</u>';
  969. if (! empty($this->ref))
  970. $tooltip .= '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
  971. $label = $this->label;
  972. if (empty($label)) $label=$this->libelle; // For backward compatibility
  973. if (! empty($label))
  974. $tooltip .= '<br><b>' . $langs->trans('Title') . ':</b> ' . $label;
  975. if (! empty($this->location))
  976. $tooltip .= '<br><b>' . $langs->trans('Location') . ':</b> ' . $this->location;
  977. $linkclose = ' class="'.$classname.' classfortooltip" title="'.dol_escape_htmltag($tooltip, 1).'">';
  978. if (! is_object($hookmanager))
  979. {
  980. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  981. $hookmanager=new HookManager($this->db);
  982. }
  983. $hookmanager->initHooks(array('actiondao'));
  984. $parameters=array('id'=>$this->id);
  985. $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
  986. $linkclose = ($hookmanager->resPrint ? $hookmanager->resPrint : $linkclose);
  987. if ($option=='birthday')
  988. $link = '<a href="'.DOL_URL_ROOT.'/contact/perso.php?id='.$this->id.'"'.$linkclose;
  989. else
  990. $link = '<a href="'.DOL_URL_ROOT.'/comm/action/card.php?id='.$this->id.'"'.$linkclose;
  991. $linkend='</a>';
  992. //print 'rrr'.$this->libelle.'-'.$withpicto;
  993. if ($withpicto == 2)
  994. {
  995. $libelle=$label;
  996. if (! empty($conf->global->AGENDA_USE_EVENT_TYPE)) $libelle=$langs->transnoentities("Action".$this->type_code);
  997. $libelleshort='';
  998. }
  999. else
  1000. {
  1001. $libelle=(empty($this->libelle)?$label:$this->libelle.(($label && $label != $this->libelle)?' '.$label:''));
  1002. if (! empty($conf->global->AGENDA_USE_EVENT_TYPE) && empty($libelle)) $libelle=($langs->transnoentities("Action".$this->type_code) != "Action".$this->type_code)?$langs->transnoentities("Action".$this->type_code):$this->type_label;
  1003. if ($maxlength < 0) $libelleshort=$this->ref;
  1004. else $libelleshort=dol_trunc($libelle,$maxlength);
  1005. }
  1006. if ($withpicto)
  1007. {
  1008. if (! empty($conf->global->AGENDA_USE_EVENT_TYPE)) // Add code into ()
  1009. {
  1010. $libelle.=(($this->type_code && $libelle!=$langs->transnoentities("Action".$this->type_code) && $langs->transnoentities("Action".$this->type_code)!="Action".$this->type_code)?' ('.$langs->transnoentities("Action".$this->type_code).')':'');
  1011. }
  1012. $result.=$link.img_object($langs->trans("ShowAction").': '.$libelle, ($overwritepicto?$overwritepicto:'action'), 'class="classfortooltip"').$linkend;
  1013. }
  1014. if ($withpicto==1) $result.=' ';
  1015. $result.=$link.$libelleshort.$linkend;
  1016. return $result;
  1017. }
  1018. /**
  1019. * Export events from database into a cal file.
  1020. *
  1021. * @param string $format 'vcal', 'ical/ics', 'rss'
  1022. * @param string $type 'event' or 'journal'
  1023. * @param int $cachedelay Do not rebuild file if date older than cachedelay seconds
  1024. * @param string $filename Force filename
  1025. * @param array $filters Array of filters
  1026. * @return int <0 if error, nb of events in new file if ok
  1027. */
  1028. function build_exportfile($format,$type,$cachedelay,$filename,$filters)
  1029. {
  1030. global $conf,$langs,$dolibarr_main_url_root,$mysoc;
  1031. require_once (DOL_DOCUMENT_ROOT ."/core/lib/xcal.lib.php");
  1032. require_once (DOL_DOCUMENT_ROOT ."/core/lib/date.lib.php");
  1033. require_once (DOL_DOCUMENT_ROOT ."/core/lib/files.lib.php");
  1034. dol_syslog(get_class($this)."::build_exportfile Build export file format=".$format.", type=".$type.", cachedelay=".$cachedelay.", filename=".$filename.", filters size=".count($filters), LOG_DEBUG);
  1035. // Check parameters
  1036. if (empty($format)) return -1;
  1037. // Clean parameters
  1038. if (! $filename)
  1039. {
  1040. $extension='vcs';
  1041. if ($format == 'ical') $extension='ics';
  1042. $filename=$format.'.'.$extension;
  1043. }
  1044. // Create dir and define output file (definitive and temporary)
  1045. $result=dol_mkdir($conf->agenda->dir_temp);
  1046. $outputfile=$conf->agenda->dir_temp.'/'.$filename;
  1047. $result=0;
  1048. $buildfile=true;
  1049. $login='';$logina='';$logind='';$logint='';
  1050. $now = dol_now();
  1051. if ($cachedelay)
  1052. {
  1053. $nowgmt = dol_now();
  1054. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  1055. if (dol_filemtime($outputfile) > ($nowgmt - $cachedelay))
  1056. {
  1057. dol_syslog(get_class($this)."::build_exportfile file ".$outputfile." is not older than now - cachedelay (".$nowgmt." - ".$cachedelay."). Build is canceled");
  1058. $buildfile = false;
  1059. }
  1060. }
  1061. if ($buildfile)
  1062. {
  1063. // Build event array
  1064. $eventarray=array();
  1065. $sql = "SELECT a.id,";
  1066. $sql.= " a.datep,"; // Start
  1067. $sql.= " a.datep2,"; // End
  1068. $sql.= " a.durationp,"; // deprecated
  1069. $sql.= " a.datec, a.tms as datem,";
  1070. $sql.= " a.label, a.code, a.note, a.fk_action as type_id,";
  1071. $sql.= " a.fk_soc,";
  1072. $sql.= " a.fk_user_author, a.fk_user_mod,";
  1073. $sql.= " a.fk_user_action,";
  1074. $sql.= " a.fk_contact, a.percent as percentage,";
  1075. $sql.= " a.fk_element, a.elementtype,";
  1076. $sql.= " a.priority, a.fulldayevent, a.location, a.punctual, a.transparency,";
  1077. $sql.= " u.firstname, u.lastname,";
  1078. $sql.= " s.nom as socname,";
  1079. $sql.= " c.id as type_id, c.code as type_code, c.libelle";
  1080. $sql.= " FROM (".MAIN_DB_PREFIX."c_actioncomm as c, ".MAIN_DB_PREFIX."actioncomm as a)";
  1081. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on u.rowid = a.fk_user_author"; // Link to get author of event for export
  1082. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on s.rowid = a.fk_soc";
  1083. // We must filter on assignement table
  1084. if ($filters['logint'] || $filters['login']) $sql.=", ".MAIN_DB_PREFIX."actioncomm_resources as ar";
  1085. $sql.= " WHERE a.fk_action=c.id";
  1086. $sql.= " AND a.entity IN (".getEntity('actioncomm', 1).")";
  1087. foreach ($filters as $key => $value)
  1088. {
  1089. if ($key == 'notolderthan' && $value != '') $sql.=" AND a.datep >= '".$this->db->idate($now-($value*24*60*60))."'";
  1090. 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))."'";
  1091. if ($key == 'id') $sql.=" AND a.id=".(is_numeric($value)?$value:0);
  1092. if ($key == 'idfrom') $sql.=" AND a.id >= ".(is_numeric($value)?$value:0);
  1093. if ($key == 'idto') $sql.=" AND a.id <= ".(is_numeric($value)?$value:0);
  1094. if ($key == 'project') $sql.=" AND a.fk_project=".(is_numeric($value)?$value:0);
  1095. // We must filter on assignement table
  1096. if ($key == 'logint' || $key == 'login') $sql.= " AND ar.fk_actioncomm = a.id AND ar.element_type='user'";
  1097. if ($key == 'logina')
  1098. {
  1099. $logina=$value;
  1100. $userforfilter=new User($this->db);
  1101. $result=$userforfilter->fetch('',$value);
  1102. $sql.= " AND a.fk_user_author = ".$userforfilter->id;
  1103. }
  1104. if ($key == 'logint' || $key == 'login')
  1105. {
  1106. $logint=$value;
  1107. $userforfilter=new User($this->db);
  1108. $result=$userforfilter->fetch('',$value);
  1109. $sql.= " AND ar.fk_element = ".$userforfilter->id;
  1110. }
  1111. }
  1112. $sql.= " AND a.datep IS NOT NULL"; // To exclude corrupted events and avoid errors in lightning/sunbird import
  1113. $sql.= " ORDER by datep";
  1114. //print $sql;exit;
  1115. dol_syslog(get_class($this)."::build_exportfile select events", LOG_DEBUG);
  1116. $resql=$this->db->query($sql);
  1117. if ($resql)
  1118. {
  1119. // Note: Output of sql request is encoded in $conf->file->character_set_client
  1120. // This assignment in condition is not a bug. It allows walking the results.
  1121. while ($obj=$this->db->fetch_object($resql))
  1122. {
  1123. $qualified=true;
  1124. // 'eid','startdate','duration','enddate','title','summary','category','email','url','desc','author'
  1125. $event=array();
  1126. $event['uid']='dolibarragenda-'.$this->db->database_name.'-'.$obj->id."@".$_SERVER["SERVER_NAME"];
  1127. $event['type']=$type;
  1128. $datestart=$this->db->jdate($obj->datep)-(empty($conf->global->AGENDA_EXPORT_FIX_TZ)?0:($conf->global->AGENDA_EXPORT_FIX_TZ*3600));
  1129. $dateend=$this->db->jdate($obj->datep2)-(empty($conf->global->AGENDA_EXPORT_FIX_TZ)?0:($conf->global->AGENDA_EXPORT_FIX_TZ*3600));
  1130. $duration=($datestart && $dateend)?($dateend - $datestart):0;
  1131. $event['summary']=$obj->label.($obj->socname?" (".$obj->socname.")":"");
  1132. $event['desc']=$obj->note;
  1133. $event['startdate']=$datestart;
  1134. $event['enddate']=$dateend; // Not required with type 'journal'
  1135. $event['duration']=$duration; // Not required with type 'journal'
  1136. $event['author']=dolGetFirstLastname($obj->firstname, $obj->lastname);
  1137. $event['priority']=$obj->priority;
  1138. $event['fulldayevent']=$obj->fulldayevent;
  1139. $event['location']=$obj->location;
  1140. $event['transparency']=(($obj->transparency > 0)?'OPAQUE':'TRANSPARENT'); // OPAQUE (busy) or TRANSPARENT (not busy)
  1141. $event['punctual']=$obj->punctual;
  1142. $event['category']=$obj->libelle; // libelle type action
  1143. // Define $urlwithroot
  1144. $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
  1145. $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  1146. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  1147. $url=$urlwithroot.'/comm/action/card.php?id='.$obj->id;
  1148. $event['url']=$url;
  1149. $event['created']=$this->db->jdate($obj->datec)-(empty($conf->global->AGENDA_EXPORT_FIX_TZ)?0:($conf->global->AGENDA_EXPORT_FIX_TZ*3600));
  1150. $event['modified']=$this->db->jdate($obj->datem)-(empty($conf->global->AGENDA_EXPORT_FIX_TZ)?0:($conf->global->AGENDA_EXPORT_FIX_TZ*3600));
  1151. if ($qualified && $datestart)
  1152. {
  1153. $eventarray[$datestart]=$event;
  1154. }
  1155. }
  1156. }
  1157. else
  1158. {
  1159. $this->error=$this->db->lasterror();
  1160. return -1;
  1161. }
  1162. $langs->load("agenda");
  1163. // Define title and desc
  1164. $more='';
  1165. if ($login) $more=$langs->transnoentities("User").' '.$login;
  1166. if ($logina) $more=$langs->transnoentities("ActionsAskedBy").' '.$logina;
  1167. if ($logint) $more=$langs->transnoentities("ActionsToDoBy").' '.$logint;
  1168. if ($logind) $more=$langs->transnoentities("ActionsDoneBy").' '.$logind;
  1169. if ($more)
  1170. {
  1171. $title='Dolibarr actions '.$mysoc->name.' - '.$more;
  1172. $desc=$more;
  1173. $desc.=' ('.$mysoc->name.' - built by Dolibarr)';
  1174. }
  1175. else
  1176. {
  1177. $title='Dolibarr actions '.$mysoc->name;
  1178. $desc=$langs->transnoentities('ListOfActions');
  1179. $desc.=' ('.$mysoc->name.' - built by Dolibarr)';
  1180. }
  1181. // Create temp file
  1182. $outputfiletmp=tempnam($conf->agenda->dir_temp,'tmp'); // Temporary file (allow call of function by different threads
  1183. @chmod($outputfiletmp, octdec($conf->global->MAIN_UMASK));
  1184. // Write file
  1185. if ($format == 'vcal') $result=build_calfile($format,$title,$desc,$eventarray,$outputfiletmp);
  1186. if ($format == 'ical') $result=build_calfile($format,$title,$desc,$eventarray,$outputfiletmp);
  1187. if ($format == 'rss') $result=build_rssfile($format,$title,$desc,$eventarray,$outputfiletmp);
  1188. if ($result >= 0)
  1189. {
  1190. if (dol_move($outputfiletmp,$outputfile,0,1)) $result=1;
  1191. else
  1192. {
  1193. $this->error='Failed to rename '.$outputfiletmp.' into '.$outputfile;
  1194. dol_syslog(get_class($this)."::build_exportfile ".$this->error, LOG_ERR);
  1195. dol_delete_file($outputfiletmp,0,1);
  1196. $result=-1;
  1197. }
  1198. }
  1199. else
  1200. {
  1201. dol_syslog(get_class($this)."::build_exportfile build_xxxfile function fails to for format=".$format." outputfiletmp=".$outputfile, LOG_ERR);
  1202. dol_delete_file($outputfiletmp,0,1);
  1203. $langs->load("errors");
  1204. $this->error=$langs->trans("ErrorFailToCreateFile",$outputfile);
  1205. }
  1206. }
  1207. return $result;
  1208. }
  1209. /**
  1210. * Initialise an instance with random values.
  1211. * Used to build previews or test instances.
  1212. * id must be 0 if object instance is a specimen.
  1213. *
  1214. * @return void
  1215. */
  1216. function initAsSpecimen()
  1217. {
  1218. global $user;
  1219. $now=dol_now();
  1220. // Initialise parametres
  1221. $this->id=0;
  1222. $this->specimen=1;
  1223. $this->type_code='AC_OTH';
  1224. $this->code='AC_SPECIMEN_CODE';
  1225. $this->label='Label of event Specimen';
  1226. $this->datec=$now;
  1227. $this->datem=$now;
  1228. $this->datep=$now;
  1229. $this->datef=$now;
  1230. $this->author=$user;
  1231. $this->usermod=$user;
  1232. $this->usertodo=$user;
  1233. $this->fulldayevent=0;
  1234. $this->punctual=0;
  1235. $this->percentage=0;
  1236. $this->location='Location';
  1237. $this->transparency=1; // 1 means opaque
  1238. $this->priority=1;
  1239. $this->note = 'Note';
  1240. $this->userownerid=$user->id;
  1241. $this->userassigned[$user->id]=array('id'=>$user->id, 'transparency'=> 1);
  1242. }
  1243. /**
  1244. * Function used to replace a thirdparty id with another one.
  1245. *
  1246. * @param DoliDB $db Database handler
  1247. * @param int $origin_id Old thirdparty id
  1248. * @param int $dest_id New thirdparty id
  1249. * @return bool
  1250. */
  1251. public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
  1252. {
  1253. $tables = array(
  1254. 'actioncomm'
  1255. );
  1256. return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
  1257. }
  1258. /**
  1259. * Is the action delayed?
  1260. *
  1261. * @return bool
  1262. */
  1263. public function hasDelay()
  1264. {
  1265. global $conf;
  1266. $now = dol_now();
  1267. return $this->datep && ($this->datep < ($now - $conf->agenda->warning_delay));
  1268. }
  1269. }