actions_ticket.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. <?php
  2. /* Copyright (C) 2013-2015 Jean-François FERRY <hello@librethic.io>
  3. * Copyright (C) 2016 Christophe Battarel <christophe@altairis.fr>
  4. * Copyright (C) 2024 Destailleur Laurent <eldy@users.sourceforge.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /**
  21. * \file ticket/class/actions_ticket.class.php
  22. * \ingroup ticket
  23. * \brief File Class ticket
  24. */
  25. require_once DOL_DOCUMENT_ROOT.'/ticket/class/ticket.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/commonhookactions.class.php';
  32. // TODO Only the last method emailElementlist is a hook method. Other must be moved into standard ticket.class.php
  33. /**
  34. * Class Actions of the module ticket
  35. */
  36. class ActionsTicket extends CommonHookActions
  37. {
  38. /**
  39. * @var DoliDB Database handler.
  40. */
  41. public $db;
  42. /**
  43. * @var Ticket Ticket
  44. */
  45. public $dao;
  46. public $mesg;
  47. /**
  48. * @var string Error code (or message)
  49. */
  50. public $error;
  51. /**
  52. * @var string[] Error codes (or messages)
  53. */
  54. public $errors = array();
  55. //! Numero de l'erreur
  56. public $errno = 0;
  57. public $template_dir;
  58. public $template;
  59. /**
  60. * @var string ticket action label
  61. */
  62. public $label;
  63. /**
  64. * @var string description
  65. */
  66. public $description;
  67. /**
  68. * @var int ID
  69. */
  70. public $fk_statut;
  71. /**
  72. * @var int Thirdparty ID
  73. */
  74. public $fk_soc;
  75. /**
  76. * Constructor
  77. *
  78. * @param DoliDB $db Database handler
  79. */
  80. public function __construct($db)
  81. {
  82. $this->db = $db;
  83. }
  84. /**
  85. * Instantiation of DAO class
  86. *
  87. * @return void
  88. */
  89. public function getInstanceDao()
  90. {
  91. if (!is_object($this->dao)) {
  92. $this->dao = new Ticket($this->db);
  93. }
  94. }
  95. /**
  96. * Fetch object
  97. *
  98. * @param int $id ID of ticket
  99. * @param string $ref Reference of ticket
  100. * @param string $track_id Track ID of ticket (for public area)
  101. * @return int Return integer <0 if KO, >0 if OK
  102. */
  103. public function fetch($id = 0, $ref = '', $track_id = '')
  104. {
  105. $this->getInstanceDao();
  106. return $this->dao->fetch($id, $ref, $track_id);
  107. }
  108. /**
  109. * Print statut
  110. *
  111. * @param int $mode Display mode
  112. * @return string Label of status
  113. */
  114. public function getLibStatut($mode = 0)
  115. {
  116. $this->getInstanceDao();
  117. $this->dao->fk_statut = $this->fk_statut;
  118. return $this->dao->getLibStatut($mode);
  119. }
  120. /**
  121. * Get ticket info
  122. *
  123. * @param int $id Object id
  124. * @return void
  125. */
  126. public function getInfo($id)
  127. {
  128. $this->getInstanceDao();
  129. $this->dao->fetch($id);
  130. $this->label = $this->dao->label;
  131. $this->description = $this->dao->description;
  132. }
  133. /**
  134. * Get action title
  135. *
  136. * @param string $action Type of action
  137. * @return string Title of action
  138. */
  139. public function getTitle($action = '')
  140. {
  141. global $langs;
  142. if ($action == 'create') {
  143. return $langs->trans("CreateTicket");
  144. } elseif ($action == 'edit') {
  145. return $langs->trans("EditTicket");
  146. } elseif ($action == 'view') {
  147. return $langs->trans("TicketCard");
  148. } elseif ($action == 'add_message') {
  149. return $langs->trans("TicketAddMessage");
  150. } else {
  151. return $langs->trans("TicketsManagement");
  152. }
  153. }
  154. /**
  155. * Show ticket original message
  156. *
  157. * @param User $user User wich display
  158. * @param string $action Action mode
  159. * @param Ticket $object Object ticket
  160. * @return void
  161. */
  162. public function viewTicketOriginalMessage($user, $action, $object)
  163. {
  164. global $conf, $langs;
  165. print '<!-- initial message of ticket -->'."\n";
  166. if ($user->hasRight('ticket', 'manage') && $action == 'edit_message_init') {
  167. // MESSAGE
  168. print '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
  169. print '<input type="hidden" name="token" value="'.newToken().'">';
  170. print '<input type="hidden" name="track_id" value="'.$object->track_id.'">';
  171. print '<input type="hidden" name="action" value="set_message">';
  172. }
  173. // Initial message
  174. print '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
  175. print '<table class="noborder centpercent margintable margintable">';
  176. print '<tr class="liste_titre trforfield"><td class="nowrap titlefield">';
  177. print $langs->trans("InitialMessage");
  178. print '</td><td>';
  179. if ($user->hasRight("ticket", "manage")) {
  180. print '<a class="editfielda" href="'.$_SERVER['PHP_SELF'].'?action=edit_message_init&token='.newToken().'&track_id='.$object->track_id.'">'.img_edit($langs->trans('Modify')).'</a>';
  181. }
  182. print '</td></tr>';
  183. print '<tr>';
  184. print '<td colspan="2">';
  185. if ($user->hasRight('ticket', 'manage') && $action == 'edit_message_init') {
  186. // MESSAGE
  187. $msg = GETPOSTISSET('message_initial') ? GETPOST('message_initial', 'restricthtml') : $object->message;
  188. include_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  189. $uselocalbrowser = true;
  190. $ckeditorenabledforticket = getDolGlobalString('FCKEDITOR_ENABLE_TICKET');
  191. $doleditor = new DolEditor('message_initial', $msg, '100%', 250, 'dolibarr_details', 'In', true, $uselocalbrowser, $ckeditorenabledforticket, ROWS_9, '95%');
  192. $doleditor->Create();
  193. } else {
  194. print '<div class="longmessagecut">';
  195. //print dol_escape_htmltag(dol_htmlwithnojs(dol_string_onlythesehtmltags(dol_htmlentitiesbr($object->message), 1, 1, 1, 0)), 1, 1, 'common', 0, 1);
  196. print dolPrintHTML($object->message);
  197. print '</div>';
  198. /*print '<div class="clear center">';
  199. print $langs->trans("More").'...';
  200. print '</div>';*/
  201. //print '<div>' . $object->message . '</div>';
  202. }
  203. if ($user->hasRight('ticket', 'manage') && $action == 'edit_message_init') {
  204. print '<div class="center">';
  205. print ' <input type="submit" class="button button-edit" value="'.$langs->trans('Modify').'">';
  206. print ' <input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
  207. print '</div>';
  208. }
  209. print '</td>';
  210. print '</tr>';
  211. print '</table>';
  212. print '</div>';
  213. if ($user->hasRight('ticket', 'manage') && $action == 'edit_message_init') {
  214. // MESSAGE
  215. print '</form>';
  216. }
  217. }
  218. /**
  219. * View html list of message for ticket
  220. *
  221. * @param boolean $show_private Show private messages
  222. * @param boolean $show_user Show user who make action
  223. * @param Ticket $object Object ticket
  224. * @return void
  225. */
  226. public function viewTicketMessages($show_private, $show_user, $object)
  227. {
  228. global $langs, $user;
  229. // Load logs in cache
  230. $ret = $this->dao->loadCacheMsgsTicket();
  231. if ($ret < 0) {
  232. dol_print_error($this->dao->db);
  233. }
  234. $action = GETPOST('action', 'aZ09');
  235. print '<div class="ticketpublicarea ticketlargemargin centpercent" style="padding-top: 0">';
  236. $this->viewTicketOriginalMessage($user, $action, $object);
  237. print '</div>';
  238. if (is_array($this->dao->cache_msgs_ticket) && count($this->dao->cache_msgs_ticket) > 0) {
  239. print '<div class="ticketpublicarea ticketlargemargin centpercent">';
  240. print '<div class="div-table-responsive-no-min">';
  241. print '<table class="border centpercent">';
  242. print '<tr class="liste_titre">';
  243. print '<td>';
  244. print $langs->trans('TicketMessagesList');
  245. print '</td>';
  246. if ($show_user) {
  247. print '<td>';
  248. print $langs->trans('User');
  249. print '</td>';
  250. }
  251. print '</tr>';
  252. foreach ($this->dao->cache_msgs_ticket as $id => $arraymsgs) {
  253. if (!$arraymsgs['private']
  254. || ($arraymsgs['private'] == "1" && $show_private)
  255. ) {
  256. //print '<tr>';
  257. print '<tr class="oddeven">';
  258. print '<td><strong>';
  259. print img_picto('', 'object_action', 'class="paddingright"').dol_print_date($arraymsgs['datep'], 'dayhour');
  260. print '<strong></td>';
  261. if ($show_user) {
  262. print '<td>';
  263. if ($arraymsgs['fk_user_author'] > 0) {
  264. $userstat = new User($this->db);
  265. $res = $userstat->fetch($arraymsgs['fk_user_author']);
  266. if ($res) {
  267. print $userstat->getNomUrl(0);
  268. }
  269. } elseif (isset($arraymsgs['fk_contact_author'])) {
  270. $contactstat = new Contact($this->db);
  271. $res = $contactstat->fetch(0, null, '', $arraymsgs['fk_contact_author']);
  272. if ($res) {
  273. print $contactstat->getNomUrl(0, 'nolink');
  274. } else {
  275. print $arraymsgs['fk_contact_author'];
  276. }
  277. } else {
  278. print $langs->trans('Customer');
  279. }
  280. print '</td>';
  281. }
  282. print '</td>';
  283. print '<tr class="oddeven">';
  284. print '<td colspan="2">';
  285. print $arraymsgs['message'];
  286. //attachment
  287. $documents = array();
  288. $sql = 'SELECT ecm.rowid as id, ecm.src_object_type, ecm.src_object_id';
  289. $sql .= ', ecm.filepath, ecm.filename, ecm.share';
  290. $sql .= ' FROM '.MAIN_DB_PREFIX.'ecm_files ecm';
  291. $sql .= " WHERE ecm.filepath = 'agenda/".$arraymsgs['id']."'";
  292. $sql .= ' ORDER BY ecm.position ASC';
  293. $resql = $this->db->query($sql);
  294. if ($resql) {
  295. if ($this->db->num_rows($resql)) {
  296. while ($obj = $this->db->fetch_object($resql)) {
  297. $documents[$obj->id] = $obj;
  298. }
  299. }
  300. }
  301. if (!empty($documents)) {
  302. $isshared = 0;
  303. $footer = '<div class="timeline-documents-container">';
  304. foreach ($documents as $doc) {
  305. if (!empty($doc->share)) {
  306. $isshared = 1;
  307. $footer .= '<span id="document_'.$doc->id.'" class="timeline-documents" ';
  308. $footer .= ' data-id="'.$doc->id.'" ';
  309. $footer .= ' data-path="'.$doc->filepath.'"';
  310. $footer .= ' data-filename="'.dol_escape_htmltag($doc->filename).'" ';
  311. $footer .= '>';
  312. $filePath = DOL_DATA_ROOT.'/'.$doc->filepath.'/'.$doc->filename;
  313. $mime = dol_mimetype($filePath);
  314. $thumb = $arraymsgs['id'].'/thumbs/'.substr($doc->filename, 0, strrpos($doc->filename, '.')).'_mini'.substr($doc->filename, strrpos($doc->filename, '.'));
  315. $doclink = DOL_URL_ROOT.'/document.php?hashp='.urlencode($doc->share);
  316. $mimeAttr = ' mime="'.$mime.'" ';
  317. $class = '';
  318. if (in_array($mime, array('image/png', 'image/jpeg', 'application/pdf'))) {
  319. $class .= ' documentpreview';
  320. }
  321. $footer .= '<a href="'.$doclink.'" class="btn-link '.$class.'" target="_blank" '.$mimeAttr.' >';
  322. $footer .= img_mime($filePath).' '.$doc->filename;
  323. $footer .= '</a>';
  324. $footer .= '</span>';
  325. }
  326. }
  327. $footer .= '</div>';
  328. if ($isshared == 1) {
  329. print '<br>';
  330. print '<br>';
  331. print $footer;
  332. }
  333. }
  334. print '</td>';
  335. print '</tr>';
  336. }
  337. }
  338. print '</table>';
  339. print '</div>';
  340. print '</div>';
  341. } else {
  342. print '<div class="ticketpublicarea ticketlargemargin centpercent">';
  343. print '<div class="info">'.$langs->trans('NoMsgForThisTicket').'</div>';
  344. print '</div>';
  345. }
  346. }
  347. /**
  348. * View list of message for ticket with timeline display
  349. *
  350. * @param boolean $show_private Show private messages
  351. * @param boolean $show_user Show user who make action
  352. * @param Ticket $object Object ticket
  353. * @return void
  354. */
  355. public function viewTicketTimelineMessages($show_private, $show_user, Ticket $object)
  356. {
  357. global $conf, $langs, $user;
  358. // Load logs in cache
  359. $ret = $object->loadCacheMsgsTicket();
  360. $action = GETPOST('action');
  361. if (is_array($object->cache_msgs_ticket) && count($object->cache_msgs_ticket) > 0) {
  362. print '<section id="cd-timeline">';
  363. foreach ($object->cache_msgs_ticket as $id => $arraymsgs) {
  364. if (!$arraymsgs['private']
  365. || ($arraymsgs['private'] == "1" && $show_private)
  366. ) {
  367. print '<div class="cd-timeline-block">';
  368. print '<div class="cd-timeline-img">';
  369. print '<img src="img/messages.png" alt="">';
  370. print '</div> <!-- cd-timeline-img -->';
  371. print '<div class="cd-timeline-content">';
  372. print $arraymsgs['message'];
  373. print '<span class="cd-date">';
  374. print dol_print_date($arraymsgs['datec'], 'dayhour');
  375. if ($show_user) {
  376. if ($arraymsgs['fk_user_action'] > 0) {
  377. $userstat = new User($this->db);
  378. $res = $userstat->fetch($arraymsgs['fk_user_action']);
  379. if ($res) {
  380. print '<br>';
  381. print $userstat->getNomUrl(1);
  382. }
  383. } else {
  384. print '<br>';
  385. print $langs->trans('Customer');
  386. }
  387. }
  388. print '</span>';
  389. print '</div> <!-- cd-timeline-content -->';
  390. print '</div> <!-- cd-timeline-block -->';
  391. }
  392. }
  393. print '</section>';
  394. } else {
  395. print '<div class="info">'.$langs->trans('NoMsgForThisTicket').'</div>';
  396. }
  397. }
  398. /**
  399. * Print html navbar with link to set ticket status
  400. *
  401. * @param Ticket $object Ticket sup
  402. * @return void
  403. */
  404. public function viewStatusActions(Ticket $object)
  405. {
  406. global $langs;
  407. print '<div class="div-table-responsive-no-min margintoponly navBarForStatus">';
  408. print '<div class="centpercent right">';
  409. // Exclude status which requires specific method
  410. $exclude_status = array(Ticket::STATUS_CLOSED, Ticket::STATUS_CANCELED);
  411. // Exclude actual status
  412. $exclude_status = array_merge($exclude_status, array(intval($object->fk_statut)));
  413. // Sort results to be similar to status object list
  414. //sort($exclude_status);
  415. foreach ($object->labelStatusShort as $status => $status_label) {
  416. if (!in_array($status, $exclude_status)) {
  417. print '<div class="inline-block center marginbottomonly">';
  418. if ($status == 1) {
  419. $urlforbutton = $_SERVER['PHP_SELF'].'?track_id='.$object->track_id.'&action=set_read&token='.newToken(); // To set as read, we use a dedicated action
  420. } else {
  421. $urlforbutton = $_SERVER['PHP_SELF'].'?track_id='.$object->track_id.'&action=confirm_set_status&token='.newToken().'&new_status='.((int) $status);
  422. }
  423. print '<a class="butAction butStatus marginbottomonly" href="'.$urlforbutton.'">';
  424. print $object->LibStatut($status, 3, 1).' ';
  425. //print img_picto($langs->trans($object->labelStatusShort[$status]), 'statut'.$status.'.png@ticket', '', false, 0, 0, '', 'valignmiddle').' ';
  426. print $langs->trans($object->labelStatusShort[$status]);
  427. print '</a>';
  428. print '</div>';
  429. }
  430. }
  431. print '</div>';
  432. print '</div>';
  433. print '<br>';
  434. }
  435. /**
  436. * Hook to add email element template
  437. *
  438. * @param array $parameters Parameters
  439. * @param Ticket $object Object for action
  440. * @param string $action Action string
  441. * @param HookManager $hookmanager Hookmanager object
  442. * @return int
  443. */
  444. public function emailElementlist($parameters, &$object, &$action, $hookmanager)
  445. {
  446. global $langs;
  447. $error = 0;
  448. if (in_array('admin', explode(':', $parameters['context']))) {
  449. $this->results = array('ticket_send' => $langs->trans('MailToSendTicketMessage'));
  450. }
  451. if (!$error) {
  452. return 0; // or return 1 to replace standard code
  453. } else {
  454. $this->errors[] = 'Error message';
  455. return -1;
  456. }
  457. }
  458. }