Skip to content

Commit

Permalink
Fixed issue #12694: Import badly set survey show success even if error
Browse files Browse the repository at this point in the history
Dev: review \Survey::model()->insertNewSurvey($data) to return \Survey (and then get the errors)
  • Loading branch information
Shnoulle committed Oct 31, 2017
1 parent 50f83de commit 819f099
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
7 changes: 4 additions & 3 deletions application/controllers/admin/surveyadmin.php
Expand Up @@ -2061,10 +2061,11 @@ function insert($iSurveyID=null)
$aInsertData['wishSID'] = $iSurveyID;
}

$iNewSurveyid = Survey::model()->insertNewSurvey($aInsertData);
if (!$iNewSurveyid){
die('Survey could not be created.');
$newSurvey = Survey::model()->insertNewSurvey($aInsertData);
if (!$newSurvey->sid){
die('Survey could not be created.'); // No error management ?
}
$iNewSurveyId = $NewSurveyid->sid;
// Prepare locale data for surveys_language_settings table
$sTitle = Yii::app()->request->getPost('surveyls_title');
$sDescription = Yii::app()->request->getPost('description');
Expand Down
21 changes: 13 additions & 8 deletions application/helpers/admin/import_helper.php
Expand Up @@ -742,7 +742,6 @@ function XMLImportSurvey($sFullFilePath,$sXMLdata=NULL,$sNewSurveyName=NULL,$iDe
{
Yii::app()->loadHelper('database');


$aGIDReplacements = array();
if ($sXMLdata == NULL){
$sXMLdata = file_get_contents($sFullFilePath);
Expand Down Expand Up @@ -842,10 +841,14 @@ function XMLImportSurvey($sFullFilePath,$sXMLdata=NULL,$sNewSurveyName=NULL,$iDe
foreach($aBadData as $key=>$value){
$results['importwarnings'][]=sprintf(gT("This survey setting has not been imported: %s => %s"),$key,$value);
}

$iNewSID = $results['newsid'] = Survey::model()->insertNewSurvey($insertdata) or safeDie(gT("Error").": Failed to insert data [1]<br />");

$results['surveys']++;
$newSurvey = Survey::model()->insertNewSurvey($insertdata);
if($newSurvey->sid) {
$iNewSID = $results['newsid'];
$results['surveys']++;
} else {
$results['error'] = gT("Unable to import survey.");
return $results;
}
}


Expand Down Expand Up @@ -2058,12 +2061,14 @@ function TSVImportSurvey($sFullFilePath)
$surveyinfo['startdate']=NULL;
$surveyinfo['active']='N';
// unset($surveyinfo['datecreated']);
$iNewSID = Survey::model()->insertNewSurvey($surveyinfo) ; //or safeDie(gT("Error").": Failed to insert survey<br />");
if ($iNewSID==false){
$results['error'] = Survey::model()->getErrors();
$newSurvey = Survey::model()->insertNewSurvey($surveyinfo) ; //or safeDie(gT("Error").": Failed to insert survey<br />");

if (!$newSurvey->sid){
$results['error'] = CHtml::errorSummary($oSurvey,gT("Error(s) when try to create survey"));
$results['bFailed'] = true;
return $results;
}
$iNewSid = $newSurvey->sid;
$surveyinfo['sid']=$iNewSID;
$results['surveys']++;
$results['newsid']=$iNewSID;
Expand Down
8 changes: 5 additions & 3 deletions application/helpers/remotecontrol/remotecontrol_handle.php
Expand Up @@ -147,9 +147,11 @@ public function add_survey($sSessionKey, $iSurveyID, $sSurveyTitle, $sSurveyLang

try
{
$iNewSurveyid = Survey::model()->insertNewSurvey($aInsertData);
if (!$iNewSurveyid)
return array('status' => 'Creation Failed');
$newSurvey = Survey::model()->insertNewSurvey($aInsertData);
if (!$newSurvey->sid) {
return array('status' => 'Creation Failed'); // status are a string, another way to send errors ?
}
$iNewSurveyid = $newSurvey->sid;

$sTitle = html_entity_decode($sSurveyTitle, ENT_QUOTES, "UTF-8");

Expand Down
14 changes: 6 additions & 8 deletions application/models/Survey.php
Expand Up @@ -730,10 +730,10 @@ public function getSurveyMenus($position=''){
}

/**
* Creates a new survey - does some basic checks of the suppplied data
* Creates a new survey - with a random sid
*
* @param array $aData Array with fieldname=>fieldcontents data
* @return integer The new survey id
* @return \Survey
*/
public function insertNewSurvey($aData)
{
Expand All @@ -746,22 +746,20 @@ public function insertNewSurvey($aData)
else{
$aData['sid'] = randomChars(6, '123456789');
}

$isresult = self::model()->findByPk($aData['sid']);
}
while (!is_null($isresult));

$survey = new self;
foreach ($aData as $k => $v)
foreach ($aData as $k => $v) {
$survey->$k = $v;
}
$sResult= $survey->save();

if (!$sResult) {
tracevar($survey->getErrors());
tracevar($aData);
return false;
$survey->sid=null;
}
else return $aData['sid'];
return $survey;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions application/models/SurveyLanguageSetting.php
Expand Up @@ -150,7 +150,7 @@ public function lsdefault($attribute)
$aDefaultTextData['admin_detailed_notification']=$aDefaultTexts['admin_detailed_notification_css'].$aDefaultTexts['admin_detailed_notification'];
}

if (empty($this->$attribute)) $this->$attribute=$aDefaultTextData[$attribute];
if (empty($this->$attribute)) $this->$attribute=$aDefaultTextData[$attribute];
}


Expand Down Expand Up @@ -216,6 +216,7 @@ public function getAllSurveys($hasPermission = FALSE)

/**
* @param array $data
* @todo : rename and fix this
* @return bool
*/
public function insertNewSurvey($data)
Expand Down Expand Up @@ -250,8 +251,9 @@ function updateRecord($data,$condition='', $xssfiltering = false)
function insertSomeRecords($data)
{
$lang = new self;
foreach ($data as $k => $v)
foreach ($data as $k => $v) {
$lang->$k = $v;
}
return $lang->save();
}
}

0 comments on commit 819f099

Please sign in to comment.