Skip to content

Commit

Permalink
Merge branch 2.06
Browse files Browse the repository at this point in the history
  • Loading branch information
SamMousa committed Apr 23, 2014
1 parent c9076a1 commit 391366d
Show file tree
Hide file tree
Showing 46 changed files with 410 additions and 408 deletions.
152 changes: 62 additions & 90 deletions application/commands/InstallCommand.php
Expand Up @@ -13,109 +13,38 @@
*/
class InstallCommand extends CConsoleCommand
{
/**
*
* @var CDbConnection
*/
public $connection;

public function run($sArgument)
{
if (!isset($sArgument) || !isset($sArgument[0]) || !isset($sArgument[1]) || !isset($sArgument[2]) || !isset($sArgument[3])) die('You have to set admin/password/full name and email address on the command line like this: php starter.php adminname mypassword fullname emailaddress');
Yii::import('application.helpers.common_helper', true);
$aConfig=Yii::app()->getComponents(false);
$bDatabaseExists=true;

try
{
$this->connection=new CDbConnection($aConfig['db']['connectionString'],$aConfig['db']['username'],$aConfig['db']['password']);
$this->connection = App()->getDb();
$this->connection->active=true;
}
catch(Exception $e){
$bDatabaseExists=false;
$sConnectionString=preg_replace('/dbname=([^;]*)/', '', $aConfig['db']['connectionString']);
try
{
$this->connection=new CDbConnection($sConnectionString, $aConfig['db']['username'], $aConfig['db']['password']);
$this->connection->active=true;
}
catch(Exception $e){
echo "Invalid access data. Check your config.php db access data"; die();
}

catch(CDbException $e){
$this->createDatabase();
};

$sDatabaseType = substr($aConfig['db']['connectionString'],0,strpos($aConfig['db']['connectionString'],':'));
$sDatabaseName= $this->getDBConnectionStringProperty('dbname');

if (!$bDatabaseExists)
{

$createDb = true; // We are thinking positive
switch ($sDatabaseType)
{
case 'mysqli':
case 'mysql':
try
{
$this->connection->createCommand("CREATE DATABASE `$sDatabaseName` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci")->execute();
}
catch(Exception $e)
{
$createDb=false;
}
break;

case 'dblib':
case 'mssql':
case 'odbc':
try
{
$this->connection->createCommand("CREATE DATABASE [$sDatabaseName];")->execute();
}
catch(Exception $e)
{
$createDb=false;
}
break;
case 'postgres':
try
{
$this->connection->createCommand("CREATE DATABASE \"$sDatabaseName\" ENCODING 'UTF8'")->execute();
}
catch (Exception $e)
{
$createdb = false;
}
break;
default:
try
{
$this->connection->createCommand("CREATE DATABASE $sDatabaseName")->execute();
}
catch(Exception $e)
{
$createDb=false;
}
break;
}
if (!$createDb)
{
echo 'Database could not be created because it either existed or you have no permissions'; die();
}
else
{
$this->connection=new CDbConnection($aConfig['db']['connectionString'],$aConfig['db']['username'],$aConfig['db']['password']);
$this->connection->active=true;

}
}

$this->connection->charset = 'utf8';
switch ($sDatabaseType) {
switch ($this->connection->driverName) {
case 'mysql':
case 'mysqli':
$this->connection->createCommand("ALTER DATABASE ". $this->connection->quoteTableName($sDatabaseName) ." DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;")->execute();
$this->connection->createCommand("ALTER DATABASE ". $this->connection->quoteTableName($this->getDBConnectionStringProperty('dbname')) ." DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;")->execute();
$sql_file = 'mysql';
break;
case 'pgsql':
if (version_compare($this->connection->getServerVersion(),'9','>=')) {
$this->connection->createCommand("ALTER DATABASE ". $this->connection->quoteTableName($sDatabaseName) ." SET bytea_output='escape';")->execute();
$this->connection->createCommand("ALTER DATABASE ". $this->connection->quoteTableName($this->getDBConnectionStringProperty('dbname')) ." SET bytea_output='escape';")->execute();
}
$sql_file = 'pgsql';
break;
Expand All @@ -124,18 +53,18 @@ public function run($sArgument)
$sql_file = 'mssql';
break;
default:
throw new Exception(sprintf('Unkown database type "%s".', $sDatabaseType));
throw new Exception(sprintf('Unkown database type "%s".', $this->getDBConnectionStringProperty('dbname')));
}
$this->_executeSQLFile(dirname(Yii::app()->basePath).'/installer/sql/create-'.$sql_file.'.sql', $aConfig['db']['tablePrefix']);
$this->connection->createCommand()->insert($aConfig['db']['tablePrefix'].'users', array(
$this->_executeSQLFile(dirname(Yii::app()->basePath).'/installer/sql/create-'.$sql_file.'.sql');
$this->connection->createCommand()->insert($this->connection->tablePrefix.'users', array(
'users_name'=>$sArgument[0],
'password'=>hash('sha256',$sArgument[1]),
'full_name'=>$sArgument[2],
'parent_id'=>0,
'lang'=>'auto',
'email'=>$sArgument[3]
));
$this->connection->createCommand()->insert($aConfig['db']['tablePrefix'].'permissions', array(
$this->connection->createCommand()->insert($this->connection->tablePrefix.'permissions', array(
'entity'=>'global',
'entity_id'=>0,
'uid'=>1,
Expand All @@ -149,7 +78,7 @@ public function run($sArgument)
));
}

function _executeSQLFile($sFileName, $sDatabasePrefix)
function _executeSQLFile($sFileName)
{
echo $sFileName;
$aMessages = array();
Expand All @@ -168,7 +97,7 @@ function _executeSQLFile($sFileName, $sDatabasePrefix)
if (substr($sLine, $iLineLength-1, 1) == ';') {
$line = substr($sLine, 0, $iLineLength-1);
$sCommand .= $sLine;
$sCommand = str_replace('prefix_', $sDatabasePrefix, $sCommand); // Table prefixes
$sCommand = str_replace('prefix_', $this->connection->tablePrefix, $sCommand); // Table prefixes

try {
$this->connection->createCommand($sCommand)->execute();
Expand All @@ -190,14 +119,57 @@ function _executeSQLFile($sFileName, $sDatabasePrefix)

function getDBConnectionStringProperty($sProperty)
{
$aConfig=Yii::app()->getComponents(false);
// Yii doesn't give us a good way to get the database name
preg_match('/'.$sProperty.'=([^;]*)/', $aConfig['db']['connectionString'], $aMatches);
preg_match('/'.$sProperty.'=([^;]*)/', $this->connection->connectionString, $aMatches);
if ( count($aMatches) === 0 ) {
return null;
}
return $aMatches[1];
}


protected function createDatabase()
{
$connectionString = $this->connection->connectionString;
$this->connection->connectionString = preg_replace('/dbname=([^;]*)/', '', $connectionString);
try
{
$this->connection->active=true;
}
catch(Exception $e){
echo "Invalid access data. Check your config.php db access data"; die();
}

$sDatabaseName= $this->getDBConnectionStringProperty('dbname');
try {
switch ($this->connection->driverName)
{
case 'mysqli':
case 'mysql':
$this->connection->createCommand("CREATE DATABASE `$sDatabaseName` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci")->execute();
break;
case 'dblib':
case 'mssql':
case 'odbc':
$this->connection->createCommand("CREATE DATABASE [$sDatabaseName];")->execute();
break;
case 'postgres':
$this->connection->createCommand("CREATE DATABASE \"$sDatabaseName\" ENCODING 'UTF8'")->execute();
break;
default:
$this->connection->createCommand("CREATE DATABASE $sDatabaseName")->execute();
break;
}
}
catch (Exception $e)
{
throw new CException('Database could not be created because it either existed or you have no permissions');
}

$this->connection->active = false;
$this->connection->connectionString = $connectionString;
$this->connection->active = true;
}

}
?>
12 changes: 12 additions & 0 deletions application/controllers/SurveysController.php
Expand Up @@ -7,6 +7,18 @@ class SurveysController extends LSYii_Controller
{
public $layout = 'bare';
public $defaultAction = 'publicList';

public function actionOrganize($surveyId)
{
$this->layout = 'main';
$groups = QuestionGroup::model()->findAllByAttributes(array(
'sid' => $surveyId
));
$this->render('organize', compact('groups'));
}



public function actionPublicList($lang = null)
{
$this->sessioncontrol();
Expand Down
2 changes: 1 addition & 1 deletion application/controllers/admin/emailtemplates.php
Expand Up @@ -103,7 +103,7 @@ function update($iSurveyId)
foreach ($attachments as $index => &$attachment)
{
// We again take the real path.
$localName = realpath(str_replace($uploadUrl, $uploadDir, $attachment['url']));
$localName = realpath(urldecode(str_replace($uploadUrl, $uploadDir, $attachment['url'])));
if ($localName !== false)
{
if (strpos($localName, $uploadDir) === 0)
Expand Down
7 changes: 4 additions & 3 deletions application/controllers/admin/participantsaction.php
Expand Up @@ -681,13 +681,14 @@ function getaddtosurveymsg()
*/
function getSearchIDs()
{
$searchcondition = basename(Yii::app()->request->getPost('searchcondition')); // get the search condition from the URL
$searchcondition = Yii::app()->request->getPost('searchcondition'); // get the search condition from the URL
$sSearchURL = basename(Yii::app()->request->getPost('searchURL')); // get the search condition from the URL
/* a search contains posted data inside $_POST['searchcondition'].
* Each separate query is made up of 3 fields, separated by double-pipes ("|")
* EG: fname||eq||jason||lname||ct||c
*
*/
if ($searchcondition != 'getParticipants_json') // if there is a search condition present
if ($sSearchURL != 'getParticipants_json') // if there is a search condition present
{
$participantid = "";
$condition = explode("||", $searchcondition); // explode the condition to the array
Expand Down Expand Up @@ -735,7 +736,7 @@ function exporttocsv()
{
if (Yii::app()->request->getPost('searchcondition','') != '') // if there is a search condition then only the participants that match the search criteria are counted
{
$condition = explode("||", $searchcondition);
$condition = explode("%7C%7C", Yii::app()->request->getPost('searchcondition',''));
$search = Participant::model()->getParticipantsSearchMultipleCondition($condition);
} else {
$search = null;
Expand Down
4 changes: 2 additions & 2 deletions application/controllers/admin/statistics.php
Expand Up @@ -496,13 +496,13 @@ function listcolumn($surveyid, $column, $sortby="", $sortmethod="", $sorttype=""
{
Yii::app()->loadHelper('admin/statistics');
$helper = new statistics_helper();
$output = $helper->_listcolumn($surveyid, $column, $sortby, $sortmethod, $sorttype);
$aData['data']=$helper->_listcolumn($surveyid, $column, $sortby, $sortmethod, $sorttype);
$aData['surveyid']=$surveyid;
$aData['data']=$output;
$aData['column']=$column;
$aData['sortby']=$sortby;
$aData['sortmethod']=$sortmethod;
$aData['sorttype']=$sorttype;
App()->getClientScript()->reset();
$this->getController()->render('export/statistics_browse_view', $aData);
}

Expand Down
6 changes: 5 additions & 1 deletion application/controllers/admin/tokens.php
Expand Up @@ -1903,7 +1903,7 @@ function import($iSurveyId)
self::_newtokentable($iSurveyId);
}

App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('adminscripts') . 'tokens.js');
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('adminscripts') . 'tokensimport.js');
$aEncodings =aEncodingsArray();
if (Yii::app()->request->getPost('submit'))
{
Expand Down Expand Up @@ -2086,6 +2086,10 @@ function import($iSurveyId)
//if(in_array($key,$oToken->attributes)) Not needed because we filter attributes before
$oToken->$key=$value;
}
// 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
$writearray['emailstatus']=isset($writearray['emailstatus'])?$writearray['emailstatus']:"OK";
$writearray['language']=isset($writearray['language'])?$writearray['language']:$sBaseLanguage;
$ir=$oToken->save();
if (!$ir)
{
Expand Down
13 changes: 8 additions & 5 deletions application/extensions/Menu/MenuWidget.php
Expand Up @@ -55,11 +55,14 @@ protected function menuMain()
'image' => 'home.png',
);
$menu['items']['left'][] = 'separator';
$menu['items']['left'][] = array(
'href' => array('admin/user'),
'alt' => gT('Manage survey administrators'),
'image' => 'security.png',
);
if(Permission::model()->hasGlobalPermission('users','read'))
{
$menu['items']['left'][] = array(
'href' => array('admin/user'),
'alt' => gT('Manage survey administrators'),
'image' => 'security.png',
);
}

$menu['items']['left'][] = $this->userGroups();
$menu['items']['left'][] = $this->globalSettings();
Expand Down
4 changes: 0 additions & 4 deletions application/helpers/qanda_helper.php
Expand Up @@ -11,10 +11,6 @@
* See COPYRIGHT.php for copyright notices and details.
*/

// Security Checked: POST, GET, SESSION, REQUEST, returnGlobal, DB

//if (!isset($homedir) || isset($_REQUEST['$homedir'])) {die("Cannot run this script directly");}

/*
* Let's explain what this strange $ia var means
*
Expand Down
12 changes: 5 additions & 7 deletions application/models/Participant.php
Expand Up @@ -284,28 +284,26 @@ private function getParticipantsSelectCommand($count = false, $attid, $search =
{
if(!is_null($search) && strpos($search->condition,'attribute'.$aAttribute['attribute_id'])!==false)
{
$attid[]=$aAttribute;
$attid[$aAttribute['attribute_id']]=$aAttribute;
}
}
$attid=array_unique($attid);
// Add survey count subquery
$subQuery = Yii::app()->db->createCommand()
->select('count(*) survey')
->from('{{survey_links}} sl')
->where('sl.participant_id = p.participant_id');
$selectValue[] = sprintf('(%s) survey',$subQuery->getText());
array_push($joinValue,"left join {{users}} luser ON luser.uid=p.owner_uid");
foreach($attid as $key=>$attid)
foreach($attid as $iAttributeID=>$aAttributeDetails)
{
$attid = $attid['attribute_id'];
$sDatabaseType = Yii::app()->db->getDriverName();
if ($sDatabaseType=='mssql' || $sDatabaseType=="sqlsrv" || $sDatabaseType == 'dblib')
{
$selectValue[]= "cast(attribute".$attid.".value as varchar(max)) as a".$attid;
$selectValue[]= "cast(attribute".$iAttributeID.".value as varchar(max)) as a".$iAttributeID;
} else {
$selectValue[]= "attribute".$attid.".value as a".$attid;
$selectValue[]= "attribute".$iAttributeID.".value as a".$iAttributeID;
}
array_push($joinValue,"LEFT JOIN {{participant_attribute}} attribute".$attid." ON attribute".$attid.".participant_id=p.participant_id AND attribute".$attid.".attribute_id=".$attid);
array_push($joinValue,"LEFT JOIN {{participant_attribute}} attribute".$iAttributeID." ON attribute".$iAttributeID.".participant_id=p.participant_id AND attribute".$iAttributeID.".attribute_id=".$iAttributeID);
}

$aConditions = array(); // this wil hold all conditions
Expand Down
2 changes: 1 addition & 1 deletion application/models/ParticipantAttributeName.php
Expand Up @@ -181,7 +181,7 @@ function getVisibleAttributes($sLanguageFilter=null)
$language=$langs[0]->lang;
$attribute_name=$langs[0]->attribute_name;
}
$output[]=array("attribute_id"=>$id->attribute_id,
$output[$id->attribute_id]=array("attribute_id"=>$id->attribute_id,
"attribute_type"=>$id->attribute_type,
"visible"=>$id->visible,
"attribute_name"=>$attribute_name,
Expand Down

0 comments on commit 391366d

Please sign in to comment.