dolibarr  13.0.2
create_ticket.php
Go to the documentation of this file.
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 
25 if (!defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1');
26 if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1');
27 if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1');
28 if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1');
29 if (!defined('NOLOGIN')) define("NOLOGIN", 1); // This means this output page does not require to be logged.
30 if (!defined('NOCSRFCHECK')) define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
31 if (!defined('NOIPCHECK')) define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
32 if (!defined('NOBROWSERNOTIF')) define('NOBROWSERNOTIF', '1');
33 
34 
35 require '../../main.inc.php';
36 require_once DOL_DOCUMENT_ROOT.'/ticket/class/actions_ticket.class.php';
37 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formticket.class.php';
38 require_once DOL_DOCUMENT_ROOT.'/core/lib/ticket.lib.php';
39 require_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
40 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
41 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
42 require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
43 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
44 
45 // Load translation files required by the page
46 $langs->loadLangs(array('companies', 'other', 'mails', 'ticket'));
47 
48 // Get parameters
49 $id = GETPOST('id', 'int');
50 $msg_id = GETPOST('msg_id', 'int');
51 
52 $action = GETPOST('action', 'aZ09');
53 
54 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
55 $hookmanager->initHooks(array('publicnewticketcard', 'globalcard'));
56 
57 $object = new Ticket($db);
58 $extrafields = new ExtraFields($db);
59 
60 $extrafields->fetch_name_optionals_label($object->table_element);
61 
62 
63 /*
64  * Actions
65  */
66 $parameters = array(
67  'id' => $id,
68 );
69 // Note that $action and $object may have been modified by some hooks
70 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action);
71 if ($reshook < 0) {
72  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
73 }
74 // Add file in email form
75 if (empty($reshook) && GETPOST('addfile', 'alpha') && !GETPOST('add', 'alpha')) {
79  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
80 
81  // Set tmp directory TODO Use a dedicated directory for temp mails files
82  $vardir = $conf->ticket->dir_output;
83  $upload_dir_tmp = $vardir.'/temp/'.session_id();
84  if (!dol_is_dir($upload_dir_tmp)) {
85  dol_mkdir($upload_dir_tmp);
86  }
87 
88  dol_add_file_process($upload_dir_tmp, 0, 0, 'addedfile', '', null, '', 0);
89  $action = 'create_ticket';
91 }
92 
93 // Remove file
94 if (empty($reshook) && GETPOST('removedfile', 'alpha') && !GETPOST('add', 'alpha')) {
95  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
96 
97  // Set tmp directory
98  $vardir = $conf->ticket->dir_output.'/';
99  $upload_dir_tmp = $vardir.'/temp/'.session_id();
100 
101  // TODO Delete only files that was uploaded from email form
102  dol_remove_file_process($_POST['removedfile'], 0, 0);
103  $action = 'create_ticket';
104 }
105 
106 if (empty($reshook) && $action == 'create_ticket' && GETPOST('add', 'alpha')) {
107  $error = 0;
108  $origin_email = GETPOST('email', 'alpha');
109  if (empty($origin_email)) {
110  $error++;
111  array_push($object->errors, $langs->trans("ErrorFieldRequired", $langs->transnoentities("Email")));
112  $action = '';
113  } else {
114  // Search company saved with email
115  $searched_companies = $object->searchSocidByEmail($origin_email, '0');
116 
117  // Chercher un contact existant avec cette adresse email
118  // Le premier contact trouvé est utilisé pour déterminer le contact suivi
119  $contacts = $object->searchContactByEmail($origin_email);
120 
121  // Option to require email exists to create ticket
122  if (!empty($conf->global->TICKET_EMAIL_MUST_EXISTS) && !$contacts[0]->socid) {
123  $error++;
124  array_push($object->errors, $langs->trans("ErrorEmailMustExistToCreateTicket"));
125  $action = '';
126  }
127  }
128 
129  if (!GETPOST("subject", "restricthtml")) {
130  $error++;
131  array_push($object->errors, $langs->trans("ErrorFieldRequired", $langs->transnoentities("Subject")));
132  $action = '';
133  } elseif (!GETPOST("message", "restricthtml")) {
134  $error++;
135  array_push($object->errors, $langs->trans("ErrorFieldRequired", $langs->transnoentities("message")));
136  $action = '';
137  }
138 
139  // Check email address
140  if (!isValidEmail($origin_email)) {
141  $error++;
142  array_push($object->errors, $langs->trans("ErrorBadEmailAddress", $langs->transnoentities("email")));
143  $action = '';
144  }
145 
146  if (!$error) {
147  $object->db->begin();
148 
149  $object->track_id = generate_random_id(16);
150 
151  $object->subject = GETPOST("subject", "restricthtml");
152  $object->message = GETPOST("message", "restricthtml");
153  $object->origin_email = $origin_email;
154 
155  $object->type_code = GETPOST("type_code", 'aZ09');
156  $object->category_code = GETPOST("category_code", 'aZ09');
157  $object->severity_code = GETPOST("severity_code", 'aZ09');
158  if (is_array($searched_companies)) {
159  $object->fk_soc = $searched_companies[0]->id;
160  }
161 
162  if (is_array($contacts) and count($contacts) > 0) {
163  $object->fk_soc = $contacts[0]->socid;
164  $usertoassign = $contacts[0]->id;
165  }
166 
167  $ret = $extrafields->setOptionalsFromPost(null, $object);
168 
169  // Generate new ref
170  $object->ref = $object->getDefaultRef();
171  if (!is_object($user)) {
172  $user = new User($db);
173  }
174 
175  $object->context['disableticketemail'] = 1; // Disable emails sent by ticket trigger when creation is done from this page, emails are already sent later
176 
177  $id = $object->create($user);
178  if ($id <= 0) {
179  $error++;
180  $errors = ($object->error ? array($object->error) : $object->errors);
181  array_push($object->errors, $object->error ? array($object->error) : $object->errors);
182  $action = 'create_ticket';
183  }
184 
185  if (!$error && $id > 0) {
186  if ($usertoassign > 0) {
187  $object->add_contact($usertoassign, "SUPPORTCLI", 'external', 0);
188  }
189  }
190 
191  if (!$error) {
192  $object->db->commit();
193  $action = "infos_success";
194  } else {
195  $object->db->rollback();
196  setEventMessages($object->error, $object->errors, 'errors');
197  $action = 'create_ticket';
198  }
199 
200  if (!$error) {
201  $res = $object->fetch($id);
202  if ($res) {
203  // Create form object
204  include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
205  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
206  $formmail = new FormMail($db);
207 
208  // Init to avoid errors
209  $filepath = array();
210  $filename = array();
211  $mimetype = array();
212 
213  $attachedfiles = $formmail->get_attached_files();
214  $filepath = $attachedfiles['paths'];
215  $filename = $attachedfiles['names'];
216  $mimetype = $attachedfiles['mimes'];
217 
218  // Send email to customer
219 
220  $subject = '['.$conf->global->MAIN_INFO_SOCIETE_NOM.'] '.$langs->transnoentities('TicketNewEmailSubject', $object->ref, $object->track_id);
221  $message = ($conf->global->TICKET_MESSAGE_MAIL_NEW ? $conf->global->TICKET_MESSAGE_MAIL_NEW : $langs->transnoentities('TicketNewEmailBody')).'<br><br>';
222  $message .= $langs->transnoentities('TicketNewEmailBodyInfosTicket').'<br>';
223 
224  $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;
225  $infos_new_ticket = $langs->transnoentities('TicketNewEmailBodyInfosTrackId', '<a href="'.$url_public_ticket.'" rel="nofollow noopener">'.$object->track_id.'</a>').'<br>';
226  $infos_new_ticket .= $langs->transnoentities('TicketNewEmailBodyInfosTrackUrl').'<br><br>';
227 
228  $message .= $infos_new_ticket;
229  $message .= $conf->global->TICKET_MESSAGE_MAIL_SIGNATURE ? $conf->global->TICKET_MESSAGE_MAIL_SIGNATURE : $langs->transnoentities('TicketMessageMailSignatureText');
230 
231  $sendto = GETPOST('email', 'alpha');
232 
233  $from = $conf->global->MAIN_INFO_SOCIETE_NOM.'<'.$conf->global->TICKET_NOTIFICATION_EMAIL_FROM.'>';
234  $replyto = $from;
235  $sendtocc = '';
236  $deliveryreceipt = 0;
237 
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, $filepath, $mimetype, $filename, $sendtocc, '', $deliveryreceipt, -1, '', '', 'tic'.$object->id, '', 'ticket');
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  // Send email to TICKET_NOTIFICATION_EMAIL_TO
254  $sendto = $conf->global->TICKET_NOTIFICATION_EMAIL_TO;
255  if ($sendto) {
256  $subject = '['.$conf->global->MAIN_INFO_SOCIETE_NOM.'] '.$langs->transnoentities('TicketNewEmailSubjectAdmin', $object->ref, $object->track_id);
257  $message_admin = $langs->transnoentities('TicketNewEmailBodyAdmin', $object->track_id).'<br><br>';
258  $message_admin .= '<ul><li>'.$langs->trans('Title').' : '.$object->subject.'</li>';
259  $message_admin .= '<li>'.$langs->trans('Type').' : '.$object->type_label.'</li>';
260  $message_admin .= '<li>'.$langs->trans('Category').' : '.$object->category_label.'</li>';
261  $message_admin .= '<li>'.$langs->trans('Severity').' : '.$object->severity_label.'</li>';
262  $message_admin .= '<li>'.$langs->trans('From').' : '.$object->origin_email.'</li>';
263  // Extrafields
264  $extrafields->fetch_name_optionals_label($object->table_element);
265  if (is_array($object->array_options) && count($object->array_options) > 0) {
266  foreach ($object->array_options as $key => $value) {
267  $key = substr($key, 8); // remove "options_"
268  $message_admin .= '<li>'.$langs->trans($extrafields->attributes[$object->element]['label'][$key]).' : '.$extrafields->showOutputField($key, $value).'</li>';
269  }
270  }
271  $message_admin .= '</ul>';
272 
273  $message_admin .= '</ul>';
274  $message_admin .= '<p>'.$langs->trans('Message').' : <br>'.$object->message.'</p>';
275  $message_admin .= '<p><a href="'.dol_buildpath('/ticket/card.php', 2).'?track_id='.$object->track_id.'" rel="nofollow noopener">'.$langs->trans('SeeThisTicketIntomanagementInterface').'</a></p>';
276 
277  $from = $conf->global->MAIN_INFO_SOCIETE_NOM.' <'.$conf->global->TICKET_NOTIFICATION_EMAIL_FROM.'>';
278  $replyto = $from;
279 
280  if (!empty($conf->global->TICKET_DISABLE_MAIL_AUTOCOPY_TO)) {
281  $old_MAIN_MAIL_AUTOCOPY_TO = $conf->global->MAIN_MAIL_AUTOCOPY_TO;
282  $conf->global->MAIN_MAIL_AUTOCOPY_TO = '';
283  }
284  include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
285  $mailfile = new CMailFile($subject, $sendto, $from, $message_admin, $filepath, $mimetype, $filename, $sendtocc, '', $deliveryreceipt, -1, '', '', 'tic'.$object->id, '', 'ticket');
286  if ($mailfile->error || $mailfile->errors) {
287  setEventMessages($mailfile->error, $mailfile->errors, 'errors');
288  } else {
289  $result = $mailfile->sendfile();
290  }
291  if (!empty($conf->global->TICKET_DISABLE_MAIL_AUTOCOPY_TO)) {
292  $conf->global->MAIN_MAIL_AUTOCOPY_TO = $old_MAIN_MAIL_AUTOCOPY_TO;
293  }
294  }
295  }
296 
297  // Copy files into ticket directory
298  $destdir = $conf->ticket->dir_output.'/'.$object->ref;
299  if (!dol_is_dir($destdir)) {
300  dol_mkdir($destdir);
301  }
302  foreach ($filename as $i => $val) {
303  dol_move($filepath[$i], $destdir.'/'.$filename[$i], 0, 1);
304  $formmail->remove_attached_files($i);
305  }
306 
307  //setEventMessages($langs->trans('YourTicketSuccessfullySaved'), null, 'mesgs');
308 
309  // Make a redirect to avoid to have ticket submitted twice if we make back
310  $messagetoshow = $langs->trans('MesgInfosPublicTicketCreatedWithTrackId', '{s1}', '{s2}');
311  $messagetoshow = str_replace(array('{s1}', '{s2}'), array('<strong>'.$object->track_id.'</strong>', '<strong>'.$object->ref.'</strong>'), $messagetoshow);
312  setEventMessages($messagetoshow, null, 'warnings');
313  setEventMessages($langs->trans('PleaseRememberThisId'), null, 'warnings');
314  header("Location: index.php");
315  exit;
316  }
317  } else {
318  setEventMessages($object->error, $object->errors, 'errors');
319  }
320 }
321 
322 
323 
324 /*
325  * View
326  */
327 
328 $form = new Form($db);
329 $formticket = new FormTicket($db);
330 
331 if (!$conf->global->TICKET_ENABLE_PUBLIC_INTERFACE) {
332  print '<div class="error">'.$langs->trans('TicketPublicInterfaceForbidden').'</div>';
333  $db->close();
334  exit();
335 }
336 
337 $arrayofjs = array();
338 $arrayofcss = array('/opensurvey/css/style.css', '/ticket/css/styles.css.php');
339 
340 llxHeaderTicket($langs->trans("CreateTicket"), "", 0, 0, $arrayofjs, $arrayofcss);
341 
342 
343 print '<div class="ticketpublicarea">';
344 
345 if ($action != "infos_success") {
346  $formticket->withfromsocid = isset($socid) ? $socid : $user->socid;
347  $formticket->withtitletopic = 1;
348  $formticket->withcompany = 0;
349  $formticket->withusercreate = 1;
350  $formticket->fk_user_create = 0;
351  $formticket->withemail = 1;
352  $formticket->ispublic = 1;
353  $formticket->withfile = 2;
354  $formticket->action = 'create_ticket';
355 
356  $formticket->param = array('returnurl' => $_SERVER['PHP_SELF'].($conf->entity > 1 ? '?entity='.$conf->entity : ''));
357 
358  print load_fiche_titre($langs->trans('NewTicket'), '', '', 0, 0, 'marginleftonly');
359 
360  if (empty($conf->global->TICKET_NOTIFICATION_EMAIL_FROM)) {
361  $langs->load("errors");
362  print '<div class="error">';
363  print $langs->trans("ErrorFieldRequired", $langs->transnoentities("TicketEmailNotificationFrom")).'<br>';
364  print $langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentities("Ticket"));
365  print '<div>';
366  } else {
367  print '<div class="info marginleftonly marginrightonly">'.$langs->trans('TicketPublicInfoCreateTicket').'</div>';
368  $formticket->showForm();
369  }
370 }
371 
372 print '</div>';
373 
374 // End of page
375 htmlPrintOnlinePaymentFooter($mysoc, $langs, 1, $suffix, $object);
376 
377 llxFooter('', 'public');
378 
379 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Classe permettant la generation du formulaire html d&#39;envoi de mail unitaire Usage: $formail = new For...
Class to manage Dolibarr users.
Definition: user.class.php:44
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:432
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Class to manage ticket.
dol_move($srcfile, $destfile, $newmask=0, $overwriteifexists=1, $testvirus=0, $indexdatabase=1)
Move a file into another name.
Definition: files.lib.php:817
Class to manage standard extra fields.
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
llxHeaderTicket($title, $head="", $disablejs=0, $disablehead=0, $arrayofjs= '', $arrayofcss= '')
Show header for public pages.
Definition: ticket.lib.php:202
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to,$css,$trackid,$moreinheader,$sendcontext,$replyto); $mailfile-&gt;sendfile();.
generate_random_id($car=16)
Generate a random id.
Definition: ticket.lib.php:180
dol_remove_file_process($filenb, $donotupdatesession=0, $donotdeletefile=1, $trackid= '')
Remove an uploaded file (for example after submitting a new file a mail form).
Definition: files.lib.php:1689
print $_SERVER["PHP_SELF"]
Edit parameters.
print
Draft customers invoices.
Definition: index.php:89
dol_add_file_process($upload_dir, $allowoverwrite=0, $donotupdatesession=0, $varfiles= 'addedfile', $savingdocmask= '', $link=null, $trackid= '', $generatethumbs=1, $object=null)
Get and save an upload file (for example after submitting a new file a mail form).
Definition: files.lib.php:1528
llxFooter()
Empty footer.
Definition: wrapper.php:59
isValidEmail($address, $acceptsupervisorkey=0)
Return true if email syntax is ok.
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)