ticket.class.php 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139
  1. <?php
  2. /* Copyright (C) 2013-2018 Jean-François Ferry <hello@librethic.io>
  3. * Copyright (C) 2016 Christophe Battarel <christophe@altairis.fr>
  4. * Copyright (C) 2019-2023 Frédéric France <frederic.france@netlogic.fr>
  5. * Copyright (C) 2020 Laurent Destailleur <eldy@users.sourceforge.net>
  6. * Copyright (C) 2023 Charlene Benke <charlene@patas-monkey.com>
  7. * Copyright (C) 2023 Benjamin Falière <benjamin.faliere@altairis.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file ticket/class/ticket.class.php
  24. * \ingroup ticket
  25. * \brief Class file for object ticket
  26. */
  27. // Put here all includes required by your class file
  28. require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
  29. require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/ticket.lib.php';
  31. /**
  32. * Class to manage ticket
  33. */
  34. class Ticket extends CommonObject
  35. {
  36. /**
  37. * @var DoliDb Database handler
  38. */
  39. public $db;
  40. /**
  41. * @var string ID to identify managed object
  42. */
  43. public $element = 'ticket';
  44. /**
  45. * @var string Name of table without prefix where object is stored
  46. */
  47. public $table_element = 'ticket';
  48. /**
  49. * @var string Name of field for link to tickets
  50. */
  51. public $fk_element = 'fk_ticket';
  52. /**
  53. * @var int Does ticketcore support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  54. */
  55. public $ismultientitymanaged = 1;
  56. /**
  57. * @var int Does ticketcore support extrafields ? 0=No, 1=Yes
  58. */
  59. public $isextrafieldmanaged = 1;
  60. /**
  61. * @var string String with name of icon for ticketcore. Must be the part after the 'object_' into object_ticketcore.png
  62. */
  63. public $picto = 'ticket';
  64. /**
  65. * @var string Hash to identify ticket publically
  66. */
  67. public $track_id;
  68. /**
  69. * @var int Thirdparty ID
  70. */
  71. public $fk_soc;
  72. public $socid;
  73. /**
  74. * @var int Project ID
  75. */
  76. public $fk_project;
  77. /**
  78. * @var int Contract ID
  79. */
  80. public $fk_contract;
  81. /**
  82. * @var string Person email who have create ticket
  83. */
  84. public $origin_email;
  85. /**
  86. * @var int User id who have create ticket
  87. */
  88. public $fk_user_create;
  89. /**
  90. * @var int User id who have ticket assigned
  91. */
  92. public $fk_user_assign;
  93. /**
  94. * var string Ticket subject
  95. */
  96. public $subject;
  97. /**
  98. * @var string Ticket message
  99. */
  100. public $message;
  101. /**
  102. * @var string Private message
  103. */
  104. public $private;
  105. /**
  106. * @var int Ticket statut
  107. * @deprecated use status
  108. * @see $status
  109. */
  110. public $fk_statut;
  111. /**
  112. * @var int Ticket status
  113. */
  114. public $status;
  115. /**
  116. * @var string State resolution
  117. */
  118. public $resolution;
  119. /**
  120. * @var int Progress in percent
  121. */
  122. public $progress;
  123. /**
  124. * @var string Duration for ticket
  125. */
  126. public $timing;
  127. /**
  128. * @var string Type code
  129. */
  130. public $type_code;
  131. /**
  132. * @var string Category code
  133. */
  134. public $category_code;
  135. /**
  136. * @var string Severity code
  137. */
  138. public $severity_code;
  139. /**
  140. * Type label
  141. */
  142. public $type_label;
  143. /**
  144. * Category label
  145. */
  146. public $category_label;
  147. /**
  148. * Severity label
  149. */
  150. public $severity_label;
  151. /**
  152. * Email from user
  153. */
  154. public $email_from;
  155. /**
  156. * @var int Creation date
  157. */
  158. public $datec = '';
  159. /**
  160. * @var int Last modification date
  161. */
  162. public $tms = '';
  163. /**
  164. * @var int Read date
  165. */
  166. public $date_read = '';
  167. /**
  168. * @var int Last message date
  169. */
  170. public $date_last_msg_sent = '';
  171. /**
  172. * @var int Close ticket date
  173. */
  174. public $date_close = '';
  175. /**
  176. * @var array cache_types_tickets
  177. */
  178. public $cache_types_tickets;
  179. /**
  180. * @var array tickets categories
  181. */
  182. public $cache_category_tickets;
  183. /**
  184. * @var array tickets severity
  185. */
  186. public $cache_severity_tickets;
  187. /**
  188. * @var array cache msgs ticket
  189. */
  190. public $cache_msgs_ticket;
  191. /**
  192. * @var array status labels
  193. */
  194. public $labelStatus;
  195. /**
  196. * @var array status short labels
  197. */
  198. public $labelStatusShort;
  199. /**
  200. * @var int Notify thirdparty at create
  201. */
  202. public $notify_tiers_at_create;
  203. /**
  204. * @var string Email MSGID
  205. */
  206. public $email_msgid;
  207. /**
  208. * @var string Email Date
  209. */
  210. public $email_date;
  211. /**
  212. * @var string IP address
  213. */
  214. public $ip;
  215. /**
  216. * @var Ticket $oldcopy State of this ticket as it was stored before an update operation (for triggers)
  217. */
  218. public $oldcopy;
  219. /**
  220. * @var Ticket[] array of Tickets
  221. */
  222. public $lines;
  223. /**
  224. * @var string Regex pour les images
  225. */
  226. public $regeximgext = '\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.webp|\.xpm|\.xbm'; // See also into images.lib.php
  227. /**
  228. * Status
  229. */
  230. const STATUS_NOT_READ = 0;
  231. const STATUS_READ = 1;
  232. const STATUS_ASSIGNED = 2;
  233. const STATUS_IN_PROGRESS = 3;
  234. const STATUS_NEED_MORE_INFO = 5; // waiting requester feedback
  235. const STATUS_WAITING = 7; // on hold
  236. const STATUS_CLOSED = 8; // Closed - Solved
  237. const STATUS_CANCELED = 9; // Closed - Not solved
  238. /**
  239. * 'type' field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'sellist:TableName:LabelFieldName[:KeyFieldName[:KeyFieldParent[:Filter]]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'text:none', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password')
  240. * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
  241. * 'label' the translation key.
  242. * 'picto' is code of a picto to show before value in forms
  243. * 'enabled' is a condition when the field must be managed (Example: 1 or '$conf->global->MY_SETUP_PARAM)
  244. * 'position' is the sort order of field.
  245. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  246. * 'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing)
  247. * 'noteditable' says if field is not editable (1 or 0)
  248. * 'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created.
  249. * 'index' if we want an index in database.
  250. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  251. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  252. * 'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8).
  253. * 'css' and 'cssview' and 'csslist' is the CSS style to use on field. 'css' is used in creation and update. 'cssview' is used in view mode. 'csslist' is used for columns in lists. For example: 'maxwidth200', 'wordbreak', 'tdoverflowmax200'
  254. * 'help' is a 'TranslationString' to use to show a tooltip on field. You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click.
  255. * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
  256. * 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code.
  257. * 'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
  258. * 'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1.
  259. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  260. *
  261. * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
  262. */
  263. // BEGIN MODULEBUILDER PROPERTIES
  264. public $fields = array(
  265. 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'visible'=>-2, 'enabled'=>1, 'position'=>1, 'notnull'=>1, 'index'=>1, 'comment'=>"Id"),
  266. 'entity' => array('type'=>'integer', 'label'=>'Entity', 'visible'=>0, 'enabled'=>1, 'position'=>5, 'notnull'=>1, 'index'=>1),
  267. 'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'visible'=>1, 'enabled'=>1, 'position'=>10, 'notnull'=>1, 'index'=>1, 'searchall'=>1, 'comment'=>"Reference of object", 'css'=>'', 'showoncombobox'=>1),
  268. 'track_id' => array('type'=>'varchar(255)', 'label'=>'TicketTrackId', 'visible'=>-2, 'enabled'=>1, 'position'=>11, 'notnull'=>-1, 'searchall'=>1, 'help'=>"Help text"),
  269. 'fk_user_create' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Author', 'visible'=>1, 'enabled'=>1, 'position'=>15, 'notnull'=>1, 'csslist'=>'tdoverflowmax100 maxwidth150onsmartphone'),
  270. 'origin_email' => array('type'=>'mail', 'label'=>'OriginEmail', 'visible'=>-2, 'enabled'=>1, 'position'=>16, 'notnull'=>1, 'index'=>1, 'searchall'=>1, 'comment'=>"Reference of object", 'csslist'=>'tdoverflowmax150'),
  271. 'subject' => array('type'=>'varchar(255)', 'label'=>'Subject', 'visible'=>1, 'enabled'=>1, 'position'=>18, 'notnull'=>-1, 'searchall'=>1, 'help'=>"", 'css'=>'maxwidth200 tdoverflowmax200', 'autofocusoncreate'=>1),
  272. 'type_code' => array('type'=>'varchar(32)', 'label'=>'Type', 'visible'=>1, 'enabled'=>1, 'position'=>20, 'notnull'=>-1, 'help'=>"", 'csslist'=>'maxwidth125 tdoverflowmax50'),
  273. 'category_code' => array('type'=>'varchar(32)', 'label'=>'TicketCategory', 'visible'=>-1, 'enabled'=>1, 'position'=>21, 'notnull'=>-1, 'help'=>"", 'css'=>'maxwidth100 tdoverflowmax200'),
  274. 'severity_code' => array('type'=>'varchar(32)', 'label'=>'Severity', 'visible'=>1, 'enabled'=>1, 'position'=>22, 'notnull'=>-1, 'help'=>"", 'css'=>'maxwidth100'),
  275. 'fk_soc' => array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'visible'=>1, 'enabled'=>'isModEnabled("societe")', 'position'=>50, 'notnull'=>-1, 'index'=>1, 'searchall'=>1, 'help'=>"OrganizationEventLinkToThirdParty", 'css'=>'tdoverflowmax150 maxwidth150onsmartphone'),
  276. 'notify_tiers_at_create' => array('type'=>'integer', 'label'=>'NotifyThirdparty', 'visible'=>-1, 'enabled'=>0, 'position'=>51, 'notnull'=>1, 'index'=>1),
  277. 'fk_project' => array('type'=>'integer:Project:projet/class/project.class.php', 'label'=>'Project', 'visible'=>-1, 'enabled'=>'$conf->project->enabled', 'position'=>52, 'notnull'=>-1, 'index'=>1, 'help'=>"LinkToProject"),
  278. 'fk_contract' => array('type'=>'integer:Contrat:contrat/class/contrat.class.php', 'label'=>'Contract', 'visible'=>-1, 'enabled'=>'$conf->contract->enabled', 'position'=>53, 'notnull'=>-1, 'index'=>1, 'help'=>"LinkToContract"),
  279. //'timing' => array('type'=>'varchar(20)', 'label'=>'Timing', 'visible'=>-1, 'enabled'=>1, 'position'=>42, 'notnull'=>-1, 'help'=>""), // what is this ?
  280. 'datec' => array('type'=>'datetime', 'label'=>'DateCreation', 'visible'=>1, 'enabled'=>1, 'position'=>500, 'notnull'=>1, 'csslist'=>'nowraponall'),
  281. 'date_read' => array('type'=>'datetime', 'label'=>'TicketReadOn', 'visible'=>-1, 'enabled'=>1, 'position'=>501, 'notnull'=>1),
  282. 'date_last_msg_sent' => array('type'=>'datetime', 'label'=>'TicketLastMessageDate', 'visible'=>0, 'enabled'=>1, 'position'=>502, 'notnull'=>-1),
  283. 'fk_user_assign' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'AssignedTo', 'visible'=>1, 'enabled'=>1, 'position'=>505, 'notnull'=>1, 'csslist'=>'tdoverflowmax100 maxwidth150onsmartphone'),
  284. 'date_close' => array('type'=>'datetime', 'label'=>'TicketCloseOn', 'visible'=>-1, 'enabled'=>1, 'position'=>510, 'notnull'=>1),
  285. 'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'visible'=>-1, 'enabled'=>1, 'position'=>520, 'notnull'=>1),
  286. 'message' => array('type'=>'html', 'label'=>'Message', 'visible'=>-2, 'enabled'=>1, 'position'=>540, 'notnull'=>-1,),
  287. 'email_msgid' => array('type'=>'varchar(255)', 'label'=>'EmailMsgID', 'visible'=>-2, 'enabled'=>1, 'position'=>540, 'notnull'=>-1, 'help'=>'EmailMsgIDDesc', 'csslist'=>'tdoverflowmax100'),
  288. 'email_date' => array('type'=>'datetime', 'label'=>'EmailDate', 'visible'=>-2, 'enabled'=>1, 'position'=>541),
  289. 'progress' => array('type'=>'integer', 'label'=>'Progression', 'visible'=>-1, 'enabled'=>1, 'position'=>540, 'notnull'=>-1, 'css'=>'right', 'help'=>"", 'isameasure'=>2, 'csslist'=>'width50'),
  290. 'resolution' => array('type'=>'integer', 'label'=>'Resolution', 'visible'=>-1, 'enabled'=>'getDolGlobalString("TICKET_ENABLE_RESOLUTION")', 'position'=>550, 'notnull'=>1),
  291. '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 => 'SolvedClosed', 9 => 'Deleted')),
  292. 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>900),
  293. );
  294. // END MODULEBUILDER PROPERTIES
  295. /**
  296. * Constructor
  297. *
  298. * @param DoliDb $db Database handler
  299. */
  300. public function __construct($db)
  301. {
  302. global $conf;
  303. $this->db = $db;
  304. $this->labelStatusShort = array(
  305. self::STATUS_NOT_READ => 'Unread',
  306. self::STATUS_READ => 'Read',
  307. self::STATUS_ASSIGNED => 'Assigned',
  308. self::STATUS_IN_PROGRESS => 'InProgress',
  309. self::STATUS_WAITING => 'OnHold',
  310. self::STATUS_NEED_MORE_INFO => 'NeedMoreInformationShort',
  311. self::STATUS_CLOSED => 'SolvedClosed',
  312. self::STATUS_CANCELED => 'Canceled'
  313. );
  314. $this->labelStatus = array(
  315. self::STATUS_NOT_READ => 'Unread',
  316. self::STATUS_READ => 'Read',
  317. self::STATUS_ASSIGNED => 'Assigned',
  318. self::STATUS_IN_PROGRESS => 'InProgress',
  319. self::STATUS_WAITING => 'OnHold',
  320. self::STATUS_NEED_MORE_INFO => 'NeedMoreInformation',
  321. self::STATUS_CLOSED => 'SolvedClosed',
  322. self::STATUS_CANCELED => 'Canceled'
  323. );
  324. }
  325. /**
  326. * Check properties of ticket are ok (like ref, track_id, ...).
  327. * All properties must be already loaded on object (this->ref, this->track_id, ...).
  328. *
  329. * @return int 0 if OK, <0 if KO
  330. */
  331. private function verify()
  332. {
  333. $this->errors = array();
  334. $result = 0;
  335. // Clean parameters
  336. if (isset($this->ref)) {
  337. $this->ref = trim($this->ref);
  338. }
  339. if (isset($this->track_id)) {
  340. $this->track_id = trim($this->track_id);
  341. }
  342. if (isset($this->fk_soc)) {
  343. $this->fk_soc = (int) $this->fk_soc;
  344. }
  345. if (isset($this->fk_project)) {
  346. $this->fk_project = (int) $this->fk_project;
  347. }
  348. if (isset($this->origin_email)) {
  349. $this->origin_email = trim($this->origin_email);
  350. }
  351. if (isset($this->fk_user_create)) {
  352. $this->fk_user_create = (int) $this->fk_user_create;
  353. }
  354. if (isset($this->fk_user_assign)) {
  355. $this->fk_user_assign = (int) $this->fk_user_assign;
  356. }
  357. if (isset($this->subject)) {
  358. $this->subject = trim($this->subject);
  359. }
  360. if (isset($this->message)) {
  361. $this->message = trim($this->message);
  362. if (dol_strlen($this->message) > 65000) {
  363. $this->errors[] = 'ErrorFieldTooLong';
  364. dol_syslog(get_class($this).'::create error -1 message too long', LOG_ERR);
  365. $result = -1;
  366. }
  367. }
  368. if (isset($this->fk_statut)) {
  369. $this->fk_statut = (int) $this->fk_statut;
  370. }
  371. if (isset($this->resolution)) {
  372. $this->resolution = trim($this->resolution);
  373. }
  374. if (isset($this->progress)) {
  375. $this->progress = trim($this->progress);
  376. }
  377. if (isset($this->timing)) {
  378. $this->timing = trim($this->timing);
  379. }
  380. if (isset($this->type_code)) {
  381. $this->type_code = trim($this->type_code);
  382. }
  383. if (isset($this->category_code)) {
  384. $this->category_code = trim($this->category_code);
  385. }
  386. if (isset($this->severity_code)) {
  387. $this->severity_code = trim($this->severity_code);
  388. }
  389. if (empty($this->ref)) {
  390. $this->errors[] = 'ErrorTicketRefRequired';
  391. dol_syslog(get_class($this)."::create error -1 ref null", LOG_ERR);
  392. $result = -1;
  393. }
  394. return $result;
  395. }
  396. /**
  397. * Create object into database
  398. *
  399. * @param User $user User that creates
  400. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  401. * @return int <0 if KO, Id of created object if OK
  402. */
  403. public function create($user, $notrigger = 0)
  404. {
  405. global $conf;
  406. $error = 0;
  407. // Clean parameters
  408. $this->datec = dol_now();
  409. if (empty($this->track_id)) {
  410. $this->track_id = generate_random_id(16);
  411. }
  412. // Check more parameters
  413. // If error, this->errors[] is filled
  414. $result = $this->verify();
  415. if ($result >= 0) {
  416. $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
  417. // Insert request
  418. $sql = "INSERT INTO ".MAIN_DB_PREFIX."ticket(";
  419. $sql .= "ref,";
  420. $sql .= "track_id,";
  421. $sql .= "fk_soc,";
  422. $sql .= "fk_project,";
  423. $sql .= "fk_contract,";
  424. $sql .= "origin_email,";
  425. $sql .= "fk_user_create,";
  426. $sql .= "fk_user_assign,";
  427. $sql .= "email_msgid,";
  428. $sql .= "email_date,";
  429. $sql .= "subject,";
  430. $sql .= "message,";
  431. $sql .= "fk_statut,";
  432. $sql .= "resolution,";
  433. $sql .= "progress,";
  434. $sql .= "timing,";
  435. $sql .= "type_code,";
  436. $sql .= "category_code,";
  437. $sql .= "severity_code,";
  438. $sql .= "datec,";
  439. $sql .= "date_read,";
  440. $sql .= "date_close,";
  441. $sql .= "entity,";
  442. $sql .= "notify_tiers_at_create,";
  443. $sql .= "ip";
  444. $sql .= ") VALUES (";
  445. $sql .= " ".(!isset($this->ref) ? '' : "'".$this->db->escape($this->ref)."'").",";
  446. $sql .= " ".(!isset($this->track_id) ? 'NULL' : "'".$this->db->escape($this->track_id)."'").",";
  447. $sql .= " ".($this->fk_soc > 0 ? $this->db->escape($this->fk_soc) : "null").",";
  448. $sql .= " ".($this->fk_project > 0 ? $this->db->escape($this->fk_project) : "null").",";
  449. $sql .= " ".($this->fk_contract > 0 ? $this->db->escape($this->fk_contract) : "null").",";
  450. $sql .= " ".(!isset($this->origin_email) ? 'NULL' : "'".$this->db->escape($this->origin_email)."'").",";
  451. $sql .= " ".(!isset($this->fk_user_create) ? ($user->id > 0 ? $user->id : 'NULL') : ($this->fk_user_create > 0 ? $this->fk_user_create : 'NULL')).",";
  452. $sql .= " ".($this->fk_user_assign > 0 ? $this->fk_user_assign : 'NULL').",";
  453. $sql .= " ".(empty($this->email_msgid) ? 'NULL' : "'".$this->db->escape($this->email_msgid)."'").",";
  454. $sql .= " ".(empty($this->email_date) ? 'NULL' : "'".$this->db->idate($this->email_date)."'").",";
  455. $sql .= " ".(!isset($this->subject) ? 'NULL' : "'".$this->db->escape($this->subject)."'").",";
  456. $sql .= " ".(!isset($this->message) ? 'NULL' : "'".$this->db->escape($this->message)."'").",";
  457. $sql .= " ".(!isset($this->fk_statut) ? '0' : "'".$this->db->escape($this->fk_statut)."'").",";
  458. $sql .= " ".(!isset($this->resolution) ? 'NULL' : "'".$this->db->escape($this->resolution)."'").",";
  459. $sql .= " ".(!isset($this->progress) ? '0' : "'".$this->db->escape($this->progress)."'").",";
  460. $sql .= " ".(!isset($this->timing) ? 'NULL' : "'".$this->db->escape($this->timing)."'").",";
  461. $sql .= " ".(!isset($this->type_code) ? 'NULL' : "'".$this->db->escape($this->type_code)."'").",";
  462. $sql .= " ".(empty($this->category_code) || $this->category_code == '-1' ? 'NULL' : "'".$this->db->escape($this->category_code)."'").",";
  463. $sql .= " ".(!isset($this->severity_code) ? 'NULL' : "'".$this->db->escape($this->severity_code)."'").",";
  464. $sql .= " ".(!isset($this->datec) || dol_strlen($this->datec) == 0 ? 'NULL' : "'".$this->db->idate($this->datec)."'").",";
  465. $sql .= " ".(!isset($this->date_read) || dol_strlen($this->date_read) == 0 ? 'NULL' : "'".$this->db->idate($this->date_read)."'").",";
  466. $sql .= " ".(!isset($this->date_close) || dol_strlen($this->date_close) == 0 ? 'NULL' : "'".$this->db->idate($this->date_close)."'");
  467. $sql .= ", ".((int) $this->entity);
  468. $sql .= ", ".(!isset($this->notify_tiers_at_create) ? '1' : "'".$this->db->escape($this->notify_tiers_at_create)."'");
  469. $sql .= ", ".(!isset($this->ip) ? 'NULL' : "'".$this->db->escape($this->ip)."'");
  470. $sql .= ")";
  471. $this->db->begin();
  472. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  473. $resql = $this->db->query($sql);
  474. if (!$resql) {
  475. $error++;
  476. $this->errors[] = "Error ".$this->db->lasterror();
  477. }
  478. if (!$error) {
  479. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."ticket");
  480. }
  481. if (!$error && getDolGlobalString('TICKET_ADD_AUTHOR_AS_CONTACT')) {
  482. // add creator as contributor
  483. if ($this->add_contact($user->id, 'CONTRIBUTOR', 'internal') < 0) {
  484. $error++;
  485. }
  486. }
  487. if (!$error && $this->fk_user_assign > 0) {
  488. if ($this->add_contact($this->fk_user_assign, 'SUPPORTTEC', 'internal') < 0) {
  489. $error++;
  490. }
  491. }
  492. //Update extrafield
  493. if (!$error) {
  494. $result = $this->insertExtraFields();
  495. if ($result < 0) {
  496. $error++;
  497. }
  498. }
  499. if (!$error && !$notrigger) {
  500. // Call trigger
  501. $result = $this->call_trigger('TICKET_CREATE', $user);
  502. if ($result < 0) {
  503. $error++;
  504. }
  505. // End call triggers
  506. }
  507. // Commit or rollback
  508. if ($error) {
  509. foreach ($this->errors as $errmsg) {
  510. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  511. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  512. }
  513. $this->db->rollback();
  514. return -1 * $error;
  515. } else {
  516. $this->db->commit();
  517. return $this->id;
  518. }
  519. } else {
  520. $this->db->rollback();
  521. dol_syslog(get_class($this)."::Create fails verify ".join(',', $this->errors), LOG_WARNING);
  522. return -3;
  523. }
  524. }
  525. /**
  526. * Load object in memory from the database
  527. *
  528. * @param int $id Id object
  529. * @param string $ref Ref
  530. * @param string $track_id Track id, a hash like ref
  531. * @param string $email_msgid Email msgid
  532. * @return int <0 if KO, >0 if OK
  533. */
  534. public function fetch($id = '', $ref = '', $track_id = '', $email_msgid = '')
  535. {
  536. global $langs;
  537. // Check parameters
  538. if (empty($id) && empty($ref) && empty($track_id) && empty($email_msgid)) {
  539. $this->error = 'ErrorWrongParameters';
  540. dol_print_error('', get_class($this)."::fetch ".$this->error);
  541. return -1;
  542. }
  543. $sql = "SELECT";
  544. $sql .= " t.rowid,";
  545. $sql .= " t.entity,";
  546. $sql .= " t.ref,";
  547. $sql .= " t.track_id,";
  548. $sql .= " t.fk_soc,";
  549. $sql .= " t.fk_project,";
  550. $sql .= " t.fk_contract,";
  551. $sql .= " t.origin_email,";
  552. $sql .= " t.fk_user_create,";
  553. $sql .= " t.fk_user_assign,";
  554. $sql .= " t.email_msgid,";
  555. $sql .= " t.email_date,";
  556. $sql .= " t.subject,";
  557. $sql .= " t.message,";
  558. $sql .= " t.fk_statut as status,";
  559. $sql .= " t.resolution,";
  560. $sql .= " t.progress,";
  561. $sql .= " t.timing,";
  562. $sql .= " t.type_code,";
  563. $sql .= " t.category_code,";
  564. $sql .= " t.severity_code,";
  565. $sql .= " t.datec,";
  566. $sql .= " t.date_read,";
  567. $sql .= " t.date_last_msg_sent,";
  568. $sql .= " t.date_close,";
  569. $sql .= " t.tms,";
  570. $sql .= " t.ip,";
  571. $sql .= " type.label as type_label, category.label as category_label, severity.label as severity_label";
  572. $sql .= " FROM ".MAIN_DB_PREFIX."ticket as t";
  573. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_ticket_type as type ON type.code=t.type_code";
  574. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_ticket_category as category ON category.code=t.category_code";
  575. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_ticket_severity as severity ON severity.code=t.severity_code";
  576. if ($id) {
  577. $sql .= " WHERE t.rowid = ".((int) $id);
  578. } else {
  579. $sql .= " WHERE t.entity IN (".getEntity($this->element, 1).")";
  580. if (!empty($ref)) {
  581. $sql .= " AND t.ref = '".$this->db->escape($ref)."'";
  582. } elseif ($track_id) {
  583. $sql .= " AND t.track_id = '".$this->db->escape($track_id)."'";
  584. } else {
  585. $sql .= " AND t.email_msgid = '".$this->db->escape($email_msgid)."'";
  586. }
  587. }
  588. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  589. $resql = $this->db->query($sql);
  590. if ($resql) {
  591. if ($this->db->num_rows($resql)) {
  592. $obj = $this->db->fetch_object($resql);
  593. $this->id = $obj->rowid;
  594. $this->entity = $obj->entity;
  595. $this->ref = $obj->ref;
  596. $this->track_id = $obj->track_id;
  597. $this->fk_soc = $obj->fk_soc;
  598. $this->socid = $obj->fk_soc; // for fetch_thirdparty() method
  599. $this->fk_project = $obj->fk_project;
  600. $this->fk_contract = $obj->fk_contract;
  601. $this->origin_email = $obj->origin_email;
  602. $this->fk_user_create = $obj->fk_user_create;
  603. $this->fk_user_assign = $obj->fk_user_assign;
  604. $this->email_msgid = $obj->email_msgid;
  605. $this->email_date = $this->db->jdate($obj->email_date);
  606. $this->subject = $obj->subject;
  607. $this->message = $obj->message;
  608. $this->ip = $obj->ip;
  609. $this->status = $obj->status;
  610. $this->fk_statut = $this->status; // For backward compatibility
  611. $this->resolution = $obj->resolution;
  612. $this->progress = $obj->progress;
  613. $this->timing = $obj->timing;
  614. $this->type_code = $obj->type_code;
  615. $label_type = ($langs->trans("TicketTypeShort".$obj->type_code) != ("TicketTypeShort".$obj->type_code) ? $langs->trans("TicketTypeShort".$obj->type_code) : ($obj->type_label != '-' ? $obj->type_label : ''));
  616. $this->type_label = $label_type;
  617. $this->category_code = $obj->category_code;
  618. $label_category = ($langs->trans("TicketCategoryShort".$obj->category_code) != ("TicketCategoryShort".$obj->category_code) ? $langs->trans("TicketCategoryShort".$obj->category_code) : ($obj->category_label != '-' ? $obj->category_label : ''));
  619. $this->category_label = $label_category;
  620. $this->severity_code = $obj->severity_code;
  621. $label_severity = ($langs->trans("TicketSeverityShort".$obj->severity_code) != ("TicketSeverityShort".$obj->severity_code) ? $langs->trans("TicketSeverityShort".$obj->severity_code) : ($obj->severity_label != '-' ? $obj->severity_label : ''));
  622. $this->severity_label = $label_severity;
  623. $this->datec = $this->db->jdate($obj->datec);
  624. $this->date_creation = $this->db->jdate($obj->datec);
  625. $this->date_read = $this->db->jdate($obj->date_read);
  626. $this->date_validation = $this->db->jdate($obj->date_read);
  627. $this->date_last_msg_sent = $this->db->jdate($obj->date_last_msg_sent);
  628. $this->date_close = $this->db->jdate($obj->date_close);
  629. $this->tms = $this->db->jdate($obj->tms);
  630. $this->date_modification = $this->db->jdate($obj->tms);
  631. $this->fetch_optionals();
  632. $this->db->free($resql);
  633. return 1;
  634. } else {
  635. return 0;
  636. }
  637. } else {
  638. $this->error = "Error ".$this->db->lasterror();
  639. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  640. return -1;
  641. }
  642. }
  643. /**
  644. * Load all objects in memory from database
  645. *
  646. * @param User $user User for action
  647. * @param string $sortorder Sort order
  648. * @param string $sortfield Sort field
  649. * @param int $limit page number
  650. * @param int $offset Offset for query
  651. * @param int $arch archive or not (not used)
  652. * @param array $filter Filter for query
  653. * @return int <0 if KO, >0 if OK
  654. */
  655. public function fetchAll($user, $sortorder = 'ASC', $sortfield = 't.datec', $limit = '', $offset = 0, $arch = '', $filter = '')
  656. {
  657. global $langs, $extrafields;
  658. // fetch optionals attributes and labels
  659. $extrafields->fetch_name_optionals_label($this->table_element);
  660. $sql = "SELECT";
  661. $sql .= " t.rowid,";
  662. $sql .= " t.ref,";
  663. $sql .= " t.track_id,";
  664. $sql .= " t.fk_soc,";
  665. $sql .= " t.fk_project,";
  666. $sql .= " t.fk_contract,";
  667. $sql .= " t.origin_email,";
  668. $sql .= " t.fk_user_create, uc.lastname as user_create_lastname, uc.firstname as user_create_firstname,";
  669. $sql .= " t.fk_user_assign, ua.lastname as user_assign_lastname, ua.firstname as user_assign_firstname,";
  670. $sql .= " t.subject,";
  671. $sql .= " t.message,";
  672. $sql .= " t.fk_statut as status,";
  673. $sql .= " t.resolution,";
  674. $sql .= " t.progress,";
  675. $sql .= " t.timing,";
  676. $sql .= " t.type_code,";
  677. $sql .= " t.category_code,";
  678. $sql .= " t.severity_code,";
  679. $sql .= " t.datec,";
  680. $sql .= " t.date_read,";
  681. $sql .= " t.date_last_msg_sent,";
  682. $sql .= " t.date_close,";
  683. $sql .= " t.tms";
  684. $sql .= ", type.label as type_label, category.label as category_label, severity.label as severity_label";
  685. // Add fields for extrafields
  686. if ($extrafields->attributes[$this->table_element]['count']> 0) {
  687. foreach ($extrafields->attributes[$this->table_element]['label'] as $key => $val) {
  688. $sql .= ($extrafields->attributes[$this->table_element]['type'][$key] != 'separate' ? ",ef.".$key." as options_".$key : '');
  689. }
  690. }
  691. $sql .= " FROM ".MAIN_DB_PREFIX."ticket as t";
  692. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_ticket_type as type ON type.code=t.type_code";
  693. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_ticket_category as category ON category.code=t.category_code";
  694. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_ticket_severity as severity ON severity.code=t.severity_code";
  695. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid=t.fk_soc";
  696. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as uc ON uc.rowid=t.fk_user_create";
  697. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as ua ON ua.rowid=t.fk_user_assign";
  698. if ($extrafields->attributes[$this->table_element]['count']> 0) {
  699. if (is_array($extrafields->attributes[$this->table_element]['label']) && count($extrafields->attributes[$this->table_element]['label'])) {
  700. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."ticket_extrafields as ef on (t.rowid = ef.fk_object)";
  701. }
  702. }
  703. if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  704. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  705. }
  706. $sql .= " WHERE t.entity IN (".getEntity('ticket').")";
  707. // Manage filter
  708. if (!empty($filter)) {
  709. foreach ($filter as $key => $value) {
  710. if (strpos($key, 'date')) { // To allow $filter['YEAR(s.dated)']=>$year
  711. $sql .= " AND ".$key." = '".$this->db->escape($value)."'";
  712. } elseif (($key == 't.fk_user_assign') || ($key == 't.type_code') || ($key == 't.category_code') || ($key == 't.severity_code') || ($key == 't.fk_soc')) {
  713. $sql .= " AND ".$key." = '".$this->db->escape($value)."'";
  714. } elseif ($key == 't.fk_statut') {
  715. if (is_array($value) && count($value) > 0) {
  716. $sql .= " AND ".$key." IN (".$this->db->sanitize(implode(',', $value)).")";
  717. } else {
  718. $sql .= " AND ".$key.' = '.((int) $value);
  719. }
  720. } elseif ($key == 't.fk_contract') {
  721. $sql .= " AND ".$key.' = '.((int) $value);
  722. } else {
  723. $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
  724. }
  725. }
  726. }
  727. if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  728. $sql .= " AND t.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  729. } elseif ($user->socid) {
  730. $sql .= " AND t.fk_soc = ".((int) $user->socid);
  731. }
  732. $sql .= $this->db->order($sortfield, $sortorder);
  733. if (!empty($limit)) {
  734. $sql .= $this->db->plimit($limit + 1, $offset);
  735. }
  736. dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
  737. $resql = $this->db->query($sql);
  738. if ($resql) {
  739. $this->lines = array();
  740. $num = $this->db->num_rows($resql);
  741. $i = 0;
  742. if ($num) {
  743. while ($i < $num) {
  744. $obj = $this->db->fetch_object($resql);
  745. $line = new self($this->db);
  746. $line->id = $obj->rowid;
  747. //$line->rowid = $obj->rowid;
  748. $line->ref = $obj->ref;
  749. $line->track_id = $obj->track_id;
  750. $line->fk_soc = $obj->fk_soc;
  751. $line->fk_project = $obj->fk_project;
  752. $line->fk_contract = $obj->fk_contract;
  753. $line->origin_email = $obj->origin_email;
  754. $line->fk_user_create = $obj->fk_user_create;
  755. $line->fk_user_assign = $obj->fk_user_assign;
  756. $line->subject = $obj->subject;
  757. $line->message = $obj->message;
  758. $line->fk_statut = $obj->status;
  759. $line->status = $obj->status;
  760. $line->resolution = $obj->resolution;
  761. $line->progress = $obj->progress;
  762. $line->timing = $obj->timing;
  763. $label_type = ($langs->trans("TicketTypeShort".$obj->type_code) != ("TicketTypeShort".$obj->type_code) ? $langs->trans("TicketTypeShort".$obj->type_code) : ($obj->type_label != '-' ? $obj->type_label : ''));
  764. $line->type_label = $label_type;
  765. $this->category_code = $obj->category_code;
  766. $label_category = ($langs->trans("TicketCategoryShort".$obj->category_code) != ("TicketCategoryShort".$obj->category_code) ? $langs->trans("TicketCategoryShort".$obj->category_code) : ($obj->category_label != '-' ? $obj->category_label : ''));
  767. $line->category_label = $label_category;
  768. $this->severity_code = $obj->severity_code;
  769. $label_severity = ($langs->trans("TicketSeverityShort".$obj->severity_code) != ("TicketSeverityShort".$obj->severity_code) ? $langs->trans("TicketSeverityShort".$obj->severity_code) : ($obj->severity_label != '-' ? $obj->severity_label : ''));
  770. $line->severity_label = $label_severity;
  771. $line->datec = $this->db->jdate($obj->datec);
  772. $line->date_read = $this->db->jdate($obj->date_read);
  773. $line->date_last_msg_sent = $this->db->jdate($obj->date_last_msg_sent);
  774. $line->date_close = $this->db->jdate($obj->date_close);
  775. // Extra fields
  776. if ($extrafields->attributes[$this->table_element]['count']> 0) {
  777. if (is_array($extrafields->attributes[$this->table_element]['label']) && count($extrafields->attributes[$this->table_element]['label'])) {
  778. foreach ($extrafields->attributes[$this->table_element]['label'] as $key => $val) {
  779. $tmpkey = 'options_'.$key;
  780. $line->{$tmpkey} = $obj->$tmpkey;
  781. }
  782. }
  783. }
  784. $this->lines[$i] = $line;
  785. $i++;
  786. }
  787. }
  788. $this->db->free($resql);
  789. return $num;
  790. } else {
  791. $this->error = "Error ".$this->db->lasterror();
  792. dol_syslog(get_class($this)."::fetchAll ".$this->error, LOG_ERR);
  793. return -1;
  794. }
  795. }
  796. /**
  797. * Update object into database
  798. *
  799. * @param User $user User that modifies
  800. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  801. * @return int <0 if KO, >0 if OK
  802. */
  803. public function update($user = 0, $notrigger = 0)
  804. {
  805. $error = 0;
  806. // $this->oldcopy should have been set by the caller of update (here properties were already modified)
  807. //if (empty($this->oldcopy)) {
  808. // $this->oldcopy = dol_clone($this);
  809. //}
  810. // Clean parameters
  811. if (isset($this->ref)) {
  812. $this->ref = trim($this->ref);
  813. }
  814. if (isset($this->track_id)) {
  815. $this->track_id = trim($this->track_id);
  816. }
  817. if (isset($this->fk_soc)) {
  818. $this->fk_soc = (int) $this->fk_soc;
  819. }
  820. if (isset($this->fk_project)) {
  821. $this->fk_project = (int) $this->fk_project;
  822. }
  823. if (isset($this->fk_contract)) {
  824. $this->fk_contract = (int) $this->fk_contract;
  825. }
  826. if (isset($this->origin_email)) {
  827. $this->origin_email = trim($this->origin_email);
  828. }
  829. if (isset($this->fk_user_create)) {
  830. $this->fk_user_create = (int) $this->fk_user_create;
  831. }
  832. if (isset($this->fk_user_assign)) {
  833. $this->fk_user_assign = (int) $this->fk_user_assign;
  834. }
  835. if (isset($this->subject)) {
  836. $this->subject = trim($this->subject);
  837. }
  838. if (isset($this->message)) {
  839. $this->message = trim($this->message);
  840. if (dol_strlen($this->message) > 65000) {
  841. $this->errors[] = 'ErrorFieldTooLong';
  842. dol_syslog(get_class($this).'::update error -1 message too long', LOG_ERR);
  843. return -1;
  844. }
  845. }
  846. if (isset($this->fk_statut)) {
  847. $this->fk_statut = (int) $this->fk_statut;
  848. }
  849. if (isset($this->resolution)) {
  850. $this->resolution = trim($this->resolution);
  851. }
  852. if (isset($this->progress)) {
  853. $this->progress = trim($this->progress);
  854. }
  855. if (isset($this->timing)) {
  856. $this->timing = trim($this->timing);
  857. }
  858. if (isset($this->type_code)) {
  859. $this->timing = trim($this->type_code);
  860. }
  861. if (isset($this->category_code)) {
  862. $this->timing = trim($this->category_code);
  863. }
  864. if (isset($this->severity_code)) {
  865. $this->timing = trim($this->severity_code);
  866. }
  867. // Check parameters
  868. // Put here code to add a control on parameters values
  869. // Update request
  870. $sql = "UPDATE ".MAIN_DB_PREFIX."ticket SET";
  871. $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "").",";
  872. $sql .= " track_id=".(isset($this->track_id) ? "'".$this->db->escape($this->track_id)."'" : "null").",";
  873. $sql .= " fk_soc=".(isset($this->fk_soc) ? "'".$this->db->escape($this->fk_soc)."'" : "null").",";
  874. $sql .= " fk_project=".(isset($this->fk_project) ? "'".$this->db->escape($this->fk_project)."'" : "null").",";
  875. $sql .= " fk_contract=".(isset($this->fk_contract) ? "'".$this->db->escape($this->fk_contract)."'" : "null").",";
  876. $sql .= " origin_email=".(isset($this->origin_email) ? "'".$this->db->escape($this->origin_email)."'" : "null").",";
  877. $sql .= " fk_user_create=".(isset($this->fk_user_create) ? $this->fk_user_create : "null").",";
  878. $sql .= " fk_user_assign=".(isset($this->fk_user_assign) ? $this->fk_user_assign : "null").",";
  879. $sql .= " subject=".(isset($this->subject) ? "'".$this->db->escape($this->subject)."'" : "null").",";
  880. $sql .= " message=".(isset($this->message) ? "'".$this->db->escape($this->message)."'" : "null").",";
  881. $sql .= " fk_statut=".(isset($this->fk_statut) ? $this->fk_statut : "null").",";
  882. $sql .= " resolution=".(isset($this->resolution) ? $this->resolution : "null").",";
  883. $sql .= " progress=".(isset($this->progress) ? "'".$this->db->escape($this->progress)."'" : "null").",";
  884. $sql .= " timing=".(isset($this->timing) ? "'".$this->db->escape($this->timing)."'" : "null").",";
  885. $sql .= " type_code=".(isset($this->type_code) ? "'".$this->db->escape($this->type_code)."'" : "null").",";
  886. $sql .= " category_code=".(isset($this->category_code) ? "'".$this->db->escape($this->category_code)."'" : "null").",";
  887. $sql .= " severity_code=".(isset($this->severity_code) ? "'".$this->db->escape($this->severity_code)."'" : "null").",";
  888. $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
  889. $sql .= " date_read=".(dol_strlen($this->date_read) != 0 ? "'".$this->db->idate($this->date_read)."'" : 'null').",";
  890. $sql .= " date_last_msg_sent=".(dol_strlen($this->date_last_msg_sent) != 0 ? "'".$this->db->idate($this->date_last_msg_sent)."'" : 'null').",";
  891. $sql .= " date_close=".(dol_strlen($this->date_close) != 0 ? "'".$this->db->idate($this->date_close)."'" : 'null');
  892. $sql .= " WHERE rowid=".((int) $this->id);
  893. $this->db->begin();
  894. $resql = $this->db->query($sql);
  895. if (!$resql) {
  896. $error++;
  897. $this->errors[] = "Error ".$this->db->lasterror();
  898. }
  899. if (!$error) {
  900. // Update extrafields
  901. $result = $this->insertExtraFields();
  902. if ($result < 0) {
  903. $error++;
  904. }
  905. }
  906. if (!$error && !$notrigger) {
  907. // Call trigger
  908. $result = $this->call_trigger('TICKET_MODIFY', $user);
  909. if ($result < 0) {
  910. $error++;
  911. }
  912. // End call triggers
  913. }
  914. // Commit or rollback
  915. if ($error) {
  916. foreach ($this->errors as $errmsg) {
  917. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  918. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  919. }
  920. $this->db->rollback();
  921. return -1 * $error;
  922. } else {
  923. $this->db->commit();
  924. return 1;
  925. }
  926. }
  927. /**
  928. * Delete object in database
  929. *
  930. * @param User $user User that deletes
  931. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  932. * @return int <0 if KO, >0 if OK
  933. */
  934. public function delete($user, $notrigger = 0)
  935. {
  936. global $conf, $langs;
  937. $error = 0;
  938. $this->db->begin();
  939. if (!$error) {
  940. if (!$notrigger) {
  941. // Call trigger
  942. $result = $this->call_trigger('TICKET_DELETE', $user);
  943. if ($result < 0) {
  944. $error++;
  945. }
  946. // End call triggers
  947. }
  948. }
  949. if (!$error) {
  950. // Delete linked contacts
  951. $res = $this->delete_linked_contact();
  952. if ($res < 0) {
  953. dol_syslog(get_class($this)."::delete error", LOG_ERR);
  954. $error++;
  955. }
  956. }
  957. if (!$error) {
  958. // Delete linked object
  959. $res = $this->deleteObjectLinked();
  960. if ($res < 0) {
  961. $error++;
  962. }
  963. }
  964. // Removed extrafields
  965. if (!$error) {
  966. $result = $this->deleteExtraFields();
  967. if ($result < 0) {
  968. $error++;
  969. dol_syslog(get_class($this)."::delete error -3 ".$this->error, LOG_ERR);
  970. }
  971. }
  972. // Delete all child tables
  973. if (!$error) {
  974. $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_ticket";
  975. $sql .= " WHERE fk_ticket = ".(int) $this->id;
  976. $result = $this->db->query($sql);
  977. if (!$result) {
  978. $error++;
  979. $this->errors[] = $this->db->lasterror();
  980. }
  981. }
  982. if (!$error) {
  983. $sql = "DELETE FROM ".MAIN_DB_PREFIX."ticket";
  984. $sql .= " WHERE rowid=".((int) $this->id);
  985. dol_syslog(get_class($this)."::delete sql=".$sql);
  986. $resql = $this->db->query($sql);
  987. if (!$resql) {
  988. $error++;
  989. $this->errors[] = "Error ".$this->db->lasterror();
  990. }
  991. }
  992. // Commit or rollback
  993. if ($error) {
  994. foreach ($this->errors as $errmsg) {
  995. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  996. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  997. }
  998. $this->db->rollback();
  999. return -1 * $error;
  1000. } else {
  1001. $this->db->commit();
  1002. return 1;
  1003. }
  1004. }
  1005. /**
  1006. * Load an object from its id and create a new one in database
  1007. *
  1008. * @param User $user User that clone
  1009. * @param int $fromid Id of object to clone
  1010. * @return int New id of clone
  1011. */
  1012. public function createFromClone(User $user, $fromid)
  1013. {
  1014. $error = 0;
  1015. $object = new Ticket($this->db);
  1016. $this->db->begin();
  1017. // Load source object
  1018. $object->fetch($fromid);
  1019. $object->id = 0;
  1020. $object->statut = 0;
  1021. // Clear fields
  1022. // ...
  1023. // Create clone
  1024. $object->context['createfromclone'] = 'createfromclone';
  1025. $result = $object->create($user);
  1026. // Other options
  1027. if ($result < 0) {
  1028. $this->error = $object->error;
  1029. $error++;
  1030. }
  1031. if (!$error) {
  1032. }
  1033. unset($object->context['createfromclone']);
  1034. // End
  1035. if (!$error) {
  1036. $this->db->commit();
  1037. return $object->id;
  1038. } else {
  1039. $this->db->rollback();
  1040. return -1;
  1041. }
  1042. }
  1043. /**
  1044. * Initialise object with example values
  1045. * Id must be 0 if object instance is a specimen
  1046. *
  1047. * @return int
  1048. */
  1049. public function initAsSpecimen()
  1050. {
  1051. $this->id = 0;
  1052. $this->entity = 1;
  1053. $this->ref = 'TI0501-001';
  1054. $this->track_id = 'XXXXaaaa';
  1055. $this->origin_email = 'email@email.com';
  1056. $this->fk_project = 1;
  1057. $this->fk_user_create = 1;
  1058. $this->fk_user_assign = 1;
  1059. $this->subject = 'Subject of ticket';
  1060. $this->message = 'Message of ticket';
  1061. $this->status = 0;
  1062. $this->resolution = '1';
  1063. $this->progress = '10';
  1064. //$this->timing = '30';
  1065. $this->type_code = 'TYPECODE';
  1066. $this->category_code = 'CATEGORYCODE';
  1067. $this->severity_code = 'SEVERITYCODE';
  1068. $this->datec = '';
  1069. $this->date_read = '';
  1070. $this->date_last_msg_sent = '';
  1071. $this->date_close = '';
  1072. $this->tms = '';
  1073. return 1;
  1074. }
  1075. /**
  1076. * Print selected status
  1077. *
  1078. * @param string $selected Selected status
  1079. * @return void
  1080. */
  1081. public function printSelectStatus($selected = "")
  1082. {
  1083. print Form::selectarray('search_fk_statut', $this->labelStatusShort, $selected, $show_empty = 1, $key_in_label = 0, $value_as_key = 0, $option = '', $translate = 1, $maxlen = 0, $disabled = 0, $sort = '', $morecss = '');
  1084. }
  1085. /**
  1086. * Load into a cache the types of tickets (setup done into dictionaries)
  1087. *
  1088. * @return int Number of lines loaded, 0 if already loaded, <0 if KO
  1089. */
  1090. public function loadCacheTypesTickets()
  1091. {
  1092. global $langs;
  1093. if (!empty($this->cache_types_tickets) && count($this->cache_types_tickets)) {
  1094. return 0;
  1095. }
  1096. // Cache deja charge
  1097. $sql = "SELECT rowid, code, label, use_default, pos, description";
  1098. $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_type";
  1099. $sql .= " WHERE entity IN (".getEntity('c_ticket_type').")";
  1100. $sql .= " AND active > 0";
  1101. $sql .= " ORDER BY pos";
  1102. dol_syslog(get_class($this)."::load_cache_type_tickets", LOG_DEBUG);
  1103. $resql = $this->db->query($sql);
  1104. if ($resql) {
  1105. $num = $this->db->num_rows($resql);
  1106. $i = 0;
  1107. while ($i < $num) {
  1108. $obj = $this->db->fetch_object($resql);
  1109. $label = ($langs->trans("TicketTypeShort".$obj->code) != ("TicketTypeShort".$obj->code) ? $langs->trans("TicketTypeShort".$obj->code) : ($obj->label != '-' ? $obj->label : ''));
  1110. $this->cache_types_tickets[$obj->rowid]['code'] = $obj->code;
  1111. $this->cache_types_tickets[$obj->rowid]['label'] = $label;
  1112. $this->cache_types_tickets[$obj->rowid]['use_default'] = $obj->use_default;
  1113. $this->cache_types_tickets[$obj->rowid]['pos'] = $obj->pos;
  1114. $i++;
  1115. }
  1116. return $num;
  1117. } else {
  1118. dol_print_error($this->db);
  1119. return -1;
  1120. }
  1121. }
  1122. /**
  1123. * Load into a cache array, the list of ticket categories (setup done into dictionary)
  1124. *
  1125. * @param int $publicgroup 0=No public group, 1=Public group only, -1=All
  1126. * @return int Number of lines loaded, 0 if already loaded, <0 if KO
  1127. */
  1128. public function loadCacheCategoriesTickets($publicgroup = -1)
  1129. {
  1130. global $conf, $langs;
  1131. if ($publicgroup == -1 && !empty($this->cache_category_ticket) && count($this->cache_category_tickets)) {
  1132. // Cache already loaded
  1133. return 0;
  1134. }
  1135. $sql = "SELECT rowid, code, label, use_default, pos, description, public, active, force_severity, fk_parent";
  1136. $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_category";
  1137. $sql .= " WHERE entity IN (".getEntity('c_ticket_category').")";
  1138. $sql .= " AND active > 0";
  1139. if ($publicgroup > -1) {
  1140. $sql .= " AND public = ".((int) $publicgroup);
  1141. }
  1142. $sql .= " ORDER BY pos";
  1143. dol_syslog(get_class($this)."::load_cache_categories_tickets", LOG_DEBUG);
  1144. $resql = $this->db->query($sql);
  1145. if ($resql) {
  1146. $num = $this->db->num_rows($resql);
  1147. $i = 0;
  1148. while ($i < $num) {
  1149. $obj = $this->db->fetch_object($resql);
  1150. $this->cache_category_tickets[$obj->rowid]['code'] = $obj->code;
  1151. $this->cache_category_tickets[$obj->rowid]['use_default'] = $obj->use_default;
  1152. $this->cache_category_tickets[$obj->rowid]['pos'] = $obj->pos;
  1153. $this->cache_category_tickets[$obj->rowid]['public'] = $obj->public;
  1154. $this->cache_category_tickets[$obj->rowid]['active'] = $obj->active;
  1155. $this->cache_category_tickets[$obj->rowid]['force_severity'] = $obj->force_severity;
  1156. $this->cache_category_tickets[$obj->rowid]['fk_parent'] = $obj->fk_parent;
  1157. // If translation exists, we use it to store already translated string.
  1158. // Warning: You should not use this and recompute the translated string into caller code to get the value into expected language
  1159. $label = ($langs->trans("TicketCategoryShort".$obj->code) != ("TicketCategoryShort".$obj->code) ? $langs->trans("TicketCategoryShort".$obj->code) : ($obj->label != '-' ? $obj->label : ''));
  1160. $this->cache_category_tickets[$obj->rowid]['label'] = $label;
  1161. $i++;
  1162. }
  1163. return $num;
  1164. } else {
  1165. dol_print_error($this->db);
  1166. return -1;
  1167. }
  1168. }
  1169. /**
  1170. * Charge dans cache la liste des sévérité de tickets (paramétrable dans dictionnaire)
  1171. *
  1172. * @return int Number of lines loaded, 0 if already loaded, <0 if KO
  1173. */
  1174. public function loadCacheSeveritiesTickets()
  1175. {
  1176. global $langs;
  1177. if (!empty($this->cache_severity_tickets) && count($this->cache_severity_tickets)) {
  1178. return 0;
  1179. }
  1180. // Cache deja charge
  1181. $sql = "SELECT rowid, code, label, use_default, pos, description";
  1182. $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_severity";
  1183. $sql .= " WHERE entity IN (".getEntity('c_ticket_severity').")";
  1184. $sql .= " AND active > 0";
  1185. $sql .= " ORDER BY pos";
  1186. dol_syslog(get_class($this)."::loadCacheSeveritiesTickets", LOG_DEBUG);
  1187. $resql = $this->db->query($sql);
  1188. if ($resql) {
  1189. $num = $this->db->num_rows($resql);
  1190. $i = 0;
  1191. while ($i < $num) {
  1192. $obj = $this->db->fetch_object($resql);
  1193. $this->cache_severity_tickets[$obj->rowid]['code'] = $obj->code;
  1194. $label = ($langs->trans("TicketSeverityShort".$obj->code) != ("TicketSeverityShort".$obj->code) ? $langs->trans("TicketSeverityShort".$obj->code) : ($obj->label != '-' ? $obj->label : ''));
  1195. $this->cache_severity_tickets[$obj->rowid]['label'] = $label;
  1196. $this->cache_severity_tickets[$obj->rowid]['use_default'] = $obj->use_default;
  1197. $this->cache_severity_tickets[$obj->rowid]['pos'] = $obj->pos;
  1198. $i++;
  1199. }
  1200. return $num;
  1201. } else {
  1202. dol_print_error($this->db);
  1203. return -1;
  1204. }
  1205. }
  1206. /**
  1207. * Return status label of object
  1208. *
  1209. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  1210. * @return string Label
  1211. */
  1212. public function getLibStatut($mode = 0)
  1213. {
  1214. return $this->LibStatut($this->fk_statut, $mode, 0, $this->progress);
  1215. }
  1216. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1217. /**
  1218. * Return status label of object
  1219. *
  1220. * @param string $status Id status
  1221. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  1222. * @param int $notooltip 1=No tooltip
  1223. * @param int $progress Progression (0 to 100)
  1224. * @return string Label
  1225. */
  1226. public function LibStatut($status, $mode = 0, $notooltip = 0, $progress = 0)
  1227. {
  1228. // phpcs:enable
  1229. global $langs, $hookmanager;
  1230. $labelStatus = $this->labelStatus[$status];
  1231. $labelStatusShort = $this->labelStatusShort[$status];
  1232. if ($status == self::STATUS_NOT_READ) {
  1233. $statusType = 'status0';
  1234. } elseif ($status == self::STATUS_READ) {
  1235. $statusType = 'status1';
  1236. } elseif ($status == self::STATUS_ASSIGNED) {
  1237. $statusType = 'status2';
  1238. } elseif ($status == self::STATUS_IN_PROGRESS) {
  1239. $statusType = 'status4';
  1240. } elseif ($status == self::STATUS_WAITING) {
  1241. $statusType = 'status7';
  1242. } elseif ($status == self::STATUS_NEED_MORE_INFO) {
  1243. $statusType = 'status3';
  1244. } elseif ($status == self::STATUS_CANCELED) {
  1245. $statusType = 'status9';
  1246. } elseif ($status == self::STATUS_CLOSED) {
  1247. $statusType = 'status6';
  1248. } else {
  1249. $labelStatus = 'Unknown';
  1250. $labelStatusShort = 'Unknown';
  1251. $statusType = 'status0';
  1252. $mode = 0;
  1253. }
  1254. $parameters = array(
  1255. 'status' => $status,
  1256. 'mode' => $mode,
  1257. );
  1258. // Note that $action and $object may have been modified by hook
  1259. $reshook = $hookmanager->executeHooks('LibStatut', $parameters, $this);
  1260. if ($reshook > 0) {
  1261. return $hookmanager->resPrint;
  1262. }
  1263. $params = array();
  1264. if ($notooltip) {
  1265. $params = array('tooltip' => 'no');
  1266. }
  1267. $labelStatus = $langs->transnoentitiesnoconv($labelStatus);
  1268. $labelStatusShort = $langs->transnoentitiesnoconv($labelStatusShort);
  1269. if ($status == self::STATUS_IN_PROGRESS && $progress > 0) {
  1270. $labelStatus .= ' ('.round($progress).'%)';
  1271. $labelStatusShort .= ' ('.round($progress).'%)';
  1272. }
  1273. return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode, '', $params);
  1274. }
  1275. /**
  1276. * getTooltipContentArray
  1277. *
  1278. * @param array $params ex option, infologin
  1279. * @since v18
  1280. * @return array
  1281. */
  1282. public function getTooltipContentArray($params)
  1283. {
  1284. global $langs;
  1285. $langs->load('ticket');
  1286. $nofetch = !empty($params['nofetch']);
  1287. $datas = array();
  1288. $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Ticket").'</u>';
  1289. $datas['picto'] .= ' '.$this->getLibStatut(4);
  1290. $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
  1291. $datas['track_id'] = '<br><b>'.$langs->trans('TicketTrackId').':</b> '.$this->track_id;
  1292. $datas['subject'] = '<br><b>'.$langs->trans('Subject').':</b> '.$this->subject;
  1293. if ($this->date_creation) {
  1294. $datas['date_creation'] = '<br><b>'.$langs->trans('DateCreation').':</b> '.dol_print_date($this->date_creation, 'dayhour');
  1295. }
  1296. if ($this->date_modification) {
  1297. $datas['date_modification'] = '<br><b>'.$langs->trans('DateModification').':</b> '.dol_print_date($this->date_modification, 'dayhour');
  1298. }
  1299. // show categories for this record only in ajax to not overload lists
  1300. if (isModEnabled('categorie') && !$nofetch) {
  1301. require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
  1302. $form = new Form($this->db);
  1303. $datas['categories'] = '<br>' . $form->showCategories($this->id, Categorie::TYPE_TICKET, 1);
  1304. }
  1305. return $datas;
  1306. }
  1307. /**
  1308. * Return a link to the object card (with optionaly the picto)
  1309. *
  1310. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  1311. * @param string $option On what the link point to ('nolink', ...)
  1312. * @param int $notooltip 1=Disable tooltip
  1313. * @param string $morecss Add more css on link
  1314. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  1315. * @return string String with URL
  1316. */
  1317. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  1318. {
  1319. global $conf, $langs;
  1320. if (!empty($conf->dol_no_mouse_hover)) {
  1321. $notooltip = 1; // Force disable tooltips
  1322. }
  1323. $result = '';
  1324. $params = [
  1325. 'id' => $this->id,
  1326. 'objecttype' => $this->element,
  1327. 'option' => $option,
  1328. 'nofetch' => 1,
  1329. ];
  1330. $classfortooltip = 'classfortooltip';
  1331. $dataparams = '';
  1332. if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
  1333. $classfortooltip = 'classforajaxtooltip';
  1334. $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
  1335. $label = '';
  1336. } else {
  1337. $label = implode($this->getTooltipContentArray($params));
  1338. }
  1339. $url = DOL_URL_ROOT.'/ticket/card.php?id='.$this->id;
  1340. if ($option != 'nolink') {
  1341. // Add param to save lastsearch_values or not
  1342. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  1343. if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  1344. $add_save_lastsearch_values = 1;
  1345. }
  1346. if ($add_save_lastsearch_values) {
  1347. $url .= '&save_lastsearch_values=1';
  1348. }
  1349. }
  1350. $linkclose = '';
  1351. if (empty($notooltip)) {
  1352. if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
  1353. $label = $langs->trans("ShowTicket");
  1354. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  1355. }
  1356. $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
  1357. $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
  1358. } else {
  1359. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  1360. }
  1361. $linkstart = '<a href="'.$url.'"';
  1362. $linkstart .= $linkclose.'>';
  1363. $linkend = '</a>';
  1364. $result .= $linkstart;
  1365. if ($withpicto) {
  1366. $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), (($withpicto != 2) ? 'class="paddingright"' : ''), 0, 0, $notooltip ? 0 : 1);
  1367. }
  1368. if ($withpicto != 2) {
  1369. $result .= $this->ref;
  1370. }
  1371. $result .= $linkend;
  1372. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  1373. return $result;
  1374. }
  1375. /**
  1376. * Mark a message as read
  1377. *
  1378. * @param User $user Object user
  1379. * @param int $notrigger No trigger
  1380. * @return int <0 if KO, 0=nothing done, >0 if OK
  1381. */
  1382. public function markAsRead($user, $notrigger = 0)
  1383. {
  1384. global $langs;
  1385. $error = 0;
  1386. if ($this->statut != self::STATUS_CANCELED) { // no closed
  1387. $this->oldcopy = dol_clone($this);
  1388. $this->db->begin();
  1389. $sql = "UPDATE ".MAIN_DB_PREFIX."ticket";
  1390. $sql .= " SET fk_statut = ".Ticket::STATUS_READ.", date_read = '".$this->db->idate(dol_now())."'";
  1391. $sql .= " WHERE rowid = ".((int) $this->id);
  1392. dol_syslog(get_class($this)."::markAsRead");
  1393. $resql = $this->db->query($sql);
  1394. if ($resql) {
  1395. $this->context['actionmsg'] = $langs->trans('TicketLogMesgReadBy', $this->ref, $user->getFullName($langs));
  1396. $this->context['actionmsg2'] = $langs->trans('TicketLogMesgReadBy', $this->ref, $user->getFullName($langs));
  1397. if (!$error && !$notrigger) {
  1398. // Call trigger
  1399. $result = $this->call_trigger('TICKET_MODIFY', $user);
  1400. if ($result < 0) {
  1401. $error++;
  1402. }
  1403. // End call triggers
  1404. }
  1405. if (!$error) {
  1406. $this->db->commit();
  1407. return 1;
  1408. } else {
  1409. $this->db->rollback();
  1410. $this->error = join(',', $this->errors);
  1411. dol_syslog(get_class($this)."::markAsRead ".$this->error, LOG_ERR);
  1412. return -1;
  1413. }
  1414. } else {
  1415. $this->db->rollback();
  1416. $this->error = $this->db->lasterror();
  1417. dol_syslog(get_class($this)."::markAsRead ".$this->error, LOG_ERR);
  1418. return -1;
  1419. }
  1420. }
  1421. return 0;
  1422. }
  1423. /**
  1424. * Set an assigned user to a ticket.
  1425. *
  1426. * @param User $user Object user
  1427. * @param int $id_assign_user ID of user assigned
  1428. * @param int $notrigger Disable trigger
  1429. * @return int <0 if KO, 0=Nothing done, >0 if OK
  1430. */
  1431. public function assignUser($user, $id_assign_user, $notrigger = 0)
  1432. {
  1433. $error = 0;
  1434. $this->oldcopy = dol_clone($this);
  1435. $this->db->begin();
  1436. $sql = "UPDATE ".MAIN_DB_PREFIX."ticket";
  1437. if ($id_assign_user > 0) {
  1438. $sql .= " SET fk_user_assign=".((int) $id_assign_user).", fk_statut = ".Ticket::STATUS_ASSIGNED;
  1439. } else {
  1440. $sql .= " SET fk_user_assign=null, fk_statut = ".Ticket::STATUS_READ;
  1441. }
  1442. $sql .= " WHERE rowid = ".((int) $this->id);
  1443. dol_syslog(get_class($this)."::assignUser sql=".$sql);
  1444. $resql = $this->db->query($sql);
  1445. if ($resql) {
  1446. $this->fk_user_assign = $id_assign_user; // May be used by trigger
  1447. if (!$notrigger) {
  1448. // Call trigger
  1449. $result = $this->call_trigger('TICKET_ASSIGNED', $user);
  1450. if ($result < 0) {
  1451. $error++;
  1452. }
  1453. // End call triggers
  1454. }
  1455. if (!$error) {
  1456. $this->db->commit();
  1457. return 1;
  1458. } else {
  1459. $this->db->rollback();
  1460. $this->error = join(',', $this->errors);
  1461. dol_syslog(get_class($this)."::assignUser ".$this->error, LOG_ERR);
  1462. return -1;
  1463. }
  1464. } else {
  1465. $this->db->rollback();
  1466. $this->error = $this->db->lasterror();
  1467. dol_syslog(get_class($this)."::assignUser ".$this->error, LOG_ERR);
  1468. return -1;
  1469. }
  1470. }
  1471. /**
  1472. * Add message into database
  1473. *
  1474. * @param User $user User that creates
  1475. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  1476. * @param array $filename_list List of files to attach (full path of filename on file system)
  1477. * @param array $mimetype_list List of MIME type of attached files
  1478. * @param array $mimefilename_list List of attached file name in message
  1479. * @param boolean $send_email Whether the message is sent by email
  1480. * @param int $public_area 0=Default, 1 if we are creating the message from a public area (so we can search contact from email to add it as contact of ticket if TICKET_ASSIGN_CONTACT_TO_MESSAGE is set)
  1481. * @return int <0 if KO, >0 if OK
  1482. */
  1483. public function createTicketMessage($user, $notrigger = 0, $filename_list = array(), $mimetype_list = array(), $mimefilename_list = array(), $send_email = false, $public_area = 0)
  1484. {
  1485. global $conf, $langs;
  1486. $error = 0;
  1487. $now = dol_now();
  1488. // Clean parameters
  1489. if (isset($this->fk_track_id)) {
  1490. $this->fk_track_id = trim($this->fk_track_id);
  1491. }
  1492. if (isset($this->message)) {
  1493. $this->message = trim($this->message);
  1494. }
  1495. $this->db->begin();
  1496. // Insert entry into agenda with code 'TICKET_MSG'
  1497. include_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
  1498. $actioncomm = new ActionComm($this->db);
  1499. $actioncomm->type_code = 'AC_OTH_AUTO'; // This is not an entry that must appears into manual calendar but only into CRM calendar
  1500. $actioncomm->code = 'TICKET_MSG';
  1501. if ($this->private) {
  1502. $actioncomm->code = 'TICKET_MSG_PRIVATE';
  1503. }
  1504. if ($send_email) {
  1505. $actioncomm->code .= '_SENTBYMAIL';
  1506. }
  1507. if ((empty($user->id) || $user->id == 0) && isset($_SESSION['email_customer'])) {
  1508. $actioncomm->email_from = $_SESSION['email_customer'];
  1509. }
  1510. $actioncomm->socid = $this->socid;
  1511. $actioncomm->label = $this->subject;
  1512. $actioncomm->note_private = $this->message;
  1513. $actioncomm->userassigned = array($user->id);
  1514. $actioncomm->userownerid = $user->id;
  1515. $actioncomm->datep = $now;
  1516. $actioncomm->percentage = -1; // percentage is not relevant for punctual events
  1517. $actioncomm->elementtype = 'ticket';
  1518. $actioncomm->fk_element = $this->id;
  1519. $actioncomm->fk_project = $this->fk_project;
  1520. // add contact id from author email on public interface
  1521. if ($public_area && !empty($this->origin_email) && getDolGlobalString('TICKET_ASSIGN_CONTACT_TO_MESSAGE')) {
  1522. $contacts = $this->searchContactByEmail($this->origin_email);
  1523. if (!empty($contacts)) {
  1524. // Ensure that contact is active and select first active contact
  1525. foreach ($contacts as $contact) {
  1526. if ((int) $contact->statut == 1) {
  1527. $actioncomm->contact_id = $contact->id;
  1528. break;
  1529. }
  1530. }
  1531. }
  1532. }
  1533. $attachedfiles = array();
  1534. $attachedfiles['paths'] = $filename_list;
  1535. $attachedfiles['names'] = $mimefilename_list;
  1536. $attachedfiles['mimes'] = $mimetype_list;
  1537. if (is_array($attachedfiles) && count($attachedfiles) > 0) {
  1538. $actioncomm->attachedfiles = $attachedfiles;
  1539. }
  1540. if (!empty($mimefilename_list) && is_array($mimefilename_list)) {
  1541. $actioncomm->note_private = dol_concatdesc($actioncomm->note_private, "\n".$langs->transnoentities("AttachedFiles").': '.join(';', $mimefilename_list));
  1542. }
  1543. $actionid = $actioncomm->create($user);
  1544. if ($actionid <= 0) {
  1545. $error++;
  1546. $this->error = $actioncomm->error;
  1547. $this->errors = $actioncomm->errors;
  1548. }
  1549. // Commit or rollback
  1550. if ($error) {
  1551. $this->db->rollback();
  1552. return -1 * $error;
  1553. } else {
  1554. $this->db->commit();
  1555. return 1;
  1556. }
  1557. }
  1558. /**
  1559. * Load the list of event on ticket into ->cache_msgs_ticket
  1560. *
  1561. * @return int Number of lines loaded, 0 if already loaded, <0 if KO
  1562. */
  1563. public function loadCacheMsgsTicket()
  1564. {
  1565. if (!empty($this->cache_msgs_ticket) && is_array($this->cache_msgs_ticket) && count($this->cache_msgs_ticket)) {
  1566. return 0;
  1567. }
  1568. // Cache already loaded
  1569. $sql = "SELECT id as rowid, fk_user_author, email_from, datec, datep, label, note as message, code";
  1570. $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm";
  1571. $sql .= " WHERE fk_element = ".(int) $this->id;
  1572. $sql .= " AND elementtype = 'ticket'";
  1573. $sql .= " ORDER BY datep DESC";
  1574. dol_syslog(get_class($this)."::load_cache_actions_ticket", LOG_DEBUG);
  1575. $resql = $this->db->query($sql);
  1576. if ($resql) {
  1577. $num = $this->db->num_rows($resql);
  1578. $i = 0;
  1579. while ($i < $num) {
  1580. $obj = $this->db->fetch_object($resql);
  1581. $this->cache_msgs_ticket[$i]['id'] = $obj->rowid;
  1582. $this->cache_msgs_ticket[$i]['fk_user_author'] = $obj->fk_user_author;
  1583. if ($obj->code == 'TICKET_MSG' && empty($obj->fk_user_author)) {
  1584. $this->cache_msgs_ticket[$i]['fk_contact_author'] = $obj->email_from;
  1585. }
  1586. $this->cache_msgs_ticket[$i]['datec'] = $this->db->jdate($obj->datec);
  1587. $this->cache_msgs_ticket[$i]['datep'] = $this->db->jdate($obj->datep);
  1588. $this->cache_msgs_ticket[$i]['subject'] = $obj->label;
  1589. $this->cache_msgs_ticket[$i]['message'] = $obj->message;
  1590. $this->cache_msgs_ticket[$i]['private'] = (preg_match('/^TICKET_MSG_PRIVATE/', $obj->code) ? 1 : 0);
  1591. $i++;
  1592. }
  1593. return $num;
  1594. } else {
  1595. $this->error = "Error ".$this->db->lasterror();
  1596. dol_syslog(get_class($this)."::load_cache_actions_ticket ".$this->error, LOG_ERR);
  1597. return -1;
  1598. }
  1599. }
  1600. /**
  1601. * Close a ticket
  1602. *
  1603. * @param User $user User that close
  1604. * @param int $mode 0=Close solved, 1=Close abandonned
  1605. * @return int <0 if KO, 0=nothing done, >0 if OK
  1606. */
  1607. public function close(User $user, $mode = 0)
  1608. {
  1609. global $conf;
  1610. if ($this->status != Ticket::STATUS_CLOSED && $this->status != Ticket::STATUS_CANCELED) { // not closed
  1611. $this->db->begin();
  1612. $sql = "UPDATE ".MAIN_DB_PREFIX."ticket";
  1613. $sql .= " SET fk_statut=".($mode ? Ticket::STATUS_CANCELED : Ticket::STATUS_CLOSED).", progress=100, date_close='".$this->db->idate(dol_now())."'";
  1614. $sql .= " WHERE rowid = ".((int) $this->id);
  1615. dol_syslog(get_class($this)."::close mode=".$mode);
  1616. $resql = $this->db->query($sql);
  1617. if ($resql) {
  1618. $error = 0;
  1619. // Valid and close fichinter linked
  1620. if (isModEnabled('ficheinter') && getDolGlobalString('WORKFLOW_TICKET_CLOSE_INTERVENTION')) {
  1621. dol_syslog("We have closed the ticket, so we close all linked interventions");
  1622. $this->fetchObjectLinked($this->id, $this->element, null, 'fichinter');
  1623. if ($this->linkedObjectsIds) {
  1624. foreach ($this->linkedObjectsIds['fichinter'] as $fichinter_id) {
  1625. $fichinter = new Fichinter($this->db);
  1626. $fichinter->fetch($fichinter_id);
  1627. if ($fichinter->statut == 0) {
  1628. $result = $fichinter->setValid($user);
  1629. if (!$result) {
  1630. $this->errors[] = $fichinter->error;
  1631. $error++;
  1632. }
  1633. }
  1634. if ($fichinter->statut < 3) {
  1635. $result = $fichinter->setStatut(3);
  1636. if (!$result) {
  1637. $this->errors[] = $fichinter->error;
  1638. $error++;
  1639. }
  1640. }
  1641. }
  1642. }
  1643. }
  1644. // Call trigger
  1645. $result = $this->call_trigger('TICKET_CLOSE', $user);
  1646. if ($result < 0) {
  1647. $error++;
  1648. }
  1649. // End call triggers
  1650. if (!$error) {
  1651. $this->db->commit();
  1652. return 1;
  1653. } else {
  1654. $this->db->rollback();
  1655. $this->error = join(',', $this->errors);
  1656. dol_syslog(get_class($this)."::close ".$this->error, LOG_ERR);
  1657. return -1;
  1658. }
  1659. } else {
  1660. $this->db->rollback();
  1661. $this->error = $this->db->lasterror();
  1662. dol_syslog(get_class($this)."::close ".$this->error, LOG_ERR);
  1663. return -1;
  1664. }
  1665. }
  1666. return 0;
  1667. }
  1668. /**
  1669. * Search and fetch thirparties by email
  1670. *
  1671. * @param string $email Email
  1672. * @param int $type Type of thirdparties (0=any, 1=customer, 2=prospect, 3=supplier)
  1673. * @param array $filters Array of couple field name/value to filter the companies with the same name
  1674. * @param string $clause Clause for filters
  1675. * @return array|int Array of thirdparties object
  1676. */
  1677. public function searchSocidByEmail($email, $type = '0', $filters = array(), $clause = 'AND')
  1678. {
  1679. $thirdparties = array();
  1680. $exact = 0;
  1681. // Generation requete recherche
  1682. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
  1683. $sql .= " WHERE entity IN (".getEntity('ticket', 1).")";
  1684. if (!empty($type)) {
  1685. if ($type == 1 || $type == 2) {
  1686. $sql .= " AND client = ".((int) $type);
  1687. } elseif ($type == 3) {
  1688. $sql .= " AND fournisseur = 1";
  1689. }
  1690. }
  1691. if (!empty($email)) {
  1692. if (empty($exact)) {
  1693. $regs = array();
  1694. if (preg_match('/^([\*])?[^*]+([\*])?$/', $email, $regs) && count($regs) > 1) {
  1695. $email = str_replace('*', '%', $email);
  1696. } else {
  1697. $email = '%'.$email.'%';
  1698. }
  1699. }
  1700. $sql .= " AND ";
  1701. if (is_array($filters) && !empty($filters)) {
  1702. $sql .= "(";
  1703. }
  1704. $sql .= "email LIKE '".$this->db->escape($email)."'";
  1705. }
  1706. if (is_array($filters) && !empty($filters)) {
  1707. foreach ($filters as $field => $value) {
  1708. $sql .= " ".$clause." ".$field." LIKE '".$this->db->escape($value)."'";
  1709. }
  1710. if (!empty($email)) {
  1711. $sql .= ")";
  1712. }
  1713. }
  1714. $res = $this->db->query($sql);
  1715. if ($res) {
  1716. while ($rec = $this->db->fetch_array($res)) {
  1717. $soc = new Societe($this->db);
  1718. $soc->fetch($rec['rowid']);
  1719. $thirdparties[] = $soc;
  1720. }
  1721. return $thirdparties;
  1722. } else {
  1723. $this->error = $this->db->error().' sql='.$sql;
  1724. dol_syslog(get_class($this)."::searchSocidByEmail ".$this->error, LOG_ERR);
  1725. return -1;
  1726. }
  1727. }
  1728. /**
  1729. * Search and fetch contacts by email
  1730. *
  1731. * @param string $email Email
  1732. * @param array $socid Limit to a thirdparty
  1733. * @param string $case Respect case
  1734. * @return array|int Array of contacts object
  1735. */
  1736. public function searchContactByEmail($email, $socid = '', $case = '')
  1737. {
  1738. $contacts = array();
  1739. // Forge the search SQL
  1740. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."socpeople";
  1741. $sql .= " WHERE entity IN (".getEntity('contact').")";
  1742. if (!empty($socid)) {
  1743. $sql .= " AND fk_soc = ".((int) $socid);
  1744. }
  1745. if (!empty($email)) {
  1746. $sql .= " AND ";
  1747. if (!$case) {
  1748. $sql .= "email = '".$this->db->escape($email)."'";
  1749. } else {
  1750. $sql .= "email LIKE BINARY '".$this->db->escape($this->db->escapeforlike($email))."'";
  1751. }
  1752. }
  1753. $res = $this->db->query($sql);
  1754. if ($res) {
  1755. while ($rec = $this->db->fetch_object($res)) {
  1756. include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  1757. $contactstatic = new Contact($this->db);
  1758. $contactstatic->fetch($rec->rowid);
  1759. $contacts[] = $contactstatic;
  1760. }
  1761. return $contacts;
  1762. } else {
  1763. $this->error = $this->db->error().' sql='.$sql;
  1764. dol_syslog(get_class($this)."::searchContactByEmail ".$this->error, LOG_ERR);
  1765. return -1;
  1766. }
  1767. }
  1768. /**
  1769. * Define parent commany of current ticket
  1770. *
  1771. * @param int $id Id of thirdparty to set or '' to remove
  1772. * @return int <0 if KO, >0 if OK
  1773. */
  1774. public function setCustomer($id)
  1775. {
  1776. if ($this->id) {
  1777. $sql = "UPDATE ".MAIN_DB_PREFIX."ticket";
  1778. $sql .= " SET fk_soc = ".($id > 0 ? $id : "null");
  1779. $sql .= " WHERE rowid = ".((int) $this->id);
  1780. dol_syslog(get_class($this).'::setCustomer sql='.$sql);
  1781. $resql = $this->db->query($sql);
  1782. if ($resql) {
  1783. return 1;
  1784. } else {
  1785. return -1;
  1786. }
  1787. } else {
  1788. return -1;
  1789. }
  1790. }
  1791. /**
  1792. * Define progression of current ticket
  1793. *
  1794. * @param int $percent Progression percent
  1795. * @return int <0 if KO, >0 if OK
  1796. */
  1797. public function setProgression($percent)
  1798. {
  1799. if ($this->id) {
  1800. $sql = "UPDATE ".MAIN_DB_PREFIX."ticket";
  1801. $sql .= " SET progress = ".($percent > 0 ? $percent : "null");
  1802. $sql .= " WHERE rowid = ".((int) $this->id);
  1803. dol_syslog(get_class($this).'::set_progression sql='.$sql);
  1804. $resql = $this->db->query($sql);
  1805. if ($resql) {
  1806. return 1;
  1807. } else {
  1808. return -1;
  1809. }
  1810. } else {
  1811. return -1;
  1812. }
  1813. }
  1814. /**
  1815. * Link element with a contract
  1816. *
  1817. * @param int $contractid Contract id to link element to
  1818. * @return int <0 if KO, >0 if OK
  1819. */
  1820. public function setContract($contractid)
  1821. {
  1822. if ($this->id) {
  1823. $sql = "UPDATE ".MAIN_DB_PREFIX."ticket";
  1824. $sql .= " SET fk_contract = ".($contractid > 0 ? $contractid : "null");
  1825. $sql .= " WHERE rowid = ".((int) $this->id);
  1826. dol_syslog(get_class($this).'::setContract sql='.$sql);
  1827. $resql = $this->db->query($sql);
  1828. if ($resql) {
  1829. return 1;
  1830. } else {
  1831. return -1;
  1832. }
  1833. } else {
  1834. return -1;
  1835. }
  1836. }
  1837. /* gestion des contacts d'un ticket */
  1838. /**
  1839. * Return id des contacts interne de suivi
  1840. *
  1841. * @return array Liste des id contacts suivi ticket
  1842. */
  1843. public function getIdTicketInternalContact()
  1844. {
  1845. return $this->getIdContact('internal', 'SUPPORTTEC');
  1846. }
  1847. /**
  1848. * Retrieve informations about internal contacts
  1849. *
  1850. * @param int $status Status of user or company
  1851. * @return array Array with datas : firstname, lastname, socid (-1 for internal users), email, code, libelle, status
  1852. */
  1853. public function getInfosTicketInternalContact($status = -1)
  1854. {
  1855. return $this->listeContact(-1, 'internal', 0, '', $status);
  1856. }
  1857. /**
  1858. * Return id des contacts clients pour le suivi ticket
  1859. *
  1860. * @return array Liste des id contacts suivi ticket
  1861. */
  1862. public function getIdTicketCustomerContact()
  1863. {
  1864. return $this->getIdContact('external', 'SUPPORTCLI');
  1865. }
  1866. /**
  1867. * Retrieve informations about external contacts
  1868. *
  1869. * @param int $status Status of user or company
  1870. * @return array Array with datas : firstname, lastname, socid (-1 for internal users), email, code, libelle, status
  1871. */
  1872. public function getInfosTicketExternalContact($status = -1)
  1873. {
  1874. return $this->listeContact(-1, 'external', 0, '', $status);
  1875. }
  1876. /**
  1877. * Return id des contacts clients des intervenants
  1878. *
  1879. * @return array Liste des id contacts intervenants
  1880. */
  1881. public function getIdTicketInternalInvolvedContact()
  1882. {
  1883. return $this->getIdContact('internal', 'CONTRIBUTOR');
  1884. }
  1885. /**
  1886. * Return id des contacts clients des intervenants
  1887. *
  1888. * @return array Liste des id contacts intervenants
  1889. */
  1890. public function getIdTicketCustomerInvolvedContact()
  1891. {
  1892. return $this->getIdContact('external', 'CONTRIBUTOR');
  1893. }
  1894. /**
  1895. * Return id of all contacts for ticket
  1896. *
  1897. * @return array Array of contacts for tickets
  1898. */
  1899. public function getTicketAllContacts()
  1900. {
  1901. $array_contact = array();
  1902. $array_contact = $this->getIdTicketInternalContact();
  1903. $array_contact = array_merge($array_contact, $this->getIdTicketCustomerContact());
  1904. $array_contact = array_merge($array_contact, $this->getIdTicketInternalInvolvedContact());
  1905. $array_contact = array_merge($array_contact, $this->getIdTicketCustomerInvolvedContact());
  1906. return $array_contact;
  1907. }
  1908. /**
  1909. * Return id of all contacts for ticket
  1910. *
  1911. * @return array Array of contacts
  1912. */
  1913. public function getTicketAllCustomerContacts()
  1914. {
  1915. $array_contact = array();
  1916. $array_contact = array_merge($array_contact, $this->getIdTicketCustomerContact());
  1917. $array_contact = array_merge($array_contact, $this->getIdTicketCustomerInvolvedContact());
  1918. return $array_contact;
  1919. }
  1920. /**
  1921. * Get array of all contacts for a ticket
  1922. * Override method of file commonobject.class.php to add phone number
  1923. *
  1924. * @param int $statusoflink Status of lines to get (-1=all)
  1925. * @param string $source Source of contact: external or thirdparty (llx_socpeople) or internal (llx_user)
  1926. * @param int $list 0:Return array contains all properties, 1:Return array contains just id
  1927. * @param string $code Filter on this code of contact type ('SHIPPING', 'BILLING', ...)
  1928. * @param int $status Status of user or company
  1929. * @return array|int Array of contacts
  1930. */
  1931. public function listeContact($statusoflink = -1, $source = 'external', $list = 0, $code = '', $status = -1)
  1932. {
  1933. global $langs;
  1934. $tab = array();
  1935. $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
  1936. if ($source == 'internal') {
  1937. $sql .= ", '-1' as socid, t.statut as statuscontact";
  1938. }
  1939. if ($source == 'external' || $source == 'thirdparty') {
  1940. $sql .= ", t.fk_soc as socid, t.statut as statuscontact";
  1941. }
  1942. $sql .= ", t.civility, t.lastname as lastname, t.firstname, t.email";
  1943. if ($source == 'internal') {
  1944. $sql .= ", t.office_phone as phone, t.user_mobile as phone_mobile";
  1945. }
  1946. if ($source == 'external') {
  1947. $sql .= ", t.phone as phone, t.phone_mobile as phone_mobile, t.phone_perso as phone_perso";
  1948. }
  1949. $sql .= ", tc.source, tc.element, tc.code, tc.libelle as type_contact_label";
  1950. $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact tc";
  1951. $sql .= ", ".MAIN_DB_PREFIX."element_contact ec";
  1952. if ($source == 'internal') {
  1953. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user t on ec.fk_socpeople = t.rowid";
  1954. }
  1955. if ($source == 'external' || $source == 'thirdparty') {
  1956. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople t on ec.fk_socpeople = t.rowid";
  1957. }
  1958. $sql .= " WHERE ec.element_id = ".((int) $this->id);
  1959. $sql .= " AND ec.fk_c_type_contact=tc.rowid";
  1960. $sql .= " AND tc.element='".$this->db->escape($this->element)."'";
  1961. if ($source == 'internal') {
  1962. $sql .= " AND tc.source = 'internal'";
  1963. if ($status >= 0) {
  1964. $sql .= " AND t.statut = ".((int) $status);
  1965. }
  1966. }
  1967. if ($source == 'external' || $source == 'thirdparty') {
  1968. $sql .= " AND tc.source = 'external'";
  1969. if ($status >= 0) {
  1970. $sql .= " AND t.statut = ".((int) $status);
  1971. }
  1972. }
  1973. if (!empty($code)) {
  1974. $sql .= " AND tc.code = '".$this->db->escape($code)."'";
  1975. }
  1976. $sql .= " AND tc.active=1";
  1977. if ($statusoflink >= 0) {
  1978. $sql .= " AND ec.statut = ".((int) $statusoflink);
  1979. }
  1980. $sql .= " ORDER BY t.lastname ASC";
  1981. $resql = $this->db->query($sql);
  1982. if ($resql) {
  1983. $num = $this->db->num_rows($resql);
  1984. $i = 0;
  1985. while ($i < $num) {
  1986. $obj = $this->db->fetch_object($resql);
  1987. if (!$list) {
  1988. $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
  1989. $libelle_type = ($langs->trans($transkey) != $transkey ? $langs->trans($transkey) : $obj->type_contact_label);
  1990. $tab[$i] = array(
  1991. 'source' => $obj->source,
  1992. 'socid' => $obj->socid,
  1993. 'id' => $obj->id,
  1994. 'nom' => $obj->lastname, // For backward compatibility
  1995. 'civility' => $obj->civility,
  1996. 'lastname' => $obj->lastname,
  1997. 'firstname' => $obj->firstname,
  1998. 'email' => $obj->email,
  1999. 'rowid' => $obj->rowid,
  2000. 'code' => $obj->code,
  2001. 'libelle' => $libelle_type,
  2002. 'status' => $obj->statuslink,
  2003. 'statuscontact'=>$obj->statuscontact,
  2004. 'fk_c_type_contact' => $obj->fk_c_type_contact,
  2005. 'phone' => $obj->phone,
  2006. 'phone_mobile' => $obj->phone_mobile);
  2007. } else {
  2008. $tab[$i] = $obj->id;
  2009. }
  2010. $i++;
  2011. }
  2012. return $tab;
  2013. } else {
  2014. $this->error = $this->db->error();
  2015. dol_print_error($this->db);
  2016. return -1;
  2017. }
  2018. }
  2019. /**
  2020. * Get a default reference.
  2021. *
  2022. * @param Societe $thirdparty Thirdparty
  2023. * @return string Reference
  2024. */
  2025. public function getDefaultRef($thirdparty = '')
  2026. {
  2027. global $conf;
  2028. $defaultref = '';
  2029. $modele = !getDolGlobalString('TICKET_ADDON') ? 'mod_ticket_simple' : $conf->global->TICKET_ADDON;
  2030. // Search template files
  2031. $file = '';
  2032. $classname = '';
  2033. $filefound = 0;
  2034. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  2035. foreach ($dirmodels as $reldir) {
  2036. $file = dol_buildpath($reldir."core/modules/ticket/".$modele.'.php', 0);
  2037. if (file_exists($file)) {
  2038. $filefound = 1;
  2039. $classname = $modele;
  2040. break;
  2041. }
  2042. }
  2043. if ($filefound) {
  2044. $result = dol_include_once($reldir."core/modules/ticket/".$modele.'.php');
  2045. $modTicket = new $classname;
  2046. $defaultref = $modTicket->getNextValue($thirdparty, $this);
  2047. }
  2048. if (is_numeric($defaultref) && $defaultref <= 0) {
  2049. $defaultref = '';
  2050. }
  2051. return $defaultref;
  2052. }
  2053. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2054. /**
  2055. * Return if at least one photo is available
  2056. *
  2057. * @param string $sdir Directory to scan
  2058. * @return boolean True if at least one photo is available, False if not
  2059. */
  2060. public function is_photo_available($sdir)
  2061. {
  2062. // phpcs:enable
  2063. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  2064. global $conf;
  2065. $dir = $sdir.'/';
  2066. $nbphoto = 0;
  2067. $dir_osencoded = dol_osencode($dir);
  2068. if (file_exists($dir_osencoded)) {
  2069. $handle = opendir($dir_osencoded);
  2070. if (is_resource($handle)) {
  2071. while (($file = readdir($handle)) !== false) {
  2072. if (!utf8_check($file)) {
  2073. $file = utf8_encode($file); // To be sure data is stored in UTF8 in memory
  2074. }
  2075. if (dol_is_file($dir.$file)) {
  2076. return true;
  2077. }
  2078. }
  2079. }
  2080. }
  2081. return false;
  2082. }
  2083. /**
  2084. * Copy files defined into $_SESSION array into the ticket directory of attached files.
  2085. * Used for files linked into messages.
  2086. * Files may be renamed during copy to avoid overwriting existing files.
  2087. *
  2088. * @param string $forcetrackid Force trackid used for $keytoavoidconflict into get_attached_files()
  2089. * @return array|int Array with final path/name/mime of files.
  2090. */
  2091. public function copyFilesForTicket($forcetrackid = null)
  2092. {
  2093. global $conf;
  2094. // Create form object
  2095. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
  2096. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  2097. include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  2098. $maxwidthsmall = 270;
  2099. $maxheightsmall = 150;
  2100. $maxwidthmini = 128;
  2101. $maxheightmini = 72;
  2102. $formmail = new FormMail($this->db);
  2103. $formmail->trackid = (is_null($forcetrackid) ? 'tic'.$this->id : '');
  2104. $attachedfiles = $formmail->get_attached_files();
  2105. $filepath = $attachedfiles['paths']; // path is for example user->dir_temp.'/'.$user->id.'/'...
  2106. $filename = $attachedfiles['names'];
  2107. $mimetype = $attachedfiles['mimes'];
  2108. // Copy files into ticket directory
  2109. $destdir = $conf->ticket->dir_output.'/'.$this->ref;
  2110. if (!dol_is_dir($destdir)) {
  2111. dol_mkdir($destdir);
  2112. }
  2113. $listofpaths = array();
  2114. $listofnames = array();
  2115. foreach ($filename as $i => $val) {
  2116. $destfile = $destdir.'/'.$filename[$i];
  2117. // If destination file already exists, we add a suffix to avoid to overwrite
  2118. if (is_file($destfile)) {
  2119. $pathinfo = pathinfo($filename[$i]);
  2120. $now = dol_now();
  2121. $destfile = $destdir.'/'.$pathinfo['filename'].' - '.dol_print_date($now, 'dayhourlog').'.'.$pathinfo['extension'];
  2122. }
  2123. $moreinfo = array('description'=>'File saved by copyFilesForTicket', 'src_object_type' => $this->element, 'src_object_id' => $this->id);
  2124. $res = dol_move($filepath[$i], $destfile, 0, 1, 0, 1, $moreinfo);
  2125. if (!$res) {
  2126. // Move has failed
  2127. $this->error = "Failed to move file ".dirbasename($filepath[$i])." into ".dirbasename($destfile);
  2128. return -1;
  2129. } else {
  2130. // If file is an image, we create thumbs
  2131. if (image_format_supported($destfile) == 1) {
  2132. // Create small thumbs for image (Ratio is near 16/9)
  2133. // Used on logon for example
  2134. $imgThumbSmall = vignette($destfile, $maxwidthsmall, $maxheightsmall, '_small', 50, "thumbs");
  2135. // Create mini thumbs for image (Ratio is near 16/9)
  2136. // Used on menu or for setup page for example
  2137. $imgThumbMini = vignette($destfile, $maxwidthmini, $maxheightmini, '_mini', 50, "thumbs");
  2138. }
  2139. }
  2140. // Clear variables into session
  2141. $formmail->remove_attached_files($i);
  2142. // Fill array with new names
  2143. $listofpaths[$i] = $destfile;
  2144. $listofnames[$i] = basename($destfile);
  2145. }
  2146. return array('listofpaths'=>$listofpaths, 'listofnames'=>$listofnames, 'listofmimes'=>$mimetype);
  2147. }
  2148. /**
  2149. * Sets object to supplied categories.
  2150. *
  2151. * Deletes object from existing categories not supplied.
  2152. * Adds it to non existing supplied categories.
  2153. * Existing categories are left untouch.
  2154. *
  2155. * @param int[]|int $categories Category or categories IDs
  2156. * @return int <0 if KO, >0 if OK
  2157. */
  2158. public function setCategories($categories)
  2159. {
  2160. // Handle single category
  2161. if (!is_array($categories)) {
  2162. $categories = array($categories);
  2163. }
  2164. // Get current categories
  2165. include_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  2166. $c = new Categorie($this->db);
  2167. $existing = $c->containing($this->id, Categorie::TYPE_TICKET, 'id');
  2168. // Diff
  2169. if (is_array($existing)) {
  2170. $to_del = array_diff($existing, $categories);
  2171. $to_add = array_diff($categories, $existing);
  2172. } else {
  2173. $to_del = array(); // Nothing to delete
  2174. $to_add = $categories;
  2175. }
  2176. // Process
  2177. foreach ($to_del as $del) {
  2178. if ($c->fetch($del) > 0) {
  2179. $c->del_type($this, Categorie::TYPE_TICKET);
  2180. }
  2181. }
  2182. foreach ($to_add as $add) {
  2183. if ($c->fetch($add) > 0) {
  2184. $c->add_type($this, Categorie::TYPE_TICKET);
  2185. }
  2186. }
  2187. return 1;
  2188. }
  2189. /**
  2190. * Add new message on a ticket (private/public area).
  2191. * Can also send it by email if GETPOST('send_email', 'int') is set. For such email, header and footer is added.
  2192. *
  2193. * @param User $user User for action
  2194. * @param string $action Action string
  2195. * @param int $private 1=Message is private (must not be visible by external users)
  2196. * @param int $public_area 0=Default,
  2197. * 1=If we are creating the message from a public area, so confirmation email will be sent to the author
  2198. * and we can search contact from email to add it as contact of ticket if TICKET_ASSIGN_CONTACT_TO_MESSAGE is set
  2199. * @return int <0 if KO, >= 0 if OK
  2200. */
  2201. public function newMessage($user, &$action, $private = 1, $public_area = 0)
  2202. {
  2203. global $mysoc, $conf, $langs;
  2204. $error = 0;
  2205. $object = new Ticket($this->db);
  2206. $ret = $object->fetch('', '', GETPOST('track_id', 'alpha'));
  2207. $object->socid = $object->fk_soc;
  2208. $object->fetch_thirdparty();
  2209. $object->fetch_project();
  2210. if ($ret < 0) {
  2211. $error++;
  2212. array_push($this->errors, $langs->trans("ErrorTicketIsNotValid"));
  2213. $action = '';
  2214. }
  2215. if (!GETPOST("message")) {
  2216. $error++;
  2217. array_push($this->errors, $langs->trans("ErrorFieldRequired", $langs->transnoentities("message")));
  2218. $action = 'add_message';
  2219. }
  2220. if (!$error) {
  2221. $object->subject = GETPOST('subject', 'alphanohtml');
  2222. $object->message = GETPOST("message", "restricthtml");
  2223. $object->private = GETPOST("private_message", "alpha");
  2224. $send_email = GETPOST('send_email', 'int');
  2225. // Copy attached files (saved into $_SESSION) as linked files to ticket. Return array with final name used.
  2226. $resarray = $object->copyFilesForTicket();
  2227. if (is_numeric($resarray) && $resarray == -1) {
  2228. setEventMessages($object->error, $object->errors, 'errors');
  2229. return -1;
  2230. }
  2231. $listofpaths = $resarray['listofpaths'];
  2232. $listofnames = $resarray['listofnames'];
  2233. $listofmimes = $resarray['listofmimes'];
  2234. $id = $object->createTicketMessage($user, 0, $listofpaths, $listofmimes, $listofnames, $send_email, $public_area);
  2235. if ($id <= 0) {
  2236. $error++;
  2237. $this->error = $object->error;
  2238. $this->errors = $object->errors;
  2239. $action = 'add_message';
  2240. }
  2241. if (!$error && $id > 0) {
  2242. setEventMessages($langs->trans('TicketMessageSuccessfullyAdded'), null, 'mesgs');
  2243. //var_dump($_SESSION);
  2244. //var_dump($listofpaths);exit;
  2245. if (!empty($public_area)) {
  2246. /*
  2247. * Message created from the Public interface
  2248. *
  2249. * Send emails to assigned users (public area notification)
  2250. */
  2251. if (getDolGlobalString('TICKET_PUBLIC_NOTIFICATION_NEW_MESSAGE_ENABLED')) {
  2252. // Retrieve internal contact datas
  2253. $internal_contacts = $object->getInfosTicketInternalContact(1);
  2254. $assigned_user_dont_have_email = '';
  2255. $sendto = array();
  2256. if ($this->fk_user_assign > 0) {
  2257. $assigned_user = new User($this->db);
  2258. $assigned_user->fetch($this->fk_user_assign);
  2259. if (!empty($assigned_user->email)) {
  2260. $sendto[$assigned_user->email] = $assigned_user->getFullName($langs)." <".$assigned_user->email.">";
  2261. } else {
  2262. $assigned_user_dont_have_email = $assigned_user->getFullName($langs);
  2263. }
  2264. }
  2265. // Build array to display recipient list
  2266. foreach ($internal_contacts as $key => $info_sendto) {
  2267. // Avoid duplicate notifications
  2268. if ($info_sendto['id'] == $user->id) {
  2269. continue;
  2270. }
  2271. if ($info_sendto['email'] != '') {
  2272. if (!empty($info_sendto['email'])) {
  2273. $sendto[] = dolGetFirstLastname($info_sendto['firstname'], $info_sendto['lastname'])." <".$info_sendto['email'].">";
  2274. }
  2275. }
  2276. }
  2277. if (empty($sendto)) {
  2278. if (getDolGlobalString('TICKET_PUBLIC_NOTIFICATION_NEW_MESSAGE_DEFAULT_EMAIL')) {
  2279. $sendto[getDolGlobalString('TICKET_PUBLIC_NOTIFICATION_NEW_MESSAGE_DEFAULT_EMAIL')] = $conf->global->TICKET_PUBLIC_NOTIFICATION_NEW_MESSAGE_DEFAULT_EMAIL;
  2280. } elseif (getDolGlobalString('TICKET_NOTIFICATION_EMAIL_TO')) {
  2281. $sendto[getDolGlobalString('TICKET_NOTIFICATION_EMAIL_TO')] = $conf->global->TICKET_NOTIFICATION_EMAIL_TO;
  2282. }
  2283. }
  2284. // Add global email address recipient
  2285. if (getDolGlobalString('TICKET_NOTIFICATION_ALSO_MAIN_ADDRESS') &&
  2286. getDolGlobalString('TICKET_NOTIFICATION_EMAIL_TO') && !array_key_exists($conf->global->TICKET_NOTIFICATION_EMAIL_TO, $sendto)
  2287. ) {
  2288. $sendto[getDolGlobalString('TICKET_NOTIFICATION_EMAIL_TO')] = $conf->global->TICKET_NOTIFICATION_EMAIL_TO;
  2289. }
  2290. if (!empty($sendto)) {
  2291. $appli = getDolGlobalString('MAIN_APPLICATION_TITLE', $mysoc->name);
  2292. $subject = '['.$appli.'- ticket #'.$object->track_id.'] '.$langs->trans('TicketNewMessage');
  2293. // Message send
  2294. $message = $langs->trans('TicketMessageMailIntroText');
  2295. $message .= '<br><br>';
  2296. $messagePost = GETPOST('message', 'restricthtml');
  2297. if (!dol_textishtml($messagePost)) {
  2298. $messagePost = dol_nl2br($messagePost);
  2299. }
  2300. $message .= $messagePost;
  2301. // Customer company infos
  2302. $message .= '<br><br>';
  2303. $message .= "==============================================";
  2304. $message .= !empty($object->thirdparty->name) ? '<br>'.$langs->trans('Thirdparty')." : ".$object->thirdparty->name : '';
  2305. $message .= !empty($object->thirdparty->town) ? '<br>'.$langs->trans('Town')." : ".$object->thirdparty->town : '';
  2306. $message .= !empty($object->thirdparty->phone) ? '<br>'.$langs->trans('Phone')." : ".$object->thirdparty->phone : '';
  2307. // Email send to
  2308. $message .= '<br><br>';
  2309. if (!empty($assigned_user_dont_have_email)) {
  2310. $message .= '<br>'.$langs->trans('NoEMail').' : '.$assigned_user_dont_have_email;
  2311. }
  2312. foreach ($sendto as $val) {
  2313. $message .= '<br>'.$langs->trans('TicketNotificationRecipient').' : '.$val;
  2314. }
  2315. // URL ticket
  2316. $url_internal_ticket = dol_buildpath('/ticket/card.php', 2).'?track_id='.$object->track_id;
  2317. $message .= '<br><br>';
  2318. $message .= $langs->trans('TicketNotificationEmailBodyInfosTrackUrlinternal').' : <a href="'.$url_internal_ticket.'">'.$object->track_id.'</a>';
  2319. $this->sendTicketMessageByEmail($subject, $message, '', $sendto, $listofpaths, $listofmimes, $listofnames);
  2320. }
  2321. }
  2322. } else {
  2323. /*
  2324. * Message send from the Backoffice / Private area
  2325. *
  2326. * Send emails to internal users (linked contacts) then, if private is not set, to external users (linked contacts or thirdparty email if no contact set)
  2327. */
  2328. if ($send_email > 0) {
  2329. // Retrieve internal contact datas
  2330. $internal_contacts = $object->getInfosTicketInternalContact(1);
  2331. $sendto = array();
  2332. if (is_array($internal_contacts) && count($internal_contacts) > 0) {
  2333. // Set default subject
  2334. $appli = getDolGlobalString('MAIN_APPLICATION_TITLE', $mysoc->name);
  2335. $subject = GETPOST('subject', 'alphanohtml') ? GETPOST('subject', 'alphanohtml') : '['.$appli.' - '.$langs->trans("Ticket").' #'.$object->track_id.'] '.$langs->trans('TicketNewMessage');
  2336. $message_intro = $langs->trans('TicketNotificationEmailBody', "#".$object->id);
  2337. $message_signature = GETPOST('mail_signature') ? GETPOST('mail_signature') : getDolGlobalString('TICKET_MESSAGE_MAIL_SIGNATURE');
  2338. $message = $langs->trans('TicketMessageMailIntroText');
  2339. $message .= '<br><br>';
  2340. $messagePost = GETPOST('message', 'restricthtml');
  2341. if (!dol_textishtml($messagePost)) {
  2342. $messagePost = dol_nl2br($messagePost);
  2343. }
  2344. $message .= $messagePost;
  2345. // Data about customer
  2346. $message .= '<br><br>';
  2347. $message .= "==============================================<br>";
  2348. $message .= !empty($object->thirdparty->name) ? $langs->trans('Thirdparty')." : ".$object->thirdparty->name : '';
  2349. $message .= !empty($object->thirdparty->town) ? '<br>'.$langs->trans('Town')." : ".$object->thirdparty->town : '';
  2350. $message .= !empty($object->thirdparty->phone) ? '<br>'.$langs->trans('Phone')." : ".$object->thirdparty->phone : '';
  2351. // Build array to display recipient list
  2352. foreach ($internal_contacts as $key => $info_sendto) {
  2353. // Avoid duplicate notifications
  2354. if ($info_sendto['id'] == $user->id) {
  2355. continue;
  2356. }
  2357. if ($info_sendto['email'] != '') {
  2358. if (!empty($info_sendto['email'])) {
  2359. $sendto[$info_sendto['email']] = dolGetFirstLastname($info_sendto['firstname'], $info_sendto['lastname'])." <".$info_sendto['email'].">";
  2360. }
  2361. // Contact type
  2362. $recipient = dolGetFirstLastname($info_sendto['firstname'], $info_sendto['lastname'], '-1').' ('.strtolower($info_sendto['libelle']).')';
  2363. $message .= (!empty($recipient) ? $langs->trans('TicketNotificationRecipient').' : '.$recipient.'<br>' : '');
  2364. }
  2365. }
  2366. $message .= '<br>';
  2367. // URL ticket
  2368. $url_internal_ticket = dol_buildpath('/ticket/card.php', 2).'?track_id='.$object->track_id;
  2369. // Add html link on url
  2370. $message .= '<br>'.$langs->trans('TicketNotificationEmailBodyInfosTrackUrlinternal').' : <a href="'.$url_internal_ticket.'">'.$object->track_id.'</a><br>';
  2371. // Add global email address recipient
  2372. if (getDolGlobalString('TICKET_NOTIFICATION_ALSO_MAIN_ADDRESS') !== '' && !array_key_exists(getDolGlobalString('TICKET_NOTIFICATION_EMAIL_TO'), $sendto)) {
  2373. if (getDolGlobalString('TICKET_NOTIFICATION_EMAIL_TO') !== '') {
  2374. $sendto[getDolGlobalString('TICKET_NOTIFICATION_EMAIL_TO')] = getDolGlobalString('TICKET_NOTIFICATION_EMAIL_TO');
  2375. }
  2376. }
  2377. // dont try to send email if no recipient
  2378. if (!empty($sendto)) {
  2379. $this->sendTicketMessageByEmail($subject, $message, '', $sendto, $listofpaths, $listofmimes, $listofnames);
  2380. }
  2381. }
  2382. /*
  2383. * Send emails for externals users if not private (linked contacts)
  2384. */
  2385. if (empty($object->private)) {
  2386. // Retrieve email of all contacts (external)
  2387. $external_contacts = $object->getInfosTicketExternalContact(1);
  2388. // If no contact, get email from thirdparty
  2389. if (is_array($external_contacts) && count($external_contacts) === 0) {
  2390. if (!empty($object->fk_soc)) {
  2391. $object->fetch_thirdparty($object->fk_soc);
  2392. $array_company = array(array('firstname' => '', 'lastname' => $object->thirdparty->name, 'email' => $object->thirdparty->email, 'libelle' => $langs->transnoentities('Customer'), 'socid' => $object->thirdparty->id));
  2393. $external_contacts = array_merge($external_contacts, $array_company);
  2394. } elseif (empty($object->fk_soc) && !empty($object->origin_email)) {
  2395. $array_external = array(array('firstname' => '', 'lastname' => $object->origin_email, 'email' => $object->thirdparty->email, 'libelle' => $langs->transnoentities('Customer'), 'socid' => $object->thirdparty->id));
  2396. $external_contacts = array_merge($external_contacts, $array_external);
  2397. }
  2398. }
  2399. $sendto = array();
  2400. if (is_array($external_contacts) && count($external_contacts) > 0) {
  2401. // Get default subject for email to external contacts
  2402. $appli = getDolGlobalString('MAIN_APPLICATION_TITLE', $mysoc->name);
  2403. $subject = GETPOST('subject') ? GETPOST('subject') : '['.$appli.' - '.$langs->trans("Ticket").' #'.$object->track_id.'] '.$langs->trans('TicketNewMessage');
  2404. $message_intro = GETPOST('mail_intro') ? GETPOST('mail_intro', 'restricthtml') : getDolGlobalString('TICKET_MESSAGE_MAIL_INTRO');
  2405. $message_signature = GETPOST('mail_signature') ? GETPOST('mail_signature', 'restricthtml') : getDolGlobalString('TICKET_MESSAGE_MAIL_SIGNATURE');
  2406. if (!dol_textishtml($message_intro)) {
  2407. $message_intro = dol_nl2br($message_intro);
  2408. }
  2409. if (!dol_textishtml($message_signature)) {
  2410. $message_signature = dol_nl2br($message_signature);
  2411. }
  2412. // We put intro after
  2413. $messagePost = GETPOST('message', 'restricthtml');
  2414. if (!dol_textishtml($messagePost)) {
  2415. $messagePost = dol_nl2br($messagePost);
  2416. }
  2417. $message = $messagePost;
  2418. $message .= '<br><br>';
  2419. foreach ($external_contacts as $key => $info_sendto) {
  2420. // altairis: avoid duplicate emails to external contacts
  2421. if ($info_sendto['id'] == $user->contact_id) {
  2422. continue;
  2423. }
  2424. if ($info_sendto['email'] != '' && $info_sendto['email'] != $object->origin_email) {
  2425. if (!empty($info_sendto['email'])) {
  2426. $sendto[$info_sendto['email']] = trim($info_sendto['firstname']." ".$info_sendto['lastname'])." <".$info_sendto['email'].">";
  2427. }
  2428. $recipient = dolGetFirstLastname($info_sendto['firstname'], $info_sendto['lastname'], '-1').' ('.strtolower($info_sendto['libelle']).')';
  2429. $message .= (!empty($recipient) ? $langs->trans('TicketNotificationRecipient').' : '.$recipient.'<br>' : '');
  2430. }
  2431. }
  2432. // If public interface is not enable, use link to internal page into mail
  2433. $url_public_ticket = (getDolGlobalInt('TICKET_ENABLE_PUBLIC_INTERFACE') ?
  2434. (getDolGlobalString('TICKET_URL_PUBLIC_INTERFACE') !== '' ? getDolGlobalString('TICKET_URL_PUBLIC_INTERFACE') . '/view.php' : dol_buildpath('/public/ticket/view.php', 2)) : dol_buildpath('/ticket/card.php', 2)).'?track_id='.$object->track_id;
  2435. $message .= '<br>'.$langs->trans('TicketNewEmailBodyInfosTrackUrlCustomer').' : <a href="'.$url_public_ticket.'">'.$object->track_id.'</a><br>';
  2436. // Build final message
  2437. $message = $message_intro.'<br><br>'.$message;
  2438. // Add signature
  2439. $message .= '<br>'.$message_signature;
  2440. if (!empty($object->origin_email)) {
  2441. $sendto[$object->origin_email] = $object->origin_email;
  2442. }
  2443. if ($object->fk_soc > 0 && !array_key_exists($object->origin_email, $sendto)) {
  2444. $object->socid = $object->fk_soc;
  2445. $object->fetch_thirdparty();
  2446. if (!empty($object->thirdparty->email)) {
  2447. $sendto[$object->thirdparty->email] = $object->thirdparty->email;
  2448. }
  2449. }
  2450. // Add global email address recipient
  2451. if (getDolGlobalString('TICKET_NOTIFICATION_ALSO_MAIN_ADDRESS') !== '' && !array_key_exists(getDolGlobalString('TICKET_NOTIFICATION_EMAIL_TO'), $sendto)) {
  2452. if (getDolGlobalString('TICKET_NOTIFICATION_EMAIL_TO') !== '') {
  2453. $sendto[getDolGlobalString('TICKET_NOTIFICATION_EMAIL_TO')] = getDolGlobalString('TICKET_NOTIFICATION_EMAIL_TO');
  2454. }
  2455. }
  2456. // Dont try to send email when no recipient
  2457. if (!empty($sendto)) {
  2458. $result = $this->sendTicketMessageByEmail($subject, $message, '', $sendto, $listofpaths, $listofmimes, $listofnames);
  2459. if ($result) {
  2460. // update last_msg_sent date (for last message sent to external users)
  2461. $this->date_last_msg_sent = dol_now();
  2462. $this->update($user, 1); // disable trigger when updating date_last_msg_sent. sendTicketMessageByEmail already create an event in actioncomm table.
  2463. }
  2464. }
  2465. }
  2466. }
  2467. }
  2468. }
  2469. // Set status back to "In progress" if not set yet, but only if internal user and not a private message
  2470. // Or set status to "In porgress" if the client has answered and if the ticket has started
  2471. // So we are sure to leave the STATUS_DRAFT, STATUS_NEED_INFO.
  2472. if (($object->status < self::STATUS_IN_PROGRESS && !$user->socid && !$private) ||
  2473. ($object->status > self::STATUS_IN_PROGRESS && $public_area)
  2474. ) {
  2475. $object->setStatut($object::STATUS_IN_PROGRESS);
  2476. }
  2477. return 1;
  2478. } else {
  2479. setEventMessages($object->error, $object->errors, 'errors');
  2480. return -1;
  2481. }
  2482. } else {
  2483. setEventMessages($this->error, $this->errors, 'errors');
  2484. return -1;
  2485. }
  2486. }
  2487. /**
  2488. * Send ticket by email to linked contacts
  2489. *
  2490. * @param string $subject Email subject
  2491. * @param string $message Email message
  2492. * @param int $send_internal_cc Receive a copy on internal email ($conf->global->TICKET_NOTIFICATION_EMAIL_FROM)
  2493. * @param array $array_receiver Array of receiver. exemple array('name' => 'John Doe', 'email' => 'john@doe.com', etc...)
  2494. * @param array $filename_list List of files to attach (full path of filename on file system)
  2495. * @param array $mimetype_list List of MIME type of attached files
  2496. * @param array $mimefilename_list List of attached file name in message
  2497. * @return boolean True if mail sent to at least one receiver, false otherwise
  2498. */
  2499. public function sendTicketMessageByEmail($subject, $message, $send_internal_cc = 0, $array_receiver = array(), $filename_list = array(), $mimetype_list = array(), $mimefilename_list = array())
  2500. {
  2501. global $conf, $langs, $user;
  2502. if (getDolGlobalInt('TICKET_DISABLE_ALL_MAILS')) {
  2503. dol_syslog(get_class($this).'::sendTicketMessageByEmail: Emails are disable into ticket setup by option TICKET_DISABLE_ALL_MAILS', LOG_WARNING);
  2504. return false;
  2505. }
  2506. $langs->load("mails");
  2507. include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  2508. //$contactstatic = new Contact($this->db);
  2509. // If no receiver defined, load all ticket linked contacts
  2510. if (!is_array($array_receiver) || !count($array_receiver) > 0) {
  2511. $array_receiver = $this->getInfosTicketInternalContact(1);
  2512. $array_receiver = array_merge($array_receiver, $this->getInfosTicketExternalContact(1));
  2513. }
  2514. if ($send_internal_cc) {
  2515. $sendtocc = getDolGlobalString('TICKET_NOTIFICATION_EMAIL_FROM');
  2516. } else {
  2517. $sendtocc = '';
  2518. }
  2519. $from = getDolGlobalString('TICKET_NOTIFICATION_EMAIL_FROM');
  2520. $is_sent = false;
  2521. if (is_array($array_receiver) && count($array_receiver) > 0) {
  2522. foreach ($array_receiver as $key => $receiver) {
  2523. $deliveryreceipt = 0;
  2524. $filepath = $filename_list;
  2525. $filename = $mimefilename_list;
  2526. $mimetype = $mimetype_list;
  2527. // Send email
  2528. $old_MAIN_MAIL_AUTOCOPY_TO = getDolGlobalString('MAIN_MAIL_AUTOCOPY_TO');
  2529. if (getDolGlobalString('TICKET_DISABLE_MAIL_AUTOCOPY_TO') !== '') {
  2530. $conf->global->MAIN_MAIL_AUTOCOPY_TO = '';
  2531. }
  2532. $upload_dir_tmp = $conf->user->dir_output."/".$user->id.'/temp';
  2533. include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  2534. $trackid = "tic".$this->id;
  2535. $moreinheader = 'X-Dolibarr-Info: sendTicketMessageByEmail'."\r\n";
  2536. if (!empty($this->email_msgid)) {
  2537. $moreinheader .= 'References <'.$this->email_msgid.'>'."\r\n";
  2538. }
  2539. $mailfile = new CMailFile($subject, $receiver, $from, $message, $filepath, $mimetype, $filename, $sendtocc, '', $deliveryreceipt, -1, '', '', $trackid, $moreinheader, 'ticket', '', $upload_dir_tmp);
  2540. if ($mailfile->error) {
  2541. setEventMessages($mailfile->error, null, 'errors');
  2542. } else {
  2543. $result = $mailfile->sendfile();
  2544. if ($result) {
  2545. setEventMessages($langs->trans('MailSuccessfulySent', $mailfile->getValidAddress($from, 2), $mailfile->getValidAddress($receiver, 2)), null, 'mesgs');
  2546. $is_sent = true;
  2547. } else {
  2548. $langs->load("other");
  2549. if ($mailfile->error) {
  2550. setEventMessages($langs->trans('ErrorFailedToSendMail', $from, $receiver), null, 'errors');
  2551. dol_syslog($langs->trans('ErrorFailedToSendMail', $from, $receiver).' : '.$mailfile->error);
  2552. } else {
  2553. setEventMessages('No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS', null, 'errors');
  2554. }
  2555. }
  2556. }
  2557. if (getDolGlobalString('TICKET_DISABLE_MAIL_AUTOCOPY_TO') !== '') {
  2558. $conf->global->MAIN_MAIL_AUTOCOPY_TO = $old_MAIN_MAIL_AUTOCOPY_TO;
  2559. }
  2560. }
  2561. } else {
  2562. $langs->load("other");
  2563. setEventMessages($langs->trans('ErrorMailRecipientIsEmptyForSendTicketMessage'), null, 'warnings');
  2564. }
  2565. return $is_sent;
  2566. }
  2567. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2568. /**
  2569. * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
  2570. *
  2571. * @param User $user Object user
  2572. * @param int $mode "opened" for askprice to close, "signed" for proposal to invoice
  2573. * @return WorkboardResponse|int <0 if KO, WorkboardResponse if OK
  2574. */
  2575. public function load_board($user, $mode)
  2576. {
  2577. // phpcs:enable
  2578. global $user, $langs;
  2579. $now = dol_now();
  2580. $delay_warning = 0;
  2581. $clause = " WHERE";
  2582. $sql = "SELECT p.rowid, p.ref, p.datec as datec";
  2583. $sql .= " FROM ".MAIN_DB_PREFIX."ticket as p";
  2584. if (isModEnabled('societe') && !$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  2585. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc";
  2586. $sql .= " WHERE sc.fk_user = ".((int) $user->id);
  2587. $clause = " AND";
  2588. }
  2589. $sql .= $clause." p.entity IN (".getEntity('ticket').")";
  2590. if ($mode == 'opened') {
  2591. $sql .= " AND p.fk_statut NOT IN (".Ticket::STATUS_CLOSED.", ".Ticket::STATUS_CANCELED.")";
  2592. }
  2593. if ($user->socid) {
  2594. $sql .= " AND p.fk_soc = ".((int) $user->socid);
  2595. }
  2596. $resql = $this->db->query($sql);
  2597. if ($resql) {
  2598. $label = $labelShort = '';
  2599. $status = '';
  2600. if ($mode == 'opened') {
  2601. $status = 'openall';
  2602. //$delay_warning = $conf->ticket->warning_delay;
  2603. $delay_warning = 0;
  2604. $label = $langs->trans("MenuListNonClosed");
  2605. $labelShort = $langs->trans("MenuListNonClosed");
  2606. }
  2607. $response = new WorkboardResponse();
  2608. //$response->warning_delay = $delay_warning / 60 / 60 / 24;
  2609. $response->label = $label;
  2610. $response->labelShort = $labelShort;
  2611. $response->url = DOL_URL_ROOT.'/ticket/list.php?search_fk_statut[]='.$status;
  2612. $response->img = img_object('', "ticket");
  2613. // This assignment in condition is not a bug. It allows walking the results.
  2614. while ($obj = $this->db->fetch_object($resql)) {
  2615. $response->nbtodo++;
  2616. if ($mode == 'opened') {
  2617. $datelimit = $this->db->jdate($obj->datec) + $delay_warning;
  2618. if ($datelimit < $now) {
  2619. //$response->nbtodolate++;
  2620. }
  2621. }
  2622. }
  2623. return $response;
  2624. } else {
  2625. $this->error = $this->db->lasterror();
  2626. return -1;
  2627. }
  2628. }
  2629. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2630. /**
  2631. * Load indicator this->nb of global stats widget
  2632. *
  2633. * @return int <0 if ko, >0 if ok
  2634. */
  2635. public function load_state_board()
  2636. {
  2637. // phpcs:enable
  2638. global $conf, $user;
  2639. $this->nb = array();
  2640. $clause = "WHERE";
  2641. $sql = "SELECT count(p.rowid) as nb";
  2642. $sql .= " FROM ".MAIN_DB_PREFIX."ticket as p";
  2643. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid";
  2644. if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) {
  2645. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
  2646. $sql .= " WHERE sc.fk_user = ".((int) $user->id);
  2647. $clause = "AND";
  2648. }
  2649. $sql .= " ".$clause." p.entity IN (".getEntity('ticket').")";
  2650. $resql = $this->db->query($sql);
  2651. if ($resql) {
  2652. // This assignment in condition is not a bug. It allows walking the results.
  2653. while ($obj = $this->db->fetch_object($resql)) {
  2654. $this->nb["ticket"] = $obj->nb;
  2655. }
  2656. $this->db->free($resql);
  2657. return 1;
  2658. } else {
  2659. dol_print_error($this->db);
  2660. $this->error = $this->db->lasterror();
  2661. return -1;
  2662. }
  2663. }
  2664. /**
  2665. * Function used to replace a thirdparty id with another one.
  2666. *
  2667. * @param DoliDB $db Database handler
  2668. * @param int $origin_id Old thirdparty id
  2669. * @param int $dest_id New thirdparty id
  2670. * @return bool
  2671. */
  2672. public static function replaceThirdparty($db, $origin_id, $dest_id)
  2673. {
  2674. $tables = array('ticket');
  2675. return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
  2676. }
  2677. /**
  2678. * Return clicable link of object (with eventually picto)
  2679. *
  2680. * @param string $option Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link)
  2681. * @param array $arraydata Array of data
  2682. * @return string HTML Code for Kanban thumb.
  2683. */
  2684. public function getKanbanView($option = '', $arraydata = null)
  2685. {
  2686. global $langs;
  2687. $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
  2688. $return = '<div class="box-flex-item box-flex-grow-zero">';
  2689. $return .= '<div class="info-box info-box-sm">';
  2690. $return .= '<span class="info-box-icon bg-infobox-action">';
  2691. $return .= img_picto('', $this->picto);
  2692. $return .= '</span>';
  2693. $return .= '<div class="info-box-content">';
  2694. $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</span>';
  2695. if ($selected >= 0) {
  2696. $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
  2697. }
  2698. if (!empty($arraydata['user_assignment'])) {
  2699. $return .= '<br><span class="info-box-label" title="'.dol_escape_htmltag($langs->trans("AssignedTo")).'">'.$arraydata['user_assignment'].'</span>';
  2700. }
  2701. if (property_exists($this, 'type_code') && !empty($this->type_code)) {
  2702. $return .= '<br>';
  2703. $return .= '<div class="tdoverflowmax125 inline-block">'.$langs->getLabelFromKey($this->db, 'TicketTypeShort'.$this->type_code, 'c_ticket_type', 'code', 'label', $this->type_code).'</div>';
  2704. }
  2705. if (method_exists($this, 'getLibStatut')) {
  2706. $return .= '<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3).'</div>';
  2707. }
  2708. $return .= '</div>';
  2709. $return .= '</div>';
  2710. $return .= '</div>';
  2711. return $return;
  2712. }
  2713. }