From b8ba07b02a9e97e415efa49cc2e986dc36e771f0 Mon Sep 17 00:00:00 2001 From: "o.smith" Date: Wed, 6 Nov 2024 11:24:01 +0000 Subject: [PATCH] Fix #10570 - Email Signature Compose View Issues --- .../EmailsControllerActionGetFromFields.php | 12 ++--- modules/Emails/EmailsDataAddressCollector.php | 15 ++++--- .../include/ComposeView/EmailsComposeView.js | 45 +++++++++++++++---- 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/modules/Emails/EmailsControllerActionGetFromFields.php b/modules/Emails/EmailsControllerActionGetFromFields.php index 442a13d49f8..cf2a35465ba 100644 --- a/modules/Emails/EmailsControllerActionGetFromFields.php +++ b/modules/Emails/EmailsControllerActionGetFromFields.php @@ -89,10 +89,10 @@ public function handleActionGetFromFields(Email $email, InboundEmail $ie) $ie->email = $email; $ieAccounts = $ie->retrieveAllByGroupIdWithGroupAccounts($this->currentUser->id); $accountSignatures = $this->currentUser->getPreference('account_signatures', 'Emails'); - $showFolders = sugar_unserialize(base64_decode($this->currentUser->getPreference('showFolders', 'Emails'))); + $showFolders = sugar_unserialize(base64_decode($this->currentUser->getPreference('showFolders', 'Emails'))) ?: []; $emailSignatures = $this->getEmailSignatures($accountSignatures); $defaultEmailSignature = $this->getDefaultSignatures(); - $prependSignature = $this->currentUser->getPreference('signature_prepend'); + $prependSignature = $this->currentUser->getPreference('signature_prepend') ?? false; $dataAddresses = $this->collector->collectDataAddressesFromIEAccounts( $ieAccounts, $showFolders, @@ -103,7 +103,7 @@ public function handleActionGetFromFields(Email $email, InboundEmail $ie) $dataAddresses = $dataAddresses ?? []; - $this->addOutboundEmailAccounts($dataAddresses); + $this->addOutboundEmailAccounts($dataAddresses, $prependSignature, $defaultEmailSignature); $dataEncoded = json_encode(array('data' => $dataAddresses), JSON_UNESCAPED_UNICODE); $results = mb_convert_encoding($dataEncoded, 'ISO-8859-1'); @@ -178,7 +178,7 @@ protected function getDefaultSignatures() * @param array $dataAddresses * @return void */ - protected function addOutboundEmailAccounts(array &$dataAddresses): void + protected function addOutboundEmailAccounts(array &$dataAddresses, bool $prependSignature = false, array $defaultEmailSignature = []): void { /** @var OutboundEmailAccounts $outboundAccount */ $outboundAccount = BeanFactory::newBean('OutboundEmailAccounts'); @@ -195,7 +195,7 @@ protected function addOutboundEmailAccounts(array &$dataAddresses): void $replyToAddress = $userOutboundAccount->getReplyToAddress(); $replyToName = $userOutboundAccount->getReplyToName(); $type = $userOutboundAccount->type ?? ''; - $signature = $userOutboundAccount->signature ?? ''; + $signature = $defaultEmailSignature['signature_html'] ?? $userOutboundAccount->signature ?? ''; $isPersonal = $type === 'user'; $isGroup = $type === 'group'; $entry = [ @@ -209,7 +209,7 @@ protected function addOutboundEmailAccounts(array &$dataAddresses): void 'reply_to' => $replyToAddress, 'reply_to_name' => $replyToName ], - 'prepend' => false, + 'prepend' => $prependSignature, 'isPersonalEmailAccount' => $isPersonal, 'isGroupEmailAccount' => $isGroup, 'emailSignatures' => [ diff --git a/modules/Emails/EmailsDataAddressCollector.php b/modules/Emails/EmailsDataAddressCollector.php index 4ca945eb239..62c8b3bca8c 100644 --- a/modules/Emails/EmailsDataAddressCollector.php +++ b/modules/Emails/EmailsDataAddressCollector.php @@ -493,7 +493,8 @@ protected function fillDataAddress($dataAddresses, $defaultEmailSignature, $prep ); $dataAddressesWithUserAddressesAndSystem = $this->fillDataAddressWithSystemMailerSettings( $dataAddressesWithUserAddresses, - $defaultEmailSignature + $defaultEmailSignature, + $prependSignature ); return @@ -661,9 +662,10 @@ protected function getCollectDataAddressArrayFromUserAddresses( * * @param array $dataAddresses * @param array $defaultEmailSignature + * @param boolean $prependSignature * @return array */ - protected function fillDataAddressWithSystemMailerSettings($dataAddresses, $defaultEmailSignature) + protected function fillDataAddressWithSystemMailerSettings($dataAddresses, $defaultEmailSignature, $prependSignature) { $this->setOe(new OutboundEmail()); if ($this->getOe()->isAllowUserAccessToSystemDefaultOutbound()) { @@ -674,7 +676,8 @@ protected function fillDataAddressWithSystemMailerSettings($dataAddresses, $defa $system->smtp_from_name, $system->smtp_from_addr, $system->mail_smtpuser, - $defaultEmailSignature + $defaultEmailSignature, + $prependSignature ); } @@ -744,6 +747,7 @@ protected function getOe() * @param string $fromAddr * @param string $mailUser * @param array $defaultEmailSignature + * @param boolean $prependSignature * @return array */ protected function getFillDataAddressArray( @@ -752,7 +756,8 @@ protected function getFillDataAddressArray( $fromName, $fromAddr, $mailUser, - $defaultEmailSignature + $defaultEmailSignature, + bool $prependSignature ) { $dataAddress = new EmailsDataAddress(); @@ -763,7 +768,7 @@ protected function getFillDataAddressArray( $fromAddr, $fromName, false, - false, + $prependSignature, true, $id, $name, diff --git a/modules/Emails/include/ComposeView/EmailsComposeView.js b/modules/Emails/include/ComposeView/EmailsComposeView.js index 549afbe90f5..fec45a32a02 100644 --- a/modules/Emails/include/ComposeView/EmailsComposeView.js +++ b/modules/Emails/include/ComposeView/EmailsComposeView.js @@ -59,6 +59,12 @@ */ self.prependSignature = false; + /** + * Determines if a Signature has already been added to the message + * @type {boolean} + */ + self.signatureAdded = false; + /** * Defines the buttons that are displayed when the user focuses in on a to, cc and bcc field. * @@ -347,7 +353,19 @@ return false; }; - + + /** + * Remove any existing signature element class in the email thread + * @type {self.removeExistingSignatureClass} + */ + + $.fn.EmailsComposeView.removeExistingSignatureClass = self.removeExistingSignatureClass = function() { + let body = tinymce.activeEditor.getContent(); + let $body = $('
').append($(body)); + let $signatureElement = $body.find('div.email-signature-element'); + $signatureElement.removeClass('email-signature-element'); + tinymce.activeEditor.setContent($body.html(), {format: 'html'}) + } $.fn.EmailsComposeView.updateSignature = self.updateSignature = function ($selected) { if(!$selected) { @@ -362,9 +380,13 @@ var body = tinymce.activeEditor.getContent(); if (body !== '' && $(body).hasClass('email-signature-element')) { - var $body = $(body); - var $existingSignature = $body.find('.email-signature-element'); - $existingSignature.remove(); + var $body = $('
').append($(body)); + var $existingSignature = $body.find('div.email-signature-element'); + if(self.prependSignature) { + $existingSignature.outerText = ''; + } else { + $existingSignature.remove(); + } tinymce.activeEditor.setContent($body.html(), {format: 'html'}); } @@ -1301,11 +1323,16 @@ $(self).trigger('emailComposeViewGetFromFields'); - if (tinymce.initialized === true) { + if (tinymce.initialized === true && !self.signatureAdded) { + self.removeExistingSignatureClass(); self.updateSignature(); - } else if(tinymce.EditorManager && tinymce.EditorManager.activeEditor) { + self.signatureAdded = true; + } else if(tinymce.EditorManager && tinymce.EditorManager.activeEditor && !self.signatureAdded) { tinymce.EditorManager.activeEditor.on('init', function(e) { + self.removeExistingSignatureClass(); self.updateSignature(); + self.signatureAdded = true; + }); } } @@ -1357,11 +1384,13 @@ var intervalCheckTinymce = window.setInterval(function () { var isFromPopulated = $('#from_addr_name').prop("tagName").toLowerCase() === 'select'; - if (tinymce.editors.length > 0 && isFromPopulated === true) { + if (tinymce.editors.length > 0 && isFromPopulated === true && !self.signatureAdded) { + self.removeExistingSignatureClass(); self.updateSignature(); + self.signatureAdded = true; clearInterval(intervalCheckTinymce); } - }, 300); + }, 500); tinymce.init(opts.tinyMceOptions);