Skip to content

Commit

Permalink
Merge pull request pkp#3469 from Vitaliy-1/i7286_convert_mailables
Browse files Browse the repository at this point in the history
pkp/pkp-lib#7286 Refactor email sending code
  • Loading branch information
NateWr authored Aug 11, 2022
2 parents 5dace88 + 094b152 commit fde50da
Show file tree
Hide file tree
Showing 130 changed files with 3,349 additions and 5,119 deletions.
77 changes: 77 additions & 0 deletions Jobs/Notifications/IssuePublishedMailUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* @file Jobs/Notifications/IssuePublishedMailUsers.php
*
* Copyright (c) 2014-2022 Simon Fraser University
* Copyright (c) 2000-2022 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class IssuePublishedMailUsers
* @ingroup jobs
*
* @brief Class to send emails when a new issue is published
*/

namespace APP\Jobs\Notifications;

use APP\core\Application;
use APP\facades\Repo;
use APP\mail\mailables\IssuePublishedNotify;
use Illuminate\Bus\Batchable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Mail;
use PKP\context\Context;
use PKP\emailTemplate\EmailTemplate;
use PKP\Support\Jobs\BaseJob;
use PKP\user\User;

class IssuePublishedMailUsers extends BaseJob
{
use Batchable;

protected Collection $recipientIds;
protected int $contextId;
protected User $sender;
protected string $locale;

public function __construct(Collection $recipientIds, int $contextId, User $sender, string $locale)
{
parent::__construct();

$this->recipientIds = $recipientIds;
$this->contextId = $contextId;
$this->sender = $sender;
$this->locale = $locale;
}

public function handle()
{
$context = Application::getContextDAO()->getById($this->contextId);
$template = Repo::emailTemplate()->getByKey($this->contextId, IssuePublishedNotify::getEmailTemplateKey());
foreach ($this->recipientIds as $recipientId) {
$recipient = Repo::user()->get($recipientId);
if (!$recipient) {
continue;
}
$mailable = $this->createMailable($context, $recipient, $template);
$mailable->setData($this->locale);
Mail::send($mailable);
}
}

/**
* Creates new issue published notification email
*/
protected function createMailable(Context $context, User $recipient, EmailTemplate $template): IssuePublishedNotify
{
$mailable = new IssuePublishedNotify($context);
$mailable
->recipients([$recipient])
->sender($this->sender)
->body($template->getData('body', $this->locale))
->subject($template->getData('subject', $this->locale));

return $mailable;
}
}
77 changes: 77 additions & 0 deletions Jobs/Notifications/OpenAccessMailUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* @file Jobs/Notifications/OpenAccessMailUsers.php
*
* Copyright (c) 2014-2022 Simon Fraser University
* Copyright (c) 2000-2022 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class OpenAccessMailUsers
* @ingroup jobs
*
* @brief Class to send issue open access notification to users
*/

namespace APP\Jobs\Notifications;

use APP\core\Application;
use APP\facades\Repo;
use APP\journal\Journal;
use APP\mail\mailables\OpenAccessNotify;
use APP\template\TemplateManager;
use Illuminate\Bus\Batchable;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\LazyCollection;
use PKP\Support\Jobs\BaseJob;
use Symfony\Component\Mime\Message;

class OpenAccessMailUsers extends BaseJob
{
use Batchable;

protected LazyCollection $users;
protected int $contextId;
protected int $issueId;

public function __construct(LazyCollection $users, int $contextId, int $issueId)
{
parent::__construct();

$this->users = $users;
$this->contextId = $contextId;
$this->issueId = $issueId;
}

public function handle()
{
$contextDao = Application::getContextDAO();
$context = $contextDao->getById($this->contextId); /** @var Journal $context */
$issue = Repo::issue()->get($this->issueId);
$locale = $context->getPrimaryLocale();
$template = Repo::emailTemplate()->getByKey($this->contextId, OpenAccessNotify::getEmailTemplateKey());

foreach ($this->users as $user) {
$mailable = new OpenAccessNotify($context, $issue);
$mailable
->from($context->getData('contactEmail'), $context->getData('contactName'))
->recipients([$user]);

$templateMgr = TemplateManager::getManager(Application::get()->getRequest());
$templateMgr->assign($mailable->getSmartyTemplateVariables());

$mailable
->subject($template->getData('subject', $locale))
->body($templateMgr->fetch('payments/openAccessNotifyEmail.tpl'));

$mailable->withSymfonyMessage(function (Message $message) use ($templateMgr) {
$message->getHeaders()->addHeader(
'Content-Type',
'multipart/alternative; boundary="' . $templateMgr->getTemplateVars('mimeBoundary') . '"'
);
});

Mail::send($mailable);
}
}
}
38 changes: 21 additions & 17 deletions classes/controllers/grid/issues/IssueGridHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
use APP\controllers\tab\pubIds\form\PublicIdentifiersForm;
use APP\facades\Repo;
use APP\issue\Collector;
use APP\Jobs\Notifications\IssuePublishedMailUsers;
use APP\notification\Notification;
use APP\notification\NotificationManager;
use APP\publication\Publication;
use APP\security\authorization\OjsIssueRequiredPolicy;
use APP\submission\Submission;
use APP\template\TemplateManager;
use Illuminate\Support\Facades\Bus;
use PKP\controllers\grid\GridColumn;
use PKP\controllers\grid\GridHandler;
use PKP\core\Core;
Expand All @@ -41,6 +43,8 @@
use PKP\db\DAORegistry;
use PKP\facades\Locale;
use PKP\file\TemporaryFileManager;
use PKP\mail\Mailer;
use PKP\notification\NotificationSubscriptionSettingsDAO;
use PKP\plugins\HookRegistry;
use PKP\plugins\PluginRegistry;
use PKP\security\authorization\ContextAccessPolicy;
Expand Down Expand Up @@ -594,26 +598,26 @@ public function publishIssue($args, $request)

// Send a notification to associated users if selected and context is publishing content online with OJS
if ($request->getUserVar('sendIssueNotification') && $context->getData('publishingMode') != \APP\journal\Journal::PUBLISHING_MODE_NONE) {
$notificationManager = new NotificationManager();
$notificationUsers = [];
$userGroupDao = DAORegistry::getDAO('UserGroupDAO'); /** @var UserGroupDAO $userGroupDao */
$allUsers = $userGroupDao->getUsersByContextId($contextId);
while ($user = $allUsers->next()) {
if ($user->getDisabled()) {
continue;
}
$notificationUsers[] = ['id' => $user->getId()];
}
foreach ($notificationUsers as $userRole) {
$notificationManager->createNotification(
$request,
$userRole['id'],
Notification::NOTIFICATION_TYPE_PUBLISHED_ISSUE,
/** @var NotificationSubscriptionSettingsDAO $notificationSubscriptionSettingsDao */
$notificationSubscriptionSettingsDao = DAORegistry::getDAO('NotificationSubscriptionSettingsDAO');

$userIdsToMail = $notificationSubscriptionSettingsDao->getSubscribedUserIds(
[NotificationSubscriptionSettingsDAO::BLOCKED_EMAIL_NOTIFICATION_KEY],
[Notification::NOTIFICATION_TYPE_PUBLISHED_ISSUE],
[$contextId]
);

$jobs = [];
foreach ($userIdsToMail->chunk(Mailer::BULK_EMAIL_SIZE_LIMIT) as $mailUserIds) {
$mailJob = new IssuePublishedMailUsers(
$mailUserIds,
$contextId,
ASSOC_TYPE_ISSUE,
$issue->getId()
$request->getUser(),
Locale::getLocale()
);
$jobs[] = $mailJob;
}
Bus::batch($jobs)->dispatch();
}

$json = DAO::getDataChangedEvent();
Expand Down
1 change: 1 addition & 0 deletions classes/emailTemplate/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected function variablesToRename(): array
return [
'contextName' => 'journalName',
'contextUrl' => 'journalUrl',
'contextSignature' => 'journalSignature',
];
}
}
67 changes: 67 additions & 0 deletions classes/mail/mailables/IssuePublishedNotify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* @file classes/mail/mailables/IssuePublishedNotify.php
*
* Copyright (c) 2014-2022 Simon Fraser University
* Copyright (c) 2000-2022 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class IssuePublishedNotify
* @ingroup mail_mailables
*
* @brief Email is sent automatically to all registered users to notify about new published issue
*/

namespace APP\mail\mailables;

use APP\facades\Repo;
use PKP\context\Context;
use PKP\mail\Mailable;
use PKP\mail\traits\Configurable;
use PKP\mail\traits\Recipient;
use PKP\mail\traits\Sender;
use PKP\security\Role;

class IssuePublishedNotify extends Mailable
{
use Configurable;
use Recipient;
use Sender;

protected static ?string $name = 'mailable.issuePublishNotify.name';
protected static ?string $description = 'mailable.issuePublishNotify.description';
protected static ?string $emailTemplateKey = 'ISSUE_PUBLISH_NOTIFY';
protected static array $groupIds = [self::GROUP_OTHER];
protected static array $fromRoleIds = [Role::ROLE_ID_MANAGER];
protected static array $toRoleIds = [Role::ROLE_ID_READER];

protected static string $issueIdentification = 'issueIdentification';

public function __construct(Context $context)
{
parent::__construct(func_get_args());
$this->setupIssueIdentificationVariable($context);
}

/**
* Adds description to the issueIdentification email template variable
*/
public static function getDataDescriptions(): array
{
$variables = parent::getDataDescriptions();
$variables[static::$issueIdentification] = __('emailTemplate.variable.issue.issueIdentification');
return $variables;
}

/**
* Include current issue identification to be used as an email template variable
*/
protected function setupIssueIdentificationVariable(Context $context)
{
$issue = Repo::issue()->get($context->getData('currentIssueId'));
$this->addData([
static::$issueIdentification => $issue->getData('identification')
]);
}
}
59 changes: 59 additions & 0 deletions classes/mail/mailables/OpenAccessNotify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* @file classes/mail/mailables/OpenAccessNotify.php
*
* Copyright (c) 2014-2022 Simon Fraser University
* Copyright (c) 2000-2022 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class OpenAccessNotify
*
* @brief Email sent to notify user about issue open access
*/

namespace APP\mail\mailables;

use APP\facades\Repo;
use APP\issue\Issue;
use APP\journal\Journal;
use PKP\mail\Mailable;
use PKP\mail\traits\Configurable;
use PKP\mail\traits\Recipient;
use PKP\security\Role;

class OpenAccessNotify extends Mailable
{
use Configurable;
use Recipient;

protected static ?string $name = 'mailable.openAccessNotify.name';
protected static ?string $description = 'mailable.openAccessNotify.description';
protected static ?string $emailTemplateKey = 'OPEN_ACCESS_NOTIFY';
protected static array $groupIds = [self::GROUP_OTHER];
protected static array $toRoleIds = [Role::ROLE_ID_READER];

protected Journal $context;
protected Issue $issue;

public function __construct(Journal $context, Issue $issue)
{
parent::__construct([$context]);
$this->context = $context;
$this->issue = $issue;
}

/**
* Setup Smarty variables for the message template
*/
public function getSmartyTemplateVariables(): array
{
$template = Repo::emailTemplate()->getByKey($this->context->getId(), static::$emailTemplateKey);
return [
'body' => $template->getData('body', $this->context->getPrimaryLocale()),
'mimeBoundary' => '==boundary_' . md5(microtime()),
'issue' => $this->issue,
'publishedSubmissions' => Repo::submission()->getInSections($this->issue->getId(), $this->issue->getJournalId()),
];
}
}
Loading

0 comments on commit fde50da

Please sign in to comment.