Skip to content

Commit

Permalink
Dev #T1114: fix missing state for custom surveytokenattributes in arc…
Browse files Browse the repository at this point in the history
…hived tables
  • Loading branch information
ptelu committed Sep 6, 2021
1 parent a936ae0 commit dfe1cc3
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 131 deletions.
1 change: 1 addition & 0 deletions application/controllers/SurveyAdministrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,7 @@ public function actionDeactivate()
$archivedTokenSettings->tbl_type = 'token';
$archivedTokenSettings->created = $DBDate;
$archivedTokenSettings->properties = $aData['aSurveysettings']['tokenencryptionoptions'];
$archivedTokenSettings->attributes = json_encode($aData['aSurveysettings']['attributedescriptions']);
$archivedTokenSettings->save();

$aData['tnewtable'] = $tnewtable;
Expand Down
174 changes: 88 additions & 86 deletions application/controllers/admin/tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -1146,11 +1146,11 @@ public function deletetokenattributes($iSurveyId)
*/
public function updatetokenattributedescriptions($iSurveyId)
{
$iSurveyId = (int) $iSurveyId;
$iSurveyId = (int)$iSurveyId;
$oSurvey = Survey::model()->findByPk($iSurveyId);
if (!Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'update') && !Permission::model()->hasSurveyPermission($iSurveyId, 'surveysettings', 'update')) {
Yii::app()->session['flashmessage'] = gT("You do not have permission to access this page.");
$this->getController()->redirect(array("/surveyAdministration/view/surveyid/{$iSurveyId}"));
$this->getController()->redirect(["/surveyAdministration/view/surveyid/{$iSurveyId}"]);
}

$languages = array_merge((array)$oSurvey->language, $oSurvey->additionalLanguages);
Expand All @@ -1164,9 +1164,9 @@ public function updatetokenattributedescriptions($iSurveyId)
foreach ($aMandatoryAttributes['columns'] as $column => $fieldname) {
$aOptionsBeforeChange[$column]['encrypted'] = $aMandatoryAttributes['columns'][$column];

$fieldcontents[$column] = [
'encrypted' => Yii::app()->request->getPost('encrypted_' . $column) == '1' ? 'Y' : 'N',
];
$fieldcontents[$column] = [
'encrypted' => Yii::app()->request->getPost('encrypted_' . $column) == '1' ? 'Y' : 'N',
];

$aOptionsAfterChange[$column]['encrypted'] = $fieldcontents[$column]['encrypted'];
$aTokenencryptionoptions['columns'][$column] = $fieldcontents[$column]['encrypted'];
Expand All @@ -1181,13 +1181,13 @@ public function updatetokenattributedescriptions($iSurveyId)
} else {
$aOptionsBeforeChange[$fieldname]['encrypted'] = 'N';
}
$fieldcontents[$fieldname] = [
'description' => strip_tags(Yii::app()->request->getPost('description_' . $fieldname)),
'mandatory' => Yii::app()->request->getPost('mandatory_' . $fieldname) == '1' ? 'Y' : 'N',
'encrypted' => Yii::app()->request->getPost('encrypted_' . $fieldname) == '1' ? 'Y' : 'N',
'show_register' => Yii::app()->request->getPost('show_register_' . $fieldname) == '1' ? 'Y' : 'N',
'cpdbmap' => Yii::app()->request->getPost('cpdbmap_' . $fieldname)
];
$fieldcontents[$fieldname] = [
'description' => strip_tags(Yii::app()->request->getPost('description_' . $fieldname)),
'mandatory' => Yii::app()->request->getPost('mandatory_' . $fieldname) == '1' ? 'Y' : 'N',
'encrypted' => Yii::app()->request->getPost('encrypted_' . $fieldname) == '1' ? 'Y' : 'N',
'show_register' => Yii::app()->request->getPost('show_register_' . $fieldname) == '1' ? 'Y' : 'N',
'cpdbmap' => Yii::app()->request->getPost('cpdbmap_' . $fieldname)
];
$aOptionsAfterChange[$fieldname]['encrypted'] = $fieldcontents[$fieldname]['encrypted'];
foreach ($languages as $language) {
$fieldNameValue = Yii::app()->request->getPost("caption_" . $fieldname . "_" . $language);
Expand All @@ -1204,10 +1204,10 @@ public function updatetokenattributedescriptions($iSurveyId)
$this->updateEncryption($iSurveyId, $aOptionsAfterChange);

// save token encryption options if everything was ok
Survey::model()->updateByPk($iSurveyId, array('attributedescriptions' => json_encode($fieldcontents)));
Survey::model()->updateByPk($iSurveyId, ['attributedescriptions' => json_encode($fieldcontents)]);

foreach ($languages as $language) {
$ls = SurveyLanguageSetting::model()->findByAttributes(array('surveyls_survey_id' => $iSurveyId, 'surveyls_language' => $language));
$ls = SurveyLanguageSetting::model()->findByAttributes(['surveyls_survey_id' => $iSurveyId, 'surveyls_language' => $language]);
$ls->surveyls_attributecaptions = !empty($captions[$language]) ? json_encode($captions[$language]) : '';
$ls->save();
}
Expand All @@ -1221,13 +1221,13 @@ public function updatetokenattributedescriptions($iSurveyId)


//admin/tokens/sa/browse/surveyid/652779//
$this->getController()->redirect(array("/admin/tokens/sa/managetokenattributes/surveyid/{$iSurveyId}"));
$this->getController()->redirect(["/admin/tokens/sa/managetokenattributes/surveyid/{$iSurveyId}"]);
}

/**
* Updates Token encryption settings based on $iSurveyId and configuration $aEncryptionSettings
* @param $iSurveyId
* @param $aEncryptionSettings
* @param string|int $iSurveyId
* @param array $aEncryptionSettings The new attribute encryption status
*/
public function updateEncryption($iSurveyId, $aEncryptionSettings)
{
Expand All @@ -1250,22 +1250,22 @@ public function updateEncryption($iSurveyId, $aEncryptionSettings)
$aEncryptionSettings[$column] = [
'encrypted' => $aEncryptionSettings[$column]['encrypted'] === 'Y' ? 'Y' : 'N',
];

$aTokenencryptionoptions['columns'][$column] = $aEncryptionSettings[$column]['encrypted'];
}

// find custom attribute column names
$aCustomAttributes = getAttributeFieldNames($iSurveyId);
// custom attributes
$aCustomAttributes = $oSurvey->getTokenAttributes();
foreach ($aCustomAttributes as $column => $attributeName) {
if (isset(json_decode($oSurvey->attributedescriptions)->$attributeName->encrypted)) {
$aEncryptionSettingsOld[$attributeName]['encrypted'] = json_decode($oSurvey->attributedescriptions)->$attributeName->encrypted;
if (isset($attributeName['encrypted'])) {
$aEncryptionSettingsOld[$column]['encrypted'] = $attributeName['encrypted'];
} else {
$aEncryptionSettingsOld[$attributeName]['encrypted'] = 'N';
$aEncryptionSettingsOld[$column]['encrypted'] = 'N';
}
if (isset($aEncryptionSettings[$column])) {
$aEncryptionSettings[$column] = [
'encrypted' => $aEncryptionSettings[$column]['encrypted'] === 'Y' ? 'Y' : 'N',
];
}
$aEncryptionSettings[$attributeName] = [
'encrypted' => $aEncryptionSettings[$attributeName]['encrypted'] === 'Y' ? 'Y' : 'N',
];
}

foreach ($oTokens as $token) {
Expand Down Expand Up @@ -2334,6 +2334,7 @@ public function kill($iSurveyId)
$archivedTokenSettings->tbl_type = 'token';
$archivedTokenSettings->created = $DBDate;
$archivedTokenSettings->properties = $aData['thissurvey']['tokenencryptionoptions'];
$archivedTokenSettings->attributes = json_encode($aData['thissurvey']['attributedescriptions']);
$archivedTokenSettings->save();

//Remove any survey_links to the CPDB
Expand Down Expand Up @@ -2547,82 +2548,83 @@ private function _newtokentable($iSurveyId)
)), $aData);
} elseif (returnGlobal('restoretable') === "Y" && Yii::app()->request->getPost('oldtable')) {
/* Restore a previously deleted tokens table */
//Rebuild attributedescription value for the surveys table
$archivedTable = Yii::app()->request->getPost('oldtable');
$table = Yii::app()->db->schema->getTable("{{" . $archivedTable . "}}");
$fields = array_filter(array_keys($table->columns), 'filterForAttributes');
$fieldcontents = $aSurveyInfo['attributedescriptions'];
if (!is_array($fieldcontents)) {
$fieldcontents = [];
}
foreach ($fields as $fieldname) {
$name = $fieldname;
if ($fieldname[10] === 'c') {
//This belongs to a cpdb attribute
$cpdbattid = substr($fieldname, 15);
$data = ParticipantAttributeName::model()->getAttributeName($cpdbattid, Yii::app()->session['adminlang']);
$name = $data['attribute_name'];
}
if (!isset($fieldcontents[$fieldname])) {
$fieldcontents[$fieldname] = [
'description' => $name,
'mandatory' => 'N',
'encrypted' => 'N',
'show_register' => 'N'
];
}
}

// Set the $tokenencryptionoptions from the encryption column in ArchivedTableSettings tokens_282267_20210422115937
$archivedTableSettings = ArchivedTableSettings::model()->findByAttributes(['tbl_name' => $archivedTable]);
$tokenencryptionoptions = $tokenencryptionoptionsOld = ls_json_encode($aTokenencryptionoptions);
if ($archivedTableSettings->properties) {

// Set the $tokenencryptionoptions from the encryption column in ArchivedTableSettings
if (isset($archivedTableSettings)) {
$tokenencryptionoptions = $archivedTableSettings->properties;
}
// if the encryption status is unknown
$archivedTableSettingsArray = json_decode($archivedTableSettings->properties, true);
foreach ($archivedTableSettingsArray as $archivedTableSetting) {
if ($archivedTableSetting === 'unknown') {
$tokenencryptionoptions = $tokenencryptionoptionsOld;
$tokenencryptionoptionsOld = $aTokenencryptionoptions;
$tokenencryptionoptionsArray = json_decode($tokenencryptionoptions, true);
foreach ($tokenencryptionoptionsArray as $tokenencryptionoption) {
// if the encryption status is unknown
if ($tokenencryptionoption === 'unknown') {
$tokenencryptionoptions = json_encode($tokenencryptionoptionsOld);
$tokenencryptionoptionsArray = $tokenencryptionoptionsOld;
}
}
}

$oDB = Yii::app()->db;
$oTransaction = $oDB->beginTransaction();
try {
Survey::model()->updateByPk($iSurveyId, ['attributedescriptions' => json_encode($fieldcontents), 'tokenencryptionoptions' => $tokenencryptionoptions]);
Yii::app()->db->createCommand()->renameTable("{{{$archivedTable}}}", "{{tokens_" . (int)$iSurveyId . "}}");
$archivedTableSettings->delete();
// Refresh schema cache just in case the table existed in the past
Yii::app()->db->schema->getTable("{{tokens_" . (int)$iSurveyId . "}}", true);

foreach ($aTokenencryptionoptions['columns'] as $column => $fieldname) {
$aEncryptionSettings[$column]['encrypted'] = $fieldname;
$attributedescriptions = $archivedTableSettings->attributes;
$attributedescriptionsOld = $aSurveyInfo['attributedescriptions'];
$attributedescriptionsArray = json_decode($attributedescriptions, true);
foreach ($attributedescriptionsArray as $attributedescription) {
// if the encryption status is unknown
if ($attributedescription === 'unknown') {
$attributedescriptions = json_encode($attributedescriptionsOld);
$attributedescriptionsArray = $attributedescriptionsOld;
}
}
$this->updateEncryption($iSurveyId, $aEncryptionSettings);

//Add any survey_links from the renamed table
SurveyLink::model()->rebuildLinksFromTokenTable($iSurveyId);
$oTransaction->commit();
$oDB = Yii::app()->db;
$oTransaction = $oDB->beginTransaction();
try {
$aEncryptionSettings = [];
foreach ($tokenencryptionoptionsArray['columns'] as $column => $encrypted) {
$aEncryptionSettings[$column]['encrypted'] = $encrypted;
}
foreach ($attributedescriptionsArray as $attribute => $description) {
$aEncryptionSettings[$attribute]['encrypted'] = $description['encrypted'];
}
Yii::app()->db->createCommand()->renameTable("{{{$archivedTable}}}", "{{tokens_" . (int)$iSurveyId . "}}");
$archivedTableSettings->delete();
$this->updateEncryption($iSurveyId, $aEncryptionSettings);
Survey::model()->updateByPk($iSurveyId, ['attributedescriptions' => $attributedescriptions]);
// Refresh schema cache just in case the table existed in the past
Yii::app()->db->schema->getTable("{{tokens_" . (int)$iSurveyId . "}}", true);
//Add any survey_links from the renamed table
SurveyLink::model()->rebuildLinksFromTokenTable($iSurveyId);
$oTransaction->commit();

$this->_renderWrappedTemplate(
'token',
[
'message' => [
'title' => gT("Import old participant table"),
'message' => gT("A survey participants table has been created for this survey and the old participants were imported.") . " (\"" . Yii::app()->db->tablePrefix . "tokens_$iSurveyId" . "\")<br /><br />\n"
. "<input type='submit' class='btn btn-default' value='"
. gT("Continue") . "' onclick=\"window.open('" . $this->getController()->createUrl("admin/tokens/sa/index/surveyid/$iSurveyId") . "', '_top')\" />\n"
]
],
$aData
);
} catch (\Exception $e) {
$oTransaction->rollback();
return;
}

LimeExpressionManager::SetDirtyFlag(); // so that knows that survey participants tables have changed
} else {
$this->_renderWrappedTemplate(
'token',
[
'message' => [
'title' => gT("Import old participant table"),
'message' => gT("A survey participants table has been created for this survey and the old participants were imported.") . " (\"" . Yii::app()->db->tablePrefix . "tokens_$iSurveyId" . "\")<br /><br />\n"
. "<input type='submit' class='btn btn-default' value='"
. gT("Continue") . "' onclick=\"window.open('" . $this->getController()->createUrl("admin/tokens/sa/index/surveyid/$iSurveyId") . "', '_top')\" />\n"
'class' => 'message-box-error',
'title' => gT("Import failed"),
'message' => gT("There are no matching settings to start the restore of the tokens table Error: 1628493792215")
]
],
$aData
);
} catch (\Exception $e) {
$oTransaction->rollback();
return;
}

LimeExpressionManager::SetDirtyFlag(); // so that knows that survey participants tables have changed
} else {
Yii::app()->loadHelper('database');
Survey::model()->updateByPk($iSurveyId, array('tokenencryptionoptions' => ls_json_encode($aTokenencryptionoptions)));
Expand Down

0 comments on commit dfe1cc3

Please sign in to comment.