Skip to content

Commit

Permalink
New feature #16378: New Plugin Event: beforeTokenImport
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Oct 23, 2020
2 parents 926bfe9 + 52b28cb commit 509e2f1
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 3 deletions.
93 changes: 90 additions & 3 deletions application/controllers/admin/tokens.php
Expand Up @@ -58,7 +58,7 @@ public function index($surveyid)

// CHECK TO SEE IF A Survey participants table EXISTS FOR THIS SURVEY
if (!$survey->hasTokensTable) {
//If no tokens table exists
//If no tokens table exists
$this->_newtokentable($iSurveyId);
} else {
$aData['thissurvey'] = $thissurvey;
Expand Down Expand Up @@ -2166,7 +2166,30 @@ public function import($iSurveyId)
}
}

if (!$bDuplicateFound && !$bInvalidEmail && !$bInvalidToken) {
// Dispatch beforeTokenImport event
$aOptions = array(
'csvcharset' => $sUploadCharset,
'filterduplicatetoken' => $bFilterDuplicateToken,
'filterblankemail' => $bFilterBlankEmail,
'allowinvalidemail' => $bAllowInvalidEmail,
'filterduplicatefields' => $aFilterDuplicateFields,
'separator' => $sSeparator,
'showwarningtoken' => Yii::app()->request->getPost('showwarningtoken'),
);
$aEventResult = $this->dispatchBeforeTokenImport($iSurveyId, $aWriteArray, $iRecordCount, $aOptions);
$bPluginReportedError = $aEventResult['pluginReportedError'];
$bImportDone = $aEventResult['importDone'];

if ($bPluginReportedError) {
// If plugin says import is not valid, append the error
$sErrorMessage = $aEventResult['errorMessage'];
$aPluginErrorMessageList[$sErrorMessage][] = $aEventResult['tokenSpecificErrorMessage'];
} else {
// If plugin says import is OK, replace token data from the event
$aWriteArray = $aEventResult['token'];
}

if (!$bDuplicateFound && !$bInvalidEmail && !$bInvalidToken && !$bPluginReportedError && !$bImportDone) {
// unset all empty value
foreach ($aWriteArray as $key => $value) {
if ($aWriteArray[$key] == "") {
Expand All @@ -2190,9 +2213,12 @@ public function import($iSurveyId)
$errors = ($oToken->getErrors());
$aModelErrorList[] = sprintf(gT("Line %s : %s"), $iRecordCount, print_r($errors, true));
} else {
$iRecordImported++;
$bImportDone = true;
}
}
if ($bImportDone) {
$iRecordImported++;
}
$iRecordOk++;
}
$iRecordCount++;
Expand All @@ -2208,6 +2234,7 @@ public function import($iSurveyId)
$aData['aInvalidTokenList'] = $aInvalidTokenList;
$aData['aInvalidFormatList'] = $aInvalidFormatList;
$aData['aInvalidEmailList'] = $aInvalidEmailList;
$aData['aPluginErrorMessageList'] = $aPluginErrorMessageList;
$aData['aModelErrorList'] = $aModelErrorList;
$aData['iInvalidEmailCount'] = $iInvalidEmailCount;
$aData['thissurvey'] = getSurveyInfo($iSurveyId);
Expand Down Expand Up @@ -2787,4 +2814,64 @@ protected function tokenIsSetInEmailCache($iSurveyId, $tid, $bIsInvitation)
&& isset($_SESSION[$cacheName][$sType][$tid])
&& $_SESSION[$cacheName][$sType][$tid] > 0;
}

/**
* Dispatches the beforeTokenImport event
* @param int $iSurveyId
* @param array $aToken
* @param int $iRecordCount
* @param array $aOptions
* @return array
*/
protected function dispatchBeforeTokenImport($iSurveyId, $aToken, $iRecordCount, $aOptions)
{
$aParams = array_merge(
array(
'csvcharset' => 'UTF-8',
'filterduplicatetoken' => false,
'filterblankemail' => true,
'allowinvalidemail' => false,
'filterduplicatefields' => array(),
'separator' => ',',
'showwarningtoken' => true,
),
$aOptions
);
$event = new PluginEvent('beforeTokenImport');
$event->set('importType', 'CSV');
$event->set('surveyId', $iSurveyId);
$event->set('params', $aParams);
$event->set('recordCount', $iRecordCount);
$event->set('token', $aToken);
$event->set('importDone', false);
$event->set('importValid', true);
App()->getPluginManager()->dispatchEvent($event);

$bPluginReportedError = !$event->get('importValid');
$bImportDone = $event->get('importDone');

$sTokenSpecificErrorMessage = '';
$sErrorMessage = '';
if ($bPluginReportedError) {
$sErrorMessage = $event->get('errorMessage');
if (empty($sErrorMessage)) {
$sErrorMessage = gT("%s records with other errors");
}

$sTokenSpecificErrorMessage = $event->get('tokenSpecificErrorMessage');
if (empty($sTokenSpecificErrorMessage)) {
$sTokenSpecificErrorMessage = sprintf(gT("Line %s : %s %s (%s)"), $iRecordCount, $aToken['firstname'], $aToken['lastname'], $aToken['email']);
}
}

$aResult = array(
'pluginReportedError' => $bPluginReportedError,
'importDone' => $bImportDone,
'errorMessage' => $sErrorMessage,
'tokenSpecificErrorMessage' => $sTokenSpecificErrorMessage,
'token' => $event->get('token')
);

return $aResult;
}
}
19 changes: 19 additions & 0 deletions application/views/admin/token/csvimportresult.php
Expand Up @@ -41,6 +41,7 @@
!empty($aInvalidFormatList) ||
!empty($aInvalidEmailList) ||
!empty($aModelErrorList) ||
!empty($aPluginErrorMessageList) ||
!empty($aInvalideAttrFieldName) ||
!empty($aMissingAttrFieldName)) { ?>
<h2 class='text-warning'><?php eT('Warnings'); ?></h2>
Expand Down Expand Up @@ -101,6 +102,24 @@
</li>
<?php } ?>

<?php if (!empty($aPluginErrorMessageList)) {
$iPluginErrorIndex=0;
foreach ($aPluginErrorMessageList as $sPluginErrorMessage => $aTokenSpecificErrorList) {
$iPluginErrorIndex++; ?>
<li>
<?php printf($sPluginErrorMessage, count($aTokenSpecificErrorList)); ?>
[<a href='#' onclick='$("#pluginerrorlist-<?=$iPluginErrorIndex?>").toggle();'><?php eT("List"); ?></a>]
<div class='badtokenlist well' id='pluginerrorlist-<?=$iPluginErrorIndex?>' style='display: none;'>
<ul class="list-unstyled">
<?php foreach ($aTokenSpecificErrorList as $sTokenSpecificErrorMessage) { ?>
<li><?php echo $sTokenSpecificErrorMessage; ?></li>
<?php } ?>
</ul>
</div>
</li>
<?php }
} ?>

<?php if (!empty($aModelErrorList)) { ?>
<li>
<?php printf(gT("%s records with other invalid information"), count($aModelErrorList)); ?>
Expand Down
38 changes: 38 additions & 0 deletions plugins/Demo/BeforeTokenImportDemo/BeforeTokenImportDemo.php
@@ -0,0 +1,38 @@
<?php
/**
* BeforeTokenImportDemo Plugin
*
* Demo plugin for beforeTokenImport event
*/
class BeforeTokenImportDemo extends \PluginBase
{
static protected $description = 'BeforeTokenImportDemo';
static protected $name = 'BeforeTokenImportDemo';

public function init()
{
$this->subscribe('beforeTokenImport');
}

public function beforeTokenImport()
{
$oEvent = $this->event;

// Retrieve token data from event
$tokenData = $oEvent->get('token');

// Reject tokens without Last Name
if (empty($tokenData['lastname'])) {
$oEvent->set('errorMessage', '%s records with empty Last Name ignored');
$oEvent->set('tokenSpecificErrorMessage', "Record ".$oEvent->get('recordCount')." doesn't have a last name");
$oEvent->set('importValid', false);
return;
}

// Replace Last Name by its first letter
$tokenData['lastname'] = strtoupper(substr($tokenData['lastname'], 0, 1));
$oEvent->set('token', $tokenData);
$oEvent->set('importValid', true);
}

}

0 comments on commit 509e2f1

Please sign in to comment.