Skip to content

Commit

Permalink
Closes #3193 Added user email templates to the discussion and grouped…
Browse files Browse the repository at this point in the history
… user templates in the assign participant.
  • Loading branch information
jonasraoni committed Feb 11, 2021
1 parent 2e9f5e0 commit 68a2a6e
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 34 deletions.
26 changes: 24 additions & 2 deletions controllers/grid/queries/QueriesGridHandler.inc.php
Expand Up @@ -37,7 +37,7 @@ function __construct() {
array('fetchGrid', 'fetchRow', 'readQuery', 'participants', 'addQuery', 'editQuery', 'updateQuery', 'deleteQuery'));
$this->addRoleAssignment(
array(ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR, ROLE_ID_ASSISTANT),
array('openQuery', 'closeQuery', 'saveSequence'));
array('openQuery', 'closeQuery', 'saveSequence', 'fetchTemplateBody'));
$this->addRoleAssignment(
array(ROLE_ID_MANAGER),
array('leaveQuery'));
Expand Down Expand Up @@ -571,7 +571,6 @@ function updateQuery($args, $request) {
function leaveQuery($args, $request) {
$queryId = $args['queryId'];
$user = $request->getUser();
$context = $request->getContext();
if ($user && $this->_getCurrentUserCanLeave($queryId)) {
$queryDao = DAORegistry::getDAO('QueryDAO'); /* @var $queryDao QueryDAO */
$queryDao->removeParticipant($queryId, $user->getId());
Expand Down Expand Up @@ -600,7 +599,30 @@ function _getCurrentUserCanLeave($queryId) {
}
$user = Application::get()->getRequest()->getUser();
return in_array($user->getId(), $participantIds);
}

/**
* Fetches an email template's message body.
* @param array $args
* @param PKPRequest $request
* @return JSONMessage JSON object
*/
public function fetchTemplateBody(array $args, PKPRequest $request) : JSONMessage {
$templateId = $request->getUserVar('template');
import('lib.pkp.classes.mail.SubmissionMailTemplate');
$template = new SubmissionMailTemplate($this->getSubmission(), $templateId);
if ($template) {
$user = $request->getUser();
$template->assignParams([
'editorialContactSignature' => $user->getContactSignature(),
'signatureFullName' => $user->getFullname(),
]);
$template->replaceParams();
return new JSONMessage(
true,
['body' => $template->getBody()]
);
}
}
}

Expand Down
37 changes: 34 additions & 3 deletions controllers/grid/queries/form/QueryForm.inc.php
Expand Up @@ -170,6 +170,7 @@ function initData() {
'subject' => $headNote?$headNote->getTitle():null,
'comment' => $headNote?$headNote->getContents():null,
'userIds' => $queryDao->getParticipantIds($query->getId()),
'template' => null,
);
} else {
// set intial defaults for queries.
Expand Down Expand Up @@ -198,12 +199,42 @@ function fetch($request, $template = null, $display = false, $actionArgs = array
'noteId' => $headNote->getId(),
'actionArgs' => $actionArgs,
'csrfToken' => $request->getSession()->getCSRFToken(),
'stageId' => $this->getStageId(),
'assocId' => $query->getAssocId(),
'assocType' => $query->getAssocType(),
));

// Queryies only support ASSOC_TYPE_SUBMISSION so far
if ($query->getAssocType() == ASSOC_TYPE_SUBMISSION) {
/** @var Submission */
$submission = Services::get('submission')->get($query->getAssocId());

$stageAssignmentDao = DAORegistry::getDAO('StageAssignmentDAO'); /* @var $stageAssignmentDao StageAssignmentDAO */
// All stages can select the default template
$templateKeys = [];
// Determine if the current user can use any custom templates defined.
$user = $request->getUser();
if (Services::get('user')->userHasRole($user->getId(), [ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR, ROLE_ID_ASSISTANT], $context->getId())) {
$emailTemplates = Services::get('emailTemplate')->getMany([
'contextId' => $context->getId(),
'isCustom' => true,
]);
$templateKeys[] = 'NOTIFICATION_CENTER_DEFAULT';
foreach ($emailTemplates as $emailTemplate) {
$templateKeys[] = $emailTemplate->getData('key');
}
}

import('lib.pkp.classes.mail.SubmissionMailTemplate');
$templates = [];
foreach ($templateKeys as $templateKey) {
$mailTemplate = new SubmissionMailTemplate($submission, $templateKey);
$mailTemplate->assignParams([]);
$mailTemplate->replaceParams();
$templates[$templateKey] = $mailTemplate->getSubject();
}
$templateMgr->assign('templates', $templates);

$stageAssignmentDao = DAORegistry::getDAO('StageAssignmentDAO'); /** @var StageAssignmentDAO $stageAssignmentDao */

// Get currently selected participants in the query
$queryDao = DAORegistry::getDAO('QueryDAO'); /* @var $queryDao QueryDAO */
Expand All @@ -218,8 +249,7 @@ function fetch($request, $template = null, $display = false, $actionArgs = array
if ($query->getStageId() == WORKFLOW_STAGE_ID_EXTERNAL_REVIEW || $query->getStageId() == WORKFLOW_STAGE_ID_INTERNAL_REVIEW) {

// Get all review assignments for current submission
$reviewAssignmentDao = DAORegistry::getDAO('ReviewAssignmentDAO'); /* @var $reviewAssignmentDao ReviewAssignmentDAO */
$reviewAssignments = $reviewAssignmentDao->getBySubmissionId($query->getAssocId());
$reviewAssignments = Services::get('submission')->getReviewAssignments($submission);

// Get current users roles
$assignedRoles = [];
Expand Down Expand Up @@ -320,6 +350,7 @@ function readInputData() {
'subject',
'comment',
'users',
'template',
));
}

Expand Down
Expand Up @@ -60,42 +60,39 @@ function __construct($itemId, $itemType, $stageId, $template = null) {
* @copydoc Form::fetch()
*/
function fetch($request, $template = null, $display = false) {
$submissionDao = DAORegistry::getDAO('SubmissionDAO'); /* @var $submissionDao SubmissionDAO */
$submission = $submissionDao->getById($this->_submissionId);
/** @var Submission $submissionDao */
$submission = Services::get('submission')->get($this->_submissionId);

// All stages can choose the default template
$templateKeys = array('NOTIFICATION_CENTER_DEFAULT');

// Determine if the current user can use any custom templates defined.
$user = $request->getUser();
$roleDao = DAORegistry::getDAO('RoleDAO'); /* @var $roleDao RoleDAO */
$userRoles = $roleDao->getByUserId($user->getId(), $submission->getData('contextId'));
foreach ($userRoles as $userRole) {
if (in_array($userRole->getId(), array(ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR, ROLE_ID_ASSISTANT))) {
$emailTemplatesIterator = Services::get('emailTemplate')->getMany([
'contextId' => $submission->getData('contextId'),
'isCustom' => true,
]);
$customTemplateKeys = [];
foreach ($emailTemplatesIterator as $emailTemplate) {
$customTemplateKeys[] = $emailTemplate->getData('key');
}
$templateKeys = array_merge($templateKeys, $customTemplateKeys);
break;
$customTemplateKeys = [];
if (Services::get('user')->userHasRole($user->getId(), [ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR, ROLE_ID_ASSISTANT], $submission->getData('contextId'))) {
$emailTemplates = Services::get('emailTemplate')->getMany([
'contextId' => $submission->getData('contextId'),
'isCustom' => true,
]);
foreach ($emailTemplates as $emailTemplate) {
$customTemplateKeys[] = $emailTemplate->getData('key');
}
}

$stageTemplates = $this->_getStageTemplates();
$currentStageId = $this->getStageId();
if (array_key_exists($currentStageId, $stageTemplates)) {
$templateKeys = array_merge($templateKeys, $stageTemplates[$currentStageId]);
}
$templates = array();
foreach ($templateKeys as $templateKey) {
$thisTemplate = $this->_getMailTemplate($submission, $templateKey);
$thisTemplate->assignParams(array());
$thisTemplate->replaceParams();
$templates[$templateKey] = $thisTemplate->getSubject();
$templateKeys = array_merge($templateKeys, $stageTemplates[$currentStageId] ?? []);

$templateKeyToSubject = function ($templateKey) use ($submission) {
$mailTemplate = $this->_getMailTemplate($submission, $templateKey);
$mailTemplate->assignParams([]);
$mailTemplate->replaceParams();
return $mailTemplate->getSubject();
};

$templates = array_map($templateKeyToSubject, $templateKeys);
if (count($customTemplateKeys)) {
$templates[__('manager.emails.otherTemplates')] = array_map($templateKeyToSubject, $customTemplateKeys);
}

$templateMgr = TemplateManager::getManager($request);
Expand Down Expand Up @@ -154,8 +151,6 @@ function sendMessage($userId, $submission, $request) {
$email = $this->_getMailTemplate($submission, $template, false);
$email->setReplyTo($fromUser->getEmail(), $fromUser->getFullName());

$dispatcher = $request->getDispatcher();

$userDao = DAORegistry::getDAO('UserDAO'); /* @var $userDao UserDAO */
$user = $userDao->getById($userId);
if (isset($user)) {
Expand Down
123 changes: 123 additions & 0 deletions js/controllers/grid/queries/QueryFormHandler.js
@@ -0,0 +1,123 @@
/**
* @defgroup js_controllers_grid_queries
*/
/**
* @file js/controllers/grid/queries/QueryFormHandler.js
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2000-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ReadQueryHandler
* @ingroup js_controllers_grid_queries
*
* @brief Handler for a query form modal
*
*/
(function($) {

/** @type {Object} */
$.pkp.controllers.grid.queries =
$.pkp.controllers.grid.queries || {};



/**
* @constructor
*
* @extends $.pkp.controllers.form.CancelActionAjaxFormHandler
*
* @param {jQueryObject} $form The query form element
* @param {Object} options non-default Dialog options
* to be passed into the dialog widget.
*
* Options are:
* - all options documented for the CancelActionAjaxFormHandler.
* - templateUrl: The URL to retrieve templates from.
*/
$.pkp.controllers.grid.queries.QueryFormHandler =
function($form, options) {
this.parent($form, options);

// Set the URL to retrieve templates from.
if (options.templateUrl) {
this.templateUrl_ = options.templateUrl;
}

this.textSubject_ = null;

// Attach form elements events.
$form.find('#template').change(
this.callbackWrapper(this.selectTemplateHandler_));
};
$.pkp.classes.Helper.inherits($.pkp.controllers.grid.queries.
QueryFormHandler, $.pkp.controllers.form.CancelActionAjaxFormHandler);


//
// Private properties
//
/**
* The URL to use to retrieve template bodies
* @private
* @type {string?}
*/
$.pkp.controllers.grid.queries.
QueryFormHandler.prototype.templateUrl_ = null;


//
// Private methods
//
/**
* Respond to an "item selected" call by triggering a published event.
*
* @param {HTMLElement} sourceElement The element that
* issued the event.
* @param {Event} event The triggering event.
* @private
*/
$.pkp.controllers.grid.queries.
QueryFormHandler.prototype.selectTemplateHandler_ =
function(sourceElement, event) {
var $form = this.getHtmlElement();
var template = $form.find('[name="template"]');
$form.find('[name="subject"]').val(template.find('option:selected').text());
this.textSubject_
$.post(this.templateUrl_, template.serialize(),
this.callbackWrapper(this.updateTemplate), 'json');
};


//
// Private methods
//
/**
* Internal callback to replace the textarea with the contents of the
* template body.
*
* @param {HTMLElement} formElement The wrapped HTML form.
* @param {Object} jsonData The data returned from the server.
* @return {boolean} The response status.
*/
$.pkp.controllers.grid.queries.
QueryFormHandler.prototype.updateTemplate =
function(formElement, jsonData) {

var $form = this.getHtmlElement(),
processedJsonData = this.handleJson(jsonData),
jsonDataContent =
/** @type {{variables: Object, body: string}} */ (jsonData.content),
$textarea = $form.find('textarea[name="comment"]'),
editor =
tinyMCE.EditorManager.get(/** @type {string} */ ($textarea.attr('id')));

if (jsonDataContent.variables) {
$textarea.attr('data-variables', JSON.stringify(jsonDataContent.variables));
}
editor.setContent(jsonDataContent.body);

return processedJsonData.status;
};

}(jQuery));
3 changes: 3 additions & 0 deletions locale/en_US/manager.po
Expand Up @@ -215,6 +215,9 @@ msgstr "Email Template"
msgid "manager.emails.emailTemplates"
msgstr "Email Templates"

msgid "manager.emails.otherTemplates"
msgstr "Other Templates"

msgid "manager.emails.emailTemplate.contextRequired"
msgstr "You must provide a context ID when adding an email template."

Expand Down
13 changes: 11 additions & 2 deletions templates/controllers/grid/queries/form/queryForm.tpl
Expand Up @@ -16,9 +16,10 @@
// Attach the handler.
$(function() {ldelim}
$('#queryForm').pkpHandler(
'$.pkp.controllers.form.CancelActionAjaxFormHandler',
'$.pkp.controllers.grid.queries.QueryFormHandler',
{ldelim}
cancelUrl: {if $isNew}'{url|escape:javascript op="deleteQuery" queryId=$queryId csrfToken=$csrfToken params=$actionArgs escape=false}'{else}null{/if}
cancelUrl: {if $isNew}'{url|escape:javascript op="deleteQuery" queryId=$queryId csrfToken=$csrfToken params=$actionArgs escape=false}'{else}null{/if},
templateUrl: {url|json_encode router=$smarty.const.ROUTE_COMPONENT component='grid.queries.QueriesGridHandler' op='fetchTemplateBody' stageId=$stageId submissionId=$assocId escape=false},
{rdelim}
);
{rdelim});
Expand All @@ -35,6 +36,14 @@
{/foreach}
{/fbvFormSection}

{if count($templates)}
{fbvFormArea id="queryTemplateArea"}
{fbvFormSection title="stageParticipants.notify.chooseMessage" for="template" size=$fbvStyles.size.medium}
{fbvElement type="select" from=$templates translate=false id="template" selected=$template defaultValue="" defaultLabel=""}
{/fbvFormSection}
{/fbvFormArea}
{/if}

{fbvFormArea id="queryContentsArea"}
{fbvFormSection title="common.subject" for="subject" required="true"}
{fbvElement type="text" id="subject" value=$subject required="true"}
Expand Down

0 comments on commit 68a2a6e

Please sign in to comment.