actions_ticket.class.php 13 KB

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