Skip to content

Commit

Permalink
Dev Created better dynamic models
Browse files Browse the repository at this point in the history
Dev Created (new) dynamic token model and replaced most usages of the old
one.
  • Loading branch information
SamMousa committed Sep 2, 2013
1 parent 8235c5c commit 8072410
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 226 deletions.
11 changes: 10 additions & 1 deletion application/config/internal.php
Expand Up @@ -48,7 +48,16 @@
'user' => array(
'class' => 'LSWebUser',
),

'log' => array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class' => 'CWebLogRoute',
//'enabled' => ,
'categories' => 'system.db.*',
),
)
)
)
);

Expand Down
12 changes: 6 additions & 6 deletions application/controllers/OptinController.php
Expand Up @@ -64,21 +64,21 @@ function actiontokens($surveyid, $token, $langcode = '')
}
else
{
$row = TokenDynamic::model($iSurveyID)->getEmailStatus($sToken);
$tokenInstance = Token::model(null, $iSurveyID)->findByAttributes(array('token' => $token));

if ($row == false)
if (!isset($tokenInstance))
{
$html = $clang->gT('You are not a participant in this survey.');
}
else
{
$usresult = $row['emailstatus'];
if ($usresult=='OptOut')
if ($tokenInstance->emailstatus =='OptOut')
{
$usresult = TokenDynamic::model($iSurveyID)->updateEmailStatus($sToken, 'OK');
$tokenInstance->emailstatus = 'OK';
$tokenInstance->save();
$html = $clang->gT('You have been successfully added back to this survey.');
}
else if ($usresult=='OK')
elseif ($tokenInstance->emailstatus == 'OK')
{
$html = $clang->gT('You are already a part of this survey.');
}
Expand Down
26 changes: 12 additions & 14 deletions application/controllers/OptoutController.php
Expand Up @@ -70,18 +70,18 @@ function actiontokens()
}
else
{
$row = TokenDynamic::model($iSurveyID)->getEmailStatus($sToken);
$tokenInstance = Token::model(null, $iSurveyID)->findByAttributes($sToken);

if ($row == false)
if (!isset($tokenInstance))
{
$html = $clang->gT('You are not a participant in this survey.');
}
else
{
$usresult = $row['emailstatus'];
if ($usresult == 'OK')
if ($tokenInstance->emailstatus == 'OK')
{
$usresult = TokenDynamic::model($iSurveyID)->updateEmailStatus($sToken, 'OptOut');
$tokenInstance->emailstatus = 'OptOut';
$tokenInstance->save();
$html = $clang->gT('You have been successfully removed from this survey.');
}
else
Expand Down Expand Up @@ -153,29 +153,27 @@ function actionparticipants()
}
else
{
$row = TokenDynamic::model($iSurveyID)->getEmailStatus($sToken);
$datas = TokenDynamic::model($iSurveyID)->find('token = :token', array(":token"=>$sToken));

if ($row == false)
$tokenInstance = Token::model(null, $iSurveyID)->findByAttributes(array('token' => $sToken));
if (!isset($tokenInstance))
{
$html = $clang->gT('You are not a participant in this survey.');
}
else
{
$usresult = $row['emailstatus'];
if ($usresult == 'OK')
if ($tokenInstance->emailstatus == 'OK')
{
$usresult = TokenDynamic::model($iSurveyID)->updateEmailStatus($sToken, 'OptOut');
$tokenInstance->emailstatus = 'OptOut';
$tokenInstance->save();
$html = $clang->gT('You have been successfully removed from this survey.');
}
else
{
$html = $clang->gT('You have been already removed from this survey.');
}
if(!empty($datas->participant_id) && $datas->participant_id != "")
if(!empty($tokenInstance->participant_id))
{
//Participant also exists in central db
$cpdb = Participant::model()->find('participant_id = :participant_id', array(":participant_id"=>$datas->participant_id));
$cpdb = Participant::model()->findByPk($tokenInstance->participant_id);
if($cpdb->blacklisted=="Y")
{
$html .= "<br />";
Expand Down
8 changes: 2 additions & 6 deletions application/controllers/RegisterController.php
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
TokenDynamic::sid($thissurvey['sid']);
$token = new TokenDynamic;
$token = new Token('insert', $thissurvey['sid']);
$token->firstname = $postfirstname;
$token->lastname = $postlastname;
$token->email = Yii::app()->request->getPost('register_email');
Expand All @@ -171,8 +170,7 @@ function actionIndex($surveyid = null)
$token->validfrom = $starttime;
$token->validuntil = $endtime;
}
foreach ($attributeinsertdata as $k => $v)
$token->$k = $v;
$token->setAttributes($attributeinsertdata);
$result = $token->save();

/**
Expand All @@ -186,8 +184,6 @@ function actionIndex($surveyid = null)
) or safeDie ($query."<br />".$connect->ErrorMsg()); //Checked - According to adodb docs the bound variables are quoted automatically
*/
$tid = getLastInsertID($token->tableName());;
$token=$token->token;

$fieldsarray["{ADMINNAME}"]=$thissurvey['adminname'];
$fieldsarray["{ADMINEMAIL}"]=$thissurvey['adminemail'];
$fieldsarray["{SURVEYNAME}"]=$thissurvey['name'];
Expand Down
2 changes: 0 additions & 2 deletions application/controllers/admin/dataentry.php
Expand Up @@ -1810,9 +1810,7 @@ public function insert()
(".implode(',',$columns).")
VALUES
(".implode(',',$values).")";
//$this->TokenDynamic_model->insertToken($surveyid,$tokendata);
dbExecuteAssoc($SQL);
//Yii::app()->db->AutoExecute(db_table_name("tokens_".$surveyid), $tokendata,'INSERT');
$aDataentrymsgs[] = CHtml::tag('font', array('class'=>'successtitle'), $clang->gT("A token entry for the saved survey has been created too."));
//$aDataentryoutput .= "<font class='successtitle'></font><br />\n";
}
Expand Down
2 changes: 1 addition & 1 deletion application/controllers/admin/responses.php
Expand Up @@ -308,7 +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 . '}}');
$aData['tokeninfo'] = TokenDynamic::model($iSurveyID)->summary();
$aData['tokeninfo'] = Token::model(null, $iSurveyID)->summary();
}

$aViewUrls[] = 'browseindex_view';
Expand Down
109 changes: 51 additions & 58 deletions application/controllers/admin/tokens.php
Expand Up @@ -52,12 +52,14 @@ function index($iSurveyId)
else
{
//Check that the tokens table has the required fields
/**
* @todo is this still needed? What versions of token tables require this??
*/
TokenDynamic::model($iSurveyId)->checkColumns();
$aData['thissurvey'] = $thissurvey;
$aData['surveyid'] = $iSurveyId;
$aData['queries'] = TokenDynamic::model($iSurveyId)->summary();

$this->_renderWrappedTemplate('token', array('tokenbar', 'tokensummary'), $aData);
$aData['queries'] = Token::model(null, $iSurveyId)->summary();
$this->_renderWrappedTemplate('token', array('tokenbar', 'tokensummary'), $aData);
}
}

Expand Down Expand Up @@ -200,7 +202,7 @@ function bounceprocessing($iSurveyId)
);
$condn = array('token' => $tokenBounce[1]);

$record = TokenDynamic::model($iSurveyId)->findByAttributes($condn);
$record = Token::model(null,$iSurveyId)->findByAttributes($condn);
foreach ($aData as $k => $v)
$record->$k = $v;
$record->save();
Expand Down Expand Up @@ -283,7 +285,8 @@ function browse($iSurveyId, $limit = 50, $start = 0, $order = false, $searchstri

$limit = (int) $limit;
$start = (int) $start;
$tkcount = TokenDynamic::model($iSurveyId)->count();

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

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

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

if ($limit>$aData->records)
{
Expand Down Expand Up @@ -544,9 +547,7 @@ function editToken($iSurveyId) // Used ? 2013-01-29
// {
// $sLang = Yii::app()->request->getPost('language');
// }
TokenDynamic::model($iSurveyId);


echo $from . ',' . $until;
$aData = array(
'firstname' => Yii::app()->request->getPost('firstname'),
Expand All @@ -570,7 +571,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);
}
$token = TokenDynamic::model()->find('tid=' . Yii::app()->getRequest()->getPost('id'));
$token = Token::model(null, $iSurveyId)->find('tid=' . Yii::app()->getRequest()->getPost('id'));

foreach ($aData as $k => $v)
$token->$k = $v;
Expand Down Expand Up @@ -602,9 +603,8 @@ 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 TokenDynamic;
foreach ($aData as $k => $v)
$token->$k = $v;
$token = new Token('insert', $iSurveyId);
$token->setAttributes($aData);
echo $token->save();
}
elseif ($sOperation == 'del' && Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'update'))
Expand Down Expand Up @@ -716,13 +716,12 @@ function addnew($iSurveyId)
$aData[$attr_name] = Yii::app()->getRequest()->getPost($attr_name);
}

$udresult = TokenDynamic::model($iSurveyId)->findAll("token <> '' and token = '$sanitizedtoken'");
$udresult = Token::model(null, $iSurveyId)->findAll("token <> '' and token = '$sanitizedtoken'");
if (count($udresult) == 0)
{
// AutoExecute
$token = new TokenDynamic;
foreach ($aData as $k => $v)
$token->$k = $v;
$token = new Token('insert', $iSurveyId);
$token->setAttributes($aData);
$inresult = $token->save();
$aData['success'] = true;
}
Expand Down Expand Up @@ -806,7 +805,7 @@ function edit($iSurveyId, $iTokenId)
$aTokenData['remindersent'] = Yii::app()->request->getPost('remindersent');
$aTokenData['remindercount'] = intval(Yii::app()->request->getPost('remindercount'));

$udresult = TokenDynamic::model($iSurveyId)->findAll("tid <> '$iTokenId' and token <> '' and token = '$santitizedtoken'");
$udresult = Token::model(null, $iSurveyId)->findAll("tid <> '$iTokenId' and token <> '' and token = '$santitizedtoken'");

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

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

//Then delete the tokens
TokenDynamic::model($iSurveyID)->deleteRecords($aTokenIds);
Token::model(null, $iSurveyID)->deleteByPk($aTokenIds);
}
}

Expand Down Expand Up @@ -954,46 +953,40 @@ function addDummies($iSurveyId, $subaction = '')
$tokenlength = sanitize_int(Yii::app()->request->getPost('tokenlen'));

// Fill an array with all existing tokens
$criteria = TokenDynamic::model($iSurveyId)->getDbCriteria();
$criteria = Token::model(null, $iSurveyId)->getDbCriteria();
$criteria->select = 'token';
$ntresult = TokenDynamic::model($iSurveyId)->findAllAsArray($criteria); //Use AsArray to skip active record creation
$ntresult = Token::model(null, $iSurveyId)->findAll($criteria);
$existingtokens=array();
foreach ($ntresult as $tkrow)
{
$existingtokens[$tkrow['token']] = true ;
}
$invalidtokencount=0;
$newDummyToken=0;
while ($newDummyToken<$amount && $invalidtokencount<50)
while ($newDummyToken < $amount && $invalidtokencount < 50)
{
$aDataToInsert = $aData;
$aDataToInsert['firstname'] = str_replace('{TOKEN_COUNTER}', $newDummyToken, $aDataToInsert['firstname']);
$aDataToInsert['lastname'] = str_replace('{TOKEN_COUNTER}', $newDummyToken, $aDataToInsert['lastname']);
$aDataToInsert['email'] = str_replace('{TOKEN_COUNTER}', $newDummyToken, $aDataToInsert['email']);

$isvalidtoken = false;
while ($isvalidtoken == false && $invalidtokencount<50)
{
$newtoken = randomChars($tokenlength);
if (!isset($existingtokens[$newtoken]))
{
$isvalidtoken = true;
$existingtokens[$newtoken] = true;
$invalidtokencount=0;
}
else
{
$invalidtokencount ++;
}
}
if($isvalidtoken)
{
$aDataToInsert['token'] = $newtoken;
TokenDynamic::model()->insertToken($iSurveyId, $aDataToInsert);
$newDummyToken ++;
}

}
$token = new Token('insert', $iSurveyId);
$token->setAttributes($aData);

$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 = randomChars($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;
var_dump($token->save());
$newDummyToken++;
}
$aData['thissurvey'] = getSurveyInfo($iSurveyId);
$aData['surveyid'] = $iSurveyId;
if(!$invalidtokencount)
Expand All @@ -1020,7 +1013,7 @@ function addDummies($iSurveyId, $subaction = '')
}
else
{
$tkcount = TokenDynamic::model($iSurveyId)->count();
$tkcount = Token::model(null, $iSurveyId)->count();
$tokenlength = Yii::app()->db->createCommand()->select('tokenlength')->from('{{surveys}}')->where('sid=:sid')->bindParam(":sid", $iSurveyId, PDO::PARAM_INT)->query()->readColumn(0);

if (empty($tokenlength))
Expand Down Expand Up @@ -1472,19 +1465,19 @@ function email($iSurveyId, $tokenids = null)
if (SendEmailMessage($modmessage, $modsubject, $to, $from, Yii::app()->getConfig("sitename"), $bHtml, getBounceEmail($iSurveyId), $aRelevantAttachments, $customheaders))
{
// Put date into sent
$udequery = TokenDynamic::model($iSurveyId)->findByPk($emrow['tid']);
$token = Token::model(null, $iSurveyId)->findByPk($emrow['tid']);
if ($bEmail)
{
$tokenoutput .= $clang->gT("Invitation sent to:");
$udequery->sent = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig("timeadjust"));
$token->sent = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig("timeadjust"));
}
else
{
$tokenoutput .= $clang->gT("Reminder sent to:");
$udequery->remindersent = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig("timeadjust"));
$udequery->remindercount = $udequery->remindercount + 1;
$token->remindersent = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig("timeadjust"));
$token->remindercount++;
}
$udequery->save();
$token->save();
//Update central participant survey_links
if(!empty($emrow['participant_id']))
{
Expand Down

0 comments on commit 8072410

Please sign in to comment.