ticket.class.php 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603
  1. <?php
  2. /* Copyright (C) 2013-2018 Jean-François Ferry <hello@librethic.io>
  3. * Copyright (C) 2016 Christophe Battarel <christophe@altairis.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file ticket/class/ticket.class.php
  20. * \ingroup ticket
  21. * \brief Class file for object ticket
  22. */
  23. // Put here all includes required by your class file
  24. require_once DOL_DOCUMENT_ROOT . "/core/class/commonobject.class.php";
  25. require_once DOL_DOCUMENT_ROOT . '/fichinter/class/fichinter.class.php';
  26. //require_once DOL_DOCUMENT_ROOT."/societe/class/societe.class.php";
  27. //require_once DOL_DOCUMENT_ROOT."/product/class/product.class.php";
  28. /**
  29. * Class to manage ticket
  30. */
  31. class Ticket extends CommonObject
  32. {
  33. /**
  34. * @var string ID to identify managed object
  35. */
  36. public $element = 'ticket';
  37. /**
  38. * @var string Name of table without prefix where object is stored
  39. */
  40. public $table_element = 'ticket';
  41. /**
  42. * @var string Name of field for link to tickets
  43. */
  44. public $fk_element='fk_ticket';
  45. /**
  46. * @var int Does ticketcore support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  47. */
  48. public $ismultientitymanaged = 1;
  49. /**
  50. * @var int Does ticketcore support extrafields ? 0=No, 1=Yes
  51. */
  52. public $isextrafieldmanaged = 1;
  53. /**
  54. * @var string String with name of icon for ticketcore. Must be the part after the 'object_' into object_ticketcore.png
  55. */
  56. public $picto = 'ticket';
  57. /**
  58. * @var string Hash to identify ticket
  59. */
  60. public $track_id;
  61. /**
  62. * @var int Thirdparty ID
  63. */
  64. public $fk_soc;
  65. /**
  66. * @var int Project ID
  67. */
  68. public $fk_project;
  69. /**
  70. * @var string Person email who have create ticket
  71. */
  72. public $origin_email;
  73. /**
  74. * @var int User id who have create ticket
  75. */
  76. public $fk_user_create;
  77. /**
  78. * @var int User id who have ticket assigned
  79. */
  80. public $fk_user_assign;
  81. /**
  82. * var string Ticket subject
  83. */
  84. public $subject;
  85. /**
  86. * @var string Ticket message
  87. */
  88. public $message;
  89. /**
  90. * @var int Ticket statut
  91. */
  92. public $fk_statut;
  93. /**
  94. * @var string State resolution
  95. */
  96. public $resolution;
  97. /**
  98. * @var int Progress in percent
  99. */
  100. public $progress;
  101. /**
  102. * @var int Duration for ticket
  103. */
  104. public $timing;
  105. /**
  106. * @var string Type code
  107. */
  108. public $type_code;
  109. /**
  110. * @var string Category code
  111. */
  112. public $category_code;
  113. /**
  114. * @var string Severity code
  115. */
  116. public $severity_code;
  117. /**
  118. * @var int Création date
  119. */
  120. public $datec = '';
  121. /**
  122. * @var int Read date
  123. */
  124. public $date_read = '';
  125. /**
  126. * @var int Close ticket date
  127. */
  128. public $date_close = '';
  129. /**
  130. * @var array cache_types_tickets
  131. */
  132. public $cache_types_tickets;
  133. /**
  134. * @var array tickets categories
  135. */
  136. public $cache_category_tickets;
  137. /**
  138. * @var int Notify tiers at create
  139. */
  140. public $notify_tiers_at_create;
  141. public $lines;
  142. /**
  143. * @var string Regex pour les images
  144. */
  145. public $regeximgext = '\.jpg|\.jpeg|\.bmp|\.gif|\.png|\.tiff';
  146. public $fields=array(
  147. 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'position'=>1, 'visible'=>-2, 'enabled'=>1, 'position'=>1, 'notnull'=>1, 'index'=>1, 'comment'=>"Id"),
  148. 'entity' => array('type'=>'integer', 'label'=>'Entity', 'visible'=>0, 'enabled'=>1, 'position'=>5, 'notnull'=>1, 'index'=>1),
  149. 'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'visible'=>1, 'enabled'=>1, 'position'=>10, 'notnull'=>1, 'index'=>1, 'searchall'=>1, 'comment'=>"Reference of object", 'css'=>'aaa'),
  150. 'track_id' => array('type'=>'varchar(255)', 'label'=>'TrackID', 'visible'=>0, 'enabled'=>1, 'position'=>11, 'notnull'=>-1, 'searchall'=>1, 'help'=>"Help text"),
  151. 'fk_user_create' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Author', 'visible'=>1, 'enabled'=>1, 'position'=>15, 'notnull'=>1),
  152. 'origin_email' => array('type'=>'mail', 'label'=>'OriginEmail', 'visible'=>1, 'enabled'=>1, 'position'=>16, 'notnull'=>1, 'index'=>1, 'searchall'=>1, 'comment'=>"Reference of object"),
  153. 'subject' => array('type'=>'varchar(255)', 'label'=>'Subject', 'visible'=>1, 'enabled'=>1, 'position'=>18, 'notnull'=>-1, 'searchall'=>1, 'help'=>""),
  154. 'type_code' => array('type'=>'varchar(32)', 'label'=>'Type', 'visible'=>1, 'enabled'=>1, 'position'=>20, 'notnull'=>-1, 'searchall'=>1, 'help'=>"", 'css'=>'maxwidth100'),
  155. 'category_code' => array('type'=>'varchar(32)', 'label'=>'Category', 'visible'=>1, 'enabled'=>1, 'position'=>21, 'notnull'=>-1, 'searchall'=>1, 'help'=>"", 'css'=>'maxwidth100'),
  156. 'severity_code' => array('type'=>'varchar(32)', 'label'=>'Severity', 'visible'=>1, 'enabled'=>1, 'position'=>22, 'notnull'=>-1, 'searchall'=>1, 'help'=>"", 'css'=>'maxwidth100'),
  157. 'fk_soc' => array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'visible'=>1, 'enabled'=>1, 'position'=>50, 'notnull'=>-1, 'index'=>1, 'searchall'=>1, 'help'=>"LinkToThirparty"),
  158. 'notify_tiers_at_create' => array('type'=>'integer', 'label'=>'NotifyThirdparty', 'visible'=>-2, 'enabled'=>0, 'position'=>51, 'notnull'=>1, 'index'=>1),
  159. 'fk_project' => array('type'=>'integer:Project:projet/class/project.class.php', 'label'=>'Project', 'visible'=>1, 'enabled'=>1, 'position'=>52, 'notnull'=>-1, 'index'=>1, 'searchall'=>1, 'help'=>"LinkToProject"),
  160. 'fk_user_assign' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'AssignedTo', 'visible'=>1, 'enabled'=>1, 'position'=>510, 'notnull'=>1),
  161. 'message' => array('type'=>'text', 'label'=>'Message', 'visible'=>-2, 'enabled'=>1, 'position'=>60, 'notnull'=>-1,),
  162. 'progress' => array('type'=>'varchar(100)', 'label'=>'Progression', 'visible'=>1, 'enabled'=>1, 'position'=>41, 'notnull'=>-1, 'searchall'=>1, 'css'=>'right', 'help'=>""),
  163. 'timing' => array('type'=>'varchar(20)', 'label'=>'Timing', 'visible'=>-1, 'enabled'=>1, 'position'=>42, 'notnull'=>-1, 'searchall'=>1, 'help'=>""),
  164. 'datec' => array('type'=>'datetime', 'label'=>'DateCreation', 'visible'=>-2, 'enabled'=>1, 'position'=>500, 'notnull'=>1),
  165. 'date_read' => array('type'=>'datetime', 'label'=>'TicketReadOn', 'visible'=>-2, 'enabled'=>1, 'position'=>500, 'notnull'=>1),
  166. 'date_close' => array('type'=>'datetime', 'label'=>'TicketCloseOn', 'visible'=>-2, 'enabled'=>1, 'position'=>500, 'notnull'=>1),
  167. 'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'visible'=>-2, 'enabled'=>1, 'position'=>501, 'notnull'=>1),
  168. 'resolution' => array('type'=>'integer', 'label'=>'Resolution', 'visible'=>-2, 'enabled'=>1, 'position'=>550, 'notnull'=>1),
  169. 'fk_statut' => array('type'=>'integer', 'label'=>'Status', 'visible'=>1, 'enabled'=>1, 'position'=>600, 'notnull'=>1, 'index'=>1, 'arrayofkeyval'=>array(0 => 'Unread', 1 => 'Read', 3 => 'Answered', 4 => 'Assigned', 5 => 'InProgress', 6 => 'Waiting', 8 => 'Closed', 9 => 'Deleted'))
  170. );
  171. /**
  172. * Status
  173. */
  174. const STATUS_NOT_READ = 0;
  175. const STATUS_READ = 1;
  176. const STATUS_ANSWERED = 3;
  177. const STATUS_ASSIGNED = 4;
  178. const STATUS_IN_PROGRESS = 5;
  179. const STATUS_WAITING = 6;
  180. const STATUS_CLOSED = 8;
  181. const STATUS_CANCELED = 9;
  182. /**
  183. * Constructor
  184. *
  185. * @param DoliDb $db Database handler
  186. */
  187. public function __construct($db)
  188. {
  189. $this->db = $db;
  190. $this->statuts_short = array(0 => 'Unread', 1 => 'Read', 3 => 'Answered', 4 => 'Assigned', 5 => 'InProgress', 6 => 'Waiting', 8 => 'Closed', 9 => 'Deleted');
  191. $this->statuts = array(0 => 'Unread', 1 => 'Read', 3 => 'Answered', 4 => 'Assigned', 5 => 'InProgress', 6 => 'Waiting', 8 => 'Closed', 9 => 'Deleted');
  192. }
  193. /**
  194. * Check properties of ticket are ok (like ref, track_id, ...).
  195. * All properties must be already loaded on object (this->ref, this->track_id, ...).
  196. *
  197. * @return int 0 if OK, <0 if KO
  198. */
  199. private function verify()
  200. {
  201. $this->errors = array();
  202. $result = 0;
  203. // Clean parameters
  204. if (isset($this->ref)) {
  205. $this->ref = trim($this->ref);
  206. }
  207. if (isset($this->track_id)) {
  208. $this->track_id = trim($this->track_id);
  209. }
  210. if (isset($this->fk_soc)) {
  211. $this->fk_soc = trim($this->fk_soc);
  212. }
  213. if (isset($this->fk_project)) {
  214. $this->fk_project = trim($this->fk_project);
  215. }
  216. if (isset($this->origin_email)) {
  217. $this->origin_email = trim($this->origin_email);
  218. }
  219. if (isset($this->fk_user_create)) {
  220. $this->fk_user_create = trim($this->fk_user_create);
  221. }
  222. if (isset($this->fk_user_assign)) {
  223. $this->fk_user_assign = trim($this->fk_user_assign);
  224. }
  225. if (isset($this->subject)) {
  226. $this->subject = trim($this->subject);
  227. }
  228. if (isset($this->message)) {
  229. $this->message = trim($this->message);
  230. }
  231. if (isset($this->fk_statut)) {
  232. $this->fk_statut = trim($this->fk_statut);
  233. }
  234. if (isset($this->resolution)) {
  235. $this->resolution = trim($this->resolution);
  236. }
  237. if (isset($this->progress)) {
  238. $this->progress = trim($this->progress);
  239. }
  240. if (isset($this->timing)) {
  241. $this->timing = trim($this->timing);
  242. }
  243. if (isset($this->type_code)) {
  244. $this->type_code = trim($this->type_code);
  245. }
  246. if (isset($this->category_code)) {
  247. $this->category_code = trim($this->category_code);
  248. }
  249. if (isset($this->severity_code)) {
  250. $this->severity_code = trim($this->severity_code);
  251. }
  252. if (empty($this->ref)) {
  253. $this->errors[] = 'ErrorBadRef';
  254. dol_syslog(get_class($this) . "::create error -1 ref null", LOG_ERR);
  255. $result = -1;
  256. }
  257. return $result;
  258. }
  259. /**
  260. * Create object into database
  261. *
  262. * @param User $user User that creates
  263. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  264. * @return int <0 if KO, Id of created object if OK
  265. */
  266. public function create($user, $notrigger = 0)
  267. {
  268. global $conf, $langs;
  269. $error = 0;
  270. $this->datec = dol_now();
  271. // Check more parameters
  272. // If error, this->errors[] is filled
  273. $result = $this->verify();
  274. if ($result >= 0) {
  275. // Insert request
  276. $sql = "INSERT INTO " . MAIN_DB_PREFIX . "ticket(";
  277. $sql .= "ref,";
  278. $sql .= "track_id,";
  279. $sql .= "fk_soc,";
  280. $sql .= "fk_project,";
  281. $sql .= "origin_email,";
  282. $sql .= "fk_user_create,";
  283. $sql .= "fk_user_assign,";
  284. $sql .= "subject,";
  285. $sql .= "message,";
  286. $sql .= "fk_statut,";
  287. $sql .= "resolution,";
  288. $sql .= "progress,";
  289. $sql .= "timing,";
  290. $sql .= "type_code,";
  291. $sql .= "category_code,";
  292. $sql .= "severity_code,";
  293. $sql .= "datec,";
  294. $sql .= "date_read,";
  295. $sql .= "date_close,";
  296. $sql .= "entity,";
  297. $sql .= "notify_tiers_at_create";
  298. $sql .= ") VALUES (";
  299. $sql .= " " . (!isset($this->ref) ? '' : "'" . $this->db->escape($this->ref) . "'") . ",";
  300. $sql .= " " . (!isset($this->track_id) ? 'NULL' : "'" . $this->db->escape($this->track_id) . "'") . ",";
  301. $sql .= " " . ($this->fk_soc > 0 ? $this->db->escape($this->fk_soc) : "null") . ",";
  302. $sql .= " " . ($this->fk_project > 0 ? $this->db->escape($this->fk_project) : "null") . ",";
  303. $sql .= " " . (!isset($this->origin_email) ? 'NULL' : "'" . $this->db->escape($this->origin_email) . "'") . ",";
  304. $sql .= " " . ($this->fk_user_create > 0 ? $this->fk_user_create : ($user->id > 0 ? $user->id : 'NULL')) . ",";
  305. $sql .= " " . ($this->fk_user_assign > 0 ? $this->fk_user_assign : 'NULL') . ",";
  306. $sql .= " " . (!isset($this->subject) ? 'NULL' : "'" . $this->db->escape($this->subject) . "'") . ",";
  307. $sql .= " " . (!isset($this->message) ? 'NULL' : "'" . $this->db->escape($this->message) . "'") . ",";
  308. $sql .= " " . (!isset($this->fk_statut) ? '0' : "'" . $this->db->escape($this->fk_statut) . "'") . ",";
  309. $sql .= " " . (!isset($this->resolution) ? 'NULL' : "'" . $this->db->escape($this->resolution) . "'") . ",";
  310. $sql .= " " . (!isset($this->progress) ? '0' : "'" . $this->db->escape($this->progress) . "'") . ",";
  311. $sql .= " " . (!isset($this->timing) ? 'NULL' : "'" . $this->db->escape($this->timing) . "'") . ",";
  312. $sql .= " " . (!isset($this->type_code) ? 'NULL' : "'" . $this->db->escape($this->type_code) . "'") . ",";
  313. $sql .= " " . (!isset($this->category_code) ? 'NULL' : "'" . $this->db->escape($this->category_code) . "'") . ",";
  314. $sql .= " " . (!isset($this->severity_code) ? 'NULL' : "'" . $this->db->escape($this->severity_code) . "'") . ",";
  315. $sql .= " " . (!isset($this->datec) || dol_strlen($this->datec) == 0 ? 'NULL' : "'" . $this->db->idate($this->datec) . "'") . ",";
  316. $sql .= " " . (!isset($this->date_read) || dol_strlen($this->date_read) == 0 ? 'NULL' : "'" . $this->db->idate($this->date_read) . "'") . ",";
  317. $sql .= " " . (!isset($this->date_close) || dol_strlen($this->date_close) == 0 ? 'NULL' : "'" . $this->db->idate($this->date_close) . "'") . "";
  318. $sql .= ", " . $conf->entity;
  319. $sql .= ", " . (!isset($this->notify_tiers_at_create) ? '1' : "'" . $this->db->escape($this->notify_tiers_at_create) . "'");
  320. $sql .= ")";
  321. $this->db->begin();
  322. dol_syslog(get_class($this) . "::create sql=" . $sql, LOG_DEBUG);
  323. $resql = $this->db->query($sql);
  324. if (!$resql) {
  325. $error++;
  326. $this->errors[] = "Error " . $this->db->lasterror();
  327. }
  328. if (!$error) {
  329. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "ticket");
  330. if (!$notrigger) {
  331. // Call trigger
  332. $result=$this->call_trigger('TICKET_CREATE', $user);
  333. if ($result < 0) {
  334. $error++;
  335. }
  336. // End call triggers
  337. }
  338. }
  339. //Update extrafield
  340. if (! $error) {
  341. if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) { // For avoid conflicts if trigger used
  342. $result = $this->insertExtraFields();
  343. if ($result < 0) {
  344. $error++;
  345. }
  346. }
  347. }
  348. // Commit or rollback
  349. if ($error) {
  350. foreach ($this->errors as $errmsg) {
  351. dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR);
  352. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  353. }
  354. $this->db->rollback();
  355. return -1 * $error;
  356. } else {
  357. $this->db->commit();
  358. return $this->id;
  359. }
  360. } else {
  361. $this->db->rollback();
  362. dol_syslog(get_class($this) . "::Create fails verify " . join(',', $this->errors), LOG_WARNING);
  363. return -3;
  364. }
  365. }
  366. /**
  367. * Load object in memory from the database
  368. *
  369. * @param int $id Id object
  370. * @param string $ref Ref
  371. * @param string $track_id Track id, a hash like ref
  372. * @return int <0 if KO, >0 if OK
  373. */
  374. public function fetch($id = '', $ref = '', $track_id = '')
  375. {
  376. global $langs;
  377. // Check parameters
  378. if (! $id && ! $track_id && ! $ref) {
  379. $this->error = 'ErrorWrongParameters';
  380. dol_print_error(get_class($this) . "::fetch " . $this->error);
  381. return -1;
  382. }
  383. $sql = "SELECT";
  384. $sql .= " t.rowid,";
  385. $sql .= " t.ref,";
  386. $sql .= " t.track_id,";
  387. $sql .= " t.fk_soc,";
  388. $sql .= " t.fk_project,";
  389. $sql .= " t.origin_email,";
  390. $sql .= " t.fk_user_create,";
  391. $sql .= " t.fk_user_assign,";
  392. $sql .= " t.subject,";
  393. $sql .= " t.message,";
  394. $sql .= " t.fk_statut,";
  395. $sql .= " t.resolution,";
  396. $sql .= " t.progress,";
  397. $sql .= " t.timing,";
  398. $sql .= " t.type_code,";
  399. $sql .= " t.category_code,";
  400. $sql .= " t.severity_code,";
  401. $sql .= " t.datec,";
  402. $sql .= " t.date_read,";
  403. $sql .= " t.date_close,";
  404. $sql .= " t.tms";
  405. $sql .= ", type.code as type_code, type.label as type_label, category.code as category_code, category.label as category_label, severity.code as severity_code, severity.label as severity_label";
  406. $sql .= " FROM " . MAIN_DB_PREFIX . "ticket as t";
  407. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_ticket_type as type ON type.code=t.type_code";
  408. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_ticket_category as category ON category.code=t.category_code";
  409. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_ticket_severity as severity ON severity.code=t.severity_code";
  410. if ($id) {
  411. $sql .= " WHERE t.rowid = " . $this->db->escape($id);
  412. } else {
  413. $sql .= " WHERE t.entity IN (" . getEntity($this->element, 1) . ")";
  414. if ($track_id) {
  415. $sql .= " AND t.track_id = '" . $this->db->escape($track_id) . "'";
  416. } elseif ($ref) {
  417. $sql .= " AND t.ref = '" . $this->db->escape($ref) . "'";
  418. }
  419. }
  420. dol_syslog(get_class($this) . "::fetch sql=" . $sql, LOG_DEBUG);
  421. $resql = $this->db->query($sql);
  422. if ($resql) {
  423. if ($this->db->num_rows($resql))
  424. {
  425. $obj = $this->db->fetch_object($resql);
  426. $this->id = $obj->rowid;
  427. $this->ref = $obj->ref;
  428. $this->track_id = $obj->track_id;
  429. $this->fk_soc = $obj->fk_soc;
  430. $this->socid = $obj->fk_soc; // for fetch_thirdparty() method
  431. $this->fk_project = $obj->fk_project;
  432. $this->origin_email = $obj->origin_email;
  433. $this->fk_user_create = $obj->fk_user_create;
  434. $this->fk_user_assign = $obj->fk_user_assign;
  435. $this->subject = $obj->subject;
  436. $this->message = $obj->message;
  437. $this->fk_statut = $obj->fk_statut;
  438. $this->resolution = $obj->resolution;
  439. $this->progress = $obj->progress;
  440. $this->timing = $obj->timing;
  441. $this->type_code = $obj->type_code;
  442. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  443. $label_type = ($langs->trans("TicketTypeShort" . $obj->type_code) != ("TicketTypeShort" . $obj->type_code) ? $langs->trans("TicketTypeShort" . $obj->type_code) : ($obj->type_label != '-' ? $obj->type_label : ''));
  444. $this->type_label = $label_type;
  445. $this->category_code = $obj->category_code;
  446. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  447. $label_category = ($langs->trans("TicketCategoryShort" . $obj->category_code) != ("TicketCategoryShort" . $obj->category_code) ? $langs->trans("TicketCategoryShort" . $obj->category_code) : ($obj->category_label != '-' ? $obj->category_label : ''));
  448. $this->category_label = $label_category;
  449. $this->severity_code = $obj->severity_code;
  450. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  451. $label_severity = ($langs->trans("TicketSeverityShort" . $obj->severity_code) != ("TicketSeverityShort" . $obj->severity_code) ? $langs->trans("TicketSeverityShort" . $obj->severity_code) : ($obj->severity_label != '-' ? $obj->severity_label : ''));
  452. $this->severity_label = $label_severity;
  453. $this->datec = $this->db->jdate($obj->datec);
  454. $this->date_read = $this->db->jdate($obj->date_read);
  455. $this->date_close = $this->db->jdate($obj->date_close);
  456. $this->tms = $this->db->jdate($obj->tms);
  457. $this->fetch_optionals();
  458. $this->db->free($resql);
  459. return 1;
  460. }
  461. else
  462. {
  463. return 0;
  464. }
  465. } else {
  466. $this->error = "Error " . $this->db->lasterror();
  467. dol_syslog(get_class($this) . "::fetch " . $this->error, LOG_ERR);
  468. return -1;
  469. }
  470. }
  471. /**
  472. * Load all objects in memory from database
  473. *
  474. * @param User $user User for action
  475. * @param string $sortorder Sort order
  476. * @param string $sortfield Sort field
  477. * @param int $limit page number
  478. * @param int $offset Offset for query
  479. * @param int $arch archive or not (not used)
  480. * @param array $filter Filter for query
  481. * output
  482. * @return int <0 if KO, >0 if OK
  483. */
  484. public function fetchAll($user, $sortorder = 'ASC', $sortfield = 't.datec', $limit = '', $offset = 0, $arch = '', $filter = '')
  485. {
  486. global $langs;
  487. $extrafields = new ExtraFields($this->db);
  488. // fetch optionals attributes and labels
  489. $extralabels = $extrafields->fetch_name_optionals_label($this->element);
  490. $sql = "SELECT";
  491. $sql .= " t.rowid,";
  492. $sql .= " t.ref,";
  493. $sql .= " t.track_id,";
  494. $sql .= " t.fk_soc,";
  495. $sql .= " t.fk_project,";
  496. $sql .= " t.origin_email,";
  497. $sql .= " t.fk_user_create, uc.lastname as user_create_lastname, uc.firstname as user_create_firstname,";
  498. $sql .= " t.fk_user_assign, ua.lastname as user_assign_lastname, ua.firstname as user_assign_firstname,";
  499. $sql .= " t.subject,";
  500. $sql .= " t.message,";
  501. $sql .= " t.fk_statut,";
  502. $sql .= " t.resolution,";
  503. $sql .= " t.progress,";
  504. $sql .= " t.timing,";
  505. $sql .= " t.type_code,";
  506. $sql .= " t.category_code,";
  507. $sql .= " t.severity_code,";
  508. $sql .= " t.datec,";
  509. $sql .= " t.date_read,";
  510. $sql .= " t.date_close,";
  511. $sql .= " t.tms";
  512. $sql .= ", type.label as type_label, category.label as category_label, severity.label as severity_label";
  513. // Add fields for extrafields
  514. foreach ($extrafields->attributes[$this->table_element]['label'] as $key => $val) {
  515. $sql .= ($extrafields->attributes[$this->table_element]['type'][$key] != 'separate' ? ",ef." . $key . ' as options_' . $key : '');
  516. }
  517. $sql .= " FROM " . MAIN_DB_PREFIX . "ticket as t";
  518. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_ticket_type as type ON type.code=t.type_code";
  519. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_ticket_category as category ON category.code=t.category_code";
  520. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_ticket_severity as severity ON severity.code=t.severity_code";
  521. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe as s ON s.rowid=t.fk_soc";
  522. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "user as uc ON uc.rowid=t.fk_user_create";
  523. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "user as ua ON ua.rowid=t.fk_user_assign";
  524. if (is_array($extrafields->attributes[$this->table_element]['label']) && count($extrafields->attributes[$this->table_element]['label'])) {
  525. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "ticket_extrafields as ef on (t.rowid = ef.fk_object)";
  526. }
  527. if (!$user->rights->societe->client->voir && !$user->socid) {
  528. $sql .= ", " . MAIN_DB_PREFIX . "societe_commerciaux as sc";
  529. }
  530. $sql .= " WHERE t.entity IN (" . getEntity('ticket') . ")";
  531. // Manage filter
  532. if (!empty($filter)) {
  533. foreach ($filter as $key => $value) {
  534. if (strpos($key, 'date')) { // To allow $filter['YEAR(s.dated)']=>$year
  535. $sql .= ' AND ' . $key . ' = \'' . $value . '\'';
  536. } elseif (($key == 't.fk_user_assign') || ($key == 't.type_code') || ($key == 't.category_code') || ($key == 't.severity_code') || ($key == 't.fk_soc')) {
  537. $sql .= " AND " . $key . " = '" . $this->db->escape($value) ."'";
  538. } elseif ($key == 't.fk_statut') {
  539. if (is_array($value) && count($value) > 0) {
  540. $sql .= 'AND ' . $key . ' IN (' . implode(',', $value) . ')';
  541. } else {
  542. $sql .= ' AND ' . $key . ' = ' . $this->db->escape($value);
  543. }
  544. } else {
  545. $sql .= ' AND ' . $key . ' LIKE \'%' . $value . '%\'';
  546. }
  547. }
  548. }
  549. if (!$user->rights->societe->client->voir && !$user->socid) {
  550. $sql .= " AND t.fk_soc = sc.fk_soc AND sc.fk_user = " . $user->id;
  551. } elseif ($user->socid) {
  552. $sql .= " AND t.fk_soc = " . $user->socid;
  553. }
  554. $sql .= " ORDER BY " . $sortfield . ' ' . $sortorder;
  555. if (!empty($limit)) {
  556. $sql .= ' ' . $this->db->plimit($limit + 1, $offset);
  557. }
  558. dol_syslog(get_class($this) . "::fetch_all sql=" . $sql, LOG_DEBUG);
  559. $resql = $this->db->query($sql);
  560. if ($resql) {
  561. $this->lines = array();
  562. $num = $this->db->num_rows($resql);
  563. $i = 0;
  564. if ($num) {
  565. while ($i < $num) {
  566. $obj = $this->db->fetch_object($resql);
  567. $line = new TicketsLine();
  568. $line->rowid = $obj->rowid;
  569. $line->ref = $obj->ref;
  570. $line->track_id = $obj->track_id;
  571. $line->fk_soc = $obj->fk_soc;
  572. $line->fk_project = $obj->fk_project;
  573. $line->origin_email = $obj->origin_email;
  574. $line->fk_user_create = $obj->fk_user_create;
  575. $line->user_create_lastname = $obj->user_create_lastname;
  576. $line->user_create_firstname = $obj->user_create_firstname;
  577. $line->fk_user_assign = $obj->fk_user_assign;
  578. $line->user_assign_lastname = $obj->user_assign_lastname;
  579. $line->user_assign_firstname = $obj->user_assign_firstname;
  580. $line->subject = $obj->subject;
  581. $line->message = $obj->message;
  582. $line->fk_statut = $obj->fk_statut;
  583. $line->resolution = $obj->resolution;
  584. $line->progress = $obj->progress;
  585. $line->timing = $obj->timing;
  586. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  587. $label_type = ($langs->trans("TicketTypeShort" . $obj->type_code) != ("TicketTypeShort" . $obj->type_code) ? $langs->trans("TicketTypeShort" . $obj->type_code) : ($obj->type_label != '-' ? $obj->type_label : ''));
  588. $line->type_label = $label_type;
  589. $this->category_code = $obj->category_code;
  590. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  591. $label_category = ($langs->trans("TicketCategoryShort" . $obj->category_code) != ("TicketCategoryShort" . $obj->category_code) ? $langs->trans("TicketCategoryShort" . $obj->category_code) : ($obj->category_label != '-' ? $obj->category_label : ''));
  592. $line->category_label = $label_category;
  593. $this->severity_code = $obj->severity_code;
  594. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  595. $label_severity = ($langs->trans("TicketSeverityShort" . $obj->severity_code) != ("TicketSeverityShort" . $obj->severity_code) ? $langs->trans("TicketSeverityShort" . $obj->severity_code) : ($obj->severity_label != '-' ? $obj->severity_label : ''));
  596. $line->severity_label = $label_severity;
  597. $line->datec = $this->db->jdate($obj->datec);
  598. $line->date_read = $this->db->jdate($obj->date_read);
  599. $line->date_close = $this->db->jdate($obj->date_close);
  600. // Extra fields
  601. if (is_array($extrafields->attributes[$this->table_element]['label']) && count($extrafields->attributes[$this->table_element]['label'])) {
  602. foreach ($extrafields->attributes[$this->table_element]['label'] as $key => $val) {
  603. $tmpkey = 'options_' . $key;
  604. $line->{$tmpkey} = $obj->$tmpkey;
  605. }
  606. }
  607. $this->lines[$i] = $line;
  608. $i++;
  609. }
  610. }
  611. $this->db->free($resql);
  612. return $num;
  613. } else {
  614. $this->error = "Error " . $this->db->lasterror();
  615. dol_syslog(get_class($this) . "::fetch_all " . $this->error, LOG_ERR);
  616. return -1;
  617. }
  618. }
  619. /**
  620. * Update object into database
  621. *
  622. * @param User $user User that modifies
  623. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  624. * @return int <0 if KO, >0 if OK
  625. */
  626. public function update($user = 0, $notrigger = 0)
  627. {
  628. global $conf, $langs, $hookmanager;
  629. $error = 0;
  630. // Clean parameters
  631. if (isset($this->ref)) {
  632. $this->ref = trim($this->ref);
  633. }
  634. if (isset($this->track_id)) {
  635. $this->track_id = trim($this->track_id);
  636. }
  637. if (isset($this->fk_soc)) {
  638. $this->fk_soc = trim($this->fk_soc);
  639. }
  640. if (isset($this->fk_project)) {
  641. $this->fk_project = trim($this->fk_project);
  642. }
  643. if (isset($this->origin_email)) {
  644. $this->origin_email = trim($this->origin_email);
  645. }
  646. if (isset($this->fk_user_create)) {
  647. $this->fk_user_create = trim($this->fk_user_create);
  648. }
  649. if (isset($this->fk_user_assign)) {
  650. $this->fk_user_assign = trim($this->fk_user_assign);
  651. }
  652. if (isset($this->subject)) {
  653. $this->subject = trim($this->subject);
  654. }
  655. if (isset($this->message)) {
  656. $this->message = trim($this->message);
  657. }
  658. if (isset($this->fk_statut)) {
  659. $this->fk_statut = trim($this->fk_statut);
  660. }
  661. if (isset($this->resolution)) {
  662. $this->resolution = trim($this->resolution);
  663. }
  664. if (isset($this->progress)) {
  665. $this->progress = trim($this->progress);
  666. }
  667. if (isset($this->timing)) {
  668. $this->timing = trim($this->timing);
  669. }
  670. if (isset($this->type_code)) {
  671. $this->timing = trim($this->type_code);
  672. }
  673. if (isset($this->category_code)) {
  674. $this->timing = trim($this->category_code);
  675. }
  676. if (isset($this->severity_code)) {
  677. $this->timing = trim($this->severity_code);
  678. }
  679. // Check parameters
  680. // Put here code to add a control on parameters values
  681. // Update request
  682. $sql = "UPDATE " . MAIN_DB_PREFIX . "ticket SET";
  683. $sql .= " ref=" . (isset($this->ref) ? "'" . $this->db->escape($this->ref) . "'" : "") . ",";
  684. $sql .= " track_id=" . (isset($this->track_id) ? "'" . $this->db->escape($this->track_id) . "'" : "null") . ",";
  685. $sql .= " fk_soc=" . (isset($this->fk_soc) ? "'" . $this->db->escape($this->fk_soc) . "'" : "null") . ",";
  686. $sql .= " fk_project=" . (isset($this->fk_project) ? "'" . $this->db->escape($this->fk_project) . "'" : "null") . ",";
  687. $sql .= " origin_email=" . (isset($this->origin_email) ? "'" . $this->db->escape($this->origin_email) . "'" : "null") . ",";
  688. $sql .= " fk_user_create=" . (isset($this->fk_user_create) ? $this->fk_user_create : "null") . ",";
  689. $sql .= " fk_user_assign=" . (isset($this->fk_user_assign) ? $this->fk_user_assign : "null") . ",";
  690. $sql .= " subject=" . (isset($this->subject) ? "'" . $this->db->escape($this->subject) . "'" : "null") . ",";
  691. $sql .= " message=" . (isset($this->message) ? "'" . $this->db->escape($this->message) . "'" : "null") . ",";
  692. $sql .= " fk_statut=" . (isset($this->fk_statut) ? $this->fk_statut : "null") . ",";
  693. $sql .= " resolution=" . (isset($this->resolution) ? $this->resolution : "null") . ",";
  694. $sql .= " progress=" . (isset($this->progress) ? "'" . $this->db->escape($this->progress) . "'" : "null") . ",";
  695. $sql .= " timing=" . (isset($this->timing) ? "'" . $this->db->escape($this->timing) . "'" : "null") . ",";
  696. $sql .= " type_code=" . (isset($this->type_code) ? "'" . $this->db->escape($this->type_code) . "'" : "null") . ",";
  697. $sql .= " category_code=" . (isset($this->category_code) ? "'" . $this->db->escape($this->category_code) . "'" : "null") . ",";
  698. $sql .= " severity_code=" . (isset($this->severity_code) ? "'" . $this->db->escape($this->severity_code) . "'" : "null") . ",";
  699. $sql .= " datec=" . (dol_strlen($this->datec) != 0 ? "'" . $this->db->idate($this->datec) . "'" : 'null') . ",";
  700. $sql .= " date_read=" . (dol_strlen($this->date_read) != 0 ? "'" . $this->db->idate($this->date_read) . "'" : 'null') . ",";
  701. $sql .= " date_close=" . (dol_strlen($this->date_close) != 0 ? "'" . $this->db->idate($this->date_close) . "'" : 'null') . "";
  702. $sql .= " WHERE rowid=" . $this->id;
  703. $this->db->begin();
  704. $resql = $this->db->query($sql);
  705. if (!$resql) {
  706. $error++;
  707. $this->errors[] = "Error " . $this->db->lasterror();
  708. }
  709. if (! $error) {
  710. // Update extrafields
  711. if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) { // For avoid conflicts if trigger used
  712. $result = $this->insertExtraFields();
  713. if ($result < 0) {
  714. $error++;
  715. }
  716. }
  717. }
  718. if (! $error && ! $notrigger) {
  719. // Call trigger
  720. $result=$this->call_trigger('TICKET_MODIFY', $user);
  721. if ($result < 0) {
  722. $error++;
  723. }
  724. // End call triggers
  725. }
  726. // Commit or rollback
  727. if ($error) {
  728. foreach ($this->errors as $errmsg) {
  729. dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR);
  730. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  731. }
  732. $this->db->rollback();
  733. return -1 * $error;
  734. } else {
  735. $this->db->commit();
  736. return 1;
  737. }
  738. }
  739. /**
  740. * Delete object in database
  741. *
  742. * @param User $user User that deletes
  743. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  744. * @return int <0 if KO, >0 if OK
  745. */
  746. public function delete($user, $notrigger = 0)
  747. {
  748. global $conf, $langs;
  749. $error = 0;
  750. $this->db->begin();
  751. if (!$error) {
  752. if (!$notrigger) {
  753. // Call trigger
  754. $result = $this->call_trigger('TICKET_DELETE', $user);
  755. if ($result < 0) {
  756. $error++;
  757. }
  758. // End call triggers
  759. }
  760. }
  761. if (!$error) {
  762. // Delete linked contacts
  763. $res = $this->delete_linked_contact();
  764. if ($res < 0) {
  765. dol_syslog(get_class($this) . "::delete error", LOG_ERR);
  766. $error++;
  767. }
  768. }
  769. if (!$error) {
  770. // Delete linked object
  771. $res = $this->deleteObjectLinked();
  772. if ($res < 0) $error++;
  773. }
  774. if (!$error) {
  775. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "ticket_logs";
  776. $sql .= " WHERE fk_track_id = '" . $this->db->escape($this->track_id) . "'";
  777. $resql = $this->db->query($sql);
  778. }
  779. if (!$error) {
  780. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "ticket_msg";
  781. $sql .= " WHERE fk_track_id = '" . $this->db->escape($this->track_id) . "'";
  782. $resql = $this->db->query($sql);
  783. }
  784. // Removed extrafields
  785. if (!$error) {
  786. $result = $this->deleteExtraFields();
  787. if ($result < 0) {
  788. $error++;
  789. dol_syslog(get_class($this) . "::delete error -3 " . $this->error, LOG_ERR);
  790. }
  791. }
  792. if (!$error) {
  793. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "ticket";
  794. $sql .= " WHERE rowid=" . $this->id;
  795. dol_syslog(get_class($this) . "::delete sql=" . $sql);
  796. $resql = $this->db->query($sql);
  797. if (!$resql) {
  798. $error++;
  799. $this->errors[] = "Error " . $this->db->lasterror();
  800. }
  801. }
  802. // Commit or rollback
  803. if ($error) {
  804. foreach ($this->errors as $errmsg) {
  805. dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
  806. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  807. }
  808. $this->db->rollback();
  809. return -1 * $error;
  810. } else {
  811. $this->db->commit();
  812. return 1;
  813. }
  814. }
  815. /**
  816. * Load an object from its id and create a new one in database
  817. *
  818. * @param int $fromid Id of object to clone
  819. * @return int New id of clone
  820. */
  821. public function createFromClone($fromid)
  822. {
  823. global $user, $langs;
  824. $error = 0;
  825. $object = new Ticket($this->db);
  826. $this->db->begin();
  827. // Load source object
  828. $object->fetch($fromid);
  829. $object->id = 0;
  830. $object->statut = 0;
  831. // Clear fields
  832. // ...
  833. // Create clone
  834. $result = $object->create($user);
  835. // Other options
  836. if ($result < 0) {
  837. $this->error = $object->error;
  838. $error++;
  839. }
  840. if (!$error) {
  841. }
  842. // End
  843. if (!$error) {
  844. $this->db->commit();
  845. return $object->id;
  846. } else {
  847. $this->db->rollback();
  848. return -1;
  849. }
  850. }
  851. /**
  852. * Initialise object with example values
  853. * Id must be 0 if object instance is a specimen
  854. *
  855. * @return void
  856. */
  857. public function initAsSpecimen()
  858. {
  859. $this->id = 0;
  860. $this->ref = 'TI0501-001';
  861. $this->track_id = 'XXXXaaaa';
  862. $this->origin_email = 'email@email.com';
  863. $this->fk_project = '1';
  864. $this->fk_user_create = '1';
  865. $this->fk_user_assign = '1';
  866. $this->subject = 'Subject of ticket';
  867. $this->message = 'Message of ticket';
  868. $this->fk_statut = '0';
  869. $this->resolution = '1';
  870. $this->progress = '10';
  871. $this->timing = '30';
  872. $this->type_code = 'TYPECODE';
  873. $this->category_code = 'CATEGORYCODE';
  874. $this->severity_code = 'SEVERITYCODE';
  875. $this->datec = '';
  876. $this->date_read = '';
  877. $this->date_close = '';
  878. $this->tms = '';
  879. }
  880. /**
  881. * print selected status
  882. *
  883. * @param string $selected selected status
  884. * @return void
  885. */
  886. public function printSelectStatus($selected = "")
  887. {
  888. print Form::selectarray('search_fk_statut', $this->statuts_short, $selected, $show_empty = 1, $key_in_label = 0, $value_as_key = 0, $option = '', $translate = 1, $maxlen = 0, $disabled = 0, $sort = '', $morecss = '');
  889. }
  890. /**
  891. * Charge dans cache la liste des types de tickets (paramétrable dans dictionnaire)
  892. *
  893. * @return int Nb lignes chargees, 0 si deja chargees, <0 si ko
  894. */
  895. public function loadCacheTypesTickets()
  896. {
  897. global $langs;
  898. if (! empty($this->cache_types_tickets) && count($this->cache_types_tickets)) {
  899. return 0;
  900. }
  901. // Cache deja charge
  902. $sql = "SELECT rowid, code, label, use_default, pos, description";
  903. $sql .= " FROM " . MAIN_DB_PREFIX . "c_ticket_type";
  904. $sql .= " WHERE active > 0";
  905. $sql .= " ORDER BY pos";
  906. dol_syslog(get_class($this) . "::load_cache_type_tickets sql=" . $sql, LOG_DEBUG);
  907. $resql = $this->db->query($sql);
  908. if ($resql) {
  909. $num = $this->db->num_rows($resql);
  910. $i = 0;
  911. while ($i < $num) {
  912. $obj = $this->db->fetch_object($resql);
  913. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  914. $label = ($langs->trans("TicketTypeShort" . $obj->code) != ("TicketTypeShort" . $obj->code) ? $langs->trans("TicketTypeShort" . $obj->code) : ($obj->label != '-' ? $obj->label : ''));
  915. $this->cache_types_tickets[$obj->rowid]['code'] = $obj->code;
  916. $this->cache_types_tickets[$obj->rowid]['label'] = $label;
  917. $this->cache_types_tickets[$obj->rowid]['use_default'] = $obj->use_default;
  918. $this->cache_types_tickets[$obj->rowid]['pos'] = $obj->pos;
  919. $i++;
  920. }
  921. return $num;
  922. } else {
  923. dol_print_error($this->db);
  924. return -1;
  925. }
  926. }
  927. /**
  928. * Charge dans cache la liste des catégories de tickets (paramétrable dans dictionnaire)
  929. *
  930. * @return int Nb lignes chargees, 0 si deja chargees, <0 si ko
  931. */
  932. public function loadCacheCategoriesTickets()
  933. {
  934. global $langs;
  935. if (! empty($this->cache_category_ticket) && count($this->cache_category_tickets)) {
  936. return 0;
  937. }
  938. // Cache deja charge
  939. $sql = "SELECT rowid, code, label, use_default, pos, description";
  940. $sql .= " FROM " . MAIN_DB_PREFIX . "c_ticket_category";
  941. $sql .= " WHERE active > 0";
  942. $sql .= " ORDER BY pos";
  943. dol_syslog(get_class($this) . "::load_cache_categories_tickets sql=" . $sql, LOG_DEBUG);
  944. $resql = $this->db->query($sql);
  945. if ($resql) {
  946. $num = $this->db->num_rows($resql);
  947. $i = 0;
  948. while ($i < $num) {
  949. $obj = $this->db->fetch_object($resql);
  950. $this->cache_category_tickets[$obj->rowid]['code'] = $obj->code;
  951. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  952. $label = ($langs->trans("TicketCategoryShort" . $obj->code) != ("TicketCategoryShort" . $obj->code) ? $langs->trans("TicketCategoryShort" . $obj->code) : ($obj->label != '-' ? $obj->label : ''));
  953. $this->cache_category_tickets[$obj->rowid]['label'] = $label;
  954. $this->cache_category_tickets[$obj->rowid]['use_default'] = $obj->use_default;
  955. $this->cache_category_tickets[$obj->rowid]['pos'] = $obj->pos;
  956. $i++;
  957. }
  958. return $num;
  959. } else {
  960. dol_print_error($this->db);
  961. return -1;
  962. }
  963. }
  964. /**
  965. * Charge dans cache la liste des sévérité de tickets (paramétrable dans dictionnaire)
  966. *
  967. * @return int Nb lignes chargees, 0 si deja chargees, <0 si ko
  968. */
  969. public function loadCacheSeveritiesTickets()
  970. {
  971. global $langs;
  972. if (! empty($this->cache_severity_tickets) && count($this->cache_severity_tickets)) {
  973. return 0;
  974. }
  975. // Cache deja charge
  976. $sql = "SELECT rowid, code, label, use_default, pos, description";
  977. $sql .= " FROM " . MAIN_DB_PREFIX . "c_ticket_severity";
  978. $sql .= " WHERE active > 0";
  979. $sql .= " ORDER BY pos";
  980. dol_syslog(get_class($this) . "::loadCacheSeveritiesTickets sql=" . $sql, LOG_DEBUG);
  981. $resql = $this->db->query($sql);
  982. if ($resql) {
  983. $num = $this->db->num_rows($resql);
  984. $i = 0;
  985. while ($i < $num) {
  986. $obj = $this->db->fetch_object($resql);
  987. $this->cache_severity_tickets[$obj->rowid]['code'] = $obj->code;
  988. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  989. $label = ($langs->trans("TicketSeverityShort" . $obj->code) != ("TicketSeverityShort" . $obj->code) ? $langs->trans("TicketSeverityShort" . $obj->code) : ($obj->label != '-' ? $obj->label : ''));
  990. $this->cache_severity_tickets[$obj->rowid]['label'] = $label;
  991. $this->cache_severity_tickets[$obj->rowid]['use_default'] = $obj->use_default;
  992. $this->cache_severity_tickets[$obj->rowid]['pos'] = $obj->pos;
  993. $i++;
  994. }
  995. return $num;
  996. } else {
  997. dol_print_error($this->db);
  998. return -1;
  999. }
  1000. }
  1001. /**
  1002. * Return status label of object
  1003. *
  1004. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  1005. * @return string Label
  1006. */
  1007. public function getLibStatut($mode = 0)
  1008. {
  1009. return $this->libStatut($this->fk_statut, $mode);
  1010. }
  1011. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  1012. /**
  1013. * Return status label of object
  1014. *
  1015. * @param string $statut id statut
  1016. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  1017. * @return string Label
  1018. */
  1019. function LibStatut($statut, $mode = 0)
  1020. {
  1021. // phpcs:enable
  1022. global $langs;
  1023. if ($mode == 0) {
  1024. return $langs->trans($this->statuts[$statut]);
  1025. }
  1026. elseif ($mode == 1) {
  1027. return $langs->trans($this->statuts_short[$statut]);
  1028. }
  1029. elseif ($mode == 2) {
  1030. if ($statut == 0) {
  1031. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut0.png@ticket') . ' ' . $langs->trans($this->statuts_short[$statut]);
  1032. }
  1033. elseif ($statut == 1) {
  1034. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut1.png@ticket') . ' ' . $langs->trans($this->statuts_short[$statut]);
  1035. }
  1036. elseif ($statut == 3) {
  1037. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut3.png@ticket') . ' ' . $langs->trans($this->statuts_short[$statut]);
  1038. }
  1039. elseif ($statut == 4) {
  1040. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut4.png@ticket') . ' ' . $langs->trans($this->statuts_short[$statut]);
  1041. }
  1042. elseif ($statut == 5) {
  1043. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut5.png@ticket') . ' ' . $langs->trans($this->statuts_short[$statut]);
  1044. }
  1045. elseif ($statut == 6) {
  1046. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut6.png@ticket') . ' ' . $langs->trans($this->statuts_short[$statut]);
  1047. }
  1048. elseif ($statut == 8) {
  1049. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut8.png@ticket') . ' ' . $langs->trans($this->statuts_short[$statut]);
  1050. }
  1051. elseif ($statut == 9) {
  1052. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut9.png@ticket') . ' ' . $langs->trans($this->statuts_short[$statut]);
  1053. }
  1054. }
  1055. elseif ($mode == 3) {
  1056. if ($statut == 0) {
  1057. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut0.png@ticket');
  1058. }
  1059. elseif ($statut == 1) {
  1060. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut1.png@ticket');
  1061. }
  1062. elseif ($statut == 3) {
  1063. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut3.png@ticket');
  1064. }
  1065. elseif ($statut == 4) {
  1066. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut4.png@ticket');
  1067. }
  1068. elseif ($statut == 5) {
  1069. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut5.png@ticket');
  1070. }
  1071. elseif ($statut == 6) {
  1072. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut6.png@ticket');
  1073. }
  1074. elseif ($statut == 8) {
  1075. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut8.png@ticket');
  1076. }
  1077. elseif ($statut == 9) {
  1078. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut9.png@ticket');
  1079. }
  1080. }
  1081. elseif ($mode == 4) {
  1082. if ($statut == 0) {
  1083. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut0.png@ticket') . ' ' . $langs->trans($this->statuts_short[$statut]);
  1084. }
  1085. elseif ($statut == 1) {
  1086. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut1.png@ticket') . ' ' . $langs->trans($this->statuts_short[$statut]);
  1087. }
  1088. elseif ($statut == 3) {
  1089. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut3.png@ticket') . ' ' . $langs->trans($this->statuts_short[$statut]);
  1090. }
  1091. elseif ($statut == 4) {
  1092. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut4.png@ticket') . ' ' . $langs->trans($this->statuts_short[$statut]);
  1093. }
  1094. elseif ($statut == 5) {
  1095. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut5.png@ticket') . ' ' . $langs->trans($this->statuts_short[$statut]);
  1096. }
  1097. elseif ($statut == 6) {
  1098. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut6.png@ticket') . ' ' . $langs->trans($this->statuts_short[$statut]);
  1099. }
  1100. elseif ($statut == 8) {
  1101. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut8.png@ticket') . ' ' . $langs->trans($this->statuts_short[$statut]);
  1102. }
  1103. elseif ($statut == 9) {
  1104. return img_picto($langs->trans($this->statuts_short[$statut]), 'statut9.png@ticket') . ' ' . $langs->trans($this->statuts_short[$statut]);
  1105. }
  1106. }
  1107. elseif ($mode == 5) {
  1108. if ($statut == 0) {
  1109. return $langs->trans($this->statuts_short[$statut]) . ' ' . img_picto($langs->trans($this->statuts_short[$statut]), 'statut0.png@ticket');
  1110. }
  1111. elseif ($statut == 1) {
  1112. return $langs->trans($this->statuts_short[$statut]) . ' ' . img_picto($langs->trans($this->statuts_short[$statut]), 'statut1.png@ticket');
  1113. }
  1114. elseif ($statut == 3) {
  1115. return $langs->trans($this->statuts_short[$statut]) . ' ' . img_picto($langs->trans($this->statuts_short[$statut]), 'statut3.png@ticket');
  1116. }
  1117. elseif ($statut == 4) {
  1118. return $langs->trans($this->statuts_short[$statut]) . ' ' . img_picto($langs->trans($this->statuts_short[$statut]), 'statut4.png@ticket');
  1119. }
  1120. elseif ($statut == 5) {
  1121. return $langs->trans($this->statuts_short[$statut]) . ' ' . img_picto($langs->trans($this->statuts_short[$statut]), 'statut5.png@ticket');
  1122. }
  1123. elseif ($statut == 6) {
  1124. return $langs->trans($this->statuts_short[$statut]) . ' ' . img_picto($langs->trans($this->statuts_short[$statut]), 'statut6.png@ticket');
  1125. }
  1126. elseif ($statut == 8) {
  1127. return $langs->trans($this->statuts_short[$statut]) . ' ' . img_picto($langs->trans($this->statuts_short[$statut]), 'statut8.png@ticket');
  1128. }
  1129. elseif ($statut == 9) {
  1130. return $langs->trans($this->statuts_short[$statut]) . ' ' . img_picto($langs->trans($this->statuts_short[$statut]), 'statut9.png@ticket');
  1131. }
  1132. }
  1133. }
  1134. /**
  1135. * Return a link to the object card (with optionaly the picto)
  1136. *
  1137. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  1138. * @param string $option On what the link point to ('nolink', ...)
  1139. * @param int $notooltip 1=Disable tooltip
  1140. * @param string $morecss Add more css on link
  1141. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  1142. * @return string String with URL
  1143. */
  1144. function getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
  1145. {
  1146. global $db, $conf, $langs;
  1147. global $dolibarr_main_authentication, $dolibarr_main_demo;
  1148. global $menumanager;
  1149. if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips
  1150. $result = '';
  1151. $companylink = '';
  1152. $label = '<u>' . $langs->trans("ShowTicket") . '</u>';
  1153. $label.= '<br>';
  1154. $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref.'<br>';
  1155. $label.= '<b>' . $langs->trans('TicketTrackId') . ':</b> ' . $this->track_id.'<br>';
  1156. $label.= '<b>' . $langs->trans('Subject') . ':</b> ' . $this->subject;
  1157. $url = dol_buildpath('/ticket/card.php',1).'?id='.$this->id;
  1158. if ($option != 'nolink')
  1159. {
  1160. // Add param to save lastsearch_values or not
  1161. $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
  1162. if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
  1163. if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
  1164. }
  1165. $linkclose='';
  1166. if (empty($notooltip))
  1167. {
  1168. if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
  1169. {
  1170. $label=$langs->trans("ShowTicket");
  1171. $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
  1172. }
  1173. $linkclose.=' title="'.dol_escape_htmltag($label, 1).'"';
  1174. $linkclose.=' class="classfortooltip'.($morecss?' '.$morecss:'').'"';
  1175. }
  1176. else $linkclose = ($morecss?' class="'.$morecss.'"':'');
  1177. $linkstart = '<a href="'.$url.'"';
  1178. $linkstart.=$linkclose.'>';
  1179. $linkend='</a>';
  1180. $result .= $linkstart;
  1181. if ($withpicto) $result.=img_object(($notooltip?'':$label), ($this->picto?$this->picto:'generic'), ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1);
  1182. if ($withpicto != 2) $result.= $this->ref;
  1183. $result .= $linkend;
  1184. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  1185. return $result;
  1186. }
  1187. /**
  1188. * Mark a message as read
  1189. *
  1190. * @param User $user Object user
  1191. * @param int $notrigger No trigger
  1192. * @return int <0 if KO, >0 if OK
  1193. */
  1194. public function markAsRead($user, $notrigger = 0)
  1195. {
  1196. global $conf, $langs;
  1197. if ($this->statut != 9) { // no closed
  1198. $this->db->begin();
  1199. $sql = "UPDATE " . MAIN_DB_PREFIX . "ticket";
  1200. $sql .= " SET fk_statut = 1, date_read='" . $this->db->idate(dol_now()) . "'";
  1201. $sql .= " WHERE rowid = " . $this->id;
  1202. dol_syslog(get_class($this) . "::markAsRead sql=" . $sql);
  1203. $resql = $this->db->query($sql);
  1204. if ($resql) {
  1205. if (!$error && !$notrigger) {
  1206. // Call trigger
  1207. $result=$this->call_trigger('TICKET_MARK_READ', $user);
  1208. if ($result < 0) {
  1209. $error++;
  1210. }
  1211. // End call triggers
  1212. }
  1213. if (!$error) {
  1214. $this->db->commit();
  1215. return 1;
  1216. } else {
  1217. $this->db->rollback();
  1218. $this->error = join(',', $this->errors);
  1219. dol_syslog(get_class($this) . "::markAsRead " . $this->error, LOG_ERR);
  1220. return -1;
  1221. }
  1222. } else {
  1223. $this->db->rollback();
  1224. $this->error = $this->db->lasterror();
  1225. dol_syslog(get_class($this) . "::markAsRead " . $this->error, LOG_ERR);
  1226. return -1;
  1227. }
  1228. }
  1229. }
  1230. /**
  1231. * Mark a message as read
  1232. *
  1233. * @param User $user Object user
  1234. * @param int $id_assign_user ID of user assigned
  1235. * @param int $notrigger Disable trigger
  1236. * @return int <0 if KO, 0=Nothing done, >0 if OK
  1237. */
  1238. public function assignUser($user, $id_assign_user, $notrigger = 0)
  1239. {
  1240. global $conf, $langs;
  1241. $this->db->begin();
  1242. $sql = "UPDATE " . MAIN_DB_PREFIX . "ticket";
  1243. if ($id_assign_user > 0)
  1244. {
  1245. $sql .= " SET fk_user_assign=".$id_assign_user.", fk_statut=4";
  1246. }
  1247. else
  1248. {
  1249. $sql .= " SET fk_user_assign=null, fk_statut=1";
  1250. }
  1251. $sql .= " WHERE rowid = " . $this->id;
  1252. dol_syslog(get_class($this) . "::assignUser sql=" . $sql);
  1253. $resql = $this->db->query($sql);
  1254. if ($resql) {
  1255. $this->fk_user_assign = $id_assign_user; // May be used by trigger
  1256. if (! $notrigger) {
  1257. // Call trigger
  1258. $result = $this->call_trigger('TICKET_ASSIGNED', $user);
  1259. if ($result < 0) {
  1260. $error ++;
  1261. }
  1262. // End call triggers
  1263. }
  1264. if (! $error) {
  1265. $this->db->commit();
  1266. return 1;
  1267. } else {
  1268. $this->db->rollback();
  1269. $this->error = join(',', $this->errors);
  1270. dol_syslog(get_class($this) . "::assignUser " . $this->error, LOG_ERR);
  1271. return - 1;
  1272. }
  1273. } else {
  1274. $this->db->rollback();
  1275. $this->error = $this->db->lasterror();
  1276. dol_syslog(get_class($this) . "::assignUser " . $this->error, LOG_ERR);
  1277. return - 1;
  1278. }
  1279. }
  1280. /**
  1281. * Create log for the ticket
  1282. * 1- create entry into database for message storage
  1283. * 2- if trigger, send an email to ticket contacts
  1284. *
  1285. * @param User $user User that create
  1286. * @param string $message Log message
  1287. * @param int $noemail 0=send email after, 1=disable emails
  1288. * @return int <0 if KO, >0 if OK
  1289. */
  1290. public function createTicketLog(User $user, $message, $noemail = 0)
  1291. {
  1292. global $conf, $langs;
  1293. $this->db->begin();
  1294. // Clean parameters
  1295. $this->message = trim($this->message);
  1296. // Check parameters
  1297. if (!$message) {
  1298. $this->error = 'ErrorBadValueForParameter';
  1299. return -1;
  1300. }
  1301. // Insert request
  1302. $sql = "INSERT INTO " . MAIN_DB_PREFIX . "ticket_logs(";
  1303. $sql .= "entity,";
  1304. $sql .= "datec,";
  1305. $sql .= "fk_track_id,";
  1306. $sql .= "fk_user_create,";
  1307. $sql .= "message";
  1308. $sql .= ") VALUES (";
  1309. $sql .= " " . $conf->entity . ",";
  1310. $sql .= " '" . $this->db->idate(dol_now()) . "',";
  1311. $sql .= " '" . $this->db->escape($this->track_id) . "',";
  1312. $sql .= " " . ($user->id > 0 ? $user->id : 'NULL') . ",";
  1313. $sql .= " '" . $this->db->escape($message) . "'";
  1314. $sql .= ")";
  1315. dol_syslog(get_class($this) . "::create_ticket_log sql=" . $sql, LOG_DEBUG);
  1316. $resql = $this->db->query($sql);
  1317. if ($resql) {
  1318. if ($conf->global->TICKET_ACTIVATE_LOG_BY_EMAIL && !$noemail) {
  1319. $this->sendLogByEmail($user, $message);
  1320. }
  1321. if (!$error) {
  1322. $this->db->commit();
  1323. return 1;
  1324. }
  1325. } else {
  1326. $this->db->rollback();
  1327. $this->error = "Error " . $this->db->lasterror();
  1328. dol_syslog(get_class($this) . "::create_ticket_log " . $this->error, LOG_ERR);
  1329. return -1;
  1330. }
  1331. }
  1332. /**
  1333. * Send notification of changes by email
  1334. *
  1335. * @param User $user User that create
  1336. * @param string $message Log message
  1337. * @return int <0 if KO, >0 if OK (number of emails sent)
  1338. */
  1339. private function sendLogByEmail($user, $message)
  1340. {
  1341. global $conf, $langs;
  1342. $nb_sent = 0;
  1343. $langs->load('ticket');
  1344. // Retrieve email of all contacts (internal and external)
  1345. $contacts = $this->listeContact(-1, 'internal');
  1346. $contacts = array_merge($contacts, $this->listeContact(-1, 'external'));
  1347. /* If origin_email and no socid, we add email to the list * */
  1348. if (!empty($this->origin_email) && empty($this->fk_soc)) {
  1349. $array_ext = array(array('firstname' => '', 'lastname' => '', 'email' => $this->origin_email, 'libelle' => $langs->transnoentities('TicketEmailOriginIssuer'), 'socid' => "-1"));
  1350. $contacts = array_merge($contacts, $array_ext);
  1351. }
  1352. if (!empty($this->fk_soc)) {
  1353. $this->fetch_thirdparty($this->fk_soc);
  1354. $array_company = array(array('firstname' => '', 'lastname' => $this->client->name, 'email' => $this->client->email, 'libelle' => $langs->transnoentities('Customer'), 'socid' => $this->client->id));
  1355. $contacts = array_merge($contacts, $array_company);
  1356. }
  1357. // foreach contact send email with notification message
  1358. if (count($contacts) > 0) {
  1359. foreach ($contacts as $key => $info_sendto) {
  1360. $message = '';
  1361. $subject = '[' . $conf->global->MAIN_INFO_SOCIETE_NOM . '] ' . $langs->transnoentities('TicketNotificationEmailSubject', $this->track_id);
  1362. $message .= $langs->transnoentities('TicketNotificationEmailBody', $this->track_id) . "\n\n";
  1363. $message .= $langs->transnoentities('Title') . ' : ' . $this->subject . "\n";
  1364. $recipient_name = dolGetFirstLastname($info_sendto['firstname'], $info_sendto['lastname'], '-1');
  1365. $recipient = (!empty($recipient_name) ? $recipient_name : $info_sendto['email']) . ' (' . strtolower($info_sendto['libelle']) . ')';
  1366. $message .= $langs->transnoentities('TicketNotificationRecipient') . ' : ' . $recipient . "\n";
  1367. $message .= "\n";
  1368. $message .= '* ' . $langs->transnoentities('TicketNotificationLogMessage') . ' *' . "\n";
  1369. $message .= dol_html_entity_decode($log_message, ENT_QUOTES) . "\n";
  1370. if ($info_sendto['source'] == 'internal') {
  1371. $url_internal_ticket = dol_buildpath('/ticket/card.php', 2) . '?track_id=' . $this->track_id;
  1372. $message .= "\n" . $langs->transnoentities('TicketNotificationEmailBodyInfosTrackUrlinternal') . ' : ' . '<a href="' . $url_internal_ticket . '">' . $this->track_id . '</a>' . "\n";
  1373. } else {
  1374. $url_public_ticket = ($conf->global->TICKET_URL_PUBLIC_INTERFACE ? $conf->global->TICKET_URL_PUBLIC_INTERFACE . '/' : dol_buildpath('/public/ticket/view.php', 2)) . '?track_id=' . $this->track_id;
  1375. $message .= "\n" . $langs->transnoentities('TicketNewEmailBodyInfosTrackUrlCustomer') . ' : ' . '<a href="' . $url_public_ticket . '">' . $this->track_id . '</a>' . "\n";
  1376. }
  1377. $message .= "\n";
  1378. $message .= $langs->transnoentities('TicketEmailPleaseDoNotReplyToThisEmail') . "\n";
  1379. $from = $conf->global->MAIN_INFO_SOCIETE_NOM . '<' . $conf->global->TICKET_NOTIFICATION_EMAIL_FROM . '>';
  1380. $replyto = $from;
  1381. // Init to avoid errors
  1382. $filepath = array();
  1383. $filename = array();
  1384. $mimetype = array();
  1385. $message = dol_nl2br($message);
  1386. if (!empty($conf->global->TICKET_DISABLE_MAIL_AUTOCOPY_TO)) {
  1387. $old_MAIN_MAIL_AUTOCOPY_TO = $conf->global->MAIN_MAIL_AUTOCOPY_TO;
  1388. $conf->global->MAIN_MAIL_AUTOCOPY_TO = '';
  1389. }
  1390. include_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php';
  1391. $mailfile = new CMailFile($subject, $info_sendto['email'], $from, $message, $filepath, $mimetype, $filename, $sendtocc, '', $deliveryreceipt, 0);
  1392. if ($mailfile->error || $mailfile->errors) {
  1393. setEventMessages($mailfile->error, $mailfile->errors, 'errors');
  1394. } else {
  1395. $result = $mailfile->sendfile();
  1396. if ($result > 0) {
  1397. $nb_sent++;
  1398. }
  1399. }
  1400. if (!empty($conf->global->TICKET_DISABLE_MAIL_AUTOCOPY_TO)) {
  1401. $conf->global->MAIN_MAIL_AUTOCOPY_TO = $old_MAIN_MAIL_AUTOCOPY_TO;
  1402. }
  1403. }
  1404. setEventMessages($langs->trans('TicketNotificationNumberEmailSent', $nb_sent), null, 'mesgs');
  1405. }
  1406. return $nb_sent;
  1407. }
  1408. /**
  1409. * Charge la liste des actions sur le ticket
  1410. *
  1411. * @return int Nb lignes chargees, 0 si deja chargees, <0 si ko
  1412. */
  1413. public function loadCacheLogsTicket()
  1414. {
  1415. global $langs;
  1416. if (is_array($this->cache_logs_ticket) && count($this->cache_logs_ticket)) {
  1417. return 0;
  1418. }
  1419. // Cache deja charge
  1420. $sql = "SELECT rowid, fk_user_create, datec, message";
  1421. $sql .= " FROM " . MAIN_DB_PREFIX . "ticket_logs";
  1422. $sql .= " WHERE fk_track_id ='" . $this->db->escape($this->track_id) . "'";
  1423. $sql .= " ORDER BY datec DESC";
  1424. $resql = $this->db->query($sql);
  1425. if ($resql) {
  1426. $num = $this->db->num_rows($resql);
  1427. $i = 0;
  1428. while ($i < $num) {
  1429. $obj = $this->db->fetch_object($resql);
  1430. $this->cache_logs_ticket[$i]['id'] = $obj->rowid;
  1431. $this->cache_logs_ticket[$i]['fk_user_create'] = $obj->fk_user_create;
  1432. $this->cache_logs_ticket[$i]['datec'] = $this->db->jdate($obj->datec);
  1433. $this->cache_logs_ticket[$i]['message'] = $obj->message;
  1434. $i++;
  1435. }
  1436. return $num;
  1437. } else {
  1438. $this->error = "Error " . $this->db->lasterror();
  1439. dol_syslog(get_class($this) . "::loadCacheLogsTicket " . $this->error, LOG_ERR);
  1440. return -1;
  1441. }
  1442. }
  1443. /**
  1444. * Add message into database
  1445. *
  1446. * @param User $user User that creates
  1447. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  1448. * @return int <0 if KO, Id of created object if OK
  1449. */
  1450. public function createTicketMessage($user, $notrigger = 0)
  1451. {
  1452. global $conf, $langs;
  1453. $error = 0;
  1454. // Clean parameters
  1455. if (isset($this->fk_track_id)) {
  1456. $this->fk_track_id = trim($this->fk_track_id);
  1457. }
  1458. if (isset($this->message)) {
  1459. $this->message = trim($this->message);
  1460. }
  1461. // Insert request
  1462. $sql = "INSERT INTO " . MAIN_DB_PREFIX . "ticket_msg(";
  1463. $sql .= "fk_track_id,";
  1464. $sql .= "fk_user_action,";
  1465. $sql .= "datec,";
  1466. $sql .= "message,";
  1467. $sql .= "private";
  1468. $sql .= ") VALUES (";
  1469. $sql .= " " . (!isset($this->fk_track_id) ? "'" . $this->db->escape($this->track_id) . "'" : "'" . $this->db->escape($this->fk_track_id) . "'") . ",";
  1470. $sql .= " " . ($this->fk_user_action > 0 ? $this->fk_user_action : $user->id) . ",";
  1471. $sql .= " '" . $this->db->idate(dol_now()) . "',";
  1472. $sql .= " " . (!isset($this->message) ? 'NULL' : "'" . $this->db->escape($this->message) . "'") . ",";
  1473. $sql .= " " . (empty($this->private) ? '0' : "'" . $this->db->escape($this->private) . "'") . "";
  1474. $sql .= ")";
  1475. $this->db->begin();
  1476. dol_syslog(get_class($this) . "::create_ticket_message sql=" . $sql, LOG_DEBUG);
  1477. $resql = $this->db->query($sql);
  1478. if (!$resql) {
  1479. $error++;
  1480. $this->errors[] = "Error " . $this->db->lasterror();
  1481. }
  1482. if (!$error) {
  1483. if (!$notrigger) {
  1484. // Uncomment this and change MYOBJECT to your own tag if you
  1485. // want this action calls a trigger.
  1486. //// Call triggers
  1487. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  1488. //$interface=new Interfaces($this->db);
  1489. //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
  1490. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  1491. //// End call triggers
  1492. }
  1493. }
  1494. // Commit or rollback
  1495. if ($error) {
  1496. foreach ($this->errors as $errmsg) {
  1497. dol_syslog(get_class($this) . "::create_ticket_message " . $errmsg, LOG_ERR);
  1498. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  1499. }
  1500. $this->db->rollback();
  1501. return -1 * $error;
  1502. } else {
  1503. $this->db->commit();
  1504. return 1;
  1505. }
  1506. }
  1507. /**
  1508. * Charge la liste des messages sur le ticket
  1509. *
  1510. * @return int Nb lignes chargees, 0 si deja chargees, <0 si ko
  1511. */
  1512. public function loadCacheMsgsTicket()
  1513. {
  1514. global $langs;
  1515. if (is_array($this->cache_msgs_ticket) && count($this->cache_msgs_ticket)) {
  1516. return 0;
  1517. }
  1518. // Cache deja charge
  1519. $sql = "SELECT rowid, fk_user_action, datec, message, private";
  1520. $sql .= " FROM " . MAIN_DB_PREFIX . "ticket_msg";
  1521. $sql .= " WHERE fk_track_id ='" . $this->db->escape($this->track_id) . "'";
  1522. $sql .= " ORDER BY datec DESC";
  1523. dol_syslog(get_class($this) . "::load_cache_actions_ticket sql=" . $sql, LOG_DEBUG);
  1524. $resql = $this->db->query($sql);
  1525. if ($resql) {
  1526. $num = $this->db->num_rows($resql);
  1527. $i = 0;
  1528. while ($i < $num) {
  1529. $obj = $this->db->fetch_object($resql);
  1530. $this->cache_msgs_ticket[$i]['id'] = $obj->rowid;
  1531. $this->cache_msgs_ticket[$i]['fk_user_action'] = $obj->fk_user_action;
  1532. $this->cache_msgs_ticket[$i]['datec'] = $this->db->jdate($obj->datec);
  1533. $this->cache_msgs_ticket[$i]['message'] = $obj->message;
  1534. $this->cache_msgs_ticket[$i]['private'] = $obj->private;
  1535. $i++;
  1536. }
  1537. return $num;
  1538. } else {
  1539. $this->error = "Error " . $this->db->lasterror();
  1540. dol_syslog(get_class($this) . "::load_cache_actions_ticket " . $this->error, LOG_ERR);
  1541. return -1;
  1542. }
  1543. }
  1544. /**
  1545. * Close a ticket
  1546. *
  1547. * @return int <0 if KO, >0 if OK
  1548. */
  1549. public function close()
  1550. {
  1551. global $conf, $user, $langs;
  1552. if ($this->fk_statut != 9) { // not closed
  1553. $this->db->begin();
  1554. $sql = "UPDATE " . MAIN_DB_PREFIX . "ticket";
  1555. $sql .= " SET fk_statut=8, progress=100, date_close='" . $this->db->idate(dol_now()) . "'";
  1556. $sql .= " WHERE rowid = " . $this->id;
  1557. dol_syslog(get_class($this) . "::close sql=" . $sql);
  1558. $resql = $this->db->query($sql);
  1559. if ($resql) {
  1560. $error = 0;
  1561. // Valid and close fichinter linked
  1562. $this->fetchObjectLinked($this->id, $this->element, null, 'fichinter');
  1563. if ($this->linkedObjectsIds)
  1564. {
  1565. foreach ($this->linkedObjectsIds['fichinter'] as $fichinter_id) {
  1566. $fichinter = new Fichinter($this->db);
  1567. $fichinter->fetch($fichinter_id);
  1568. if($fichinter->statut == 0) {
  1569. $result = $fichinter->setValid($user);
  1570. if (!$result) {
  1571. $this->errors[] = $fichinter->error;
  1572. $error++;
  1573. }
  1574. }
  1575. if ($fichinter->statut < 3) {
  1576. $result = $fichinter->setStatut(3);
  1577. if (!$result) {
  1578. $this->errors[] = $fichinter->error;
  1579. $error++;
  1580. }
  1581. }
  1582. }
  1583. }
  1584. // Call trigger
  1585. $result=$this->call_trigger('TICKET_CLOSE', $user);
  1586. if ($result < 0) {
  1587. $error++;
  1588. }
  1589. // End call triggers
  1590. if (!$error) {
  1591. $this->db->commit();
  1592. return 1;
  1593. } else {
  1594. $this->db->rollback();
  1595. $this->error = join(',', $this->errors);
  1596. dol_syslog(get_class($this) . "::close " . $this->error, LOG_ERR);
  1597. return -1;
  1598. }
  1599. } else {
  1600. $this->db->rollback();
  1601. $this->error = $this->db->lasterror();
  1602. dol_syslog(get_class($this) . "::close " . $this->error, LOG_ERR);
  1603. return -1;
  1604. }
  1605. }
  1606. }
  1607. /**
  1608. * Search and fetch thirparties by email
  1609. *
  1610. * @param string $email Email
  1611. * @param int $type Type of thirdparties (0=any, 1=customer, 2=prospect, 3=supplier)
  1612. * @param array $filters Array of couple field name/value to filter the companies with the same name
  1613. * @param string $clause Clause for filters
  1614. * @return array Array of thirdparties object
  1615. */
  1616. public function searchSocidByEmail($email, $type = '0', $filters = array(), $clause = 'AND')
  1617. {
  1618. $thirdparties = array();
  1619. // Generation requete recherche
  1620. $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "societe";
  1621. $sql .= " WHERE entity IN (" . getEntity('ticket', 1) . ")";
  1622. if (!empty($type)) {
  1623. if ($type == 1 || $type == 2) {
  1624. $sql .= " AND client = " . $type;
  1625. } elseif ($type == 3) {
  1626. $sql .= " AND fournisseur = 1";
  1627. }
  1628. }
  1629. if (!empty($email)) {
  1630. if (!$exact) {
  1631. if (preg_match('/^([\*])?[^*]+([\*])?$/', $email, $regs) && count($regs) > 1) {
  1632. $email = str_replace('*', '%', $email);
  1633. } else {
  1634. $email = '%' . $email . '%';
  1635. }
  1636. }
  1637. $sql .= " AND ";
  1638. if (is_array($filters) && !empty($filters)) {
  1639. $sql .= "(";
  1640. }
  1641. if (!$case) {
  1642. $sql .= "email LIKE '" . $this->db->escape($email) . "'";
  1643. } else {
  1644. $sql .= "email LIKE BINARY '" . $this->db->escape($email) . "'";
  1645. }
  1646. }
  1647. if (is_array($filters) && !empty($filters)) {
  1648. foreach ($filters as $field => $value) {
  1649. $sql .= " " . $clause . " " . $field . " LIKE BINARY '" . $this->db->escape($value) . "'";
  1650. }
  1651. if (!empty($email)) {
  1652. $sql .= ")";
  1653. }
  1654. }
  1655. $res = $this->db->query($sql);
  1656. if ($res) {
  1657. while ($rec = $this->db->fetch_array($res)) {
  1658. $soc = new Societe($this->db);
  1659. $soc->fetch($rec['rowid']);
  1660. $thirdparties[] = $soc;
  1661. }
  1662. return $thirdparties;
  1663. } else {
  1664. $this->error = $this->db->error() . ' sql=' . $sql;
  1665. dol_syslog(get_class($this) . "::searchSocidByEmail " . $this->error, LOG_ERR);
  1666. return -1;
  1667. }
  1668. }
  1669. /**
  1670. * Search and fetch contacts by email
  1671. *
  1672. * @param string $email Email
  1673. * @param array $socid Limit to a thirdparty
  1674. * @param string $case Respect case
  1675. * @return array Array of contacts object
  1676. */
  1677. public function searchContactByEmail($email, $socid = '', $case = '')
  1678. {
  1679. $contacts = array();
  1680. // Generation requete recherche
  1681. $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "socpeople";
  1682. $sql .= " WHERE entity IN (" . getEntity('socpeople') . ")";
  1683. if (!empty($socid)) {
  1684. $sql .= " AND fk_soc='" . $this->db->escape($socid) . "'";
  1685. }
  1686. if (!empty($email)) {
  1687. $sql .= " AND ";
  1688. if (!$case) {
  1689. $sql .= "email LIKE '" . $this->db->escape($email) . "'";
  1690. } else {
  1691. $sql .= "email LIKE BINARY '" . $this->db->escape($email) . "'";
  1692. }
  1693. }
  1694. $res = $this->db->query($sql);
  1695. if ($res) {
  1696. while ($rec = $this->db->fetch_array($res)) {
  1697. include_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
  1698. $contactstatic = new Contact($this->db);
  1699. $contactstatic->fetch($rec['rowid']);
  1700. $contacts[] = $contactstatic;
  1701. }
  1702. return $contacts;
  1703. } else {
  1704. $this->error = $this->db->error() . ' sql=' . $sql;
  1705. dol_syslog(get_class($this) . "::searchContactByEmail " . $this->error, LOG_ERR);
  1706. return -1;
  1707. }
  1708. }
  1709. /**
  1710. * Define parent commany of current ticket
  1711. *
  1712. * @param int $id Id of thirdparty to set or '' to remove
  1713. * @return int <0 if KO, >0 if OK
  1714. */
  1715. public function setCustomer($id)
  1716. {
  1717. if ($this->id) {
  1718. $sql = "UPDATE " . MAIN_DB_PREFIX . "ticket";
  1719. $sql .= " SET fk_soc = " . ($id > 0 ? $id : "null");
  1720. $sql .= " WHERE rowid = " . $this->id;
  1721. dol_syslog(get_class($this) . '::setCustomer sql=' . $sql);
  1722. $resql = $this->db->query($sql);
  1723. if ($resql) {
  1724. return 1;
  1725. } else {
  1726. return -1;
  1727. }
  1728. } else {
  1729. return -1;
  1730. }
  1731. }
  1732. /**
  1733. * Define progression of current ticket
  1734. *
  1735. * @param int $percent Progression percent
  1736. * @return int <0 if KO, >0 if OK
  1737. */
  1738. public function setProgression($percent)
  1739. {
  1740. if ($this->id) {
  1741. $sql = "UPDATE " . MAIN_DB_PREFIX . "ticket";
  1742. $sql .= " SET progress = " . ($percent > 0 ? $percent : "null");
  1743. $sql .= " WHERE rowid = " . $this->id;
  1744. dol_syslog(get_class($this) . '::set_progression sql=' . $sql);
  1745. $resql = $this->db->query($sql);
  1746. if ($resql) {
  1747. return 1;
  1748. } else {
  1749. return -1;
  1750. }
  1751. } else {
  1752. return -1;
  1753. }
  1754. }
  1755. /**
  1756. * Link element with a project
  1757. * Override core function because of key name 'fk_project' used for this module
  1758. *
  1759. * @param int $projectid Project id to link element to
  1760. * @return int <0 if KO, >0 if OK
  1761. */
  1762. public function setProject($projectid)
  1763. {
  1764. if (!$this->table_element) {
  1765. dol_syslog(get_class($this) . "::setProject was called on objet with property table_element not defined", LOG_ERR);
  1766. return -1;
  1767. }
  1768. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element;
  1769. if ($projectid) {
  1770. $sql .= ' SET fk_project = ' . $projectid;
  1771. } else {
  1772. $sql .= ' SET fk_project = NULL';
  1773. }
  1774. $sql .= ' WHERE rowid = ' . $this->id;
  1775. dol_syslog(get_class($this) . "::setProject sql=" . $sql);
  1776. if ($this->db->query($sql)) {
  1777. $this->fk_project = $projectid;
  1778. return 1;
  1779. } else {
  1780. dol_print_error($this->db);
  1781. return -1;
  1782. }
  1783. }
  1784. /**
  1785. * Link element with a contract
  1786. *
  1787. * @param int $contractid Contract id to link element to
  1788. * @return int <0 if KO, >0 if OK
  1789. */
  1790. public function setContract($contractid)
  1791. {
  1792. if (!$this->table_element) {
  1793. dol_syslog(get_class($this) . "::setContract was called on objet with property table_element not defined", LOG_ERR);
  1794. return -1;
  1795. }
  1796. $result = $this->add_object_linked('contrat', $contractid);
  1797. if ($result) {
  1798. $this->fk_contract = $contractid;
  1799. return 1;
  1800. } else {
  1801. dol_print_error($this->db);
  1802. return -1;
  1803. }
  1804. }
  1805. /* gestion des contacts d'un ticket */
  1806. /**
  1807. * Return id des contacts interne de suivi
  1808. *
  1809. * @return array Liste des id contacts suivi ticket
  1810. */
  1811. public function getIdTicketInternalContact()
  1812. {
  1813. return $this->getIdContact('internal', 'SUPPORTTEC');
  1814. }
  1815. /**
  1816. * Retrieve informations about internal contacts
  1817. *
  1818. * @return array Array with datas : firstname, lastname, socid (-1 for internal users), email, code, libelle, status
  1819. */
  1820. public function getInfosTicketInternalContact()
  1821. {
  1822. return $this->listeContact(-1, 'internal');
  1823. }
  1824. /**
  1825. * Return id des contacts clients pour le suivi ticket
  1826. *
  1827. * @return array Liste des id contacts suivi ticket
  1828. */
  1829. public function getIdTicketCustomerContact()
  1830. {
  1831. return $this->getIdContact('external', 'SUPPORTCLI');
  1832. }
  1833. /**
  1834. * Retrieve informations about external contacts
  1835. *
  1836. * @return array Array with datas : firstname, lastname, socid (-1 for internal users), email, code, libelle, status
  1837. */
  1838. public function getInfosTicketExternalContact()
  1839. {
  1840. return $this->listeContact(-1, 'external');
  1841. }
  1842. /**
  1843. * Return id des contacts clients des intervenants
  1844. *
  1845. * @return array Liste des id contacts intervenants
  1846. */
  1847. public function getIdTicketInternalInvolvedContact()
  1848. {
  1849. return $this->getIdContact('internal', 'CONTRIBUTOR');
  1850. }
  1851. /**
  1852. * Return id des contacts clients des intervenants
  1853. *
  1854. * @return array Liste des id contacts intervenants
  1855. */
  1856. public function getIdTicketCustomerInvolvedContact()
  1857. {
  1858. return $this->getIdContact('external', 'CONTRIBUTOR');
  1859. }
  1860. /**
  1861. * Return id of all contacts for ticket
  1862. *
  1863. * @return array Array of contacts for tickets
  1864. */
  1865. public function getTicketAllContacts()
  1866. {
  1867. $array_contact = array();
  1868. $array_contact = $this->getIdTicketInternalContact($exclude_self);
  1869. $array_contact = array_merge($array_contact, $this->getIdTicketCustomerContact($exclude_self));
  1870. $array_contact = array_merge($array_contact, $this->getIdTicketInternalInvolvedContact($exclude_self));
  1871. $array_contact = array_merge($array_contact, $this->getIdTicketCustomerInvolvedContact($exclude_self));
  1872. return $array_contact;
  1873. }
  1874. /**
  1875. * Return id of all contacts for ticket
  1876. *
  1877. * @return array Array of contacts
  1878. */
  1879. public function getTicketAllCustomerContacts()
  1880. {
  1881. $array_contact = array();
  1882. $array_contact = array_merge($array_contact, $this->getIdTicketCustomerContact($exclude_self));
  1883. $array_contact = array_merge($array_contact, $this->getIdTicketCustomerInvolvedContact($exclude_self));
  1884. return $array_contact;
  1885. }
  1886. /**
  1887. * Send message
  1888. *
  1889. * @param string $subject Subject
  1890. * @param string $texte Message to send
  1891. * @return int <0 if KO, or number of changes if OK
  1892. */
  1893. public function messageSend($subject, $texte)
  1894. {
  1895. global $conf, $langs, $mysoc, $dolibarr_main_url_root;
  1896. $langs->load("other");
  1897. dol_syslog(get_class($this) . "::message_send action=$action, socid=$socid, texte=$texte, objet_type=$objet_type, objet_id=$objet_id, file=$file");
  1898. $internal_contacts = $this->getIdContact('internal', 'SUPPORTTEC');
  1899. $external_contacts = $this->getIdContact('external', 'SUPPORTTEC');
  1900. if ($result) {
  1901. $num = $this->db->num_rows($result);
  1902. $i = 0;
  1903. while ($i < $num) { // For each notification couple defined (third party/actioncode)
  1904. $obj = $this->db->fetch_object($result);
  1905. $sendto = $obj->firstname . " " . $obj->lastname . " <" . $obj->email . ">";
  1906. $actiondefid = $obj->adid;
  1907. if (dol_strlen($sendto))
  1908. {
  1909. include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
  1910. $application = ($conf->global->MAIN_APPLICATION_TITLE ? $conf->global->MAIN_APPLICATION_TITLE : 'Dolibarr ERP/CRM');
  1911. $subject = '[' . $application . '] ' . $langs->transnoentitiesnoconv("DolibarrNotification");
  1912. $message = $langs->transnoentities("YouReceiveMailBecauseOfNotification", $application, $mysoc->name) . "\n";
  1913. $message .= $langs->transnoentities("YouReceiveMailBecauseOfNotification2", $application, $mysoc->name) . "\n";
  1914. $message .= "\n";
  1915. $message .= $texte;
  1916. // Add link
  1917. $link = '';
  1918. switch ($objet_type) {
  1919. case 'ficheinter':
  1920. $link = '/fichinter/card.php?id=' . $objet_id;
  1921. break;
  1922. case 'propal':
  1923. $link = '/comm/propal.php?id=' . $objet_id;
  1924. break;
  1925. case 'facture':
  1926. $link = '/compta/facture/card.php?facid=' . $objet_id;
  1927. break;
  1928. case 'order':
  1929. $link = '/commande/card.php?facid=' . $objet_id;
  1930. break;
  1931. case 'order_supplier':
  1932. $link = '/fourn/commande/card.php?facid=' . $objet_id;
  1933. break;
  1934. }
  1935. // Define $urlwithroot
  1936. $urlwithouturlroot = preg_replace('/' . preg_quote(DOL_URL_ROOT, '/') . '$/i', '', trim($dolibarr_main_url_root));
  1937. $urlwithroot = $urlwithouturlroot . DOL_URL_ROOT; // This is to use external domain name found into config file
  1938. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  1939. if ($link) {
  1940. $message .= "\n" . $urlwithroot . $link;
  1941. }
  1942. $filename = basename($file);
  1943. $mimefile = dol_mimetype($file);
  1944. $msgishtml = 0;
  1945. $replyto = $conf->notification->email_from;
  1946. $message = dol_nl2br($message);
  1947. if (!empty($conf->global->TICKET_DISABLE_MAIL_AUTOCOPY_TO)) {
  1948. $old_MAIN_MAIL_AUTOCOPY_TO = $conf->global->MAIN_MAIL_AUTOCOPY_TO;
  1949. $conf->global->MAIN_MAIL_AUTOCOPY_TO = '';
  1950. }
  1951. $mailfile = new CMailFile(
  1952. $subject,
  1953. $sendto,
  1954. $replyto,
  1955. $message,
  1956. array($file),
  1957. array($mimefile),
  1958. array($filename[count($filename) - 1]),
  1959. '',
  1960. '',
  1961. 0,
  1962. $msgishtml
  1963. );
  1964. if ($mailfile->sendfile()) {
  1965. $now = dol_now();
  1966. $sendto = htmlentities($sendto);
  1967. $sql = "INSERT INTO " . MAIN_DB_PREFIX . "notify (daten, fk_action, fk_contact, objet_type, objet_id, email)";
  1968. $sql .= " VALUES ('" . $this->db->idate($now) . "', " . $actiondefid . ", " . $obj->cid . ", '" . $this->db->escape($objet_type) . "', " . $objet_id . ", '" . $this->db->escape($obj->email) . "')";
  1969. dol_syslog("Notify::send sql=" . $sql);
  1970. if (!$this->db->query($sql)) {
  1971. dol_print_error($this->db);
  1972. }
  1973. } else {
  1974. $this->error = $mailfile->error;
  1975. //dol_syslog("Notify::send ".$this->error, LOG_ERR);
  1976. }
  1977. if (!empty($conf->global->TICKET_DISABLE_MAIL_AUTOCOPY_TO)) {
  1978. $conf->global->MAIN_MAIL_AUTOCOPY_TO = $old_MAIN_MAIL_AUTOCOPY_TO;
  1979. }
  1980. }
  1981. $i++;
  1982. }
  1983. return $i;
  1984. } else {
  1985. $this->error = $this->db->error();
  1986. return -1;
  1987. }
  1988. }
  1989. /**
  1990. * Get array of all contacts for a ticket
  1991. * Override method of file commonobject.class.php to add phone number
  1992. *
  1993. * @param int $statut Status of lines to get (-1=all)
  1994. * @param string $source Source of contact: external or thirdparty (llx_socpeople) or internal (llx_user)
  1995. * @param int $list 0:Return array contains all properties, 1:Return array contains just id
  1996. * @param string $code Filter on this code of contact type ('SHIPPING', 'BILLING', ...)
  1997. * @return array Array of contacts
  1998. */
  1999. function listeContact($statut = -1, $source = 'external', $list = 0, $code = '')
  2000. {
  2001. global $langs;
  2002. $tab = array();
  2003. $sql = "SELECT ec.rowid, ec.statut as statuslink, ec.fk_socpeople as id, ec.fk_c_type_contact"; // This field contains id of llx_socpeople or id of llx_user
  2004. if ($source == 'internal') {
  2005. $sql .= ", '-1' as socid, t.statut as statuscontact";
  2006. }
  2007. if ($source == 'external' || $source == 'thirdparty') {
  2008. $sql .= ", t.fk_soc as socid, t.statut as statuscontact";
  2009. }
  2010. $sql .= ", t.civility, t.lastname as lastname, t.firstname, t.email";
  2011. if ($source == 'internal') {
  2012. $sql .= ", t.office_phone as phone, t.user_mobile as phone_mobile";
  2013. }
  2014. if ($source == 'external') {
  2015. $sql .= ", t.phone as phone, t.phone_mobile as phone_mobile, t.phone_perso as phone_perso";
  2016. }
  2017. $sql .= ", tc.source, tc.element, tc.code, tc.libelle";
  2018. $sql .= " FROM " . MAIN_DB_PREFIX . "c_type_contact tc";
  2019. $sql .= ", " . MAIN_DB_PREFIX . "element_contact ec";
  2020. if ($source == 'internal') {
  2021. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "user t on ec.fk_socpeople = t.rowid";
  2022. }
  2023. if ($source == 'external' || $source == 'thirdparty') {
  2024. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "socpeople t on ec.fk_socpeople = t.rowid";
  2025. }
  2026. $sql .= " WHERE ec.element_id =" . $this->id;
  2027. $sql .= " AND ec.fk_c_type_contact=tc.rowid";
  2028. $sql .= " AND tc.element='" . $this->db->escape($this->element) . "'";
  2029. if ($source == 'internal') {
  2030. $sql .= " AND tc.source = 'internal'";
  2031. }
  2032. if ($source == 'external' || $source == 'thirdparty') {
  2033. $sql .= " AND tc.source = 'external'";
  2034. }
  2035. $sql .= " AND tc.active=1";
  2036. if ($statut >= 0) {
  2037. $sql .= " AND ec.statut = '" . $statut . "'";
  2038. }
  2039. $sql .= " ORDER BY t.lastname ASC";
  2040. $resql = $this->db->query($sql);
  2041. if ($resql) {
  2042. $num = $this->db->num_rows($resql);
  2043. $i = 0;
  2044. while ($i < $num) {
  2045. $obj = $this->db->fetch_object($resql);
  2046. if (!$list) {
  2047. $transkey = "TypeContact_" . $obj->element . "_" . $obj->source . "_" . $obj->code;
  2048. $libelle_type = ($langs->trans($transkey) != $transkey ? $langs->trans($transkey) : $obj->libelle);
  2049. $tab[$i] = array(
  2050. 'source' => $obj->source,
  2051. 'socid' => $obj->socid,
  2052. 'id' => $obj->id,
  2053. 'nom' => $obj->lastname, // For backward compatibility
  2054. 'civility' => $obj->civility,
  2055. 'lastname' => $obj->lastname,
  2056. 'firstname' => $obj->firstname,
  2057. 'email' => $obj->email,
  2058. 'rowid' => $obj->rowid,
  2059. 'code' => $obj->code,
  2060. 'libelle' => $libelle_type,
  2061. 'status' => $obj->statuslink,
  2062. 'statuscontact'=>$obj->statuscontact,
  2063. 'fk_c_type_contact' => $obj->fk_c_type_contact,
  2064. 'phone' => $obj->phone,
  2065. 'phone_mobile' => $obj->phone_mobile);
  2066. } else {
  2067. $tab[$i] = $obj->id;
  2068. }
  2069. $i++;
  2070. }
  2071. return $tab;
  2072. } else {
  2073. $this->error = $this->db->error();
  2074. dol_print_error($this->db);
  2075. return -1;
  2076. }
  2077. }
  2078. /**
  2079. * Get a default reference.
  2080. *
  2081. * @param Societe $thirdparty Thirdparty
  2082. * @return string Reference
  2083. */
  2084. public function getDefaultRef($thirdparty = '')
  2085. {
  2086. global $conf;
  2087. $defaultref = '';
  2088. $modele = empty($conf->global->TICKETSUP_ADDON) ? 'mod_ticket_simple' : $conf->global->TICKETSUP_ADDON;
  2089. // Search template files
  2090. $file = '';
  2091. $classname = '';
  2092. $filefound = 0;
  2093. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  2094. foreach ($dirmodels as $reldir) {
  2095. $file = dol_buildpath($reldir . "core/modules/ticket/" . $modele . '.php', 0);
  2096. if (file_exists($file)) {
  2097. $filefound = 1;
  2098. $classname = $modele;
  2099. break;
  2100. }
  2101. }
  2102. if ($filefound) {
  2103. $result = dol_include_once($reldir . "core/modules/ticket/" . $modele . '.php');
  2104. $modTicket = new $classname;
  2105. $defaultref = $modTicket->getNextValue($thirdparty, $this);
  2106. }
  2107. if (is_numeric($defaultref) && $defaultref <= 0) {
  2108. $defaultref = '';
  2109. }
  2110. return $defaultref;
  2111. }
  2112. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  2113. /**
  2114. * Return if at least one photo is available
  2115. *
  2116. * @param string $sdir Directory to scan
  2117. * @return boolean True if at least one photo is available, False if not
  2118. */
  2119. function is_photo_available($sdir)
  2120. {
  2121. // phpcs:enable
  2122. include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
  2123. global $conf;
  2124. $dir = $sdir . '/';
  2125. $nbphoto = 0;
  2126. $dir_osencoded = dol_osencode($dir);
  2127. if (file_exists($dir_osencoded)) {
  2128. $handle = opendir($dir_osencoded);
  2129. if (is_resource($handle)) {
  2130. while (($file = readdir($handle)) != false) {
  2131. if (!utf8_check($file)) {
  2132. $file = utf8_encode($file);
  2133. }
  2134. // To be sure data is stored in UTF8 in memory
  2135. if (dol_is_file($dir . $file)) {
  2136. return true;
  2137. }
  2138. }
  2139. }
  2140. }
  2141. return false;
  2142. }
  2143. }
  2144. /**
  2145. * Ticket line Class
  2146. */
  2147. class TicketsLine
  2148. {
  2149. /**
  2150. * @var int ID
  2151. */
  2152. public $id;
  2153. /**
  2154. * @var string $ref Ticket reference
  2155. */
  2156. public $ref;
  2157. /**
  2158. * Hash to identify ticket
  2159. */
  2160. public $track_id;
  2161. /**
  2162. * @var int Thirdparty ID
  2163. */
  2164. public $fk_soc;
  2165. /**
  2166. * Project ID
  2167. */
  2168. public $fk_project;
  2169. /**
  2170. * Person email who have create ticket
  2171. */
  2172. public $origin_email;
  2173. /**
  2174. * User id who have create ticket
  2175. */
  2176. public $fk_user_create;
  2177. /**
  2178. * User id who have ticket assigned
  2179. */
  2180. public $fk_user_assign;
  2181. /**
  2182. * Ticket subject
  2183. */
  2184. public $subject;
  2185. /**
  2186. * Ticket message
  2187. */
  2188. public $message;
  2189. /**
  2190. * Ticket statut
  2191. */
  2192. public $fk_statut;
  2193. /**
  2194. * State resolution
  2195. */
  2196. public $resolution;
  2197. /**
  2198. * Progress in percent
  2199. */
  2200. public $progress;
  2201. /**
  2202. * Duration for ticket
  2203. */
  2204. public $timing;
  2205. /**
  2206. * Type code
  2207. */
  2208. public $type_code;
  2209. /**
  2210. * Category code
  2211. */
  2212. public $category_code;
  2213. /**
  2214. * Severity code
  2215. */
  2216. public $severity_code;
  2217. /**
  2218. * Type label
  2219. */
  2220. public $type_label;
  2221. /**
  2222. * Category label
  2223. */
  2224. public $category_label;
  2225. /**
  2226. * Severity label
  2227. */
  2228. public $severity_label;
  2229. /**
  2230. * Creation date
  2231. */
  2232. public $datec = '';
  2233. /**
  2234. * Read date
  2235. */
  2236. public $date_read = '';
  2237. /**
  2238. * Close ticket date
  2239. */
  2240. public $date_close = '';
  2241. }