create_ticket.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. /* Copyright (C) 2013-2016 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 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/public/ticket/create_ticket.php
  20. * \ingroup ticket
  21. * \brief Display public form to add new ticket
  22. */
  23. if (!defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1');
  24. if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1');
  25. if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1');
  26. if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1');
  27. if (!defined('NOLOGIN')) define("NOLOGIN", 1); // This means this output page does not require to be logged.
  28. if (!defined('NOCSRFCHECK')) define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
  29. if (!defined('NOIPCHECK')) define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
  30. require '../../main.inc.php';
  31. require_once DOL_DOCUMENT_ROOT.'/ticket/class/actions_ticket.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formticket.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/lib/ticket.lib.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  36. require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  38. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  39. // Load translation files required by the page
  40. $langs->loadLangs(array('companies', 'other', 'mails', 'ticket'));
  41. // Get parameters
  42. $id = GETPOST('id', 'int');
  43. $msg_id = GETPOST('msg_id', 'int');
  44. $action = GETPOST('action', 'alpha');
  45. $object = new Ticket($db);
  46. $extrafields = new ExtraFields($db);
  47. $extrafields->fetch_name_optionals_label($object->table_element);
  48. /*
  49. * Actions
  50. */
  51. // Add file in email form
  52. if (GETPOST('addfile', 'alpha') && !GETPOST('add', 'alpha')) {
  53. ////$res = $object->fetch('','',GETPOST('track_id'));
  54. ////if($res > 0)
  55. ////{
  56. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  57. // Set tmp directory TODO Use a dedicated directory for temp mails files
  58. $vardir = $conf->ticket->dir_output;
  59. $upload_dir_tmp = $vardir.'/temp/'.session_id();
  60. if (!dol_is_dir($upload_dir_tmp)) {
  61. dol_mkdir($upload_dir_tmp);
  62. }
  63. dol_add_file_process($upload_dir_tmp, 0, 0, 'addedfile', '', null, '', 0);
  64. $action = 'create_ticket';
  65. ////}
  66. }
  67. // Remove file
  68. if (GETPOST('removedfile', 'alpha') && !GETPOST('add', 'alpha')) {
  69. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  70. // Set tmp directory
  71. $vardir = $conf->ticket->dir_output.'/';
  72. $upload_dir_tmp = $vardir.'/temp/'.session_id();
  73. // TODO Delete only files that was uploaded from email form
  74. dol_remove_file_process($_POST['removedfile'], 0, 0);
  75. $action = 'create_ticket';
  76. }
  77. if ($action == 'create_ticket' && GETPOST('add', 'alpha')) {
  78. $error = 0;
  79. $origin_email = GETPOST('email', 'alpha');
  80. if (empty($origin_email)) {
  81. $error++;
  82. array_push($object->errors, $langs->trans("ErrorFieldRequired", $langs->transnoentities("Email")));
  83. $action = '';
  84. } else {
  85. // Search company saved with email
  86. $searched_companies = $object->searchSocidByEmail($origin_email, '0');
  87. // Chercher un contact existant avec cette adresse email
  88. // Le premier contact trouvé est utilisé pour déterminer le contact suivi
  89. $contacts = $object->searchContactByEmail($origin_email);
  90. // Option to require email exists to create ticket
  91. if (!empty($conf->global->TICKET_EMAIL_MUST_EXISTS) && !$contacts[0]->socid) {
  92. $error++;
  93. array_push($object->errors, $langs->trans("ErrorEmailMustExistToCreateTicket"));
  94. $action = '';
  95. }
  96. }
  97. if (!GETPOST("subject", "none")) {
  98. $error++;
  99. array_push($object->errors, $langs->trans("ErrorFieldRequired", $langs->transnoentities("Subject")));
  100. $action = '';
  101. } elseif (!GETPOST("message", "none")) {
  102. $error++;
  103. array_push($object->errors, $langs->trans("ErrorFieldRequired", $langs->transnoentities("message")));
  104. $action = '';
  105. }
  106. // Check email address
  107. if (!isValidEmail($origin_email)) {
  108. $error++;
  109. array_push($object->errors, $langs->trans("ErrorBadEmailAddress", $langs->transnoentities("email")));
  110. $action = '';
  111. }
  112. if (!$error) {
  113. $object->db->begin();
  114. $object->track_id = generate_random_id(16);
  115. $object->subject = GETPOST("subject", "none");
  116. $object->message = GETPOST("message", "none");
  117. $object->origin_email = $origin_email;
  118. $object->type_code = GETPOST("type_code", 'aZ09');
  119. $object->category_code = GETPOST("category_code", 'aZ09');
  120. $object->severity_code = GETPOST("severity_code", 'aZ09');
  121. if (is_array($searched_companies)) {
  122. $object->fk_soc = $searched_companies[0]->id;
  123. }
  124. if (is_array($contacts) and count($contacts) > 0) {
  125. $object->fk_soc = $contacts[0]->socid;
  126. $usertoassign = $contacts[0]->id;
  127. }
  128. $ret = $extrafields->setOptionalsFromPost(null, $object);
  129. // Generate new ref
  130. $object->ref = $object->getDefaultRef();
  131. if (!is_object($user)) {
  132. $user = new User($db);
  133. }
  134. $object->context['disableticketemail'] = 1; // Disable emails sent by ticket trigger when creation is done from this page, emails are already sent later
  135. $id = $object->create($user);
  136. if ($id <= 0) {
  137. $error++;
  138. $errors = ($object->error ? array($object->error) : $object->errors);
  139. array_push($object->errors, $object->error ? array($object->error) : $object->errors);
  140. $action = 'create_ticket';
  141. }
  142. if (!$error && $id > 0) {
  143. if ($usertoassign > 0) {
  144. $object->add_contact($usertoassign, "SUPPORTCLI", 'external', 0);
  145. }
  146. }
  147. if (!$error)
  148. {
  149. $object->db->commit();
  150. $action = "infos_success";
  151. } else {
  152. $object->db->rollback();
  153. setEventMessages($object->error, $object->errors, 'errors');
  154. $action = 'create_ticket';
  155. }
  156. if (!$error)
  157. {
  158. $res = $object->fetch($id);
  159. if ($res) {
  160. // Create form object
  161. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
  162. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  163. $formmail = new FormMail($db);
  164. // Init to avoid errors
  165. $filepath = array();
  166. $filename = array();
  167. $mimetype = array();
  168. $attachedfiles = $formmail->get_attached_files();
  169. $filepath = $attachedfiles['paths'];
  170. $filename = $attachedfiles['names'];
  171. $mimetype = $attachedfiles['mimes'];
  172. // Send email to customer
  173. $subject = '['.$conf->global->MAIN_INFO_SOCIETE_NOM.'] '.$langs->transnoentities('TicketNewEmailSubject', $object->ref, $object->track_id);
  174. $message .= ($conf->global->TICKET_MESSAGE_MAIL_NEW ? $conf->global->TICKET_MESSAGE_MAIL_NEW : $langs->transnoentities('TicketNewEmailBody'))."\n\n";
  175. $message .= $langs->transnoentities('TicketNewEmailBodyInfosTicket')."\n";
  176. $url_public_ticket = ($conf->global->TICKET_URL_PUBLIC_INTERFACE ? $conf->global->TICKET_URL_PUBLIC_INTERFACE.'/' : dol_buildpath('/public/ticket/view.php', 2)).'?track_id='.$object->track_id;
  177. $infos_new_ticket = $langs->transnoentities('TicketNewEmailBodyInfosTrackId', '<a href="'.$url_public_ticket.'">'.$object->track_id.'</a>')."\n";
  178. $infos_new_ticket .= $langs->transnoentities('TicketNewEmailBodyInfosTrackUrl')."\n\n";
  179. $message .= dol_nl2br($infos_new_ticket);
  180. $message .= $conf->global->TICKET_MESSAGE_MAIL_SIGNATURE ? $conf->global->TICKET_MESSAGE_MAIL_SIGNATURE : $langs->transnoentities('TicketMessageMailSignatureText');
  181. $sendto = GETPOST('email', 'alpha');
  182. $from = $conf->global->MAIN_INFO_SOCIETE_NOM.'<'.$conf->global->TICKET_NOTIFICATION_EMAIL_FROM.'>';
  183. $replyto = $from;
  184. $sendtocc = '';
  185. $deliveryreceipt = 0;
  186. $message = dol_nl2br($message);
  187. if (!empty($conf->global->TICKET_DISABLE_MAIL_AUTOCOPY_TO)) {
  188. $old_MAIN_MAIL_AUTOCOPY_TO = $conf->global->MAIN_MAIL_AUTOCOPY_TO;
  189. $conf->global->MAIN_MAIL_AUTOCOPY_TO = '';
  190. }
  191. include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  192. $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, '', $deliveryreceipt, -1);
  193. if ($mailfile->error || $mailfile->errors) {
  194. setEventMessages($mailfile->error, $mailfile->errors, 'errors');
  195. } else {
  196. $result = $mailfile->sendfile();
  197. }
  198. if (!empty($conf->global->TICKET_DISABLE_MAIL_AUTOCOPY_TO)) {
  199. $conf->global->MAIN_MAIL_AUTOCOPY_TO = $old_MAIN_MAIL_AUTOCOPY_TO;
  200. }
  201. // Send email to TICKET_NOTIFICATION_EMAIL_TO
  202. $sendto = $conf->global->TICKET_NOTIFICATION_EMAIL_TO;
  203. if ($sendto)
  204. {
  205. $subject = '['.$conf->global->MAIN_INFO_SOCIETE_NOM.'] '.$langs->transnoentities('TicketNewEmailSubjectAdmin', $object->ref, $object->track_id);
  206. $message_admin = $langs->transnoentities('TicketNewEmailBodyAdmin', $object->track_id)."\n\n";
  207. $message_admin .= '<ul><li>'.$langs->trans('Title').' : '.$object->subject.'</li>';
  208. $message_admin .= '<li>'.$langs->trans('Type').' : '.$object->type_label.'</li>';
  209. $message_admin .= '<li>'.$langs->trans('Category').' : '.$object->category_label.'</li>';
  210. $message_admin .= '<li>'.$langs->trans('Severity').' : '.$object->severity_label.'</li>';
  211. $message_admin .= '<li>'.$langs->trans('From').' : '.$object->origin_email.'</li>';
  212. if (is_array($extrafields->attributes[$object->table_element]['label']))
  213. {
  214. foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val)
  215. {
  216. $enabled = 1;
  217. if ($qualified && isset($extrafields->attributes[$object->table_element]['list'][$key]))
  218. {
  219. $enabled = dol_eval($extrafields->attributes[$object->table_element]['list'][$key], 1);
  220. }
  221. $perms = 1;
  222. if ($perms && isset($extrafields->attributes[$object->table_element]['perms'][$key]))
  223. {
  224. $perms = dol_eval($extrafields->attributes[$object->table_element]['perms'][$key], 1);
  225. }
  226. $qualified = true;
  227. if (empty($enabled) || $enabled == 2) $qualified = false;
  228. if (empty($perms)) $qualified = false;
  229. if ($qualified) $message_admin .= '<li>'.$langs->trans($key).' : '.$value.'</li>';
  230. }
  231. }
  232. $message_admin .= '</ul>';
  233. $message_admin .= '<p>'.$langs->trans('Message').' : <br>'.$object->message.'</p>';
  234. $message_admin .= '<p><a href="'.dol_buildpath('/ticket/card.php', 2).'?track_id='.$object->track_id.'">'.$langs->trans('SeeThisTicketIntomanagementInterface').'</a></p>';
  235. $from = $conf->global->MAIN_INFO_SOCIETE_NOM.'<'.$conf->global->TICKET_NOTIFICATION_EMAIL_FROM.'>';
  236. $replyto = $from;
  237. $message_admin = dol_nl2br($message_admin);
  238. if (!empty($conf->global->TICKET_DISABLE_MAIL_AUTOCOPY_TO)) {
  239. $old_MAIN_MAIL_AUTOCOPY_TO = $conf->global->MAIN_MAIL_AUTOCOPY_TO;
  240. $conf->global->MAIN_MAIL_AUTOCOPY_TO = '';
  241. }
  242. include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  243. $mailfile = new CMailFile($subject, $sendto, $from, $message_admin, $filepath, $mimetype, $filename, $sendtocc, '', $deliveryreceipt, -1);
  244. if ($mailfile->error || $mailfile->errors) {
  245. setEventMessages($mailfile->error, $mailfile->errors, 'errors');
  246. } else {
  247. $result = $mailfile->sendfile();
  248. }
  249. if (!empty($conf->global->TICKET_DISABLE_MAIL_AUTOCOPY_TO)) {
  250. $conf->global->MAIN_MAIL_AUTOCOPY_TO = $old_MAIN_MAIL_AUTOCOPY_TO;
  251. }
  252. }
  253. }
  254. // Copy files into ticket directory
  255. $destdir = $conf->ticket->dir_output.'/'.$object->ref;
  256. if (!dol_is_dir($destdir)) {
  257. dol_mkdir($destdir);
  258. }
  259. foreach ($filename as $i => $val) {
  260. dol_move($filepath[$i], $destdir.'/'.$filename[$i], 0, 1);
  261. $formmail->remove_attached_files($i);
  262. }
  263. //setEventMessages($langs->trans('YourTicketSuccessfullySaved'), null, 'mesgs');
  264. // Make a redirect to avoid to have ticket submitted twice if we make back
  265. setEventMessages($langs->trans('MesgInfosPublicTicketCreatedWithTrackId', '<strong>'.$object->track_id.'</strong>', '<strong>'.$object->ref.'</strong>'), null, 'warnings');
  266. setEventMessages($langs->trans('PleaseRememberThisId'), null, 'warnings');
  267. header("Location: index.php");
  268. exit;
  269. }
  270. } else {
  271. setEventMessages($object->error, $object->errors, 'errors');
  272. }
  273. }
  274. /*
  275. * View
  276. */
  277. $form = new Form($db);
  278. $formticket = new FormTicket($db);
  279. if (!$conf->global->TICKET_ENABLE_PUBLIC_INTERFACE)
  280. {
  281. print '<div class="error">'.$langs->trans('TicketPublicInterfaceForbidden').'</div>';
  282. $db->close();
  283. exit();
  284. }
  285. $arrayofjs = array();
  286. $arrayofcss = array('/opensurvey/css/style.css', '/ticket/css/styles.css.php');
  287. llxHeaderTicket($langs->trans("CreateTicket"), "", 0, 0, $arrayofjs, $arrayofcss);
  288. print '<div style="width:60%; margin: 0 auto;" class="ticketpublicarea">';
  289. if ($action != "infos_success") {
  290. $formticket->withfromsocid = isset($socid) ? $socid : $user->socid;
  291. $formticket->withtitletopic = 1;
  292. $formticket->withcompany = 0;
  293. $formticket->withusercreate = 1;
  294. $formticket->fk_user_create = 0;
  295. $formticket->withemail = 1;
  296. $formticket->ispublic = 1;
  297. $formticket->withfile = 2;
  298. $formticket->action = 'create_ticket';
  299. $formticket->param = array('returnurl' => $_SERVER['PHP_SELF'].($conf->entity > 1 ? '?entity='.$conf->entity : ''));
  300. print load_fiche_titre($langs->trans('NewTicket'), '', '', 0, 0, 'marginleftonly');
  301. if (empty($conf->global->TICKET_NOTIFICATION_EMAIL_FROM)) {
  302. $langs->load("errors");
  303. print '<div class="error">';
  304. print $langs->trans("ErrorFieldRequired", $langs->transnoentities("TicketEmailNotificationFrom")).'<br>';
  305. print $langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentities("Ticket"));
  306. print '<div>';
  307. }
  308. else {
  309. print '<div class="info marginleftonly marginrightonly">'.$langs->trans('TicketPublicInfoCreateTicket').'</div>';
  310. $formticket->showForm();
  311. }
  312. }
  313. print '</div>';
  314. // End of page
  315. htmlPrintOnlinePaymentFooter($mysoc, $langs, 1, $suffix, $object);
  316. llxFooter('', 'public');
  317. $db->close();