first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-06-08 17:09:23 -04:00
commit df3a033196
17887 changed files with 8637778 additions and 0 deletions
@@ -0,0 +1,58 @@
<?php
/**
* @file classes/mail/traits/Configurable.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 Configurable
*
* @ingroup mail_traits
*
* @brief trait to support Mailable's name and description displayed in the UI
*/
namespace PKP\mail\traits;
use Exception;
trait Configurable
{
/**
* Retrieve localized Mailable's name
*
* @throws Exception
*/
public static function getName(): string
{
if (is_null(static::$name)) {
throw new Exception('Configurable mailable created without a name.');
}
return __(static::$name);
}
/**
* Retrieve localized Mailable's description
*
* @throws Exception
*/
public static function getDescription(): string
{
if (is_null(static::$description)) {
throw new Exception('Configurable mailable created without a description.');
}
return __(static::$description);
}
/**
* Retrieve a unique Mailable's ID
*/
public static function getId(): string
{
return str_replace('\\', '-', static::class);
}
}
@@ -0,0 +1,52 @@
<?php
/**
* @file classes/mail/traits/Discussion.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 Discussion
*
* @ingroup mail_traits
*
* @brief trait to support Discussion email template variables
*/
namespace PKP\mail\traits;
use PKP\mail\variables\SubmissionEmailVariable;
trait Discussion
{
use Unsubscribe {
getRequiredVariables as getTraitRequiredVariables;
}
protected function addFooter(string $locale): self
{
$this->setupUnsubscribeFooter($locale, $this->context);
return $this;
}
protected function setFooterText(string $locale, string $localeKey = null): string
{
if (is_null($localeKey)) {
$localeKey = 'emails.footer.unsubscribe.discussion';
}
return __($localeKey, [], $locale);
}
/**
* Adds email template variable class required to generate submission-related variables
*/
protected static function getRequiredVariables(): array
{
return array_merge(
static::getTraitRequiredVariables(),
[SubmissionEmailVariable::class]
);
}
}
@@ -0,0 +1,61 @@
<?php
/**
* @file classes/mail/traits/OneClickReviewerAccess.php
*
* Copyright (c) 2014-2023 Simon Fraser University
* Copyright (c) 2000-2023 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class OneClickReviewerAccess
*
* @ingroup mail_traits
*
* @brief Mailable trait to override the review assignment URL with the
* secure, one-click access URL for reviewers
*/
namespace PKP\mail\traits;
use APP\core\Application;
use PKP\context\Context;
use PKP\mail\variables\ReviewAssignmentEmailVariable;
use PKP\security\AccessKeyManager;
use PKP\submission\reviewAssignment\ReviewAssignment;
trait OneClickReviewerAccess
{
protected function setOneClickAccessUrl(Context $context, ReviewAssignment $reviewAssignment): void
{
if (!$context->getData('reviewerAccessKeysEnabled')) {
return;
}
$application = Application::get();
$request = $application->getRequest();
$dispatcher = $application->getDispatcher();
$accessKeyManager = new AccessKeyManager();
$expiryDays = ($this->context->getData('numWeeksPerReview') + 4) * 7;
$accessKey = $accessKeyManager->createKey(
$context->getId(),
$reviewAssignment->getReviewerId(),
$reviewAssignment->getId(),
$expiryDays
);
$this->viewData[ReviewAssignmentEmailVariable::REVIEW_ASSIGNMENT_URL] = $dispatcher->url(
$request,
Application::ROUTE_PAGE,
$context->getData('urlPath'),
'reviewer',
'submission',
null,
[
'submissionId' => $reviewAssignment->getSubmissionId(),
'reviewId' => $reviewAssignment->getId(),
'key' => $accessKey,
]
);
}
}
@@ -0,0 +1,57 @@
<?php
/**
* @file classes/mail/traits/PasswordResetUrl.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 PasswordResetUrl
*
* @ingroup mail_traits
*
* @brief Mailable trait to ensure compatibility of `passwordResetUrl` variable for templates:
* REVIEW_REMIND, REVIEW_REMIND_AUTO, REVIEW_REMIND_OVERDUE_AUTO, PASSWORD_RESET_CONFIRM
*/
namespace PKP\mail\traits;
use APP\core\Application;
use PKP\security\Validation;
use PKP\user\User;
trait PasswordResetUrl
{
protected static string $passwordResetUrl = 'passwordResetUrl';
/**
* Adds passwordResetUrl variable's value to the Mailable
*/
protected function setPasswordResetUrl(User $user, ?string $contextUrlPath = null): void
{
$request = Application::get()->getRequest();
$dispatcher = $request->getDispatcher();
$this->addData([self::$passwordResetUrl =>
$dispatcher->url(
$request,
Application::ROUTE_PAGE,
$contextUrlPath,
'login',
'resetPassword',
$user->getUsername(),
['confirm' => Validation::generatePasswordResetHash($user->getId())]
)
]);
}
/**
* Adds passwordResetUrl variable's description to the Mailable
*/
public static function addPasswordResetUrlDescription(array $variables): array
{
$variables[self::$passwordResetUrl] = __('emailTemplate.variable.passwordResetUrl');
return $variables;
}
}
+73
View File
@@ -0,0 +1,73 @@
<?php
/**
* @file classes/mail/traits/Recipient.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 Recipient
*
* @ingroup mail_traits
*
* @brief Mailable trait to set the recipients to an array of Users
*/
namespace PKP\mail\traits;
use BadMethodCallException;
use InvalidArgumentException;
use PKP\identity\Identity;
use PKP\mail\Mailable;
use PKP\mail\variables\RecipientEmailVariable;
trait Recipient
{
/**
* @copydoc Illuminate\Mail\Mailable::setAddress()
*
* @param null|mixed $name
*/
abstract protected function setAddress($address, $name = null, $property = 'to');
/**
* @copydoc Illuminate\Mail\Mailable::to()
*
* @param null|mixed $name
*/
public function to($address, $name = null)
{
throw new BadMethodCallException(static::class . ' doesn\'t support ' . __FUNCTION__ . '(), use recipients() instead');
}
/**
* Set recipients of the email and set values for related template variables
*
* @param Identity[] $recipients
* @param ?string $locale Optional. A locale key to use when setting the recipient names. Default: current locale
*/
public function recipients(array $recipients, ?string $locale = null): Mailable
{
$to = [];
foreach ($recipients as $recipient) {
if (!is_a($recipient, Identity::class)) {
throw new InvalidArgumentException('Expecting an array consisting of instances of ' . Identity::class . ' to be passed to ' . static::class . '::' . __FUNCTION__);
}
$to[] = [
'email' => $recipient->getEmail(),
'name' => $recipient->getFullName(true, false, $locale),
];
}
// Override the existing recipient data
$this->to = [];
$this->variables = array_filter($this->variables, function ($variable) {
return !is_a($variable, RecipientEmailVariable::class);
});
$this->setAddress($to);
$this->variables[] = new RecipientEmailVariable($recipients, $this);
return $this;
}
}
@@ -0,0 +1,145 @@
<?php
/**
* @file classes/mail/traits/ReviewerComments.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 ReviewerComments
*
* @ingroup mail_traits
*
* @brief Mailable trait to add reviewer comments to a mailable's variables
*/
namespace PKP\mail\traits;
use APP\submission\Submission;
use PKP\core\PKPString;
use PKP\db\DAORegistry;
use PKP\reviewForm\ReviewFormElementDAO;
use PKP\reviewForm\ReviewFormResponse;
use PKP\reviewForm\ReviewFormResponseDAO;
use PKP\submission\reviewAssignment\ReviewAssignment;
use PKP\submission\SubmissionComment;
use PKP\submission\SubmissionCommentDAO;
trait ReviewerComments
{
protected static string $allReviewerComments = 'allReviewerComments';
/**
* Add the reviewer comments to the list of registered variables
*/
protected static function addReviewerCommentsDescription(array $variables): array
{
$variables[static::$allReviewerComments] = __('emailTemplate.variable.allReviewersComments');
return $variables;
}
/**
* Add a variable with comments from all completed review assignments
*
* @param array<ReviewAssignment> $reviewAssignments
*/
protected function setupReviewerCommentsVariable(array $reviewAssignments, Submission $submission)
{
/** @var SubmissionCommentDAO $submissionCommentDao */
$submissionCommentDao = DAORegistry::getDAO('SubmissionCommentDAO');
$reviewerNumber = 0;
$comments = [];
foreach ($reviewAssignments as $reviewAssignment) {
$reviewerNumber++;
$submissionComments = $submissionCommentDao->getReviewerCommentsByReviewerId(
$submission->getId(),
$reviewAssignment->getReviewerId(),
$reviewAssignment->getId(),
true
);
$reviewerIdentity = $reviewAssignment->getReviewMethod() == ReviewAssignment::SUBMISSION_REVIEW_METHOD_OPEN
? $reviewAssignment->getReviewerFullName()
: __('submission.comments.importPeerReviews.reviewerLetter', ['reviewerLetter' => $reviewerNumber]);
$recommendation = $reviewAssignment->getLocalizedRecommendation();
$commentsBody = '';
/** @var SubmissionComment $comment */
while ($comment = $submissionComments->next()) {
// If the comment is viewable by the author, then add the comment.
if ($comment->getViewable()) {
$commentsBody .= PKPString::stripUnsafeHtml($comment->getComments());
}
}
$comments[] =
'<p>'
. '<strong>' . $reviewerIdentity . '</strong>'
. '<br>'
. __('submission.recommendation', ['recommendation' => $recommendation])
. '</p>'
. $commentsBody
. $this->getReviewFormComments($reviewAssignment);
}
$this->addData([
static::$allReviewerComments => join('', $comments),
]);
}
protected function getReviewFormComments(ReviewAssignment $reviewAssignment): string
{
if (!$reviewAssignment->getReviewFormId()) {
return '';
}
/** @var ReviewFormElementDAO $reviewFormElementDao */
$reviewFormElementDao = DAORegistry::getDAO('ReviewFormElementDAO');
$reviewFormElements = $reviewFormElementDao->getByReviewFormId($reviewAssignment->getReviewFormId());
if ($reviewFormElements->wasEmpty()) {
return '';
}
/** @var ReviewFormResponseDAO $reviewFormResponseDao */
$reviewFormResponseDao = DAORegistry::getDAO('ReviewFormResponseDAO');
$comments = '';
while ($reviewFormElement = $reviewFormElements->next()) {
if (!$reviewFormElement->getIncluded()) {
continue;
}
/** @var ReviewFormResponse|null $reviewFormResponse */
$reviewFormResponse = $reviewFormResponseDao->getReviewFormResponse($reviewAssignment->getId(), $reviewFormElement->getId());
if (!$reviewFormResponse) {
continue;
}
$comments .= PKPString::stripUnsafeHtml($reviewFormElement->getLocalizedQuestion());
$possibleResponses = $reviewFormElement->getLocalizedPossibleResponses();
// See issue #2437.
if (in_array($reviewFormElement->getElementType(), [$reviewFormElement::REVIEW_FORM_ELEMENT_TYPE_CHECKBOXES, $reviewFormElement::REVIEW_FORM_ELEMENT_TYPE_RADIO_BUTTONS])) {
ksort($possibleResponses);
$possibleResponses = array_values($possibleResponses);
}
if (in_array($reviewFormElement->getElementType(), $reviewFormElement->getMultipleResponsesElementTypes())) {
if ($reviewFormElement->getElementType() == $reviewFormElement::REVIEW_FORM_ELEMENT_TYPE_CHECKBOXES) {
$comments .= '<ul>';
foreach ($reviewFormResponse->getValue() as $value) {
$comments .= '<li>' . PKPString::stripUnsafeHtml($possibleResponses[$value]) . '</li>';
}
$comments .= '</ul>';
} else {
$comments .= '<p>' . PKPString::stripUnsafeHtml($possibleResponses[$reviewFormResponse->getValue()]) . '</p>';
}
} else {
$comments .= '<p>' . nl2br(htmlspecialchars($reviewFormResponse->getValue())) . '</p>';
}
}
return $comments;
}
}
+63
View File
@@ -0,0 +1,63 @@
<?php
/**
* @file classes/mail/traits/Sender.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 Sender
*
* @ingroup mail_traits
*
* @brief Mailable trait to set the sender to a User
*/
namespace PKP\mail\traits;
use BadMethodCallException;
use PKP\mail\Mailable;
use PKP\mail\variables\SenderEmailVariable;
use PKP\user\User;
trait Sender
{
/**
* User that sends mailable
*/
protected User $sender;
/**
* @copydoc Illuminate\Mail\Mailable::setAddress()
*
* @param null|mixed $name
*/
abstract protected function setAddress($address, $name = null, $property = 'to');
/**
* @copydoc Illuminate\Mail\Mailable::from()
*
* @param null|mixed $name
*/
public function from($address, $name = null)
{
throw new BadMethodCallException(static::class . ' doesn\'t support ' . __FUNCTION__ . '(), use sender() instead');
}
/**
* Set recipients of the email and set values for related template variables
*/
public function sender(User $sender, ?string $defaultLocale = null): Mailable
{
$this->setAddress($sender->getEmail(), $sender->getFullName(true, false, $defaultLocale), 'from');
$this->variables[] = new SenderEmailVariable($sender, $this);
$this->sender = $sender;
return $this;
}
public function getSenderUser(): User
{
return $this->sender;
}
}
+152
View File
@@ -0,0 +1,152 @@
<?php
/**
* @file classes/mail/traits/Unsubscribe.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 Unsubscribe
*
* @ingroup mail_traits
*
* @brief trait to embed footer with unsubscribe link to notification emails
*/
namespace PKP\mail\traits;
use APP\core\Application;
use APP\mail\variables\ContextEmailVariable;
use APP\notification\Notification;
use APP\notification\NotificationManager;
use Exception;
use Illuminate\Mail\Mailables\Headers;
use PKP\mail\Mailable;
use PKP\mail\variables\ContextEmailVariable as PKPContextEmailVariable;
trait Unsubscribe
{
protected static string $unsubscribeUrl = 'unsubscribeUrl';
// Notification to generate unsubscribe link
protected Notification $notification;
// Locale key used by default in the footer if none is specified by Mailable::setupUnsubscribeFooter()
protected string $defaultUnsubscribeLocaleKey = 'emails.footer.unsubscribe';
/**
* @var string[] template variables required for the unsubscribe footer
*/
protected static array $requiredVariables = [PKPContextEmailVariable::class];
abstract public function addData(array $data): Mailable;
abstract public function getVariables(): array;
/**
* Use this public method to set footer for this email
*/
public function allowUnsubscribe(Notification $notification): self
{
$this->notification = $notification;
if (!$this->hasRequiredVariables()) {
throw new Exception(
'Mailable should include the following template variables: ' .
implode(',', static::getRequiredVariables())
);
}
return $this;
}
/**
* Setup footer with unsubscribe link if notification is deliberately set with self::allowUnsubscribe()
*
* @param null|mixed $localeKey
*/
protected function setupUnsubscribeFooter(string $locale, $context, $localeKey = null): void
{
if (!isset($this->notification)) {
return;
}
$this->footer = $this->renameContextVariables($this->setFooterText($locale, $localeKey));
$notificationManager = new NotificationManager(); /** @var NotificationManager $notificationManager */
$request = Application::get()->getRequest();
$this->addData([
self::$unsubscribeUrl => $notificationManager->getUnsubscribeNotificationUrl(
$request,
$this->notification,
$context
),
]);
}
/**
* Adds unsubscribe headers, see pkp/pkp-lib#6627
*/
public function headers(): Headers
{
$unsubscribeUrl = $this->viewData[self::$unsubscribeUrl] ?? null;
return new Headers(
null,
[],
$unsubscribeUrl ? [
'List-Unsubscribe-Post' => 'List-Unsubscribe=One-Click',
'List-Unsubscribe' => '<' . $unsubscribeUrl . '>',
] : []
);
}
/**
* @return bool whether mailable contains variables requires for the footer
*/
protected function hasRequiredVariables(): bool
{
$included = [];
$requiredVariables = static::getRequiredVariables();
foreach ($requiredVariables as $requiredVariable) {
foreach ($this->getVariables() as $variable) {
if (is_a($variable, $requiredVariable, true)) {
$included[] = $requiredVariable;
break;
}
}
}
return count($included) === count($requiredVariables);
}
/**
* Replace email template variables in the locale string, so they correspond to the application,
* e.g., contextName => journalName/pressName/serverName
*/
protected function renameContextVariables(string $footer): string
{
$map = [
'{$' . PKPContextEmailVariable::CONTEXT_NAME . '}' => '{$' . ContextEmailVariable::CONTEXT_NAME . '}',
'{$' . PKPContextEmailVariable::CONTEXT_URL . '}' => '{$' . ContextEmailVariable::CONTEXT_URL . '}',
'{$' . PKPContextEmailVariable::CONTEXT_SIGNATURE . '}' => '{$' . ContextEmailVariable::CONTEXT_SIGNATURE . '}',
];
return str_replace(array_keys($map), array_values($map), $footer);
}
/**
* Set the message to be displayed in the footer
*/
protected function setFooterText(string $locale, string $localeKey = null): string
{
if (is_null($localeKey)) {
$localeKey = $this->defaultUnsubscribeLocaleKey;
}
return __($localeKey, [], $locale);
}
protected static function getRequiredVariables(): array
{
return static::$requiredVariables;
}
}