Skip to content

Commit

Permalink
ENH #16378: New Plugin Event: beforeTokenImport
Browse files Browse the repository at this point in the history
New plugin event implemented for CSV import.
  • Loading branch information
gabrieljenik committed Jul 15, 2020
1 parent f7b3918 commit 9a5e59d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
40 changes: 38 additions & 2 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 @@ -2148,7 +2148,42 @@ public function import($iSurveyId)
}
}

if (!$bDuplicateFound && !$bInvalidEmail && !$bInvalidToken) {
// Dispatch beforeTokenImport event
$params = array(
'csvcharset' => $sUploadCharset,
'filterduplicatetoken' => $bFilterDuplicateToken,
'filterblankemail' => $bFilterBlankEmail,
'allowinvalidemail' => $bAllowInvalidEmail,
'filterduplicatefields' => $aFilterDuplicateFields,
'separator' => $sSeparator,
'showwarningtoken' => Yii::app()->request->getPost('showwarningtoken'),
);
$event = new PluginEvent('beforeTokenImport');
$event->set('importType', 'CSV');
$event->set('surveyId', $iSurveyId);
$event->set('params', $params);
$event->set('recordCount', $iRecordCount);
$event->set('token', $aWriteArray);
App()->getPluginManager()->dispatchEvent($event);
$bPluginReportedError = !is_null($event->get('importValid')) && !$event->get('importValid');

if ($bPluginReportedError) {
// If plugin says import is not valid, append the error
$sErrorMessage = $event->get('errorMessage');
if (empty($sErrorMessage)) $sErrorMessage = gT("%s records with other errors");

$sTokenSpecificErrorMessage = $event->get('tokenSpecificErrorMessage');
if (!empty($sTokenSpecificErrorMessage)) {
$aPluginErrorMessageList[$sErrorMessage][] = $sTokenSpecificErrorMessage;
} else {
$aPluginErrorMessageList[$sErrorMessage][] = sprintf(gT("Line %s : %s %s (%s)"), $iRecordCount, $aWriteArray['firstname'], $aWriteArray['lastname'], $aWriteArray['email']);
}
} else {
// If plugin says import is OK, replace token data from the event
$aWriteArray = $event->get('token');
}

if (!$bDuplicateFound && !$bInvalidEmail && !$bInvalidToken && !$bPluginReportedError) {
// unset all empty value
foreach ($aWriteArray as $key => $value) {
if ($aWriteArray[$key] == "") {
Expand Down Expand Up @@ -2190,6 +2225,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
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

0 comments on commit 9a5e59d

Please sign in to comment.