Skip to content

Commit

Permalink
Fixed issue #10225: Import TSV can send broken CDbCommand
Browse files Browse the repository at this point in the history
Dev: fix DefaultValue::model()->insertRecords
Dev: Don't try to insert empty DefaultValue in TSV
Dev: Test is error is an arry to show it (@todo : move it to warnig for TSV)
Dev: Allow use TSV for extension : more easy with LibreOffice
  • Loading branch information
Shnoulle committed Jan 13, 2016
1 parent 6f2fd45 commit 2c61f96
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 98 deletions.
2 changes: 1 addition & 1 deletion application/controllers/admin/surveyadmin.php
Expand Up @@ -850,7 +850,7 @@ public function copy()
$aData['sErrorMessage'] = sprintf(gT("An error occurred uploading your file. This may be caused by incorrect permissions in your %s folder."), Yii::app()->getConfig('tempdir'));
$aData['bFailed'] = true;
}
if (!$aData['bFailed'] && (strtolower($sExtension) != 'csv' && strtolower($sExtension) != 'lss' && strtolower($sExtension) != 'txt' && strtolower($sExtension) != 'lsa'))
if (!$aData['bFailed'] && !in_array(strtolower($sExtension),array('lss','txt','tsv','lsa')))
{
$aData['sErrorMessage'] = sprintf(gT("Import failed. You specified an invalid file type '%s'."), $sExtension);
$aData['bFailed'] = true;
Expand Down
4 changes: 2 additions & 2 deletions application/helpers/admin/import_helper.php
Expand Up @@ -713,7 +713,7 @@ function importSurveyFile($sFullFilePath, $bTranslateLinksFields, $sNewSurveyNam
{
return XMLImportSurvey($sFullFilePath, null, $sNewSurveyName, $DestSurveyID, $bTranslateLinksFields);
}
elseif ($sExtension == 'txt')
elseif ($sExtension == 'txt' || $sExtension == 'tsv')
{
return TSVImportSurvey($sFullFilePath);
}
Expand Down Expand Up @@ -2378,7 +2378,7 @@ function TSVImportSurvey($sFullFilePath)
}

// insert default value
if (isset($row['default']))
if (isset($row['default']) && $row['default']!=="")
{
$insertdata=array();
$insertdata['qid'] = $qid;
Expand Down
15 changes: 5 additions & 10 deletions application/models/Answer.php
Expand Up @@ -145,17 +145,12 @@ public function updateRecord($data, $condition=FALSE)

function insertRecords($data)
{
$ans = new self;
$oRecord = new self;
foreach ($data as $k => $v)
$ans->$k = $v;
try
{
return $ans->save();
}
catch(Exception $e)
{
return false;
}
$oRecord->$k = $v;
if($oRecord->validate())
return $oRecord->save();
tracevar($oRecord->getErrors());
}

/**
Expand Down
132 changes: 80 additions & 52 deletions application/models/DefaultValue.php
Expand Up @@ -15,63 +15,91 @@

class DefaultValue extends LSActiveRecord
{
/**
* Returns the static model of Settings table
*
* @static
* @access public
* @param string $class
* @return CActiveRecord
*/
public static function model($class = __CLASS__)
{
return parent::model($class);
}
/* Default value when create (from DB) , leave some because add rules */
public $specialtype='';
public $scale_id='';
public $sqid=0;
public $language='';// required ?
/**
* Returns the setting's table name to be used by the model
*
* @access public
* @return string
*/
public function tableName()
{
return '{{defaultvalues}}';
}
/**
* Returns the static model of Settings table
*
* @static
* @access public
* @param string $class
* @return CActiveRecord
*/
public static function model($class = __CLASS__)
{
return parent::model($class);
}

/**
* Returns the primary key of this table
*
* @access public
* @return array
*/
public function primaryKey()
{
return array('qid', 'specialtype', 'scale_id', 'sqid', 'language');
}
/**
* Returns the setting's table name to be used by the model
*
* @access public
* @return string
*/
public function tableName()
{
return '{{defaultvalues}}';
}

/**
* Relations with questions
*
* @access public
* @return array
*/
public function relations()
{
$alias = $this->getTableAlias();
return array(
'question' => array(self::HAS_ONE, 'Question', '',
'on' => "$alias.qid = question.qid",
),
);
}
/**
* Returns the primary key of this table
*
* @access public
* @return array
*/
public function primaryKey()
{
return array('qid', 'specialtype', 'scale_id', 'sqid', 'language');
}

function insertRecords($data)
/**
* Relations with questions
*
* @access public
* @return array
*/
public function relations()
{
$alias = $this->getTableAlias();
return array(
'question' => array(self::HAS_ONE, 'Question', '',
'on' => "$alias.qid = question.qid",
),
);
}
/**
* Returns this model's validation rules
*
*/
public function rules()
{
return array(
array('qid', 'required'),
array('qid', 'numerical','integerOnly'=>true),
array('qid', 'unique', 'criteria'=>array(
'condition'=>'specialtype=:specialtype and scale_id=:scale_id and sqid=:sqid and language=:language',
'params'=>array(
':specialtype'=>$this->specialtype,
':scale_id'=>$this->scale_id,
':sqid'=>$this->sqid,
':language'=>$this->language,
)
),
'message'=>'{attribute} "{value}" is already in use.'),
);
}
function insertRecords($data)
{
$values = new self;
foreach ($data as $k => $v)
$values->$k = $v;
return $values->save();
$oRecord = new self;
foreach ($data as $k => $v)
$oRecord->$k = $v;
if($oRecord->validate())
return $oRecord->save();
tracevar($oRecord->getErrors());
}
}
?>
21 changes: 9 additions & 12 deletions application/models/Question.php
Expand Up @@ -77,7 +77,7 @@ public function relations()
*/
public function rules()
{

$aRules= array(
array('title','required','on' => 'update, insert'),// 140207 : Before was commented, put only on update/insert ?
array('title','length', 'min' => 1, 'max'=>20,'on' => 'update, insert'),
Expand Down Expand Up @@ -311,26 +311,23 @@ function getQuestionsWithSubQuestions($iSurveyID, $sLanguage, $sCondition = FALS

/**
* Insert an array into the questions table
* Returns false if insertion fails, otherwise the new QID
* Returns null if insertion fails, otherwise the new QID
*
* @param array $data
*/
function insertRecords($data)
{
// This function must be deprecated : don't find a way to have getErrors after (Shnoulle on 131206)
$questions = new self;
$oRecord = new self;
foreach ($data as $k => $v){
$questions->$k = $v;
$oRecord->$k = $v;
}
try
{
$questions->save();
return $questions->qid;
}
catch(Exception $e)
if($oRecord->validate())
{
return false;
$oRecord->save();
return $oRecord->qid;
}
tracevar($oRecord->getErrors());
}

public static function deleteAllById($questionsIds)
Expand Down Expand Up @@ -646,7 +643,7 @@ public static function typeList()
* @todo Check if this actually does anything, since the values are arrays.
*/
asort($questionTypes);

return $questionTypes;
}
/**
Expand Down
36 changes: 15 additions & 21 deletions application/views/admin/survey/importSurvey_view.php
Expand Up @@ -3,30 +3,25 @@
<?php
if ($bFailed){ ?>
<div class='errorheader'><?php eT("Error");?></div>
<?php echo $sErrorMessage; ?><br /><br />
<input type='submit' value='<?php eT("Main Admin Screen");?>' onclick="window.open('<?php echo $this->createUrl('/admin');?>', '_top')">
<?php
if(is_array($sErrorMessage))
{
foreach($sErrorMessage as $error)
echo $error."<br/>";
}
else
{
echo $sErrorMessage;
}
?>
<br /><br />
<input type='submit' value='<?php eT("Main Admin Screen");?>' onclick="window.open('<?php echo $this->createUrl('/admin');?>', '_top')">
<input type='submit' value='<?php eT("Import again");?>' onclick="window.open('<?php echo $this->createUrl('admin/survey/sa/newsurvey#import');?>', '_top')"><br /><br /></div>
<?php } else
<?php }
else
{?>
<div class='successheader'><?php eT("Success");?></div>&nbsp;<br />
<?php eT("File upload succeeded.");?> <?php eT("Reading file..");?><br />
<?php if (isset($aImportResults['error']) && $aImportResults['error']!=false)
{?>
<div class='warningheader'><?php eT("Error");?></div><br />
<?php
if(is_array($aImportResults['error']))
{
foreach($aImportResults['error'] as $error)
echo $error."<br/>";
} else
{
echo $aImportResults['error'];
}
?><br /><br />
<input type='submit' value='<?php eT("Main Admin Screen");?>' onclick="window.open('<?php echo $this->createUrl('/admin');?>', '_top')" />
<?php } else
{?>

<br /><div class='successheader'><?php eT("Success");?></div>
<strong><?php echo $sSummaryHeader; ?></strong><br />

Expand Down Expand Up @@ -93,6 +88,5 @@
<?php } ?>
<br>
<input type='submit' value='<?php eT("Go to survey");?>' onclick="window.open('<?php echo $sLink; ?>', '_top')"><br /><br />
<?php } ?>
</div><br />
<?php }?>

0 comments on commit 2c61f96

Please sign in to comment.