From 71fc59169ff8182f84d6cfce5814f027296c10f8 Mon Sep 17 00:00:00 2001 From: Maximilien Rozniecki Date: Wed, 1 Mar 2023 10:44:20 +0100 Subject: [PATCH] add a new function inside ticket.class to remove the closed contact from the receiver list of contact --- htdocs/langs/en_US/ticket.lang | 1 + htdocs/ticket/class/ticket.class.php | 37 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/htdocs/langs/en_US/ticket.lang b/htdocs/langs/en_US/ticket.lang index c0ca780ee06..d48b2c9beb2 100644 --- a/htdocs/langs/en_US/ticket.lang +++ b/htdocs/langs/en_US/ticket.lang @@ -223,6 +223,7 @@ TicketUpdated=Ticket updated SendMessageByEmail=Send message by email TicketNewMessage=New message ErrorMailRecipientIsEmptyForSendTicketMessage=Recipient is empty. No email send +WarningMailNotSendContactIsClosed=E-mail to %s refused for shipment as this contact is closed TicketGoIntoContactTab=Please go into "Contacts" tab to select them TicketMessageMailIntro=Message header TicketMessageMailIntroHelp=This text is added only at the beginning of the email and will not be saved. diff --git a/htdocs/ticket/class/ticket.class.php b/htdocs/ticket/class/ticket.class.php index 6f309b7656f..f1f84fb8a9b 100644 --- a/htdocs/ticket/class/ticket.class.php +++ b/htdocs/ticket/class/ticket.class.php @@ -2812,6 +2812,7 @@ class Ticket extends CommonObject $from = $conf->global->TICKET_NOTIFICATION_EMAIL_FROM; $is_sent = false; + $array_receiver = $this->removeClosedContact($array_receiver); if (is_array($array_receiver) && count($array_receiver) > 0) { foreach ($array_receiver as $key => $receiver) { $deliveryreceipt = 0; @@ -3028,6 +3029,42 @@ class Ticket extends CommonObject $return .= ''; return $return; } + + /** + * Remove the closed contact + * + * @param array $to Array of receiver. exemple array('john@doe.com' => 'John Doe ', etc...) + * @return array array of email => name + */ + public function removeClosedContact($to) + { + global $db, $langs; + $langs->load("ticket"); + + if (isset($this->id)) { + $sql = "SELECT IF(tc.source = 'external', sc.email, u.email)"; + $sql .= " FROM ".MAIN_DB_PREFIX."element_contact ec"; + $sql .= " INNER JOIN ".MAIN_DB_PREFIX."c_type_contact tc on (ec.element_id = ". $this->id ." AND ec.fk_c_type_contact = tc.rowid)"; + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople sc on (ec.fk_socpeople = sc.rowid AND tc.source = 'external')"; + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user u on (ec.fk_socpeople = u.rowid AND tc.source = 'internal')"; + $sql .= " WHERE IF(tc.source = 'external', sc.statut = 1, u.statut = 1)"; + $resql = $db->query($sql); + if ($resql) { + for ($i = 0; $db->num_rows($resql) > $i; $i++) { + $non_closed_contacts[] = $db->fetch_row($resql); + } + } + $to = array_filter($to, function($v, $k) use($non_closed_contacts, $langs) { + foreach($non_closed_contacts as $non_closed_contact) { + if ($k == $non_closed_contact[0]) + return true; + } + setEventMessages($langs->trans('WarningMailNotSendContactIsClosed', str_replace(array('>', '<'), '', $v)), null, 'warnings'); + return false; + }, ARRAY_FILTER_USE_BOTH); + } + return $to; + } }