Skip to content

Commit

Permalink
Dev Added code completion for Token::model($surveyId) and created helper
Browse files Browse the repository at this point in the history
function Token::create($surveyId, $scenario) to enable code completion.
  • Loading branch information
SamMousa committed Sep 17, 2013
1 parent 1778c8a commit 0f17969
Show file tree
Hide file tree
Showing 20 changed files with 293 additions and 272 deletions.
3 changes: 1 addition & 2 deletions application/controllers/OptinController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ function actiontokens($surveyid, $token, $langcode = '')
}
else
{
$tokenClass = "Token_$iSurveyID";
$tokenInstance = $tokenClass::model()->findByAttributes(array('token' => $token));
$tokenInstance = Token::model($iSurveyID)->findByAttributes(array('token' => $token));

if (!isset($tokenInstance))
{
Expand Down
6 changes: 2 additions & 4 deletions application/controllers/OptoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ function actiontokens()
}
else
{
$tokenClass = "Token_$iSurveyID";
$tokenInstance = $tokenClass::model()->findByAttributes($sToken);
$tokenInstance = Token::model($iSurveyID)->findByAttributes($sToken);

if (!isset($tokenInstance))
{
Expand Down Expand Up @@ -154,8 +153,7 @@ function actionparticipants()
}
else
{
$tokenClass = "Token_$iSurveyID";
$tokenInstance = $tokenClass::model()->findByAttributes(array('token' => $sToken));
$tokenInstance = Token::model($iSurveyID)->findByAttributes(array('token' => $sToken));
if (!isset($tokenInstance))
{
$html = $clang->gT('You are not a participant in this survey.');
Expand Down
3 changes: 1 addition & 2 deletions application/controllers/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ function actionIndex($surveyid = null)
$postattribute2=sanitize_xss_string(strip_tags(returnGlobal('register_attribute2'))); */

// Insert new entry into tokens db
$tokenClass = "Token_{$thissurvey['sid']}";
$token = new $tokenClass();
$token = Token::create($thissurvey['sid']);
$token->firstname = $postfirstname;
$token->lastname = $postlastname;
$token->email = Yii::app()->request->getPost('register_email');
Expand Down
3 changes: 1 addition & 2 deletions application/controllers/admin/responses.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,7 @@ public function index($iSurveyID)
if (tableExists('{{tokens_' . $iSurveyID . '}}') && Permission::model()->hasSurveyPermission($iSurveyID,'tokens','read'))
{
$aData['with_token']= Yii::app()->db->schema->getTable('{{tokens_' . $iSurveyID . '}}');
$tokenClass = "Token_$iSurveyID";
$aData['tokeninfo'] = $tokenClass::model()->summary();
$aData['tokeninfo'] = Token::model($iSurveyID)->summary();
}

$aViewUrls[] = 'browseindex_view';
Expand Down
3 changes: 1 addition & 2 deletions application/controllers/admin/surveyadmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,7 @@ public function getSurveys_json()
$aSurveyEntry['viewurl'] = $this->getController()->createUrl("/admin/survey/sa/view/surveyid/" . $rows['sid']);
if (tableExists('tokens_' . $rows['sid'] ))
{
$tokenClass = "Token_{$rows['sid']}";
$summary = $tokenClass::model()->summary();
$summary = Token::model($rows['sid'])->summary();
$tokens = $summary['count'];
$tokenscompleted = $summary['completed'];

Expand Down
57 changes: 22 additions & 35 deletions application/controllers/admin/tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ function index($iSurveyId)
TokenDynamic::model($iSurveyId)->checkColumns();
$aData['thissurvey'] = $thissurvey;
$aData['surveyid'] = $iSurveyId;
$tokenClass = "Token_$iSurveyId";
$aData['queries'] = $tokenClass::model()->summary();
$aData['queries'] = Token::model($iSurveyId)->summary();
$this->_renderWrappedTemplate('token', array('tokenbar', 'tokensummary'), $aData);
}
}
Expand Down Expand Up @@ -202,8 +201,7 @@ function bounceprocessing($iSurveyId)
'emailstatus' => 'bounced'
);
$condn = array('token' => $tokenBounce[1]);
$tokenClass = "Token_$iSurveyId";
$record = $tokenClass::model()->findByAttributes($condn);
$record = Token::model($iSurveyId)->findByAttributes($condn);
foreach ($aData as $k => $v)
$record->$k = $v;
$record->save();
Expand Down Expand Up @@ -287,8 +285,7 @@ function browse($iSurveyId, $limit = 50, $start = 0, $order = false, $searchstri
$limit = (int) $limit;
$start = (int) $start;

$tokenClass = "Token_$iSurveyId";
$tkcount = $tokenClass::model()->count();
$tkcount = Token::model($iSurveyId)->count();
$next = $start + $limit;
$last = $start - $limit;
$end = $tkcount - $limit;
Expand Down Expand Up @@ -334,7 +331,7 @@ function browse($iSurveyId, $limit = 50, $start = 0, $order = false, $searchstri
$iquery = '(' . implode(' OR ', $iquery) . ')';
}

$tokens = $tokenClass::model()->findAll(array('condition' => $iquery, 'limit' => $limit, 'offset' => $start, 'order' => $order));
$tokens = Token::model($iSurveyId)->findAll(array('condition' => $iquery, 'limit' => $limit, 'offset' => $start, 'order' => $order));
$aData['bresult'] = array();
foreach ($tokens as $token)
{
Expand Down Expand Up @@ -393,12 +390,11 @@ function getTokens_json($iSurveyId, $search = null)
$condition->order = $sidx. " ". $sord;
$condition->offset = ($page - 1) * $limit;
$condition->limit = $limit;
$tokenClass = "Token_$iSurveyId";
$tokens = $tokenClass::model()->findAll($condition);
$tokens = Token::model($iSurveyId)->findAll($condition);

$condition->offset=0;
$condition->limit=0;
$aData->records = $tokenClass::model()->count($condition);
$aData->records = Token::model($iSurveyId)->count($condition);

if ($limit>$aData->records)
{
Expand Down Expand Up @@ -574,8 +570,7 @@ function editToken($iSurveyId) // Used ? 2013-01-29
$this->getController()->error(sprintf($this->controller->lang->gT('%s cannot be left empty'), $desc['description']));
$aData[$attr_name] = Yii::app()->request->getPost($attr_name);
}
$tokenClass = "Token_$iSurveyId";
$token = $tokenClass::model()->find('tid=' . Yii::app()->getRequest()->getPost('id'));
$token = Token::model($iSurveyId)->find('tid=' . Yii::app()->getRequest()->getPost('id'));

foreach ($aData as $k => $v)
$token->$k = $v;
Expand Down Expand Up @@ -607,7 +602,7 @@ function editToken($iSurveyId) // Used ? 2013-01-29
$aData[$attr_name] = Yii::app()->request->getPost($attr_name);
}
echo ls_json_encode(var_export($aData));
$token = new $tokenClass('insert');
$token = Token::create($surveyId);
$token->setAttributes($aData, false);
echo $token->save();
}
Expand Down Expand Up @@ -720,12 +715,11 @@ function addnew($iSurveyId)
$aData[$attr_name] = Yii::app()->getRequest()->getPost($attr_name);
}

$tokenClass = "Token_$iSurveyId";
$udresult = $tokenClass::model()->findAll("token <> '' and token = '$sanitizedtoken'");
$udresult = Token::model($iSurveyId)->findAll("token <> '' and token = '$sanitizedtoken'");
if (count($udresult) == 0)
{
// AutoExecute
$token = new $tokenClass('insert', $iSurveyId);
$token = Token::create($iSurveyId);
$token->setAttributes($aData, false);
$inresult = $token->save();
$aData['success'] = true;
Expand Down Expand Up @@ -809,8 +803,7 @@ function edit($iSurveyId, $iTokenId)
$aTokenData['validuntil'] = Yii::app()->request->getPost('validuntil');
$aTokenData['remindersent'] = Yii::app()->request->getPost('remindersent');
$aTokenData['remindercount'] = intval(Yii::app()->request->getPost('remindercount'));
$tokenClass = "Token_$iSurveyId";
$udresult = $tokenClass::model()->findAll("tid <> '$iTokenId' and token <> '' and token = '$santitizedtoken'");
$udresult = Token::model($iSurveyId)->findAll("tid <> '$iTokenId' and token <> '' and token = '$santitizedtoken'");

if (count($udresult) == 0)
{
Expand All @@ -825,7 +818,7 @@ function edit($iSurveyId, $iTokenId)
$aTokenData[$attr_name] = Yii::app()->request->getPost($attr_name);
}

$token = $tokenClass::model()->findByPk($iTokenId);
$token = Token::model($iSurveyId)->findByPk($iTokenId);
foreach ($aTokenData as $k => $v)
$token->$k = $v;
$token->save();
Expand Down Expand Up @@ -880,8 +873,7 @@ function delete($iSurveyID)
SurveyLink::model()->deleteTokenLink($aTokenIds, $iSurveyID);

//Then delete the tokens
$tokenClass = "Token_$iSurveyID";
$tokenClass::model()->deleteByPk($aTokenIds);
Token::model($iSurveyID)->deleteByPk($aTokenIds);
}
}

Expand Down Expand Up @@ -959,10 +951,9 @@ function addDummies($iSurveyId, $subaction = '')
$tokenlength = sanitize_int(Yii::app()->request->getPost('tokenlen'));

// Fill an array with all existing tokens
$tokenClass = "Token_$iSurveyId";
$criteria = $tokenClass::model()->getDbCriteria();
$criteria = Token::model($iSurveyId)->getDbCriteria();
$criteria->select = 'token';
$ntresult = $tokenClass::model()->findAll($criteria);
$ntresult = Token::model($iSurveyId)->findAll($criteria);
$existingtokens=array();
foreach ($ntresult as $tkrow)
{
Expand All @@ -972,7 +963,7 @@ function addDummies($iSurveyId, $subaction = '')
$newDummyToken=0;
while ($newDummyToken < $amount && $invalidtokencount < 50)
{
$token = new $tokenClass();
$token = Token::create($iSurveyId);
$token->setAttributes($aData, false);

$token->firstname = str_replace('{TOKEN_COUNTER}', $newDummyToken, $token->firstname);
Expand Down Expand Up @@ -1020,8 +1011,8 @@ function addDummies($iSurveyId, $subaction = '')
}
else
{
$tkcount = $tokenClass::model()->count();
$tokenlength = !empty($tokenClass::model()->survey->tokenlength) ? $tokenClass::model()->survey->tokenlength : 15;
$tkcount = Token::model($iSurveyId)->count();
$tokenlength = !empty(Token::model($iSurveyId)->survey->tokenlength) ? Token::model($iSurveyId)->survey->tokenlength : 15;

$thissurvey = getSurveyInfo($iSurveyId);
$aData['thissurvey'] = $thissurvey;
Expand Down Expand Up @@ -1271,8 +1262,7 @@ function email($iSurveyId, $tokenids = null)
Yii::app()->loadHelper('/admin/htmleditor');
Yii::app()->loadHelper('replacements');

$tokenClass = "Token_$iSurveyId";
$token = $tokenClass::model()->find();
$token = Token::model($iSurveyId)->find();

$aExampleRow = isset($token) ? $token->attributes : array();
$aSurveyLangs = Survey::model()->findByPk($iSurveyId)->additionalLanguages;
Expand Down Expand Up @@ -1471,8 +1461,7 @@ function email($iSurveyId, $tokenids = null)
if (SendEmailMessage($modmessage, $modsubject, $to, $from, Yii::app()->getConfig("sitename"), $bHtml, getBounceEmail($iSurveyId), $aRelevantAttachments, $customheaders))
{
// Put date into sent
$tokenClass = "Token_$iSurveyId";
$token = $tokenClass::model()->findByPk($emrow['tid']);
$token = Token::model($iSurveyId)->findByPk($emrow['tid']);
if ($bEmail)
{
$tokenoutput .= $clang->gT("Invitation sent to:");
Expand Down Expand Up @@ -1570,8 +1559,7 @@ function exportdialog($iSurveyId)
}
else
{
$tokenClass = "Token_$iSurveyId";
$aData['resultr'] = $tokenClass::model()->findAll(array('select' => 'language', 'group' => 'language'));
$aData['resultr'] = Token::model($iSurveyId)->findAll(array('select' => 'language', 'group' => 'language'));
$aData['thissurvey'] = getSurveyInfo($iSurveyId);
$aData['surveyid'] = $iSurveyId;

Expand Down Expand Up @@ -2355,8 +2343,7 @@ function _handletokenform($iSurveyId, $subaction, $iTokenId="")
if ($subaction == "edit")
{
$aData['tokenid'] = $iTokenId;
$tokenClass = "Token_$iSurveyId";
$aData['tokendata'] = $tokenClass::model()->findByPk($iTokenId);
$aData['tokendata'] = Token::model($iSurveyId)->findByPk($iTokenId);
}

$thissurvey = getSurveyInfo($iSurveyId);
Expand Down
11 changes: 5 additions & 6 deletions application/controllers/survey/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,15 +458,14 @@ function sendreq(surveyid)
// bypass only this check at first page (Step=0) because
// this check is done in buildsurveysession and error message
// could be more interresting there (takes into accound captcha if used)
$tokenClass = "Token_$surveyid";
if ($tokensexist == 1 && isset($token) && $token &&
if ($tokensexist == 1 && isset($token) && $token &&
isset($_SESSION['survey_'.$surveyid]['step']) && $_SESSION['survey_'.$surveyid]['step']>0 && tableExists("tokens_{$surveyid}}}"))
{
// check also if it is allowed to change survey after completion
if ($thissurvey['alloweditaftercompletion'] == 'Y' ) {
$tokenInstance = $tokenClass::model()->findByAttributes(array('token' => $token));
$tokenInstance = Token::model($surveyid)->findByAttributes(array('token' => $token));
} else {
$tokenInstance = $tokenClass::model()->usable()->incomplete()->findByAttributes(array('token' => $token));
$tokenInstance = Token::model($surveyid)->usable()->incomplete()->findByAttributes(array('token' => $token));
}

if (!isset($tokenInstance) && !$previewmode)
Expand All @@ -491,9 +490,9 @@ function sendreq(surveyid)
{
// check also if it is allowed to change survey after completion
if ($thissurvey['alloweditaftercompletion'] == 'Y' ) {
$tokenInstance = $tokenClass::model()->usable()->findByAttributes(array('token' => $token));
$tokenInstance = Token::model($surveyid)->usable()->findByAttributes(array('token' => $token));
} else {
$tokenInstance = $tokenClass::model()->usable()->incomplete()->findByAttributes(array('token' => $token));
$tokenInstance = Token::model($surveyid)->usable()->incomplete()->findByAttributes(array('token' => $token));

}
if (!isset($tokenInstance))
Expand Down
4 changes: 2 additions & 2 deletions application/core/LSYii_Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ public function __construct($config = null)
public function init() {
parent::init();
Yii::import('application.helpers.ClassFactory');
ClassFactory::registerClass('Token_', 'Token2');
ClassFactory::registerClass('Response_', 'Response2');
ClassFactory::registerClass('Token_', 'Token');
ClassFactory::registerClass('Response_', 'Response');
}
/**
* This method handles initialization of the plugin manager
Expand Down
5 changes: 2 additions & 3 deletions application/helpers/admin/import_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4058,8 +4058,7 @@ function XMLImportTokens($sFullFilepath,$iSurveyID,$sCreateMissingAttributeField
}

switchMSSQLIdentityInsert('tokens_'.$iSurveyID,true);
$tokenClass = "Token_$iSurveyID";
foreach ($xml->tokens->rows->row as $row)
foreach ($xml->tokens->rows->row as $row)
{
$insertdata=array();

Expand All @@ -4068,7 +4067,7 @@ function XMLImportTokens($sFullFilepath,$iSurveyID,$sCreateMissingAttributeField
$insertdata[(string)$key]=(string)$value;
}

$token = new $tokenClass();
$token = Token::create($iSurveyID);
$token->setAttributes($insertdata, false);
$result = $token->save() or safeDie($clang->gT("Error").": " . print_r($token->errors, true));

Expand Down
3 changes: 1 addition & 2 deletions application/helpers/admin/token_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ function emailTokens($iSurveyID,$aResultTokens,$sType)

$attributes = array_keys(getTokenFieldsAndNames($iSurveyID));
$oSurveyLocale=SurveyLanguageSetting::model()->findAllByAttributes(array('surveyls_survey_id' => $iSurveyID));
$tokenClass = "Token_$iSurveyID";
$oTokens = $tokenClass::model();
$oTokens = Token::model($iSurveyID);
$aSurveyLangs = $oSurvey->additionalLanguages;
array_unshift($aSurveyLangs, $oSurvey->language);

Expand Down
3 changes: 1 addition & 2 deletions application/helpers/common_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5543,8 +5543,7 @@ function getAttributeValue($surveyid,$attrName,$token)
return null;
}

$tokenClass = "Token_$surveyid";
$token = $tokenClass::model()->findByAttributes(array("token"=>$token));
$token = Token::model($surveyid)->findByAttributes(array("token"=>$token));
return isset($token->$attrName) ? $token->$attrName : null;
}

Expand Down
3 changes: 1 addition & 2 deletions application/helpers/export_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1779,8 +1779,7 @@ function tokensExport($iSurveyID)

if (Yii::app()->request->getPost('tokendeleteexported') && !empty($aExportedTokens))
{
$tokenClass = "Token_$iSurveyID";
$tokenClass::model()->deleteByPk($aExportedTokens);
Token::model($iSurveyID)->deleteByPk($aExportedTokens);
}
}

Expand Down
3 changes: 1 addition & 2 deletions application/helpers/expressions/em_manager_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3460,8 +3460,7 @@ private function setVariableAndTokenMappingsForExpressionManager($surveyid,$forc
if (isset($_SESSION[$this->sessid]['token']) && $_SESSION[$this->sessid]['token'] != '')
{
//Gather survey data for tokenised surveys, for use in presenting questions
$tokenClass = "Token_$surveyid";
$_SESSION[$this->sessid]['thistoken'] = $tokenClass::model()->findByToken($_SESSION[$this->sessid]['token']);
$_SESSION[$this->sessid]['thistoken'] = Token::model($surveyid)->findByToken($_SESSION[$this->sessid]['token']);
$this->knownVars['TOKEN:TOKEN'] = array(

This comment has been minimized.

Copy link
@SamMousa

SamMousa Sep 20, 2013

Author Contributor

@c-schmitz Is it supposed to make the token available in anonymized surveys?
-- It seems to check that only for the other attributes..

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Sep 20, 2013

Collaborator

@SamMousa : think EM core replace TOKEN:... by emptry line. But right : no need to put token information in session, except : token code+submitdate

This comment has been minimized.

Copy link
@c-schmitz

c-schmitz via email Sep 20, 2013

Contributor

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Sep 20, 2013

Collaborator

Yes, needed:
token (code)
token->submitdate (if another user open the token on another browser we don't need 2 token line in responses)
token->useleft (same then submitdate)

All other are not needed ?

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Sep 20, 2013

Collaborator

Correction:
We have already [$this->sessid]['token'], thistoken arry is used for replacement

'code'=>$_SESSION[$this->sessid]['token'],
'jsName_on'=>'',
Expand Down

5 comments on commit 0f17969

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure is linked, but now i have:
array_keys() expects parameter 1 to be array, object given
/application/helpers/expressions/em_manager_helper.php(3473)

  foreach (array_keys($_SESSION[$this->sessid]['thistoken']) as $tokenkey)

Myabe we have to allways have:

  $_SESSION['survey_'.$surveyid]['thistoken'] = Token::model($surveyid)->findByToken($_SESSION['survey_'.$surveyid]['token'])->attributes;

I do soome test

@SamMousa
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shnoulle that is definitely better; its just one query and the session is a mess anyway.

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have some other bug with new token model .
Can not update token/remove token etc ...
Can you have a look ? Or did you need a bug report ;)

Here, i think EM use $_SESSION part for replacing only (EM core), then need a array for token in Session.

Update EM core can be done : but need more time than toda ;)

@SamMousa
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug is a CSRF bug as far as I can tell, this was solved in most cases but not the jqGrid. Please make a bug report (and note that this is not related to the token model).

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, have some other js issue in 'jqGrid' BUT /admin/tokens/sa/edit/surveyid/439566/tokenid/1 (accessible via browse response) have some 500 too. I lokk for js and maybe for CRSF
You're right for CRSF, sorry. I have a look

Please sign in to comment.