Skip to content

Commit

Permalink
Merge pull request pkp#223 from Vitaliy-1/i210_mailable
Browse files Browse the repository at this point in the history
pkp#210 Refactor email management
  • Loading branch information
withanage authored Oct 24, 2022
2 parents 21656e2 + 7615fe3 commit 7dbbd98
Show file tree
Hide file tree
Showing 36 changed files with 267 additions and 123 deletions.
97 changes: 42 additions & 55 deletions OrcidProfilePlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
/**
* @file OrcidProfilePlugin.inc.php
*
* Copyright (c) 2015-2019 University of Pittsburgh
* Copyright (c) 2014-2020 Simon Fraser University
* Copyright (c) 2003-2020 John Willinsky
* Copyright (c) 2015-2022 University of Pittsburgh
* Copyright (c) 2014-2022 Simon Fraser University
* Copyright (c) 2003-2022 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class OrcidProfilePlugin
Expand Down Expand Up @@ -33,17 +33,18 @@
use APP\core\Application;
use APP\decision\Decision;
use APP\facades\Repo;
use APP\plugins\generic\orcidProfile\mailables\OrcidRequestAuthorAuthorization;
use APP\template\TemplateManager;
use Illuminate\Support\Facades\Mail;
use PKP\config\Config;
use PKP\core\JSONMessage;
use PKP\linkAction\LinkAction;

use PKP\plugins\Hook;
use PKP\linkAction\request\AjaxModal;
use PKP\mail\MailTemplate;
use PKP\plugins\GenericPlugin;
use PKP\plugins\HookRegistry;
use PKP\plugins\PluginRegistry;
use PKP\submission\PKPSubmission;
use APP\plugins\generic\orcidProfile\mailables\OrcidCollectAuthorId;

class OrcidProfilePlugin extends GenericPlugin
{
Expand All @@ -67,32 +68,32 @@ public function register($category, $path, $mainContextId = null)
if ($success && $this->getEnabled($mainContextId)) {
$contextId = ($mainContextId === null) ? $this->getCurrentContextId() : $mainContextId;

HookRegistry::register('ArticleHandler::view', [&$this, 'submissionView']);
HookRegistry::register('PreprintHandler::view', [&$this, 'submissionView']);
Hook::add('ArticleHandler::view', [&$this, 'submissionView']);
Hook::add('PreprintHandler::view', [&$this, 'submissionView']);

// Insert the OrcidHandler to handle ORCID redirects
HookRegistry::register('LoadHandler', [$this, 'setupCallbackHandler']);
Hook::add('LoadHandler', [$this, 'setupCallbackHandler']);

// Register callback for Smarty filters; add CSS
HookRegistry::register('TemplateManager::display', [$this, 'handleTemplateDisplay']);
Hook::add('TemplateManager::display', [$this, 'handleTemplateDisplay']);

// Add "Connect ORCID" button to PublicProfileForm
HookRegistry::register('User::PublicProfile::AdditionalItems', [$this, 'handleUserPublicProfileDisplay']);
Hook::add('User::PublicProfile::AdditionalItems', [$this, 'handleUserPublicProfileDisplay']);

// Display additional ORCID access information and checkbox to send e-mail to authors in the AuthorForm
HookRegistry::register('authorform::display', [$this, 'handleFormDisplay']);
Hook::add('authorform::display', [$this, 'handleFormDisplay']);

// Send email to author, if the added checkbox was ticked
HookRegistry::register('authorform::execute', [$this, 'handleAuthorFormExecute']);
Hook::add('authorform::execute', [$this, 'handleAuthorFormExecute']);

// Handle ORCID on user registration
HookRegistry::register('registrationform::execute', [$this, 'collectUserOrcidId']);
Hook::add('registrationform::execute', [$this, 'collectUserOrcidId']);

// Send emails to authors without ORCID id upon submission
HookRegistry::register('submissionsubmitstep3form::execute', [$this, 'handleSubmissionSubmitStep3FormExecute']);
Hook::add('submissionsubmitstep3form::execute', [$this, 'handleSubmissionSubmitStep3FormExecute']);

// Add more ORCiD fields to user Schema
HookRegistry::register('Schema::get::user', function ($hookName, $args) {
Hook::add('Schema::get::user', function ($hookName, $args) {
$schema = $args[0];

$schema->properties->orcidAccessToken = (object)[
Expand Down Expand Up @@ -124,13 +125,13 @@ public function register($category, $path, $mainContextId = null)

// Send emails to authors without authorised ORCID access on promoting a submission to copy editing. Not included in OPS.
if ($this->getSetting($contextId, 'sendMailToAuthorsOnPublication')) {
HookRegistry::register('EditorAction::recordDecision', [$this, 'handleEditorAction']);
Hook::add('EditorAction::recordDecision', [$this, 'handleEditorAction']);
}

HookRegistry::register('Publication::publish', [$this, 'handlePublicationStatusChange']);
Hook::add('Publication::publish', [$this, 'handlePublicationStatusChange']);

// Add more ORCiD fields to author Schema
HookRegistry::register('Schema::get::author', function ($hookName, $args) {
Hook::add('Schema::get::author', function ($hookName, $args) {
$schema = $args[0];

$schema->properties->orcidSandbox = (object)[
Expand Down Expand Up @@ -174,6 +175,8 @@ public function register($category, $path, $mainContextId = null)
'validation' => ['nullable']
];
});

Hook::add('Mailer::Mailables', [$this, 'addMailable']);
}

return $success;
Expand Down Expand Up @@ -723,19 +726,6 @@ public function getIcon()
return file_exists($path) ? file_get_contents($path) : '';
}

/**
* Instantiate a MailTemplate
*
* @param string $emailKey
* @param Context $context
*
* @return MailTemplate
*/
public function getMailTemplate($emailKey, $context = null)
{
return new MailTemplate($emailKey, null, $context, false);
}

/**
* Send mail with ORCID authorization link to the e-mail address of the supplied Author object.
*
Expand All @@ -751,37 +741,26 @@ public function sendAuthorMail($author, $updateAuthor = false)
// This should only ever happen within a context, never site-wide.
if ($context != null) {
$contextId = $context->getId();
$publicationId = $author->getData('publicationId');
$publication = Repo::publication()->get($publicationId);
$submission = Repo::submission()->get($publication->getData('submissionId'));

if ($this->isMemberApiEnabled($contextId)) {
$mailTemplate = 'ORCID_REQUEST_AUTHOR_AUTHORIZATION';
} else {
$mailTemplate = 'ORCID_COLLECT_AUTHOR_ID';
}

$mail = $this->getMailTemplate($mailTemplate, $context);
$emailToken = md5(microtime() . $author->getEmail());

$author->setData('orcidEmailToken', $emailToken);
$oauthUrl = $this->buildOAuthUrl('orcidVerify', ['token' => $emailToken, 'publicationId' => $publicationId]);

$publication = Repo::publication()->get($author->getData('publicationId'));

$oauthUrl = $this->buildOAuthUrl('orcidVerify', ['token' => $emailToken, 'publicationId' => $publication->getId()]);
$aboutUrl = $request->getDispatcher()->url($request, Application::ROUTE_PAGE, null, 'orcidapi', 'about', null);
if ($this->isMemberApiEnabled($contextId)) {
$mailable = new OrcidRequestAuthorAuthorization($context, $submission, $oauthUrl);
} else {
$mailable = new OrcidCollectAuthorId($context, $submission, $oauthUrl);
}

// Set From to primary journal contact
$mail->setFrom($context->getData('contactEmail'), $context->getData('contactName'));
$mailable->from($context->getData('contactEmail'), $context->getData('contactName'));

// Send to author
$mail->setRecipients([['name' => $author->getFullName(), 'email' => $author->getEmail()]]);

// Send the mail with parameters
$mail->sendWithParams([
'orcidAboutUrl' => $aboutUrl,
'authorOrcidUrl' => $oauthUrl,
'authorName' => htmlspecialchars($author->getFullName()),
'articleTitle' => htmlspecialchars($publication->getLocalizedTitle()), // Backwards compatibility only
'submissionTitle' => htmlspecialchars($publication->getLocalizedTitle()),
]);
$mailable->recipients([$author]);
Mail::send($mailable);

if ($updateAuthor) {
Repo::author()->dao->update($author);
Expand Down Expand Up @@ -1366,4 +1345,12 @@ public function isMemberApiEnabled($contextId)
$apiUrl = $this->getSetting($contextId, 'orcidProfileAPIPath');
return in_array($apiUrl, [ORCID_API_URL_MEMBER, ORCID_API_URL_MEMBER_SANDBOX]);
}

/**
* Add mailable to the list of mailables in the application
*/
public function addMailable(string $hookName, array $args): void
{
$args[0] = array_merge($args[0], [OrcidCollectAuthorId::class, OrcidRequestAuthorAuthorization::class]);
}
}
10 changes: 5 additions & 5 deletions emailTemplates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
<!--
* emailTemplates.xml
*
* Copyright (c) 2015-2019 University of Pittsburgh
* Copyright (c) 2013-2020 Simon Fraser University
* Copyright (c) 2003-2020 John Willinsky
* Copyright (c) 2015-2022 University of Pittsburgh
* Copyright (c) 2013-2022 Simon Fraser University
* Copyright (c) 2003-2022 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* Email templates XML file.
-->
<emails>
<email key="ORCID_COLLECT_AUTHOR_ID" can_edit="1" can_disable="0" subject="emails.orcidCollectAuthorId.subject" description="emails.orcidCollectAuthorId.description" body="emails.orcidCollectAuthorId.body"/>
<email key="ORCID_REQUEST_AUTHOR_AUTHORIZATION" can_edit="1" can_disable="0" subject="emails.orcidRequestAuthorAuthorization.subject" description="emails.orcidRequestAuthorAuthorization.description" body="emails.orcidRequestAuthorAuthorization.body"/>
<email key="ORCID_COLLECT_AUTHOR_ID" subject="emails.orcidCollectAuthorId.subject" body="emails.orcidCollectAuthorId.body"/>
<email key="ORCID_REQUEST_AUTHOR_AUTHORIZATION" subject="emails.orcidRequestAuthorAuthorization.subject" body="emails.orcidRequestAuthorAuthorization.body"/>
</emails>
4 changes: 2 additions & 2 deletions locale/ar_IQ/emails.po
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ msgstr "تقديم ORCID"

msgid "emails.orcidCollectAuthorId.body"
msgstr ""
">عزيزنا {$authorName}،<br/>\n"
">عزيزنا {$recipientName}،<br/>\n"
"<br/>\n"
"لقد تم إدراجك بمثابة مؤلف مشارك في عمل مقدم إلى {$contextName}.<br/>\n"
"لتأكيد مصادقتك، لطفاً، أضف رمزك التعريفي في ORCID إلى طلب التقديم هذا عبر "
Expand Down Expand Up @@ -49,7 +49,7 @@ msgstr "إلتماس الوصو إلى حساب ORCID"

msgid "emails.orcidRequestAuthorAuthorization.body"
msgstr ""
"عزيزنا {$authorName}،<br>\n"
"عزيزنا {$recipientName}،<br>\n"
"<br>\n"
"لقد تم إدراجك بمثابة مؤلف مشارك في العمل الموسوم \"{$submissionTitle}\" "
"والمقدم إلى {$contextName}.\n"
Expand Down
4 changes: 2 additions & 2 deletions locale/bg_BG/emails.po
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ msgstr "Предоставяне на ORCID"

msgid "emails.orcidCollectAuthorId.body"
msgstr ""
"Здравейте, {$authorName},<br/>\n"
"Здравейте, {$recipientName},<br/>\n"
"<br/>\n"
"Вие сте посочени като автор в ръкопис, предложен за публикуване в "
"{$contextName}.<br/>\n"
Expand Down Expand Up @@ -49,7 +49,7 @@ msgstr "Изискване достъп до ORCID запис"

msgid "emails.orcidRequestAuthorAuthorization.body"
msgstr ""
"Здравейте, {$authorName},<br>\n"
"Здравейте, {$recipientName},<br>\n"
"<br>\n"
"Вие сте посочени като автор в ръкопис, предложен за публикуване \""
"{$submissionTitle}\" в {$contextName}.\n"
Expand Down
4 changes: 2 additions & 2 deletions locale/ca_ES/emails.po
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ msgstr "ORCID de tramesa"

msgid "emails.orcidRequestAuthorAuthorization.body"
msgstr ""
"Benvolgut/da {$authorName},<br>\n"
"Benvolgut/da {$recipientName},<br>\n"
"<br>\n"
"Us han afegit com a autor/a de l'article \"{$submissionTitle}\" per a "
"{$contextName}.\n"
Expand All @@ -57,7 +57,7 @@ msgstr ""

msgid "emails.orcidCollectAuthorId.body"
msgstr ""
"Benvolgut/da {$authorName},<br/>\n"
"Benvolgut/da {$recipientName},<br/>\n"
"<br/>\n"
"Us han afegit com a autor/a d'un article per a {$contextName}.<br/>\n"
"Per confirmar la vostra autoria, afegiu el vostre iD ORCID a aquesta tramesa "
Expand Down
4 changes: 2 additions & 2 deletions locale/cs_CZ/emails.po
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ msgstr "ORCID ID příspěvku"

msgid "emails.orcidCollectAuthorId.body"
msgstr ""
"Vážený (á) {$authorName},<br/>\n"
"Vážený (á) {$recipientName},<br/>\n"
"<br/>\n"
"Byl jste uveden(a) jako autor manuskriptu příspěvku do {$contextName}.<br/>\n"
"Pro potvrzení vašeho autorství přidejte, prosím, pomocí níže uvedeného "
Expand Down Expand Up @@ -49,7 +49,7 @@ msgstr ""

msgid "emails.orcidRequestAuthorAuthorization.body"
msgstr ""
"Vážený(á) {$authorName},<br>\n"
"Vážený(á) {$recipientName},<br>\n"
"<br>\n"
"Byl(a) jste uveden(a) jako autor manuskriptu příspěvku \"{$submissionTitle}\""
" to {$contextName}.\n"
Expand Down
4 changes: 2 additions & 2 deletions locale/da_DK/emails.po
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ msgstr "Indsendelse ORCID"

msgid "emails.orcidCollectAuthorId.body"
msgstr ""
"Kære {$authorName},<br/>\n"
"Kære {$recipientName},<br/>\n"
"<br/>\n"
"Du står opført som forfatter til et manuskriptet i {$contextName}.<br/>\n"
"<br/>\n"
Expand Down Expand Up @@ -49,7 +49,7 @@ msgstr ""

msgid "emails.orcidRequestAuthorAuthorization.body"
msgstr ""
"Kære {$authorName}<br>\n"
"Kære {$recipientName}<br>\n"
"<br>\n"
"Du er blevet opført som forfatter på manuskriptindsendelsen\"{$submissionTitle}"
"\" til {$contextName}.\n"
Expand Down
4 changes: 2 additions & 2 deletions locale/de_DE/emails.po
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ msgstr "ORCID Zugriff erbeten"

msgid "emails.orcidCollectAuthorId.body"
msgstr ""
"Liebe/r {$authorName},<br/>\n"
"Liebe/r {$recipientName},<br/>\n"
"<br/>\n"
"Sie sind als Autor/in eines eingereichten Beitrags bei der Zeitschrift "
"{$contextName} benannt worden.<br/>\n"
Expand All @@ -46,7 +46,7 @@ msgstr "ORCID Zugriff erbeten"

msgid "emails.orcidRequestAuthorAuthorization.body"
msgstr ""
"Liebe/r {$authorName},<br>\n"
"Liebe/r {$recipientName},<br>\n"
"<br>\n"
"Sie sind als Autor/in des eingereichten Beitrags \"{$submissionTitle}\" bei "
"der Zeitschrift {$contextName} benannt worden.<br>\n"
Expand Down
4 changes: 2 additions & 2 deletions locale/el_GR/emails.po
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ msgstr "Υποβολή ORCID"

msgid "emails.orcidCollectAuthorId.body"
msgstr ""
"Αγαπητέ/η {$authorName},\n"
"Αγαπητέ/η {$recipientName},\n"
"\n"
"Έχετε καταχωρηθεί ως ένας/μια από τους συγγραφείς στην υποβολή \"{$submissionTitle}\" στο \"{$contextName}\".\n"
"\n"
Expand All @@ -39,7 +39,7 @@ msgstr "Αίτημα πρόσβασης στο ORCID σας"

msgid "emails.orcidRequestAuthorAuthorization.body"
msgstr ""
"Αγαπητέ/η {$authorName},<br>\n"
"Αγαπητέ/η {$recipientName},<br>\n"
"<br>\n"
"Έχετε καταχωρηθεί ως συγγραφέας στην υποβολή \"{$submissionTitle}\" στο \"{$contextName}\".\n"
"<br>\n"
Expand Down
14 changes: 10 additions & 4 deletions locale/en_US/emails.po
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ msgstr "Submission ORCID"
#, fuzzy
msgid "emails.orcidCollectAuthorId.body"
msgstr ""
"Dear {$authorName},<br/>\n"
"Dear {$recipientName},<br/>\n"
"<br/>\n"
"You have been listed as an author on a manuscript submission to "
"{$contextName}.<br/>\n"
Expand All @@ -42,15 +42,15 @@ msgstr ""
"{$principalContactSignature}<br/>\n"

msgid "emails.orcidCollectAuthorId.description"
msgstr "This email template is used to collect the ORCID id's from authors."
msgstr "This email is sent to collect the ORCID id's from authors."

msgid "emails.orcidRequestAuthorAuthorization.subject"
msgstr "Requesting ORCID record access"

#, fuzzy
msgid "emails.orcidRequestAuthorAuthorization.body"
msgstr ""
"Dear {$authorName},<br>\n"
"Dear {$recipientName},<br>\n"
"<br>\n"
"You have been listed as an author on the manuscript submission \""
"{$submissionTitle}\" to {$contextName}.\n"
Expand All @@ -74,4 +74,10 @@ msgstr ""
"{$principalContactSignature}<br>\n"

msgid "emails.orcidRequestAuthorAuthorization.description"
msgstr "This email template is used to request ORCID record access from authors."
msgstr "This email is sent to request ORCID record access from authors."

msgid "emailTemplate.variable.authorOrcidUrl"
msgstr "ORCID OAuth authorization link"

msgid "emailTemplate.variable.orcidAboutUrl"
msgstr "URL to the page about ORCID"
4 changes: 2 additions & 2 deletions locale/es_ES/emails.po
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ msgstr "ORCID de envío"

msgid "emails.orcidCollectAuthorId.body"
msgstr ""
"Estimado/a {$authorName},\n"
"Estimado/a {$recipientName},\n"
"<br/>\n"
"Se le ha añadido como coautor/a de un artículo para {$contextName}. <br/>\n"
"Para confirmar su autoría, añada su identificador ORCID a este envío "
Expand Down Expand Up @@ -55,7 +55,7 @@ msgstr "Solicitando acceso de registro ORCID"

msgid "emails.orcidRequestAuthorAuthorization.body"
msgstr ""
"Estimado/a {$authorName},<br>\n"
"Estimado/a {$recipientName},<br>\n"
"<br>\n"
"Usted ha sido incluido como autor en la presentación del manuscrito \""
"{$submissionTitle}\" a {$contextName}.\n"
Expand Down
Loading

0 comments on commit 7dbbd98

Please sign in to comment.