actioncomm.class.php 68 KB

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