From 8bf9afb7ca2542f448ed7c23226fe8db00e3a65b Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 4 Oct 2019 19:41:35 +0200 Subject: [PATCH] FIX Attachement of linked files on ticket when sending a message --- htdocs/core/class/html.formmail.class.php | 2 +- htdocs/ticket/card.php | 4 +- htdocs/ticket/class/ticket.class.php | 182 +++++++++++++--------- 3 files changed, 112 insertions(+), 76 deletions(-) diff --git a/htdocs/core/class/html.formmail.class.php b/htdocs/core/class/html.formmail.class.php index b0cdcd23a96..99153f667e9 100644 --- a/htdocs/core/class/html.formmail.class.php +++ b/htdocs/core/class/html.formmail.class.php @@ -212,7 +212,7 @@ class FormMail extends Form /** * Remove a file from the list of attached files (stored in SECTION array) * - * @param string $keytodelete Key in file array (0, 1, 2, ...) + * @param string $keytodelete Key index in file array (0, 1, 2, ...) * @return void */ public function remove_attached_files($keytodelete) diff --git a/htdocs/ticket/card.php b/htdocs/ticket/card.php index eb47bc7044f..9ab82b0c3d8 100644 --- a/htdocs/ticket/card.php +++ b/htdocs/ticket/card.php @@ -371,7 +371,7 @@ if ($action == "assign_user" && GETPOST('btn_assign_user', 'aplha') && $user->ri $action = 'view'; } -if ($action == "add_message" && GETPOST('btn_add_message') && $user->rights->ticket->read) { +if ($action == 'add_message' && GETPOSTISSET('btn_add_message') && $user->rights->ticket->read) { $ret = $object->newMessage($user, $action, (GETPOST('private_message', 'alpha') == "on" ? 1 : 0)); if ($ret > 0) { @@ -1290,10 +1290,8 @@ if (empty($action) || $action == 'view' || $action == 'addlink' || $action == 'd //$formticket->param['socid']=$object->fk_soc; $formticket->param['returnurl']=$_SERVER["PHP_SELF"].'?track_id='.$object->track_id; - $formticket->withsubstit = 1; $formticket->substit = $substitutionarray; - $formticket->showMessageForm('100%'); print ''; } diff --git a/htdocs/ticket/class/ticket.class.php b/htdocs/ticket/class/ticket.class.php index 1ae0bb56aae..cdf2c14a500 100644 --- a/htdocs/ticket/class/ticket.class.php +++ b/htdocs/ticket/class/ticket.class.php @@ -1602,13 +1602,16 @@ class Ticket extends CommonObject } /** - * Add message into database + * Add message into database * - * @param User $user User that creates - * @param int $notrigger 0=launch triggers after, 1=disable triggers - * @return int <0 if KO, Id of created object if OK + * @param User $user User that creates + * @param int $notrigger 0=launch triggers after, 1=disable triggers + * @param array $filename_list List of files to attach (full path of filename on file system) + * @param array $mimetype_list List of MIME type of attached files + * @param array $mimefilename_list List of attached file name in message + * @return void */ - public function createTicketMessage($user, $notrigger = 0) + public function createTicketMessage($user, $notrigger = 0, $filename_list = array(), $mimetype_list = array(), $mimefilename_list = array()) { global $conf, $langs; $error = 0; @@ -1633,7 +1636,7 @@ class Ticket extends CommonObject $actioncomm->code = 'TICKET_MSG'; $actioncomm->socid = $this->socid; $actioncomm->label = $this->subject; - $actioncomm->note = $this->message; + $actioncomm->note_private = $this->message; $actioncomm->userassigned = array($user->id); $actioncomm->userownerid = $user->id; $actioncomm->datep = $now; @@ -1641,6 +1644,19 @@ class Ticket extends CommonObject $actioncomm->elementtype = 'ticket'; $actioncomm->fk_element = $this->id; + $attachedfiles = array(); + $attachedfiles['paths'] = $filename_list; + $attachedfiles['names'] = $mimefilename_list; + $attachedfiles['mimes'] = $mimetype_list; + if (is_array($attachedfiles) && count($attachedfiles)>0) { + $actioncomm->attachedfiles = $attachedfiles; + } + + if (! empty($mimefilename_list) && is_array($mimefilename_list)) + { + $actioncomm->note_private=dol_concatdesc($actioncomm->note_private, "\n".$langs->transnoentities("AttachedFiles").': '.join(';', $mimefilename_list)); + } + $actionid = $actioncomm->create($user); if ($actionid <= 0) { @@ -2389,10 +2405,11 @@ class Ticket extends CommonObject /** - * Copy files into ticket directory - * Used for files linked into messages + * Copy files defined into $_SESSION array into the ticket directory of attached files. + * Used for files linked into messages. + * Files may be renamed during copy to avoid overwriting existing files. * - * @return void + * @return array Array with final path/name/mime of files. */ public function copyFilesForTicket() { @@ -2422,23 +2439,42 @@ class Ticket extends CommonObject if (!dol_is_dir($destdir)) { dol_mkdir($destdir); } + + $listofpaths = array(); + $listofnames = array(); foreach ($filename as $i => $val) { - $res = dol_move($filepath[$i], $destdir . '/' . $filename[$i]); - if (image_format_supported($destdir . '/' . $filename[$i]) == 1) { + $destfile = $destdir . '/' . $filename[$i]; + // If destination file already exists, we add a suffix to avoid to overwrite + if (is_file($destfile)) + { + $now = dol_now(); + $destfile.='.'.dol_print_date($now, 'dayhourlog'); + } + + $res = dol_move($filepath[$i], $destfile, 0, 1); + + if (image_format_supported($destfile) == 1) { // Create small thumbs for image (Ratio is near 16/9) // Used on logon for example - $imgThumbSmall = vignette($destdir . '/' . $filename[$i], $maxwidthsmall, $maxheightsmall, '_small', 50, "thumbs"); + $imgThumbSmall = vignette($destfile, $maxwidthsmall, $maxheightsmall, '_small', 50, "thumbs"); // Create mini thumbs for image (Ratio is near 16/9) // Used on menu or for setup page for example - $imgThumbMini = vignette($destdir . '/' . $filename[$i], $maxwidthmini, $maxheightmini, '_mini', 50, "thumbs"); + $imgThumbMini = vignette($destfile, $maxwidthmini, $maxheightmini, '_mini', 50, "thumbs"); } + $formmail->remove_attached_files($i); + + // Fill array with new names + $listofpaths[$i] = $destfile; + $listofnames[$i] = basename($destfile); } + + return array('listofpaths'=>$listofpaths, 'listofnames'=>$listofnames, 'listofmimes'=>$mimetype); } /** - * Add new message on a ticket (private area) + * Add new message on a ticket (private area). Can also send it be email if GETPOST('send_email', 'int') is set. * * @param User $user User for action * @param string $action Action string @@ -2449,19 +2485,15 @@ class Ticket extends CommonObject { global $mysoc, $conf, $langs; - if (!class_exists('Contact')) { - include_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php'; - } - - $contactstatic = new Contact($this->db); - $object = new Ticket($this->db); - $error = 0; + $object = new Ticket($this->db); + $ret = $object->fetch('', '', GETPOST('track_id', 'alpha')); $object->socid = $object->fk_soc; $object->fetch_thirdparty(); + if ($ret < 0) { $error++; array_push($this->errors, $langs->trans("ErrorTicketIsNotValid")); @@ -2478,9 +2510,17 @@ class Ticket extends CommonObject $object->subject = GETPOST('subject', 'alphanohtml'); $object->message = GETPOST("message", "none"); $object->private = GETPOST("private_message", "alpha"); + $send_email = GETPOST('send_email', 'int'); - $id = $object->createTicketMessage($user); + // Copy attached files (saved into $_SESSION) as linked files to ticket. Return array with final name used. + $resarray = $object->copyFilesForTicket(); + + $listofpaths = $resarray['listofpaths']; + $listofnames = $resarray['listofnames']; + $listofmimes = $resarray['listofmimes']; + + $id = $object->createTicketMessage($user, 0, $listofpaths, $listofmimes, $listofnames); if ($id <= 0) { $error++; $this->errors = $object->error; @@ -2491,24 +2531,27 @@ class Ticket extends CommonObject if (!$error && $id > 0) { setEventMessages($langs->trans('TicketMessageSuccessfullyAdded'), null, 'mesgs'); + //var_dump($_SESSION); var_dump($listofpaths);exit; + /* - * Send email to linked contacts + * Send emails to internal users (linked contacts) */ if ($send_email > 0) { // Retrieve internal contact datas $internal_contacts = $object->getInfosTicketInternalContact(); + $sendto = array(); if (is_array($internal_contacts) && count($internal_contacts) > 0) { // altairis: set default subject $label_title = empty($conf->global->MAIN_APPLICATION_TITLE) ? $mysoc->name : $conf->global->MAIN_APPLICATION_TITLE; - $subject = GETPOST('subject') ? GETPOST('subject') : '[' . $label_title . '- ticket #' . $object->track_id . '] ' . $langs->trans('TicketNewMessage'); + $subject = GETPOST('subject', 'nohtml') ? GETPOST('subject', 'nohtml') : '[' . $label_title . '- ticket #' . $object->track_id . '] ' . $langs->trans('TicketNewMessage'); $message_intro = $langs->trans('TicketNotificationEmailBody', "#" . $object->id); $message_signature = GETPOST('mail_signature') ? GETPOST('mail_signature') : $conf->global->TICKET_MESSAGE_MAIL_SIGNATURE; $message = $langs->trans('TicketMessageMailIntroText'); $message .= "\n\n"; - $message .= GETPOST('message'); + $message .= GETPOST('message', 'restricthtml'); // Coordonnées client $message .= "\n\n"; @@ -2547,12 +2590,12 @@ class Ticket extends CommonObject // altairis: dont try to send email if no recipient if (!empty($sendto)) { - $this->sendTicketMessageByEmail($subject, $message, '', $sendto); + $this->sendTicketMessageByEmail($subject, $message, '', $sendto, $listofpaths, $listofmimes, $listofnames); } } /* - * Email for externals users if not private + * Send emails for externals users if not private (linked contacts) */ if (empty($object->private)) { // Retrieve email of all contacts (external) @@ -2599,47 +2642,42 @@ class Ticket extends CommonObject // If public interface is not enable, use link to internal page into mail $url_public_ticket = (!empty($conf->global->TICKET_ENABLE_PUBLIC_INTERFACE) ? - (!empty($conf->global->TICKET_URL_PUBLIC_INTERFACE) ? - $conf->global->TICKET_URL_PUBLIC_INTERFACE . '/view.php' : - dol_buildpath('/public/ticket/view.php', 2) - ) : - dol_buildpath('/ticket/card.php', 2) - ) . '?track_id=' . $object->track_id; - $message .= "\n" . $langs->trans('TicketNewEmailBodyInfosTrackUrlCustomer') . ' : ' . '' . $object->track_id . '' . "\n"; + (!empty($conf->global->TICKET_URL_PUBLIC_INTERFACE) ? $conf->global->TICKET_URL_PUBLIC_INTERFACE . '/view.php' : dol_buildpath('/public/ticket/view.php', 2)) : + dol_buildpath('/ticket/card.php', 2)) . '?track_id=' . $object->track_id; + $message .= "\n" . $langs->trans('TicketNewEmailBodyInfosTrackUrlCustomer') . ' : ' . '' . $object->track_id . '' . "\n"; - // Build final message - $message = $message_intro . $message; + // Build final message + $message = $message_intro . $message; - // Add signature - $message .= '
' . $message_signature; + // Add signature + $message .= '
' . $message_signature; - if (!empty($object->origin_email)) { - $sendto[] = $object->origin_email; - } + if (!empty($object->origin_email)) { + $sendto[] = $object->origin_email; + } - if ($object->fk_soc > 0 && ! in_array($object->origin_email, $sendto)) { - $object->socid = $object->fk_soc; - $object->fetch_thirdparty(); - if(!empty($object->thirdparty->email)) $sendto[] = $object->thirdparty->email; - } + if ($object->fk_soc > 0 && ! in_array($object->origin_email, $sendto)) { + $object->socid = $object->fk_soc; + $object->fetch_thirdparty(); + if(!empty($object->thirdparty->email)) $sendto[] = $object->thirdparty->email; + } - // altairis: Add global email address reciepient - if ($conf->global->TICKET_NOTIFICATION_ALSO_MAIN_ADDRESS && !in_array($conf->global->TICKET_NOTIFICATION_EMAIL_TO, $sendto)) { - if(!empty($conf->global->TICKET_NOTIFICATION_EMAIL_TO)) $sendto[] = $conf->global->TICKET_NOTIFICATION_EMAIL_TO; - } + // altairis: Add global email address reciepient + if ($conf->global->TICKET_NOTIFICATION_ALSO_MAIN_ADDRESS && !in_array($conf->global->TICKET_NOTIFICATION_EMAIL_TO, $sendto)) { + if(!empty($conf->global->TICKET_NOTIFICATION_EMAIL_TO)) $sendto[] = $conf->global->TICKET_NOTIFICATION_EMAIL_TO; + } - // altairis: dont try to send email when no recipient - if (!empty($sendto)) { - $this->sendTicketMessageByEmail($subject, $message, '', $sendto); - } + // altairis: dont try to send email when no recipient + if (!empty($sendto)) { + $this->sendTicketMessageByEmail($subject, $message, '', $sendto, $listofpaths, $listofmimes, $listofnames); + } } } } - $object->copyFilesForTicket(); - - // Set status to "answered" if not set yet, only for internal users - if ($object->fk_statut < 3 && !$user->societe_id) { + // Set status to "answered" if not set yet, but only if internal user + if ($object->fk_statut < 3 && ! $user->socid) + { $object->setStatut(3); } @@ -2658,13 +2696,16 @@ class Ticket extends CommonObject /** * Send ticket by email to linked contacts * - * @param string $subject Email subject - * @param string $message Email message - * @param int $send_internal_cc Receive a copy on internal email ($conf->global->TICKET_NOTIFICATION_EMAIL_FROM) - * @param array $array_receiver Array of receiver. exemple array('name' => 'John Doe', 'email' => 'john@doe.com', etc...) + * @param string $subject Email subject + * @param string $message Email message + * @param int $send_internal_cc Receive a copy on internal email ($conf->global->TICKET_NOTIFICATION_EMAIL_FROM) + * @param array $array_receiver Array of receiver. exemple array('name' => 'John Doe', 'email' => 'john@doe.com', etc...) + * @param array $filename_list List of files to attach (full path of filename on file system) + * @param array $mimetype_list List of MIME type of attached files + * @param array $mimefilename_list List of attached file name in message * @return void */ - public function sendTicketMessageByEmail($subject, $message, $send_internal_cc = 0, $array_receiver = array()) + public function sendTicketMessageByEmail($subject, $message, $send_internal_cc = 0, $array_receiver = array(), $filename_list = array(), $mimetype_list = array(), $mimefilename_list = array()) { global $conf, $langs; @@ -2690,15 +2731,12 @@ class Ticket extends CommonObject $from = $conf->global->TICKET_NOTIFICATION_EMAIL_FROM; if (is_array($array_receiver) && count($array_receiver) > 0) { - foreach ($array_receiver as $key => $receiver) { - // Create form object - include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php'; - $formmail = new FormMail($this->db); - - $attachedfiles = $formmail->get_attached_files(); - $filepath = $attachedfiles['paths']; - $filename = $attachedfiles['names']; - $mimetype = $attachedfiles['mimes']; + foreach ($array_receiver as $key => $receiver) + { + $deliveryreceipt = 0; + $filepath = $filename_list; + $filename = $mimefilename_list; + $mimetype = $mimetype_list; $message_to_send = dol_nl2br($message);