actioncomm.class.php 67 KB

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