getClientScript()->registerScriptFile(App()->getConfig('adminscripts').'tokens.js', LSYii_ClientScript::POS_BEGIN);
$iSurveyId = $surveyid;
$iSurveyId = (int) $iSurveyId;
$survey = Survey::model()->findByPk($iSurveyId);
//// TODO : check if it does something different than the model function
$thissurvey = getSurveyInfo($iSurveyId);
if (!Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'read') && !Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'create') && !Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'update')
&& !Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'export') && !Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'import')
&& !Permission::model()->hasSurveyPermission($iSurveyId, 'surveysettings', 'update')
) {
Yii::app()->session['flashmessage'] = gT("You do not have permission to access this page.");
$this->getController()->redirect(array("/admin/survey/sa/view/surveyid/{$iSurveyId}"));
}
Yii::app()->loadHelper("surveytranslator");
$aData = array();
$aData['surveyprivate'] = $thissurvey['anonymized'];
$aData['title_bar']['title'] = $survey->currentLanguageSettings->surveyls_title." (".gT("ID").":".$iSurveyId.")";
$aData['sidemenu']["token_menu"] = true;
$aData['token_bar']['buttons']['view'] = true;
// CHECK TO SEE IF A Survey participants table EXISTS FOR THIS SURVEY
if (!$survey->hasTokensTable) {
//If no tokens table exists
$this->_newtokentable($iSurveyId);
} else {
$aData['thissurvey'] = $thissurvey;
$aData['surveyid'] = $iSurveyId;
$aData['queries'] = Token::model($iSurveyId)->summary();
$this->_renderWrappedTemplate('token', array('tokensummary'), $aData);
}
}
/**
* tokens::bounceprocessing()
* @param int $iSurveyId
* @return void
*/
public function bounceprocessing($iSurveyId)
{
$iSurveyId = (int) $iSurveyId;
$survey = Survey::model()->findByPk($iSurveyId);
if (!$survey->hasTokensTable) {
eT("No survey participants table.");
return;
}
$thissurvey = getSurveyInfo($iSurveyId);
if (!Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'update')) {
eT("We are sorry but you don't have permissions to do this.");
return;
}
if ($thissurvey['bounceprocessing'] != 'N' || ($thissurvey['bounceprocessing'] == 'G' && getGlobalSetting('bounceaccounttype') != 'off')) {
if (!function_exists('imap_open')) {
eT("The imap PHP library is not installed or not activated. Please contact your system administrator.");
return;
}
$bouncetotal = 0;
$checktotal = 0;
if ($thissurvey['bounceprocessing'] == 'G') {
$accounttype = strtoupper(getGlobalSetting('bounceaccounttype'));
$hostname = getGlobalSetting('bounceaccounthost');
$username = getGlobalSetting('bounceaccountuser');
$pass = getGlobalSetting('bounceaccountpass');
$hostencryption = strtoupper(getGlobalSetting('bounceencryption'));
} else {
$accounttype = strtoupper($thissurvey['bounceaccounttype']);
$hostname = $thissurvey['bounceaccounthost'];
$username = $thissurvey['bounceaccountuser'];
$pass = $thissurvey['bounceaccountpass'];
$hostencryption = strtoupper($thissurvey['bounceaccountencryption']);
}
@list($hostname, $port) = explode(':', $hostname);
if (empty($port)) {
if ($accounttype == "IMAP") {
switch ($hostencryption) {
case "OFF":
$hostname = $hostname.":143";
break;
case "SSL":
$hostname = $hostname.":993";
break;
case "TLS":
$hostname = $hostname.":993";
break;
}
} else {
switch ($hostencryption) {
case "OFF":
$hostname = $hostname.":110";
break;
case "SSL":
$hostname = $hostname.":995";
break;
case "TLS":
$hostname = $hostname.":995";
break;
}
}
} else {
$hostname = $hostname.":".$port;
}
$flags = "";
switch ($accounttype) {
case "IMAP":
$flags .= "/imap";
break;
case "POP":
$flags .= "/pop3";
break;
}
switch ($hostencryption) // novalidate-cert to have personal CA , maybe option.
{
case "OFF":
$flags .= "/notls"; // Really Off
break;
case "SSL":
$flags .= "/ssl/novalidate-cert";
break;
case "TLS":
$flags .= "/tls/novalidate-cert";
break;
}
$mbox = @imap_open('{'.$hostname.$flags.'}INBOX', $username, $pass);
if ($mbox) {
imap_errors();
$count = imap_num_msg($mbox);
if ($count > 0) {
$aMessageIDs = imap_search($mbox, 'UNSEEN', SE_UID);
if ($aMessageIDs === false) {
$aMessageIDs = array();
}
foreach ($aMessageIDs as $sMessageID) {
$header = explode("\r\n", imap_body($mbox, $sMessageID, FT_UID | FT_PEEK)); // Don't mark messages as read
$iSurveyIdBounce = '';
foreach ($header as $item) {
if (preg_match('/^X-surveyid/', $item)) {
$iSurveyIdBounce = explode(": ", $item);
}
if (preg_match('/^X-tokenid/', $item)) {
$tokenBounce = explode(": ", $item);
if ($iSurveyId == $iSurveyIdBounce[1]) {
$condn = array('token' => $tokenBounce[1]);
$record = Token::model($iSurveyId)->findByAttributes($condn);
if (!empty($record) && $record->emailstatus != 'bounced') {
$record->emailstatus = 'bounced';
$record->save();
$bouncetotal++;
}
imap_body($mbox, $sMessageID, FT_UID); // Mark as read
if (isset($thissurvey['bounceremove']) && $thissurvey['bounceremove']) {
imap_delete($mbox, $sMessageID, FT_UID); // Mark for delete
}
}
}
}
$checktotal++;
}
}
imap_close($mbox);
if ($bouncetotal > 0) {
printf(gT("%s unread messages were scanned out of which %s were marked as bounce by the system."), $checktotal, $bouncetotal);
eT("You can now close this modal box.");
} else {
printf(gT("%s unread messages were scanned, none were marked as bounce by the system."), $checktotal);
eT("You can now close this modal box.");
}
} else {
$sSettingsUrl = App()->createUrl('admin/tokens/sa/bouncesettings/surveyid/'.$iSurveyId);
eT("Failed to open the inbox of the bounce email account.");
echo "
";
printf(gT("Please %s check your settings %s."), '', '');
echo "
";
eT("Error message returned by IMAP:");
echo "
";
$aErrors = @imap_errors();
foreach ($aErrors as $sError) {
echo $sError.'
';
}
echo "
";
eT("You can now close this modal box.");
}
} else {
eT("Bounce processing is deactivated either application-wide or for this survey in particular.");
return;
}
}
/**
* @return boolean
*/
public function deleteMultiple()
{
// TODO: permission checks
$aTokenIds = json_decode(Yii::app()->getRequest()->getPost('sItems'));
$iSid = Yii::app()->getRequest()->getPost('sid');
if (!Permission::model()->hasSurveyPermission($iSid, 'tokens', 'delete')) {
throw new CHttpException(403, gT("You do not have permission to access this page."));
}
TokenDynamic::model($iSid)->deleteRecords($aTokenIds);
return true;
}
/**
* @return boolean
*/
public function deleteToken()
{
// TODO: permission checks
$aTokenId = Yii::app()->getRequest()->getParam('sItem');
$iSid = Yii::app()->getRequest()->getParam('sid');
if (!Permission::model()->hasSurveyPermission($iSid, 'tokens', 'delete')) {
throw new CHttpException(403, gT("You do not have permission to access this page."));
}
TokenDynamic::model($iSid)->deleteRecords(array($aTokenId));
return true;
}
/**
* Browse Tokens
* @param int $iSurveyId
* @param int $limit
* @param int $start
* @return void
*/
public function browse($iSurveyId, $limit = 50, $start = 0)
{
$iSurveyId = (int) $iSurveyId;
$survey = Survey::model()->findByPk($iSurveyId);
/* Check permissions */
if (!Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'read')) {
Yii::app()->session['flashmessage'] = gT("You do not have permission to access this page.");
$this->getController()->redirect(array("/admin/tokens/sa/index/surveyid/{$iSurveyId}"));
}
// TODO: Why needed?
App()->clientScript->registerPackage('bootstrap-switch');
if (!$survey->hasTokensTable) {
$this->_newtokentable($iSurveyId);
}
/* build JS variable to hide buttons forbidden for the current user */
$aData = [];
$aData['showDelButton'] = Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'delete') ? 'true' : 'false';
$aData['showInviteButton'] = Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'update') ? 'true' : 'false';
$aData['showBounceButton'] = Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'update') ? 'true' : 'false';
$aData['showRemindButton'] = Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'update') ? 'true' : 'false';
// Javascript
App()->getClientScript()->registerScriptFile(App()->getConfig('adminscripts').'tokens.js', LSYii_ClientScript::POS_BEGIN);
Yii::app()->loadHelper('surveytranslator');
Yii::import('application.libraries.Date_Time_Converter', true);
$dateformatdetails = getDateFormatData(Yii::app()->session['dateformat']);
$limit = (int) $limit;
$start = (int) $start;
$tkcount = Token::model($iSurveyId)->count();
$next = $start + $limit;
$last = $start - $limit;
$end = $tkcount - $limit;
if ($end < 0) {
$end = 0;
}
if ($last < 0) {
$last = 0;
}
if ($next >= $tkcount) {
$next = $tkcount - $limit;
}
if ($end < 0) {
$end = 0;
}
$order = Yii::app()->request->getPost('order', 'tid');
$order = preg_replace('/[^_ a-z0-9-]/i', '', $order);
$aData['next'] = $next;
$aData['last'] = $last;
$aData['end'] = $end;
$searchstring = Yii::app()->request->getPost('searchstring');
$aData['thissurvey'] = getSurveyInfo($iSurveyId);
$aData['searchstring'] = $searchstring;
$aData['surveyid'] = $iSurveyId;
$aData['bgc'] = "";
$aData['limit'] = $limit;
$aData['start'] = $start;
$aData['order'] = $order;
$aData['surveyprivate'] = $aData['thissurvey']['anonymized'];
$aData['dateformatdetails'] = $dateformatdetails;
$aLanguageCodes = Survey::model()->findByPk($iSurveyId)->getAllLanguages();
$aLanguages = array();
foreach ($aLanguageCodes as $aCode) {
$aLanguages[$aCode] = getLanguageNameFromCode($aCode, false);
}
$aData['aLanguages'] = $aLanguages;
$aData['title_bar']['title'] = $survey->currentLanguageSettings->surveyls_title." (".gT("ID").":".$iSurveyId.")";
$aData['sidemenu']["token_menu"] = true;
$aData['sidemenu']['state'] = false;
$aData['token_bar']['buttons']['view'] = true;
/// FOR GRID View
$model = TokenDynamic::model($iSurveyId);
$filterForm = Yii::app()->request->getPost('TokenDynamic', false);
if ($filterForm) {
$model->setAttributes($filterForm, false);
}
$aData['model'] = $model;
// Set number of page
if (isset($_POST['pageSizeTokenView'])) {
Yii::app()->user->setState('pageSizeTokenView', (int) $_POST['pageSizeTokenView']);
}
$aData['massiveAction'] = App()->getController()->renderPartial('/admin/token/massive_actions/_selector', $aData, true, false);
$this->_renderWrappedTemplate('token', array('browse'), $aData);
}
/**
* The fields with a value "lskeep" will not be updated
*/
public function editMultiple()
{
$aTokenIds = json_decode(Yii::app()->request->getPost('sItems'));
$iSurveyId = Yii::app()->request->getPost('sid');
$aResults = array();
if (Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'update')) {
// CHECK TO SEE IF A Survey participants table EXISTS FOR THIS SURVEY
if (tableExists('{{tokens_'.$iSurveyId.'}}')) {
// First we create the array of fields to update
$aData = array();
$aResults['global']['result'] = true;
// Valid from
if (trim(Yii::app()->request->getPost('validfrom', 'lskeep')) != 'lskeep') {
if (trim(Yii::app()->request->getPost('validfrom', 'lskeep')) == '') {
$aData['validfrom'] = null;
} else {
$aData['validfrom'] = date('Y-m-d H:i:s', strtotime(trim($_POST['validfrom'])));
}
}
// Valid until
if (trim(Yii::app()->request->getPost('validuntil', 'lskeep')) != 'lskeep') {
if (trim(Yii::app()->request->getPost('validuntil')) == '') {
$aData['validuntil'] = null;
} else {
$aData['validuntil'] = date('Y-m-d H:i:s', strtotime(trim($_POST['validuntil'])));
}
}
// Email
if (trim(Yii::app()->request->getPost('email', 'lskeep')) != 'lskeep') {
$isValid = preg_match('/^([a-zA-Z0-9.!#$%&’*+\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+))(,([a-zA-Z0-9.!#$%&’*+\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)))*$/', Yii::app()->request->getPost('email'));
if ($isValid) {
$aData['email'] = 'lskeep';
} else {
$aData['email'] = Yii::app()->request->getPost('email');
}
}
// Core Fields
$aCoreTokenFields = array('firstname', 'lastname', 'emailstatus', 'token', 'language', 'sent', 'remindersent', 'completed', 'usesleft');
foreach ($aCoreTokenFields as $sCoreTokenField) {
if (trim(Yii::app()->request->getPost($sCoreTokenField, 'lskeep')) != 'lskeep') {
$value = flattenText(Yii::app()->request->getPost($sCoreTokenField));
if ($sCoreTokenField == 'language' and empty($value)){
continue;
}
$aData[$sCoreTokenField] = $value;
}
}
// Attibutes fields
$attrfieldnames = GetParticipantAttributes($iSurveyId);
foreach ($attrfieldnames as $attr_name => $desc) {
if (trim(Yii::app()->request->getPost($attr_name, 'lskeep')) != 'lskeep') {
$value = flattenText(Yii::app()->request->getPost($attr_name));
if ($desc['mandatory'] == 'Y' && trim($value) == '') {
Yii::app()->setFlashMessage(sprintf(gT('%s cannot be left empty'), $desc['description']), 'error');
$this->getController()->refresh();
}
$aData[$attr_name] = $value;
}
}
if (count($aData) > 0) {
foreach ($aTokenIds as $iTokenId) {
$iTokenId = (int) $iTokenId;
$token = Token::model($iSurveyId)->find('tid='.$iTokenId);
foreach ($aData as $k => $v) {
$token->$k = $v;
}
$bUpdateSuccess = $token->update();
if ($bUpdateSuccess) {
$aResults[$iTokenId]['status'] = true;
$aResults[$iTokenId]['message'] = gT('Updated');
} else {
$aResults[$iTokenId]['status'] = false;
$aResults[$iTokenId]['message'] = $token->error;
}
}
} else {
$aResults['global']['result'] = false;
$aResults['global']['message'] = gT('Nothing to update');
}
} else {
$aResults['global']['result'] = false;
$aResults['global']['message'] = gT('No participant table found for this survey!');
}
} else {
$aResults['global']['result'] = false;
$aResults['global']['message'] = gT("We are sorry but you don't have permissions to do this.");
}
Yii::app()->getController()->renderPartial('/admin/token/massive_actions/_update_results', array('aResults'=>$aResults));
}
/**
* Called by if a token is saved after editing
* @todo Check if method is still in use
* @param int $iSurveyId The Survey ID
* @return void
*/
public function editToken($iSurveyId)
{
$iSurveyId = (int) $iSurveyId;
$aData = [];
App()->getClientScript()->registerScriptFile(App()->getConfig('adminscripts').'tokens.js', LSYii_ClientScript::POS_BEGIN);
if (!Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'update') && !Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'create')) {
eT("We are sorry but you don't have permissions to do this."); // return json ? error not treated in js.
return;
}
$survey = Survey::model()->findByPk($iSurveyId);
if (!$survey->hasTokensTable) {
$this->_newtokentable($iSurveyId);
}
$sOperation = Yii::app()->request->getPost('oper');
if (trim(Yii::app()->request->getPost('validfrom')) == '') {
$from = null;
} else {
$from = date('Y-m-d H:i:s', strtotime(trim($_POST['validfrom'])));
}
if (trim(Yii::app()->request->getPost('validuntil')) == '') {
$until = null;
} else {
$until = date('Y-m-d H:i:s', strtotime(trim($_POST['validuntil'])));
}
// if edit it will update the row
if ($sOperation == 'edit' && Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'update')) {
// if (Yii::app()->request->getPost('language') == '')
// {
// $sLang = Yii::app()->session['adminlang'];
// }
// else
// {
// $sLang = Yii::app()->request->getPost('language');
// }
echo $from.','.$until;
$aData = array(
'firstname' => flattenText(Yii::app()->request->getPost('firstname')),
'lastname' => flattenText(Yii::app()->request->getPost('lastname')),
'email' => flattenText(Yii::app()->request->getPost('email')),
'emailstatus' => flattenText(Yii::app()->request->getPost('emailstatus')),
'token' => Token::sanitizeToken(Yii::app()->request->getPost('token')),
'language' => flattenText(Yii::app()->request->getPost('language')),
'sent' => flattenText(Yii::app()->request->getPost('sent')),
'remindersent' => flattenText(Yii::app()->request->getPost('remindersent')),
'remindercount' => flattenText(Yii::app()->request->getPost('remindercount')),
'completed' => flattenText(Yii::app()->request->getPost('completed')),
'usesleft' => flattenText(Yii::app()->request->getPost('usesleft')),
'validfrom' => $from,
'validuntil' => $until);
$attrfieldnames = getParticipantAttributes($iSurveyId);
foreach ($attrfieldnames as $attr_name => $desc) {
$value = flattenText(Yii::app()->request->getPost($attr_name));
if ($desc['mandatory'] == 'Y' && trim($value) == '') {
Yii::app()->setFlashMessage(sprintf(gT('%s cannot be left empty'), $desc['description']), 'error');
$this->getController()->refresh();
}
$aData[$attr_name] = $value;
}
$token = Token::model($iSurveyId)->find('tid='.Yii::app()->getRequest()->getPost('id'));
foreach ($aData as $k => $v) {
$token->$k = $v;
}
echo $token->update();
}
// if add it will insert a new row
elseif ($sOperation == 'add' && Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'create')) {
if (Yii::app()->request->getPost('language') == '') {
$aData = array('firstname' => flattenText(Yii::app()->request->getPost('firstname')),
'lastname' => flattenText(Yii::app()->request->getPost('lastname')),
'email' => flattenText(Yii::app()->request->getPost('email')),
'emailstatus' => flattenText(Yii::app()->request->getPost('emailstatus')),
'token' => Token::sanitizeToken(Yii::app()->request->getPost('token')),
'language' => flattenText(Yii::app()->request->getPost('language')),
'sent' => flattenText(Yii::app()->request->getPost('sent')),
'remindersent' => flattenText(Yii::app()->request->getPost('remindersent')),
'remindercount' => flattenText(Yii::app()->request->getPost('remindercount')),
'completed' => flattenText(Yii::app()->request->getPost('completed')),
'usesleft' => flattenText(Yii::app()->request->getPost('usesleft')),
'validfrom' => $from,
'validuntil' => $until);
}
$attrfieldnames = Survey::model()->findByPk($iSurveyId)->tokenAttributes;
foreach ($attrfieldnames as $attr_name => $desc) {
$value = flattenText(Yii::app()->request->getPost($attr_name));
if ($desc['mandatory'] == 'Y' && trim($value) == '') {
Yii::app()->setFlashMessage(sprintf(gT('%s cannot be left empty'), $desc['description']), 'error');
$this->getController()->refresh();
}
$aData[$attr_name] = $value;
}
$token = Token::create($iSurveyId);
$token->setAttributes($aData, false);
echo $token->save();
} elseif ($sOperation == 'del' && Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'update')) {
$_POST['tid'] = Yii::app()->request->getPost('id');
$this->delete($iSurveyId);
} else {
eT("We are sorry but you don't have permissions to do this."); // return json ? error not treated in js.
return;
}
}
/**
* Add new token form
* @param int $iSurveyId
* @return void
*/
public function addnew($iSurveyId)
{
$aData = array();
App()->getClientScript()->registerScriptFile(App()->getConfig('adminscripts').'tokens.js', LSYii_ClientScript::POS_BEGIN);
$iSurveyId = (int) $iSurveyId;
$survey = Survey::model()->findByPk($iSurveyId);
// Check permission
if (!Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'create')) {
Yii::app()->session['flashmessage'] = gT("You do not have permission to access this page.");
$this->getController()->redirect(array("/admin/survey/sa/view/surveyid/{$iSurveyId}"));
}
if (!$survey->hasTokensTable) {
// If no tokens table exists
$this->_newtokentable($iSurveyId);
}
Yii::app()->loadHelper("surveytranslator");
$dateformatdetails = getDateFormatData(Yii::app()->session['dateformat']);
$aData['title_bar']['title'] = $survey->currentLanguageSettings->surveyls_title." (".gT("ID").":".$iSurveyId.")";
$aData['sidemenu']["token_menu"] = true;
$aData['token_bar']['buttons']['view'] = true;
App()->getClientScript()->registerScriptFile(App()->getConfig('adminscripts').'tokens.js', LSYii_ClientScript::POS_BEGIN);
$request = Yii::app()->request;
if ($request->getPost('subaction') == 'inserttoken') {
Yii::import('application.libraries.Date_Time_Converter');
// Fix up dates and match to database format
if (trim($request->getPost('validfrom')) == '') {
$validfrom = null;
} else {
$datetimeobj = new Date_Time_Converter(
trim($request->getPost('validfrom')),
$dateformatdetails['phpdate'].' H:i'
);
$validfrom = $datetimeobj->convert('Y-m-d H:i:s');
}
if (trim(Yii::app()->request->getPost('validuntil')) == '') {
$validuntil = null;
} else {
$datetimeobj = new Date_Time_Converter(
trim($request->getPost('validuntil')),
$dateformatdetails['phpdate'].' H:i'
);
$validuntil = $datetimeobj->convert('Y-m-d H:i:s');
}
$sanitizedtoken = Token::sanitizeToken($request->getPost('token'));
$aData = array(
'firstname' => flattenText($request->getPost('firstname')),
'lastname' => flattenText($request->getPost('lastname')),
'email' => flattenText($request->getPost('email')),
'emailstatus' => flattenText($request->getPost('emailstatus')),
'token' => $sanitizedtoken,
'language' => sanitize_languagecode($request->getPost('language')),
'sent' => flattenText($request->getPost('sent')),
'remindersent' => flattenText($request->getPost('remindersent')),
'completed' => flattenText($request->getPost('completed')),
'usesleft' => flattenText($request->getPost('usesleft')),
'validfrom' => $validfrom,
'validuntil' => $validuntil,
);
// Add attributes
$attrfieldnames = Survey::model()->findByPk($iSurveyId)->tokenAttributes;
$aTokenFieldNames = Yii::app()->db->getSchema()->getTable("{{tokens_$iSurveyId}}", true);
$aTokenFieldNames = array_keys($aTokenFieldNames->columns);
foreach ($attrfieldnames as $attr_name => $desc) {
if (!in_array($attr_name, $aTokenFieldNames)) {
continue;
}
$value = Yii::app()->getRequest()->getPost($attr_name);
if ($desc['mandatory'] == 'Y' && trim($value) == '') {
Yii::app()->setFlashMessage(sprintf(gT('%s cannot be left empty'), $desc['description']), 'error');
$this->getController()->refresh();
}
$aData[$attr_name] = Yii::app()->getRequest()->getPost($attr_name);
}
$udresult = Token::model($iSurveyId)->findAll("token <> '' and token = '$sanitizedtoken'");
if (count($udresult) == 0) {
// AutoExecute
$token = Token::create($iSurveyId);
$token->setAttributes($aData, false);
$inresult = $token->save();
$aData['success'] = $inresult;
$aData['errors'] = $token->getErrors();
} else {
$aData['success'] = false;
$aData['errors'] = array(
'token' => array(gT("There is already an entry with that exact token in the table. The same token cannot be used in multiple entries."))
);
}
$aData['thissurvey'] = getSurveyInfo($iSurveyId);
$aData['surveyid'] = $iSurveyId;
$aData['iTokenLength'] = !empty(Token::model($iSurveyId)->survey->tokenlength) ? Token::model($iSurveyId)->survey->tokenlength : 15;
$aData['sidemenu']['state'] = false;
$this->_renderWrappedTemplate('token', array('addtokenpost'), $aData);
} else {
$this->_handletokenform($iSurveyId, "addnew");
}
}
/**
* Edit Tokens
* @param int $iSurveyId
* @param int $iTokenId
* @param boolean $ajax
* @return false|null
* @todo When is this function used without Ajax?
*/
public function edit($iSurveyId, $iTokenId, $ajax = false)
{
App()->getClientScript()->registerScriptFile(App()->getConfig('adminscripts').'tokens.js', LSYii_ClientScript::POS_BEGIN);
$iSurveyId = (int) $iSurveyId;
$iTokenId = (int) $iTokenId;
$survey = Survey::model()->findByPk($iSurveyId);
// Check permission
if (!Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'update')) {
if ($ajax) {
eT("You do not have permission to access this page.");
return false;
} else {
Yii::app()->session['flashmessage'] = gT("You do not have permission to access this page.");
$this->getController()->redirect(array("/admin/survey/sa/view/surveyid/{$iSurveyId}"));
}
}
if (!$survey->hasTokensTable) {
// If no tokens table exists
$this->_newtokentable($iSurveyId);
}
Yii::app()->loadHelper("surveytranslator");
$dateformatdetails = getDateFormatData(Yii::app()->session['dateformat']);
$request = Yii::app()->request;
if ($request->getPost('subaction')) {
Yii::import('application.helpers.admin.ajax_helper', true);
Yii::import('application.libraries.Date_Time_Converter', true);
$aTokenData = [];
if (trim($request->getPost('validfrom')) == '') {
$_POST['validfrom'] = null;
} else {
$datetimeobj = new Date_Time_Converter(trim($request->getPost('validfrom')), $dateformatdetails['phpdate'].' H:i');
$_POST['validfrom'] = $datetimeobj->convert('Y-m-d H:i:s');
}
if (trim($request->getPost('validuntil')) == '') {
$_POST['validuntil'] = null;
} else {
$datetimeobj = new Date_Time_Converter(trim($request->getPost('validuntil')), $dateformatdetails['phpdate'].' H:i');
$_POST['validuntil'] = $datetimeobj->convert('Y-m-d H:i:s');
}
if (trim($request->getPost('completed')) == 'N') {
$_POST['completed'] = 'N';
} else {
$datetimeobj = new Date_Time_Converter(trim($request->getPost('completed')), $dateformatdetails['phpdate'].' H:i');
$_POST['completed'] = $datetimeobj->convert('Y-m-d H:i');
}
//sent
if (trim($request->getPost('sent')) == 'N') {
$_POST['sent'] = 'N';
} else {
$datetimeobj = new Date_Time_Converter(trim($request->getPost('sent')), $dateformatdetails['phpdate'].' H:i');
$_POST['sent'] = $datetimeobj->convert('Y-m-d H:i');
}
// remindersent
if (trim($request->getPost('remindersent')) == 'N') {
$_POST['remindersent'] = 'N';
} else {
$datetimeobj = new Date_Time_Converter(trim($request->getPost('remindersent')), $dateformatdetails['phpdate'].' H:i');
$_POST['remindersent'] = $datetimeobj->convert('Y-m-d H:i');
}
$aTokenData['firstname'] = flattenText($request->getPost('firstname'));
$aTokenData['lastname'] = flattenText($request->getPost('lastname'));
$aTokenData['email'] = flattenText($request->getPost('email'));
$aTokenData['emailstatus'] = flattenText($request->getPost('emailstatus'));
$sSanitizedToken = Token::sanitizeToken($request->getPost('token'));
$aTokenData['token'] = $sSanitizedToken;
$aTokenData['language'] = sanitize_languagecode($request->getPost('language'));
$aTokenData['sent'] = flattenText($request->getPost('sent'));
$aTokenData['completed'] = flattenText($request->getPost('completed'));
$aTokenData['usesleft'] = flattenText($request->getPost('usesleft'));
$aTokenData['validfrom'] = $request->getPost('validfrom');
$aTokenData['validuntil'] = $request->getPost('validuntil');
$aTokenData['remindersent'] = flattenText($request->getPost('remindersent'));
$aTokenData['remindercount'] = intval(flattenText($request->getPost('remindercount')));
$udresult = Token::model($iSurveyId)->findAll("tid <> :tid and token <> '' and token = :token", [':tid' => $iTokenId, ':token' => $sSanitizedToken]);
$sOutput = '';
if (count($udresult) == 0) {
$thissurvey = getSurveyInfo($iSurveyId);
$aAdditionalAttributeFields = $thissurvey['attributedescriptions'];
foreach ($aAdditionalAttributeFields as $attr_name => $desc) {
$value = $request->getPost($attr_name);
if ($desc['mandatory'] == 'Y' && trim($value) == '') {
$sOutput .= sprintf(gT("Notice: Field '%s' was left empty, even though it is a mandatory attribute."), $desc['description']).'
';
}
$aTokenData[$attr_name] = $request->getPost($attr_name);
}
$token = Token::model($iSurveyId)->findByPk($iTokenId);
foreach ($aTokenData as $k => $v) {
$token->$k = $v;
}
$result = $token->save();
if ($result) {
\ls\ajax\AjaxHelper::outputSuccess($sOutput.gT('The survey participant was successfully updated.'));
} else {
$errors = $token->getErrors();
$firstError = reset($errors);
\ls\ajax\AjaxHelper::outputError($firstError[0]);
}
} else {
\ls\ajax\AjaxHelper::outputError(gT('There is already an entry with that exact token in the table. The same token cannot be used in multiple entries.'));
}
} else {
$this->_handletokenform($iSurveyId, "edit", $iTokenId, $ajax);
}
}
/**
* Delete tokens
* @param int $iSurveyID
* @return void
*/
public function delete($iSurveyID)
{
App()->getClientScript()->registerScriptFile(App()->getConfig('adminscripts').'tokens.js', LSYii_ClientScript::POS_BEGIN);
$iSurveyID = (int) $iSurveyID;
$sTokenIDs = Yii::app()->request->getPost('tid');
$survey = Survey::model()->findByPk($iSurveyID);
/* Check permissions */
if (!Permission::model()->hasSurveyPermission($iSurveyID, 'tokens', 'update')) {
Yii::app()->session['flashmessage'] = gT("You do not have permission to access this page.");
$this->getController()->redirect(array("/admin/survey/sa/view/surveyid/{$iSurveyID}"));
}
if (!$survey->hasTokensTable) {
// If no tokens table exists
$this->_newtokentable($iSurveyId);
}
$beforeTokenDelete = new PluginEvent('beforeTokenDelete');
$beforeTokenDelete->set('sTokenIds', $sTokenIDs);
$beforeTokenDelete->set('iSurveyID', $iSurveyID);
App()->getPluginManager()->dispatchEvent($beforeTokenDelete);
if (Permission::model()->hasSurveyPermission($iSurveyID, 'tokens', 'delete')) {
$aTokenIds = explode(',', $sTokenIDs); //Make the tokenids string into an array
//Delete any survey_links
SurveyLink::model()->deleteTokenLink($aTokenIds, $iSurveyID);
//Then delete the tokens
Token::model($iSurveyID)->deleteByPk($aTokenIds);
}
}
/**
* Add dummy tokens form
* @param int $iSurveyId
* @param string $subaction
* @return void
* @throws Exception
*/
public function addDummies($iSurveyId, $subaction = '')
{
$iSurveyId = (int) $iSurveyId;
$survey = Survey::model()->findByPk($iSurveyId);
if (!Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'create')) {
Yii::app()->session['flashmessage'] = gT("You do not have permission to access this page.");
$this->getController()->redirect(array("/admin/survey/sa/view/surveyid/{$iSurveyId}"));
}
if (!$survey->hasTokensTable) {
// If no tokens table exists
$this->_newtokentable($iSurveyId);
}
Yii::app()->loadHelper("surveytranslator");
$aData = array();
$aData['sidemenu']['state'] = false;
$aData['title_bar']['title'] = $survey->currentLanguageSettings->surveyls_title." (".gT("ID").":".$iSurveyId.")";
$aData['sidemenu']["token_menu"] = true;
$aData['token_bar']['savebutton']['form'] = true;
$aData['token_bar']['closebutton']['url'] = 'admin/tokens/sa/index/surveyid/'.$iSurveyId; // Close button
if (!empty($subaction) && $subaction == 'add') {
$message = '';
$this->getController()->loadLibrary('Date_Time_Converter');
$dateformatdetails = getDateFormatData(Yii::app()->session['dateformat']);
//Fix up dates and match to database format
if (trim(Yii::app()->request->getPost('validfrom')) == '') {
$aData['validfrom'] = null;
} else {
$datetimeobj = new Date_Time_Converter(trim(Yii::app()->request->getPost('validfrom')), $dateformatdetails['phpdate'].' H:i');
$aData['validfrom'] = $datetimeobj->convert('Y-m-d H:i:s');
}
if (trim(Yii::app()->request->getPost('validuntil')) == '') {
$aData['validuntil'] = null;
} else {
$datetimeobj = new Date_Time_Converter(trim(Yii::app()->request->getPost('validuntil')), $dateformatdetails['phpdate'].' H:i');
$aData['validuntil'] = $datetimeobj->convert('Y-m-d H:i:s');
}
$aData['firstname'] = flattenText(Yii::app()->request->getPost('firstname'));
$aData['lastname'] = flattenText(Yii::app()->request->getPost('lastname'));
$aData['email'] = flattenText(Yii::app()->request->getPost('email'));
$aData['token'] = '';
$aData['language'] = sanitize_languagecode(Yii::app()->request->getPost('language'));
$aData['sent'] = 'N';
$aData['remindersent'] = 'N';
$aData['completed'] = 'N';
$aData['usesleft'] = flattenText(Yii::app()->request->getPost('usesleft'));
$aData['amount'] = Yii::app()->request->getPost('amount');
$aData['tokenlength'] = Yii::app()->request->getPost('tokenlen');
// add attributes
$cntAttributeErrors = 0;
$attrfieldnames = getTokenFieldsAndNames($iSurveyId, true);
foreach ($attrfieldnames as $attr_name => $desc) {
$value = flattenText(Yii::app()->request->getPost($attr_name));
if ($desc['mandatory'] == 'Y' && trim($value) == '') {
Yii::app()->setFlashMessage(sprintf(gT('%s cannot be left empty'), $desc['description']), 'error');
$cntAttributeErrors+=1;
}
$aData[$attr_name] = $value;
}
$aData['amount'] = (int) Yii::app()->request->getPost('amount');
$aData['tokenlength'] = (int) Yii::app()->request->getPost('tokenlen');
// Fill an array with all existing tokens
$existingtokens = array();
$tokenModel = Token::model($iSurveyId);
$criteria = $tokenModel->getDbCriteria();
$criteria->select = 'token';
$criteria->distinct = true;
$command = $tokenModel->getCommandBuilder()->createFindCommand($tokenModel->getTableSchema(), $criteria);
$result = $command->query();
while ($tokenRow = $result->read()) {
$existingtokens[$tokenRow['token']] = true;
}
$result->close();
$invalidtokencount = 0;
$newDummyToken = 0;
while ($newDummyToken < $aData['amount'] && $invalidtokencount < 50) {
$token = Token::create($iSurveyId);
$token->setAttributes($aData, false);
$token->firstname = str_replace('{TOKEN_COUNTER}', $newDummyToken, $token->firstname);
$token->lastname = str_replace('{TOKEN_COUNTER}', $newDummyToken, $token->lastname);
$token->email = str_replace('{TOKEN_COUNTER}', $newDummyToken, $token->email);
$attempts = 0;
do {
$token->token = Token::generateRandomToken($aData['tokenlength']);
$attempts++;
} while (isset($existingtokens[$token->token]) && $attempts < 50);
if ($attempts == 50) {
throw new Exception('Something is wrong with your random generator.');
}
$existingtokens[$token->token] = true;
$token->save();
$newDummyToken++;
}
$aData['thissurvey'] = getSurveyInfo($iSurveyId);
$aData['surveyid'] = $iSurveyId;
if ($cntAttributeErrors > 0){ // attribute validation errors
$aData['dateformatdetails'] = getDateFormatData(Yii::app()->session['dateformat'], App()->language);
$aData['aAttributeFields'] = getParticipantAttributes($iSurveyId);
$this->_renderWrappedTemplate('token', array('dummytokenform'), $aData);
} elseif (!$invalidtokencount) {
$aData['success'] = true;
Yii::app()->session['flashmessage'] = gT("New dummy participants were added.");
//admin/tokens/sa/browse/surveyid/652779//
$this->getController()->redirect(array("/admin/tokens/sa/browse/surveyid/{$iSurveyId}"));
} else {
$aData['success'] = false;
$message = array(
'title' => gT("Failed"),
'message' => "
".sprintf(gT("Only %s new dummy participants were added after %s trials."), $newDummyToken, $invalidtokencount) .gT("Try with a bigger token length.")."
" ."\ngetController()->createUrl("admin/tokens/sa/browse/surveyid/$iSurveyId")."', '_top')\" />\n" ); $this->_renderWrappedTemplate('token', array('message' => $message), $aData); } } else { // default values $aData['firstname'] = ''; $aData['lastname'] = ''; $aData['email'] = ''; $aData['token'] = ''; $aData['language'] = Survey::model()->findByPk($iSurveyId)->language; $aData['sent'] = 'N'; $aData['remindersent'] = 'N'; $aData['completed'] = 'N'; $aData['usesleft'] = 1; $aData['validfrom'] = null; $aData['validuntil'] = null; $aData['amount'] = 100; $aData['tokenlength'] = !empty(Token::model($iSurveyId)->survey->tokenlength) ? Token::model($iSurveyId)->survey->tokenlength : 15; $thissurvey = getSurveyInfo($iSurveyId); $aData['thissurvey'] = $thissurvey; $aData['surveyid'] = $iSurveyId; $aData['dateformatdetails'] = getDateFormatData(Yii::app()->session['dateformat'], App()->language); $aData['aAttributeFields'] = getParticipantAttributes($iSurveyId); $this->_renderWrappedTemplate('token', array('dummytokenform'), $aData); } } /** * Handle managetokenattributes action * @param int $iSurveyId * @return void */ public function managetokenattributes($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("/admin/survey/sa/view/surveyid/{$iSurveyId}")); } // CHECK TO SEE IF A Survey participants table EXISTS FOR THIS SURVEY $bTokenExists = $oSurvey->hasTokensTable; if (!$bTokenExists) { //If no tokens table exists $this->_newtokentable($iSurveyId); } Yii::app()->loadHelper("surveytranslator"); $aData = array(); $aData['sidemenu']['state'] = false; $aData['title_bar']['title'] = $oSurvey->currentLanguageSettings->surveyls_title." (".gT("ID").":".$iSurveyId.")"; $aData['sidemenu']["token_menu"] = true; $aData['token_bar']['closebutton']['url'] = 'admin/tokens/sa/index/surveyid/'.$iSurveyId; // Close button $aData['thissurvey'] = $oSurvey->attributes; $aData['surveyid'] = $iSurveyId; $aData['tokenfields'] = getAttributeFieldNames($iSurveyId); $aData['tokenfielddata'] = $oSurvey->decodedAttributedescriptions; // Prepare token fiel list for dropDownList $tokenfieldlist = array(); foreach ($aData['tokenfields'] as $tokenfield) { if (isset($aData['tokenfielddata'][$tokenfield])) { $description = $aData['tokenfielddata'][$tokenfield]['description']; } else { $description = ""; } $description = sprintf(gT("Attribute %s (%s)"), str_replace("attribute_", "", $tokenfield), $description); $tokenfieldlist[] = array("id"=>$tokenfield, "description"=>$description); } $aData['tokenfieldlist'] = $tokenfieldlist; $languages = array_merge((array) Survey::model()->findByPk($iSurveyId)->language, Survey::model()->findByPk($iSurveyId)->additionalLanguages); $captions = array(); foreach ($languages as $language) { $captions[$language] = SurveyLanguageSetting::model()->findByAttributes(array('surveyls_survey_id' => $iSurveyId, 'surveyls_language' => $language))->attributeCaptions; } $aData['languages'] = $languages; $aData['tokencaptions'] = $captions; $aData['nrofattributes'] = 0; $aData['examplerow'] = TokenDynamic::model($iSurveyId)->find(); $aData['aCPDBAttributes'][''] = gT('(none)'); foreach (ParticipantAttributeName::model()->getCPDBAttributes() as $aCPDBAttribute) { $aData['aCPDBAttributes'][$aCPDBAttribute['attribute_id']] = $aCPDBAttribute['attribute_name']; } $this->_renderWrappedTemplate('token', array('managetokenattributes'), $aData); } /** * Update token attributes * @param int $iSurveyId * @return void */ public function updatetokenattributes($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("/admin/survey/sa/view/surveyid/{$iSurveyId}")); } if (!$oSurvey->hasTokensTable) { // If no tokens table exists $this->_newtokentable($iSurveyId); } $number2add = sanitize_int(Yii::app()->request->getPost('addnumber'), 1, 100); $tokenattributefieldnames = getAttributeFieldNames($iSurveyId); $i = 1; for ($b = 0; $b < $number2add; $b++) { while (in_array('attribute_'.$i, $tokenattributefieldnames) !== false) { $i++; } $tokenattributefieldnames[] = 'attribute_'.$i; Yii::app()->db->createCommand(Yii::app()->db->getSchema()->addColumn("{{tokens_".intval($iSurveyId)."}}", 'attribute_'.$i, 'text'))->execute(); } Yii::app()->db->schema->getTable($oSurvey->tokensTableName, true); // Refresh schema cache just in case the table existed in the past LimeExpressionManager::SetDirtyFlag(); // so that knows that survey participants tables have changed Yii::app()->session['flashmessage'] = sprintf(gT("%s field(s) were successfully added."), $number2add); Yii::app()->getController()->redirect(array("/admin/tokens/sa/managetokenattributes/surveyid/$iSurveyId")); } /** * Delete token attributes * @param int $iSurveyId * @return void */ public function deletetokenattributes($iSurveyId) { $iSurveyId = (int) $iSurveyId; $oSurvey = Survey::model()->findByPk($iSurveyId); // CHECK TO SEE IF A Survey participants table EXISTS FOR THIS SURVEY if (!$oSurvey->hasTokensTable) { Yii::app()->session['flashmessage'] = gT("No survey participants table."); $this->getController()->redirect($this->getController()->createUrl("/admin/survey/sa/view/surveyid/{$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($this->getController()->createUrl("/admin/survey/sa/view/surveyid/{$iSurveyId}")); } $aData = array(); $aData['thissurvey'] = getSurveyInfo($iSurveyId); $aData['surveyid'] = $iSurveyId; $confirm = Yii::app()->request->getPost('confirm', ''); $cancel = Yii::app()->request->getPost('cancel', ''); $tokenfields = getAttributeFieldNames($iSurveyId); $sAttributeToDelete = Yii::app()->request->getPost('deleteattribute', ''); if (!in_array($sAttributeToDelete, $tokenfields)) { $sAttributeToDelete = false; } if ($cancel == 'cancel') { Yii::app()->getController()->redirect(Yii::app()->getController()->createUrl("/admin/tokens/sa/managetokenattributes/surveyid/$iSurveyId")); } elseif ($confirm != 'confirm' && $sAttributeToDelete) { $aData['sidemenu']['state'] = false; $this->_renderWrappedTemplate('token', array('message' => array( 'title' => sprintf(gT("Delete token attribute %s"), $sAttributeToDelete), 'message' => "".gT("If you remove this attribute, you will lose all information.")."
\n" . CHtml::form(array("admin/tokens/sa/deletetokenattributes/surveyid/{$iSurveyId}"), 'post', array('id'=>'attributenumber')) . CHtml::hiddenField('deleteattribute', $sAttributeToDelete) . CHtml::hiddenField('sid', $iSurveyId) . CHtml::htmlButton(gT('Delete attribute'), array('type'=>'submit', 'value'=>'confirm', 'name'=>'confirm', 'class'=>'btn btn-default btn-lg')) . CHtml::htmlButton(gT('Cancel'), array('type'=>'submit', 'value'=>'cancel', 'name'=>'cancel', 'class'=>'btn btn-default btn-lg')) . CHtml::endForm() )), $aData); } elseif ($sAttributeToDelete) { // Update field attributedescriptions in survey table $aTokenAttributeDescriptions = $oSurvey->decodedAttributedescriptions; unset($aTokenAttributeDescriptions[$sAttributeToDelete]); Survey::model()->updateByPk($iSurveyId, array('attributedescriptions' => json_encode($aTokenAttributeDescriptions))); $sTableName = "{{tokens_".intval($iSurveyId)."}}"; Yii::app()->db->createCommand(Yii::app()->db->getSchema()->dropColumn($sTableName, $sAttributeToDelete))->execute(); Yii::app()->db->schema->getTable($sTableName, true); // Refresh schema cache LimeExpressionManager::SetDirtyFlag(); Yii::app()->session['flashmessage'] = sprintf(gT("Attribute %s was deleted."), $sAttributeToDelete); Yii::app()->getController()->redirect(Yii::app()->getController()->createUrl("/admin/tokens/sa/managetokenattributes/surveyid/$iSurveyId")); } else { Yii::app()->session['flashmessage'] = gT("The selected attribute was invalid."); Yii::app()->getController()->redirect(Yii::app()->getController()->createUrl("/admin/tokens/sa/managetokenattributes/surveyid/$iSurveyId")); } } /** * updatetokenattributedescriptions action * @param int $iSurveyId * @return void */ public function updatetokenattributedescriptions($iSurveyId) { $iSurveyId = (int) $iSurveyId; $oSurvey = Survey::model()->findByPk($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("/admin/survey/sa/view/surveyid/{$iSurveyId}")); } if (!$oSurvey->hasTokensTable) { // If no tokens table exists $this->_newtokentable($iSurveyId); } // find out the existing token attribute fieldnames $tokenattributefieldnames = getAttributeFieldNames($iSurveyId); $languages = array_merge((array) $oSurvey->language, $oSurvey->additionalLanguages); $fieldcontents = array(); $captions = array(); foreach ($tokenattributefieldnames as $fieldname) { $fieldcontents[$fieldname] = array( 'description' => strip_tags(Yii::app()->request->getPost('description_'.$fieldname)), 'mandatory' => Yii::app()->request->getPost('mandatory_'.$fieldname) == '1' ? 'Y' : 'N', 'show_register' => Yii::app()->request->getPost('show_register_'.$fieldname) == '1' ? 'Y' : 'N', 'cpdbmap' => Yii::app()->request->getPost('cpdbmap_'.$fieldname) ); foreach ($languages as $language) { $fieldNameValue = Yii::app()->request->getPost("caption_".$fieldname."_".$language); $captions[$language][$fieldname] = $fieldNameValue; } } Survey::model()->updateByPk($iSurveyId, array('attributedescriptions' => json_encode($fieldcontents))); foreach ($languages as $language) { $ls = SurveyLanguageSetting::model()->findByAttributes(array('surveyls_survey_id' => $iSurveyId, 'surveyls_language' => $language)); $ls->surveyls_attributecaptions = json_encode($captions[$language]); $ls->save(); } Yii::app()->session['flashmessage'] = gT('Token attribute descriptions were successfully updated.'); //admin/tokens/sa/browse/surveyid/652779// $this->getController()->redirect(array("/admin/tokens/sa/managetokenattributes/surveyid/{$iSurveyId}")); } /** * Handle email action * @param int $iSurveyId * @param string $tokenids Int list separated with |? * @return void */ public function email($iSurveyId) { $iSurveyId = (int) $iSurveyId; $aData = array(); $survey = Survey::model()->findByPk($iSurveyId); if (!Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'update')) { Yii::app()->session['flashmessage'] = gT("You do not have permission to access this page."); $this->getController()->redirect(array("/admin/survey/sa/view/surveyid/{$iSurveyId}")); } if (!$survey->hasTokensTable) { // If no tokens table exists, redirect to create token page. $this->_newtokentable($iSurveyId); } $aData['sidemenu']['state'] = false; $aData['title_bar']['title'] = $survey->currentLanguageSettings->surveyls_title." (".gT("ID").":".$iSurveyId.")"; $aData['sidemenu']["token_menu"] = true; $aData['token_bar']['closebutton']['url'] = 'admin/tokens/sa/index/surveyid/'.$iSurveyId; // Close button if (Yii::app()->request->getParam('action') == "remind") { $aData['token_bar']['sendreminderbutton'] = true; } else { $aData['token_bar']['sendinvitationbutton'] = true; // Invitation button } $aTokenIds = $this->getTokenIds(); $sSubAction = $this->getSubAction(); $bEmail = $sSubAction == 'invite'; Yii::app()->loadHelper('surveytranslator'); Yii::app()->loadHelper('/admin/htmleditor'); Yii::app()->session['FileManagerContext'] = "edit:emailsettings:{$iSurveyId}"; initKcfinder(); Yii::app()->loadHelper('replacements'); $token = Token::model($iSurveyId)->find(); $aExampleRow = isset($token) ? $token->attributes : array(); $aSurveyLangs = Survey::model()->findByPk($iSurveyId)->additionalLanguages; $sBaseLanguage = Survey::model()->findByPk($iSurveyId)->language; array_unshift($aSurveyLangs, $sBaseLanguage); $aTokenFields = getTokenFieldsAndNames($iSurveyId, true); $iAttributes = 0; $bHtml = (getEmailFormat($iSurveyId) == 'html'); $aData['thissurvey'] = getSurveyInfo($iSurveyId); foreach ($aSurveyLangs as $sSurveyLanguage) { $aData['thissurvey'][$sSurveyLanguage] = getSurveyInfo($iSurveyId, $sSurveyLanguage); } $aData['surveyid'] = $iSurveyId; $aData['sSubAction'] = $sSubAction; $aData['bEmail'] = $bEmail; $aData['aSurveyLangs'] = $aData['surveylangs'] = $aSurveyLangs; $aData['baselang'] = $sBaseLanguage; $aData['tokenfields'] = array_keys($aTokenFields); $aData['nrofattributes'] = $iAttributes; $aData['examplerow'] = $aExampleRow; $aData['tokenids'] = $aTokenIds; $aData['ishtml'] = $bHtml; $iMaxEmails = Yii::app()->getConfig('maxemails'); // TODO: Rename 'ok' to something meaningful. if (!Yii::app()->request->getPost('ok')) { $this->clearEmailSessionCache($iSurveyId); $this->showInviteOrReminderEmailForm($iSurveyId, $aSurveyLangs, $aData); } else { $SQLemailstatuscondition = $this->getSQLemailstatuscondition(); $SQLremindercountcondition = $this->getSQLremindercountcondition(); $SQLreminderdelaycondition = $this->getSQLreminderdelaycondition($bEmail); $ctresult = TokenDynamic::model($iSurveyId)->findUninvitedIDs($aTokenIds, 0, $bEmail, $SQLemailstatuscondition, $SQLremindercountcondition, $SQLreminderdelaycondition); $ctcount = count($ctresult); $emresult = TokenDynamic::model($iSurveyId)->findUninvited($aTokenIds, $iMaxEmails, $bEmail, $SQLemailstatuscondition, $SQLremindercountcondition, $SQLreminderdelaycondition); $emcount = count($emresult); foreach ($aSurveyLangs as $language) { // See #08683 : this allow use of {TOKEN:ANYTHING}, directly replaced by {ANYTHING} $sSubject[$language] = preg_replace("/{TOKEN:([A-Z0-9_]+)}/", "{"."$1"."}", Yii::app()->request->getPost('subject_'.$language, '')); $sMessage[$language] = preg_replace("/{TOKEN:([A-Z0-9_]+)}/", "{"."$1"."}", Yii::app()->request->getPost('message_'.$language, '')); if ($bHtml) { $sMessage[$language] = html_entity_decode($sMessage[$language], ENT_QUOTES, Yii::app()->getConfig("emailcharset")); } } $tokenoutput = ""; $bInvalidDate = false; $bSendError = false; if ($emcount > 0) { foreach ($emresult as $emrow) { if ($this->tokenIsSetInEmailCache($iSurveyId, $emrow['tid'])) { // The email has already been send this session, skip. // Happens if user reloads page or double clicks on "Send". if ($bEmail) { $tokenoutput .= sprintf(gT("Invitation %s skipped, already sent."), $emrow['tid']) . "$iq\n"); } } // End for each entry } // End foreach responseGroup } // End of if resnum >= 1 $aData['duplicatelist'] = $duplicatelist; $aData['invalidemaillist'] = $invalidemaillist; $aData['invalidemailcount'] = $invalidemailcount; $aData['resultnum'] = $resultnum; $aData['xv'] = $xv; $aData['xy'] = $xy; $aData['xz'] = $xz; $this->_renderWrappedTemplate('token', array('ldappost'), $aData); } else { $sErrorMessage = ldap_error($ds); define("LDAP_OPT_DIAGNOSTIC_MESSAGE", 0x0032); if (ldap_get_option($ds, LDAP_OPT_DIAGNOSTIC_MESSAGE, $extended_error)) { $sErrorMessage .= ' - '.$extended_error; } $aData['sError'] = sprintf(gT("Can't bind to the LDAP directory. Error message: %s"), ldap_error($ds)); $this->_renderWrappedTemplate('token', array('ldapform'), $aData); } try { @ldap_close($ds); } catch (Exception $e) { // No handling needed } } else { $aData['sError'] = gT("Can't connect to the LDAP directory"); $this->_renderWrappedTemplate('token', array('ldapform'), $aData); } } } /** * import from csv * @param int $iSurveyId * @return void */ public function import($iSurveyId) { $aData = array(); $iSurveyId = (int) $iSurveyId; $survey = Survey::model()->findByPk($iSurveyId); if (!Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'import')) { Yii::app()->session['flashmessage'] = gT("You do not have permission to access this page."); $this->getController()->redirect(array("/admin/survey/sa/view/surveyid/{$iSurveyId}")); } if (!$survey->hasTokensTable) { // If no tokens table exists $this->_newtokentable($iSurveyId); } $aData['sidemenu']['state'] = false; $aData['title_bar']['title'] = $survey->currentLanguageSettings->surveyls_title." (".gT("ID").":".$iSurveyId.")"; $aData['sidemenu']["token_menu"] = true; $aData['token_bar']['closebutton']['url'] = 'admin/tokens/sa/index/surveyid/'.$iSurveyId; App()->getClientScript()->registerScriptFile(App()->getConfig('adminscripts').'tokensimport.js'); $aEncodings = aEncodingsArray(); if (Yii::app()->request->isPostRequest) { $sUploadCharset = Yii::app()->request->getPost('csvcharset'); if (!array_key_exists($sUploadCharset, $aEncodings)) { // Validate sUploadCharset $sUploadCharset = 'auto'; } $bFilterDuplicateToken = Yii::app()->request->getPost('filterduplicatetoken'); $bFilterBlankEmail = Yii::app()->request->getPost('filterblankemail'); $bAllowInvalidEmail = Yii::app()->request->getPost('allowinvalidemail'); $aAttrFieldNames = getAttributeFieldNames($iSurveyId); $aDuplicateList = array(); $aInvalidTokenList = array(); $aInvalidEmailList = array(); // ------------------------ // @EDIT-BLACKLIST Add blacklist $aBlacklist = array(); // ------------------------ $aInvalidFormatList = array(); $aModelErrorList = array(); $aFirstLine = array(); $oFile = CUploadedFile::getInstanceByName("the_file"); $sPath = Yii::app()->getConfig('tempdir'); $sFileName = $sPath.'/'.randomChars(20); if ($_FILES['the_file']['error'] == 1 || $_FILES['the_file']['error'] == 2) { Yii::app()->setFlashMessage(sprintf(gT("Sorry, this file is too large. Only files up to %01.2f MB are allowed."), getMaximumFileUploadSize() / 1024 / 1024), 'error'); } elseif (strtolower($oFile->getExtensionName()) != 'csv') { Yii::app()->setFlashMessage(gT("Only CSV files are allowed."), 'error'); } elseif (!@$oFile->saveAs($sFileName)) { Yii::app()->setFlashMessage(sprintf(gT("Upload file not found. Check your permissions and path (%s) for the upload directory"), $sPath), 'error'); } else { $iRecordImported = 0; $iRecordCount = 0; $iRecordOk = 0; $iInvalidEmailCount = 0; // Count invalid email imported // This allows to read file with MAC line endings too @ini_set('auto_detect_line_endings', '1'); // open it and trim the endings $aTokenListArray = file($sFileName, FILE_SKIP_EMPTY_LINES); $sBaseLanguage = Survey::model()->findByPk($iSurveyId)->language; if (!Yii::app()->request->getPost('filterduplicatefields') || (Yii::app()->request->getPost('filterduplicatefields') && count(Yii::app()->request->getPost('filterduplicatefields')) == 0)) { $aFilterDuplicateFields = array('firstname', 'lastname', 'email'); } else { $aFilterDuplicateFields = Yii::app()->request->getPost('filterduplicatefields'); } $sSeparator = Yii::app()->request->getPost('separator'); $aMissingAttrFieldName = $aInvalideAttrFieldName = array(); // ------------------------ // @EDIT Remove first 2 rows (csv header starts at row 3) $aTokenListArray = array_splice($aTokenListArray, 2); // ------------------------ foreach ($aTokenListArray as $buffer) { $buffer = @mb_convert_encoding($buffer, "UTF-8", $sUploadCharset); if ($iRecordCount == 0) { // Parse first line (header) from CSV $buffer = removeBOM($buffer); // We alow all field except tid because this one is really not needed. $aAllowedFieldNames = Token::model($iSurveyId)->tableSchema->getColumnNames(); if (($kTid = array_search('tid', $aAllowedFieldNames)) !== false) { unset($aAllowedFieldNames[$kTid]); } // Some header don't have same column name $aReplacedFields = array( 'invited'=>'sent', 'reminded'=>'remindersent', // ------------------------ // @EDIT Custom 'No.'=>'tid', 'Vorname'=>'firstname', 'Name'=>'lastname', 'Kontakt/E-Mail-Adresse'=>'email', // ------------------------ ); switch ($sSeparator) { case 'comma': $sSeparator = ','; break; case 'semicolon': $sSeparator = ';'; break; default: $comma = substr_count($buffer, ','); $semicolon = substr_count($buffer, ';'); if ($semicolon > $comma) { $sSeparator = ';'; } else { $sSeparator = ','; } } $aFirstLine = str_getcsv($buffer, $sSeparator, '"'); $aFirstLine = array_map('trim', $aFirstLine); $aIgnoredColumns = array(); // ------------------------ // @EDIT Functionality to find an attribute by its description //2018-09-27 START $oSurvey = Survey::model()->findByPk($iSurveyId); $thissurvey = $oSurvey->attributes; $aAdditionalAttributeFields = $oSurvey->decodedAttributedescriptions; //OLD:$surveyAttributes = json_decode($surveyinfo['attributedescriptions'], true); //NEW: //$surveyAttributes = json_decode($surveyinfo['aAdditionalAttributeFields'], true); //2018-09-27 END function findAttributeByDescription($attributes, $description) { $index = 0; foreach ($attributes as $element) { if ($description == $element["description"]) { return array_keys($attributes)[$index]; } else { $index += 1; } } return null; } // ------------------------ // Now check the first line for invalid fields foreach ($aFirstLine as $index => $sFieldname) { $aFirstLine[$index] = preg_replace("/(.*) <[^,]*>$/", "$1", $sFieldname); $sFieldname = $aFirstLine[$index]; if (!in_array($sFieldname, $aAllowedFieldNames)) { $aIgnoredColumns[] = $sFieldname; } if (array_key_exists($sFieldname, $aReplacedFields)) { $aFirstLine[$index] = $aReplacedFields[$sFieldname]; } // ------------------------ // @EDIT If field was not already replaced, check if it is a valid attribute else { $attributeName = findAttributeByDescription($aAdditionalAttributeFields, $sFieldname); if ($attributeName !== null) { $aFirstLine[$index] = $attributeName; } } // ------------------------ // Attribute not in list if (strpos($aFirstLine[$index], 'attribute_') !== false and !in_array($aFirstLine[$index], $aAttrFieldNames) and Yii::app()->request->getPost('showwarningtoken')) { $aInvalideAttrFieldName[] = $aFirstLine[$index]; } } //compare attributes with source csv if (Yii::app()->request->getPost('showwarningtoken')) { $aMissingAttrFieldName = array_diff($aAttrFieldNames, $aFirstLine); // get list of mandatory attributes $allAttrFieldNames = getParticipantAttributes($iSurveyId); //if it isn't mandantory field we don't need to show in warning if (!empty($aAttrFieldNames)) { if (!empty($aMissingAttrFieldName)) { foreach ($aMissingAttrFieldName as $index=>$AttrFieldName) { if (isset($allAttrFieldNames[$AttrFieldName]) and strtolower($allAttrFieldNames[$AttrFieldName]["mandatory"]) != "y") { unset($aMissingAttrFieldName[$index]); } } } if (isset($aInvalideAttrFieldName) and !empty($aInvalideAttrFieldName)) { foreach ($aInvalideAttrFieldName as $index=>$AttrFieldName) { if (isset($allAttrFieldNames[$AttrFieldName]) and strtolower($allAttrFieldNames[$AttrFieldName]["mandatory"]) != "y") { unset($aInvalideAttrFieldName[$index]); } } } } } } else { $line = str_getcsv($buffer, $sSeparator, '"'); if (count($aFirstLine) != count($line)) { $aInvalidFormatList[] = sprintf(gT("Line %s"), $iRecordCount); $iRecordCount++; continue; } $aWriteArray = array_combine($aFirstLine, $line); //kick out ignored columns foreach ($aIgnoredColumns as $column) { unset($aWriteArray[$column]); } $bDuplicateFound = false; $bInvalidEmail = false; // ------------------------ // @EDIT-BLACKLIST Add blacklist state $bBlacklist = false; // ------------------------ $bInvalidToken = false; $aWriteArray['email'] = isset($aWriteArray['email']) ? trim($aWriteArray['email']) : ""; $aWriteArray['firstname'] = isset($aWriteArray['firstname']) ? $aWriteArray['firstname'] : ""; $aWriteArray['lastname'] = isset($aWriteArray['lastname']) ? $aWriteArray['lastname'] : ""; $aWriteArray['language'] = isset($aWriteArray['language']) ? $aWriteArray['language'] : $sBaseLanguage; if ($bFilterDuplicateToken) { $aParams = array(); $oCriteria = new CDbCriteria; $oCriteria->condition = ""; foreach ($aFilterDuplicateFields as $field) { if (isset($aWriteArray[$field])) { $oCriteria->addCondition("{$field} = :{$field}"); $aParams[":{$field}"] = $aWriteArray[$field]; } } if (!empty($aParams)) { $oCriteria->params = $aParams; } $dupresult = TokenDynamic::model($iSurveyId)->count($oCriteria); if ($dupresult > 0) { $bDuplicateFound = true; $aDuplicateList[] = sprintf(gT("Line %s : %s %s (%s)"), $iRecordCount, $aWriteArray['firstname'], $aWriteArray['lastname'], $aWriteArray['email']); } } //treat blank emails if (!$bDuplicateFound && $bFilterBlankEmail && $aWriteArray['email'] == '') { $bInvalidEmail = true; $aInvalidEmailList[] = sprintf(gT("Line %s : %s %s"), $iRecordCount, CHtml::encode($aWriteArray['firstname']), CHtml::encode($aWriteArray['lastname'])); } // ------------------------ // @EDIT-BLACKLIST Check if email address is blacklisted if ($aWriteArray['email'] != '') { $cpdbParticipants = Participant::model()->getParticipantsWithoutLimit(); foreach ($cpdbParticipants as $cpdbParticipant) { if ($cpdbParticipant["email"] == $aWriteArray["email"] && $cpdbParticipant["blacklisted"] == "Y") { $bBlacklist = true; $aBlacklist[] = sprintf(gT("Line %s : %s %s (%s)"), $iRecordCount, CHtml::encode($aWriteArray['firstname']), CHtml::encode($aWriteArray['lastname']), $aWriteArray['email']); } } } // ------------------------ if (!$bDuplicateFound && $aWriteArray['email'] != '') { $aEmailAddresses = preg_split("/(,|;)/", $aWriteArray['email']); foreach ($aEmailAddresses as $sEmailaddress) { if (!validateEmailAddress($sEmailaddress)) { if ($bAllowInvalidEmail) { $iInvalidEmailCount++; if (empty($aWriteArray['emailstatus']) || strtoupper($aWriteArray['emailstatus'] == "OK")) { $aWriteArray['emailstatus'] = "invalid"; } } else { $bInvalidEmail = true; $aInvalidEmailList[] = sprintf(gT("Line %s : %s %s (%s)"), $iRecordCount, CHtml::encode($aWriteArray['firstname']), CHtml::encode($aWriteArray['lastname']), CHtml::encode($aWriteArray['email'])); } } } } // ------------------------ // @EDIT-BLACKLIST Add !$bBlacklist check if (!$bDuplicateFound && !$bInvalidEmail && !$bBlacklist && isset($aWriteArray['token']) && trim($aWriteArray['token'])!='') { if (trim($aWriteArray['token']) != Token::sanitizeToken($aWriteArray['token'])) { $aInvalidTokenList[] = sprintf(gT("Line %s : %s %s (%s) - token : %s"), $iRecordCount, CHtml::encode($aWriteArray['firstname']), CHtml::encode($aWriteArray['lastname']), CHtml::encode($aWriteArray['email']), CHtml::encode($aWriteArray['token'])); $bInvalidToken = true; } // We allways search for duplicate token (it's in model. Allow to reset or update token ? if (Token::model($iSurveyId)->count("token=:token", array(":token"=>$aWriteArray['token']))) { $bDuplicateFound = true; $aDuplicateList[] = sprintf(gT("Line %s : %s %s (%s) - token : %s"), $iRecordCount, CHtml::encode($aWriteArray['firstname']), CHtml::encode($aWriteArray['lastname']), CHtml::encode($aWriteArray['email']), CHtml::encode($aWriteArray['token'])); } } // ------------------------ // ------------------------ // @EDIT-BLACKLIST Add !$bBlacklist check if (!$bDuplicateFound && !$bInvalidEmail && !$bBlacklist && !$bInvalidToken) { // unset all empty value foreach ($aWriteArray as $key=>$value) { if ($aWriteArray[$key] == "") { unset($aWriteArray[$key]); } if (substr($value, 0, 1) == '"' && substr($value, -1) == '"') { // Fix CSV quote $aWriteArray[$key] = substr($value, 1, -1); } } // Some default value : to be moved to Token model rules in future release ? // But think we have to accept invalid email etc ... then use specific scenario $oToken = Token::create($iSurveyId); if ($bAllowInvalidEmail) { $oToken->scenario = 'allowinvalidemail'; } foreach ($aWriteArray as $key => $value) { $oToken->$key = $value; } if (!$oToken->save()) { $errors = ($oToken->getErrors()); $aModelErrorList[] = sprintf(gT("Line %s : %s"), $iRecordCount, print_r($errors, true)); } else { $iRecordImported++; } } // ------------------------ $iRecordOk++; } $iRecordCount++; } $iRecordCount = $iRecordCount - 1; unlink($sFileName); $aData['aTokenListArray'] = $aTokenListArray; // Big array in memory, just for success ? $aData['iRecordImported'] = $iRecordImported; $aData['iRecordOk'] = $iRecordOk; $aData['iRecordCount'] = $iRecordCount; $aData['aFirstLine'] = $aFirstLine; // Seem not needed $aData['aDuplicateList'] = $aDuplicateList; $aData['aInvalidTokenList'] = $aInvalidTokenList; $aData['aInvalidFormatList'] = $aInvalidFormatList; $aData['aInvalidEmailList'] = $aInvalidEmailList; // ------------------------ // @EDIT-BLACKLIST Add blacklist $aData['aBlacklist'] = $aBlacklist; // ------------------------ $aData['aModelErrorList'] = $aModelErrorList; $aData['iInvalidEmailCount'] = $iInvalidEmailCount; $aData['thissurvey'] = getSurveyInfo($iSurveyId); $aData['iSurveyId'] = $aData['surveyid'] = $iSurveyId; $aData['aInvalideAttrFieldName'] = $aInvalideAttrFieldName; $aData['aMissingAttrFieldName'] = $aMissingAttrFieldName; $this->_renderWrappedTemplate('token', array('csvimportresult'), $aData); Yii::app()->end(); } } // If there are error with file : show the form $aData['aEncodings'] = $aEncodings; asort($aData['aEncodings']); $aData['iSurveyId'] = $iSurveyId; $aData['thissurvey'] = getSurveyInfo($iSurveyId); $aData['surveyid'] = $iSurveyId; $aTokenTableFields = getTokenFieldsAndNames($iSurveyId); unset($aTokenTableFields['sent']); unset($aTokenTableFields['remindersent']); unset($aTokenTableFields['remindercount']); unset($aTokenTableFields['usesleft']); foreach ($aTokenTableFields as $sKey=>$sValue) { if ($sValue['description'] != $sKey) { $sValue['description'] .= ' - '.$sKey; } $aNewTokenTableFields[$sKey] = $sValue['description']; } $aData['aTokenTableFields'] = $aNewTokenTableFields; // Get default character set from global settings $thischaracterset = getGlobalSetting('characterset'); // If no encoding was set yet, use the old "auto" default if ($thischaracterset == "") { $thischaracterset = "auto"; } $aData['thischaracterset'] = $thischaracterset; $this->_renderWrappedTemplate('token', array('csvupload'), $aData); } /** * Generate tokens * @param int $iSurveyId * @return void */ public function tokenify($iSurveyId) { $iSurveyId = (int) $iSurveyId; $survey = Survey::model()->findByPk($iSurveyId); if (!Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'update')) { Yii::app()->session['flashmessage'] = gT("You do not have permission to access this page."); $this->getController()->redirect(array("/admin/survey/sa/view/surveyid/{$iSurveyId}")); } if (!$survey->hasTokensTable) { // If no tokens table exists $this->_newtokentable($iSurveyId); } $aData = array(); $aData['thissurvey'] = getSurveyInfo($iSurveyId); $aData['surveyid'] = $iSurveyId; $aData['sidemenu']['state'] = false; $aData['title_bar']['title'] = $survey->currentLanguageSettings->surveyls_title." (".gT("ID").":".$iSurveyId.")"; $aData['sidemenu']["token_menu"] = true; if (!Yii::app()->request->getParam('ok')) { $aData['sidemenu']['state'] = false; $this->_renderWrappedTemplate('token', array('message' => array( 'title' => gT("Create tokens"), 'message' => gT("Clicking 'Yes' will generate tokens for all those in this token list that have not been issued one. Continue?")."