From 5258aa6ba973c32104aa2b85120db52b3ee047b4 Mon Sep 17 00:00:00 2001 From: Roland Dalmulder Date: Sat, 18 Jul 2020 11:26:58 +0200 Subject: [PATCH] Fixed sending email to developer #15 Signed-off-by: Roland Dalmulder --- .../com_jed/controllers/ajax.json.php | 3 +- .../com_jed/language/en-GB/en-GB.com_jed.ini | 2 + .../components/com_jed/models/email.php | 49 +++++++++++++------ .../components/com_jed/models/extension.php | 6 ++- .../com_jed/views/extension/tmpl/edit.php | 7 +-- public_html/media/com_jed/js/jed.js | 11 ++++- 6 files changed, 54 insertions(+), 24 deletions(-) diff --git a/public_html/administrator/components/com_jed/controllers/ajax.json.php b/public_html/administrator/components/com_jed/controllers/ajax.json.php index c0f7e4bb5..96fc2e080 100644 --- a/public_html/administrator/components/com_jed/controllers/ajax.json.php +++ b/public_html/administrator/components/com_jed/controllers/ajax.json.php @@ -71,11 +71,12 @@ public function sendMessage(): void $body = $this->input->get('body', '', 'raw'); $messageId = $this->input->getInt('messageId'); $developerId = $this->input->getInt('developerId'); + $extensionId = $this->input->getInt('extensionId'); $userId = $this->input->getInt('userId'); /** @var JedModelEmail $model */ $model = $this->getModel('Email', 'JedModel'); - $model->sendEmail($body, $messageId, $developerId, $userId); + $model->sendEmail($body, $messageId, $developerId, $userId, $extensionId); $message = Text::_('COM_JED_MESSAGE_SENT_TO_DEVELOPER'); $error = false; diff --git a/public_html/administrator/components/com_jed/language/en-GB/en-GB.com_jed.ini b/public_html/administrator/components/com_jed/language/en-GB/en-GB.com_jed.ini index c85f4f8ca..5b65e589b 100644 --- a/public_html/administrator/components/com_jed/language/en-GB/en-GB.com_jed.ini +++ b/public_html/administrator/components/com_jed/language/en-GB/en-GB.com_jed.ini @@ -184,6 +184,7 @@ COM_JED_EXTENSIONS_SELECT_TEMPLATE="- Select Template -" COM_JED_SEND_EMAIL="Send Email" COM_JED_EXTENSIONS_MISSING_MESSAGE_ID="No message has been selected" COM_JED_EXTENSIONS_MISSING_DEVELOPER_ID="No developer found for this extension" +COM_JED_EXTENSIONS_MISSING_EXTENSION_ID="The extension ID is missing" COM_JED_EXTENSIONS_ERROR_DURING_SEND_EMAIL="An error occurred sending the email" COM_JED_TRANSACTION_TYPE_EXTENSION="Extension" COM_JED_EXTENSION_HISTORY_DATE="Date" @@ -193,6 +194,7 @@ COM_JED_EXTENSION_HISTORY_MEMBER="Member" COM_JED_EXTENSION_HISTORY_USER="User" COM_JED_EXTENSION_HISTORY_LOG_MAIL="Email" COM_JED_EXTENSION_HISTORY_LOG_ACTIONLOG="Action" +COM_JED_DEVELOPER_NOT_FOUND="Developer has not been found" ; Tabs COM_JED_EXTENSIONS_GENERAL_TAB="General" diff --git a/public_html/administrator/components/com_jed/models/email.php b/public_html/administrator/components/com_jed/models/email.php index 93809a57f..42d5840cd 100644 --- a/public_html/administrator/components/com_jed/models/email.php +++ b/public_html/administrator/components/com_jed/models/email.php @@ -52,7 +52,7 @@ public function getForm($data = [], $loadData = true) ['control' => 'jform', 'load_data' => $loadData] ); - if (!is_object($form)) + if ( ! is_object($form)) { return false; } @@ -83,11 +83,11 @@ public function testEmail(): array $result['msg'] = ''; $result['state'] = 'error'; - if (!$cids || !$email) + if ( ! $cids || ! $email) { $result['msg'] = Text::_('COM_JED_NO_EMAILS_FOUND'); - if (!$email) + if ( ! $email) { $result['msg'] = Text::_('COM_JED_MISSING_EMAIL_ADDRESS'); } @@ -116,7 +116,10 @@ public function testEmail(): array { $mail->clearAddresses(); - if ($mail->sendMail($from, $fromName, $email, $details->subject, $details->body, true)) + if ($mail->sendMail( + $from, $fromName, $email, $details->subject, $details->body, + true + )) { $result['msg'] = Text::_('COM_JED_TESTEMAIL_SENT'); $result['state'] = ''; @@ -134,18 +137,27 @@ public function testEmail(): array * @param int $messageId The message details to use for sending * @param int $developerId The developer to send the message to * @param int $userId The JED member sending the message + * @param int $extensionId The extension ID the message is about * * @return void * * @since 4.0.0 * - * @throws Exception */ - public function sendEmail(string $body, int $messageId, int $developerId, int $userId): void - { + public function sendEmail(string $body, int $messageId, int $developerId, + int $userId, int $extensionId + ): void { // Get the developer details $developer = User::getInstance($developerId); - $sender = User::getInstance($userId); + + if ($developer->get('id', null) === null) + { + throw new InvalidArgumentException( + Text::_('COM_JED_DEVELOPER_NOT_FOUND') + ); + } + + $sender = User::getInstance($userId); // Get the mail details $mail = $this->getItem($messageId); @@ -171,7 +183,7 @@ public function sendEmail(string $body, int $messageId, int $developerId, int $u throw new RuntimeException($result->getMessage()); } - $this->storeEmail($subject, $body, $developer, $sender); + $this->storeEmail($extensionId, $subject, $body, $developer, $sender); } /** @@ -195,20 +207,23 @@ private function setupMailer(string $fromName): void /** * Store the sent email. * - * @param string $subject The message subject - * @param string $body The message body - * @param User $developer The developer details - * @param User $sender The JED member details + * @param int $extensionId The extension ID the message is about + * @param string $subject The message subject + * @param string $body The message body + * @param User $developer The developer details + * @param User $sender The JED member details * * @return void * * @since 4.0.0 */ - private function storeEmail(string $subject, string $body, User $developer, User $sender): void - { + private function storeEmail(int $extensionId, string $subject, string $body, + User $developer, User $sender + ): void { $emailTable = Table::getInstance('Emaillog', 'Table'); $emailTable->save( [ + 'extension_id' => $extensionId, 'subject' => $subject, 'body' => $body, 'developer_id' => $developer->get('id'), @@ -232,7 +247,9 @@ private function storeEmail(string $subject, string $body, User $developer, User protected function loadFormData() { // Check the session for previously entered form data. - $data = Factory::getApplication()->getUserState('com_jed.edit.email.data', []); + $data = Factory::getApplication()->getUserState( + 'com_jed.edit.email.data', [] + ); if (0 === count($data)) { diff --git a/public_html/administrator/components/com_jed/models/extension.php b/public_html/administrator/components/com_jed/models/extension.php index 53b0be597..2b3c0d3cb 100644 --- a/public_html/administrator/components/com_jed/models/extension.php +++ b/public_html/administrator/components/com_jed/models/extension.php @@ -537,13 +537,13 @@ public function getHistory(int $extensionId): array 'actionLogs.message_language_key', 'actionLogs.message', 'actionLogs.log_date', - 'users.name' + 'users.name', ], [ 'message_language_key', 'message', 'logDate', - 'name' + 'name', ] ) ) @@ -571,6 +571,7 @@ public function getHistory(int $extensionId): array 'emailLogs.developer_email', 'emailLogs.created_by', 'emailLogs.created', + 'emailLogs.developer_id', 'users.name' ], [ @@ -581,6 +582,7 @@ public function getHistory(int $extensionId): array 'developerEmail', 'created_by', 'logDate', + 'developerId', 'memberName' ] ) diff --git a/public_html/administrator/components/com_jed/views/extension/tmpl/edit.php b/public_html/administrator/components/com_jed/views/extension/tmpl/edit.php index a43e0be9c..d36029107 100644 --- a/public_html/administrator/components/com_jed/views/extension/tmpl/edit.php +++ b/public_html/administrator/components/com_jed/views/extension/tmpl/edit.php @@ -25,6 +25,7 @@ Text::script('COM_JED_EXTENSIONS_ERROR_DURING_SEND_EMAIL', true); Text::script('COM_JED_EXTENSIONS_MISSING_MESSAGE_ID', true); Text::script('COM_JED_EXTENSIONS_MISSING_DEVELOPER_ID', true); +Text::script('COM_JED_EXTENSIONS_MISSING_EXTENSION_ID', true); $extensionUrl = Uri::root() . 'extension/' . $this->item->alias; $downloadUrl = 'index.php?option=com_jed&task=extension.download&id=' . $this->item->id; @@ -180,7 +181,7 @@ item->history as $history): ?>logDate; ?>logDate, Text::_('DATE_FORMAT_LC6')); ?>type); ?>type === 'mail') @@ -190,12 +191,12 @@ body; ?> memberName; ?>developerName; ?> <developerEmail; ?>>developerId, $history->developerName); ?> <developerEmail; ?>>type === 'actionLog') { ?>name; ?>name; ?> { const messageId = document.getElementById('jform_template').value, - developerId = document.getElementById('jform_created_by_id').value; + developerId = document.getElementById('jform_created_by_id').value, + extensionId = document.getElementById('jform_id').value; // Check if we have any values if (isNaN(parseInt(messageId)) === true) { @@ -71,12 +72,18 @@ const jed = (function () { return false; } + if (isNaN(parseInt(extensionId)) === true) { + renderMessage(Joomla.JText._('COM_JED_EXTENSIONS_MISSING_EXTENSION_ID'), 'error'); + return false; + } + let data = new FormData(); data.append('option', 'com_jed'); data.append('task', 'ajax.sendMessage'); data.append('body', tinyMCE.activeEditor.getContent()); data.append('messageId', messageId); data.append('developerId', developerId); + data.append('extensionId', extensionId); data.append('userId', Joomla.getOptions('joomla.userId')); data.append('format', 'json'); @@ -152,4 +159,4 @@ const jed = (function () { // Return the public parts return jed; -}()); \ No newline at end of file +}());