FIX Attachement of linked files on ticket when sending a message

This commit is contained in:
Laurent Destailleur 2019-10-04 19:41:35 +02:00
parent 503973cec9
commit 8bf9afb7ca
3 changed files with 112 additions and 76 deletions

View File

@ -212,7 +212,7 @@ class FormMail extends Form
/** /**
* Remove a file from the list of attached files (stored in SECTION array) * 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 * @return void
*/ */
public function remove_attached_files($keytodelete) public function remove_attached_files($keytodelete)

View File

@ -371,7 +371,7 @@ if ($action == "assign_user" && GETPOST('btn_assign_user', 'aplha') && $user->ri
$action = 'view'; $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)); $ret = $object->newMessage($user, $action, (GETPOST('private_message', 'alpha') == "on" ? 1 : 0));
if ($ret > 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['socid']=$object->fk_soc;
$formticket->param['returnurl']=$_SERVER["PHP_SELF"].'?track_id='.$object->track_id; $formticket->param['returnurl']=$_SERVER["PHP_SELF"].'?track_id='.$object->track_id;
$formticket->withsubstit = 1; $formticket->withsubstit = 1;
$formticket->substit = $substitutionarray; $formticket->substit = $substitutionarray;
$formticket->showMessageForm('100%'); $formticket->showMessageForm('100%');
print '</div>'; print '</div>';
} }

View File

@ -1606,9 +1606,12 @@ class Ticket extends CommonObject
* *
* @param User $user User that creates * @param User $user User that creates
* @param int $notrigger 0=launch triggers after, 1=disable triggers * @param int $notrigger 0=launch triggers after, 1=disable triggers
* @return int <0 if KO, Id of created object if OK * @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; global $conf, $langs;
$error = 0; $error = 0;
@ -1633,7 +1636,7 @@ class Ticket extends CommonObject
$actioncomm->code = 'TICKET_MSG'; $actioncomm->code = 'TICKET_MSG';
$actioncomm->socid = $this->socid; $actioncomm->socid = $this->socid;
$actioncomm->label = $this->subject; $actioncomm->label = $this->subject;
$actioncomm->note = $this->message; $actioncomm->note_private = $this->message;
$actioncomm->userassigned = array($user->id); $actioncomm->userassigned = array($user->id);
$actioncomm->userownerid = $user->id; $actioncomm->userownerid = $user->id;
$actioncomm->datep = $now; $actioncomm->datep = $now;
@ -1641,6 +1644,19 @@ class Ticket extends CommonObject
$actioncomm->elementtype = 'ticket'; $actioncomm->elementtype = 'ticket';
$actioncomm->fk_element = $this->id; $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); $actionid = $actioncomm->create($user);
if ($actionid <= 0) if ($actionid <= 0)
{ {
@ -2389,10 +2405,11 @@ class Ticket extends CommonObject
/** /**
* Copy files into ticket directory * Copy files defined into $_SESSION array into the ticket directory of attached files.
* Used for files linked into messages * 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() public function copyFilesForTicket()
{ {
@ -2422,23 +2439,42 @@ class Ticket extends CommonObject
if (!dol_is_dir($destdir)) { if (!dol_is_dir($destdir)) {
dol_mkdir($destdir); dol_mkdir($destdir);
} }
$listofpaths = array();
$listofnames = array();
foreach ($filename as $i => $val) { foreach ($filename as $i => $val) {
$res = dol_move($filepath[$i], $destdir . '/' . $filename[$i]); $destfile = $destdir . '/' . $filename[$i];
if (image_format_supported($destdir . '/' . $filename[$i]) == 1) { // 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) // Create small thumbs for image (Ratio is near 16/9)
// Used on logon for example // 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) // Create mini thumbs for image (Ratio is near 16/9)
// Used on menu or for setup page for example // 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); $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 User $user User for action
* @param string $action Action string * @param string $action Action string
@ -2449,19 +2485,15 @@ class Ticket extends CommonObject
{ {
global $mysoc, $conf, $langs; 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; $error = 0;
$object = new Ticket($this->db);
$ret = $object->fetch('', '', GETPOST('track_id', 'alpha')); $ret = $object->fetch('', '', GETPOST('track_id', 'alpha'));
$object->socid = $object->fk_soc; $object->socid = $object->fk_soc;
$object->fetch_thirdparty(); $object->fetch_thirdparty();
if ($ret < 0) { if ($ret < 0) {
$error++; $error++;
array_push($this->errors, $langs->trans("ErrorTicketIsNotValid")); array_push($this->errors, $langs->trans("ErrorTicketIsNotValid"));
@ -2478,9 +2510,17 @@ class Ticket extends CommonObject
$object->subject = GETPOST('subject', 'alphanohtml'); $object->subject = GETPOST('subject', 'alphanohtml');
$object->message = GETPOST("message", "none"); $object->message = GETPOST("message", "none");
$object->private = GETPOST("private_message", "alpha"); $object->private = GETPOST("private_message", "alpha");
$send_email = GETPOST('send_email', 'int'); $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) { if ($id <= 0) {
$error++; $error++;
$this->errors = $object->error; $this->errors = $object->error;
@ -2491,24 +2531,27 @@ class Ticket extends CommonObject
if (!$error && $id > 0) { if (!$error && $id > 0) {
setEventMessages($langs->trans('TicketMessageSuccessfullyAdded'), null, 'mesgs'); 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) { if ($send_email > 0) {
// Retrieve internal contact datas // Retrieve internal contact datas
$internal_contacts = $object->getInfosTicketInternalContact(); $internal_contacts = $object->getInfosTicketInternalContact();
$sendto = array(); $sendto = array();
if (is_array($internal_contacts) && count($internal_contacts) > 0) { if (is_array($internal_contacts) && count($internal_contacts) > 0) {
// altairis: set default subject // altairis: set default subject
$label_title = empty($conf->global->MAIN_APPLICATION_TITLE) ? $mysoc->name : $conf->global->MAIN_APPLICATION_TITLE; $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_intro = $langs->trans('TicketNotificationEmailBody', "#" . $object->id);
$message_signature = GETPOST('mail_signature') ? GETPOST('mail_signature') : $conf->global->TICKET_MESSAGE_MAIL_SIGNATURE; $message_signature = GETPOST('mail_signature') ? GETPOST('mail_signature') : $conf->global->TICKET_MESSAGE_MAIL_SIGNATURE;
$message = $langs->trans('TicketMessageMailIntroText'); $message = $langs->trans('TicketMessageMailIntroText');
$message .= "\n\n"; $message .= "\n\n";
$message .= GETPOST('message'); $message .= GETPOST('message', 'restricthtml');
// Coordonnées client // Coordonnées client
$message .= "\n\n"; $message .= "\n\n";
@ -2547,12 +2590,12 @@ class Ticket extends CommonObject
// altairis: dont try to send email if no recipient // altairis: dont try to send email if no recipient
if (!empty($sendto)) { 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)) { if (empty($object->private)) {
// Retrieve email of all contacts (external) // Retrieve email of all contacts (external)
@ -2599,12 +2642,8 @@ class Ticket extends CommonObject
// If public interface is not enable, use link to internal page into mail // If public interface is not enable, use link to internal page into mail
$url_public_ticket = (!empty($conf->global->TICKET_ENABLE_PUBLIC_INTERFACE) ? $url_public_ticket = (!empty($conf->global->TICKET_ENABLE_PUBLIC_INTERFACE) ?
(!empty($conf->global->TICKET_URL_PUBLIC_INTERFACE) ? (!empty($conf->global->TICKET_URL_PUBLIC_INTERFACE) ? $conf->global->TICKET_URL_PUBLIC_INTERFACE . '/view.php' : dol_buildpath('/public/ticket/view.php', 2)) :
$conf->global->TICKET_URL_PUBLIC_INTERFACE . '/view.php' : dol_buildpath('/ticket/card.php', 2)) . '?track_id=' . $object->track_id;
dol_buildpath('/public/ticket/view.php', 2)
) :
dol_buildpath('/ticket/card.php', 2)
) . '?track_id=' . $object->track_id;
$message .= "\n" . $langs->trans('TicketNewEmailBodyInfosTrackUrlCustomer') . ' : ' . '<a href="' . $url_public_ticket . '">' . $object->track_id . '</a>' . "\n"; $message .= "\n" . $langs->trans('TicketNewEmailBodyInfosTrackUrlCustomer') . ' : ' . '<a href="' . $url_public_ticket . '">' . $object->track_id . '</a>' . "\n";
// Build final message // Build final message
@ -2630,16 +2669,15 @@ class Ticket extends CommonObject
// altairis: dont try to send email when no recipient // altairis: dont try to send email when no recipient
if (!empty($sendto)) { if (!empty($sendto)) {
$this->sendTicketMessageByEmail($subject, $message, '', $sendto); $this->sendTicketMessageByEmail($subject, $message, '', $sendto, $listofpaths, $listofmimes, $listofnames);
} }
} }
} }
} }
$object->copyFilesForTicket(); // Set status to "answered" if not set yet, but only if internal user
if ($object->fk_statut < 3 && ! $user->socid)
// Set status to "answered" if not set yet, only for internal users {
if ($object->fk_statut < 3 && !$user->societe_id) {
$object->setStatut(3); $object->setStatut(3);
} }
@ -2662,9 +2700,12 @@ class Ticket extends CommonObject
* @param string $message Email message * @param string $message Email message
* @param int $send_internal_cc Receive a copy on internal email ($conf->global->TICKET_NOTIFICATION_EMAIL_FROM) * @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 $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 * @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; global $conf, $langs;
@ -2690,15 +2731,12 @@ class Ticket extends CommonObject
$from = $conf->global->TICKET_NOTIFICATION_EMAIL_FROM; $from = $conf->global->TICKET_NOTIFICATION_EMAIL_FROM;
if (is_array($array_receiver) && count($array_receiver) > 0) { if (is_array($array_receiver) && count($array_receiver) > 0) {
foreach ($array_receiver as $key => $receiver) { foreach ($array_receiver as $key => $receiver)
// Create form object {
include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php'; $deliveryreceipt = 0;
$formmail = new FormMail($this->db); $filepath = $filename_list;
$filename = $mimefilename_list;
$attachedfiles = $formmail->get_attached_files(); $mimetype = $mimetype_list;
$filepath = $attachedfiles['paths'];
$filename = $attachedfiles['names'];
$mimetype = $attachedfiles['mimes'];
$message_to_send = dol_nl2br($message); $message_to_send = dol_nl2br($message);