Skip to content

Commit

Permalink
Replaced the handmade validator by FormValidatorArrayCustom and some …
Browse files Browse the repository at this point in the history
…minor fixes on the settings form.
  • Loading branch information
jonasraoni committed Feb 24, 2019
1 parent f275272 commit 7d1c4bb
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Once enabled, the plugin will send an email notification to a configurable list of emails whenever a new user gets registered.

## Configuration
Each journal must be configured individually since the settings are not shared.
Each journal/press must be configured individually since the settings are not shared.

### Recipients
The emails must be defined through the plugin settings, the interface supports adding a list of recipients.
Expand Down
29 changes: 19 additions & 10 deletions RegistrationNotificationSettingsForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,20 @@ public function __construct(RegistrationNotificationPlugin $plugin, $contextId)
$this->_plugin = $plugin;
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
import('lib.pkp.classes.validation.ValidatorEmail');
$emailValidator = new ValidatorEmail;
$this->addCheck(new FormValidatorArrayCustom($this, 'email', 'required', 'form.emailRequired', function ($value) use ($emailValidator) {
return $emailValidator->isValid($value);
}));
}

/**
* @copydoc Form::initData()
*/
public function initData() {
$recipientList = $this->_plugin->getSetting($this->_contextId, 'recipientList');
$this->setData('recipientList', is_array($recipientList) ? $recipientList : []);
$this->setData('email', is_array($recipientList) ? array_keys($recipientList) : []);
$this->setData('name', is_array($recipientList) ? array_values($recipientList) : []);
return parent::initData();
}

Expand All @@ -50,6 +56,17 @@ public function initData() {
*/
public function readInputData() {
$this->readUserVars(array('email', 'name'));
$emails = $this->getData('email');
$names = $this->getData('name');
foreach($emails as $i => $email) {
//clean empty entries
if(empty($email) && empty($names[$i])){
unset($emails[$i]);
unset($names[$i]);
}
}
$this->setData('email', array_values($emails));
$this->setData('name', array_values($names));
return parent::readInputData();
}

Expand All @@ -65,15 +82,7 @@ public function fetch($request) {
* @copydoc Form::fetch()
*/
public function execute() {
import('lib.pkp.classes.validation.ValidatorEmail');

$emailValidator = new ValidatorEmail;
$data = array_filter(
array_combine($this->getData('email'), $this->getData('name')),
[$emailValidator, 'isValid'],
ARRAY_FILTER_USE_KEY
);
$this->_plugin->updateSetting($this->_contextId, 'recipientList', $data, 'object');
$this->_plugin->updateSetting($this->_contextId, 'recipientList', array_combine($this->getData('email'), $this->getData('name')), 'object');
return parent::execute();
}
}
2 changes: 1 addition & 1 deletion locale/en_US/emailTemplates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<br /><strong>Email:</strong> {$userEmail}</a>
<br />
<br />
This email was generated by Open Journal Systems' Registration Notification plugin.]]></body>
This email was generated by the Public Knowledge Project's Registration Notification plugin.]]></body>
<description>This email template is used to notify that a new user has been registered.</description>
</email_text>
</email_texts>
65 changes: 38 additions & 27 deletions templates/settingsForm.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,64 @@
* Registration Notification plugin settings
*
*}
<div id="registrationNotificationSettings">
<div id="description">{translate key="plugins.generic.registrationNotification.description"}</div>

<h3>{translate key="navigation.settings"}</h3>

<script>
$(function() {ldelim}
function setValidation(container) {ldelim}
container.find('[name="email\[\]"]').attr("data-rule-email", "true");
{rdelim};
// Attach the form handler.
$('#registrationNotificationSettingsForm')
setValidation($('#registrationNotificationSettingsForm')
.on('click', '.remove-button', function() {ldelim}
$(this).parents('.section').remove();
{rdelim})
.on('click', '.insert-button', function() {ldelim}
var section = $(this).parents('.section');
var clone = section.clone();
section.find('.error').remove();
clone.find('[id]').each(function() {ldelim}
var
item = $(this),
oldId = item.attr('id'),
newId = 'id-' + Math.random().toString(36);
item.attr('id', newId);
clone.find('[for=' + oldId + ']').attr('for', newId);
{rdelim});
setValidation(clone);
section.find(':input:not(button)').val('');
clone.find('.insert-button')
.removeClass('insert-button pkp_button_primary')
.addClass('remove-button')
.text('{translate key="common.remove"}');
clone.insertBefore(section);
{rdelim})
.pkpHandler('$.pkp.controllers.form.AjaxFormHandler');
.pkpHandler('$.pkp.controllers.form.AjaxFormHandler'));
{rdelim});
</script>

<form class="pkp_form" id="registrationNotificationSettingsForm" method="post" action="{url router=$smarty.const.ROUTE_COMPONENT op="manage" category="generic" plugin=$pluginName verb="settings" save=true}">
{csrf}
{include file="controllers/notification/inPlaceNotification.tpl" notificationId="registrationNotificationSettingsFormNotification"}
<div id="registrationNotificationSettings">
<div id="description">{translate key="plugins.generic.registrationNotification.description"}</div>

<h3>{translate key="navigation.settings"}</h3>
{csrf}
{include file="controllers/notification/inPlaceNotification.tpl" notificationId="registrationNotificationSettingsFormNotification"}

{fbvFormArea id="registrationNotificationSettingsFormArea"}
{foreach from=$recipientList key=email item=name name=email}
{fbvFormSection}
{assign var="index" value=$smarty.foreach.email.index}
{fbvElement type="text" required="required" label="email.email" id="email-`$index`" name="email[]" value=$email inline=true size=$fbvStyles.size.SMALL}
{fbvElement type="text" label="common.name" id="name-`$index`" name="name[]" value=$name inline=true size=$fbvStyles.size.MEDIUM}
{fbvElement type="button" label="common.remove" id="remove-`$index`" inline=true class="default remove-button"}
{/fbvFormSection}
{/foreach}
{fbvFormSection}
{fbvElement type="text" required="required" label="email.email" id="new-email" name="email[]" inline=true size=$fbvStyles.size.SMALL}
{fbvElement type="text" label="common.name" id="new-name" name="name[]" inline=true size=$fbvStyles.size.MEDIUM}
{fbvElement type="button" label="common.more" id="insert" inline=true class="pkp_button_primary default insert-button"}
{/fbvFormSection}
{/fbvFormArea}
{fbvFormArea id="registrationNotificationSettingsFormArea"}
{foreach from=$email key=index item=value}
{fbvFormSection}
{fbvElement type="text" label="email.email" id="email-`$index`" name="email[]" value=$value inline=true size=$fbvStyles.size.SMALL}
{fbvElement type="text" label="common.name" id="name-`$index`" name="name[]" value=$name[$index] inline=true size=$fbvStyles.size.MEDIUM}
{fbvElement type="button" label="common.remove" id="remove-`$index`" inline=true class="default remove-button"}
{/fbvFormSection}
{/foreach}
{fbvFormSection}
{fbvElement type="text" label="email.email" id="new-email" name="email[]" inline=true size=$fbvStyles.size.SMALL}
{fbvElement type="text" label="common.name" id="new-name" name="name[]" inline=true size=$fbvStyles.size.MEDIUM}
{fbvElement type="button" label="common.more" id="insert" inline=true class="pkp_button_primary default insert-button"}
{/fbvFormSection}
{/fbvFormArea}

{fbvFormButtons}
<p><span class="formRequired">{translate key="common.requiredField"}</span></p>
{fbvFormButtons}
<p><span class="formRequired">{translate key="common.requiredField"}</span></p>
</div>
</form>
</div>
4 changes: 2 additions & 2 deletions version.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<version>
<application>registrationNotification</application>
<type>plugins.generic</type>
<release>1.0.0.1</release>
<date>2019-01-21</date>
<release>1.0.0.2</release>
<date>2019-02-24</date>
<lazy-load>1</lazy-load>
<class>RegistrationNotificationPlugin</class>
</version>

0 comments on commit 7d1c4bb

Please sign in to comment.