From 301c4e0ac6196239d5ce3c8585641b37a2cc1d20 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 16 Oct 2015 12:33:11 +0200 Subject: [PATCH] Fixed issue #09662 --- protected/controllers/SurveysController.php | 2 +- protected/models/ActiveRecord.php | 120 ++++------- protected/models/Condition.php | 224 +++++--------------- protected/models/Question.php | 39 +--- protected/models/QuestionGroup.php | 25 --- protected/models/Quota.php | 54 ----- protected/models/Survey.php | 46 +--- protected/models/SurveyLanguageSetting.php | 3 +- 8 files changed, 108 insertions(+), 405 deletions(-) diff --git a/protected/controllers/SurveysController.php b/protected/controllers/SurveysController.php index 0e4ce0c6b15..6ebaa5b5d28 100644 --- a/protected/controllers/SurveysController.php +++ b/protected/controllers/SurveysController.php @@ -289,7 +289,7 @@ public function actionDelete($id) { 'entity_id' => $survey->primaryKey, 'crud' => 'delete' ])) { - $survey->deleteDependent(); + $survey->deleteDependent(App()->db); App()->user->setFlash('success', gT("Survey deleted")); $this->redirect(['surveys/index']); } else { diff --git a/protected/models/ActiveRecord.php b/protected/models/ActiveRecord.php index a8398bf6b90..4caf3b99b91 100644 --- a/protected/models/ActiveRecord.php +++ b/protected/models/ActiveRecord.php @@ -1,13 +1,16 @@ beforeFind(); - $this->applyScopes($criteria); - if (!$all) { - $criteria->limit = 1; - } - - $command = $this->getCommandBuilder()->createFindCommand($this->getTableSchema(), $criteria); - //For debug, this command will get you the generated sql: - //echo $command->getText(); - - return $all ? $command->queryAll() : $command->queryRow(); - } - } - - /** - * Finds all active records satisfying the specified condition but returns them as array - * - * See {@link find()} for detailed explanation about $condition and $params. - * @param mixed $condition query condition or criteria. - * @param array $params parameters to be bound to an SQL statement. - * @return array list of active records satisfying the specified condition. An empty array is returned if none is found. - */ - public function findAllAsArray($condition = '', $params = array()) - { - Yii::trace(get_class($this) . '.findAll()', 'system.db.ar.CActiveRecord'); - $criteria = $this->getCommandBuilder()->createCriteria($condition, $params); - - return $this->query($criteria, true, false); //Notice the third parameter 'false' - } /** @@ -85,12 +42,10 @@ public function findAllAsArray($condition = '', $params = array()) * @param boolean $forceRefresh Don't use value from static cache but always requery the database * @return false|int */ - public function getMaxId($field = null, $forceRefresh = false) + public function getMaxId($field = null) { - static $maxIds = array(); - if (is_null($field)) { - $primaryKey = $this->getMetaData()->tableSchema->primaryKey; + $primaryKey = $this->primaryKey(); if (is_string($primaryKey)) { $field = $primaryKey; } else { @@ -100,17 +55,12 @@ public function getMaxId($field = null, $forceRefresh = false) } } - if ($forceRefresh || !array_key_exists($field, $maxIds)) { - $maxId = $this->dbConnection->createCommand() - ->select('MAX(' . $this->dbConnection->quoteColumnName($field) . ')') - ->from($this->tableName()) - ->queryScalar(); + return $this->dbConnection->createCommand() + ->select('MAX(' . $this->dbConnection->quoteColumnName($field) . ')') + ->from($this->tableName()) + ->queryScalar(); - // Save so we can reuse in the same request - $maxIds[$field] = $maxId; - } - return $maxIds[$field]; } /** @@ -139,20 +89,6 @@ public function deleteAllByAttributes($attributes, $condition = '', $params = ar return parent::deleteAllByAttributes(array(), $criteria, array()); } - /** - * @param null $class - * @return static - */ - public static function model($class = null) - { - if (!isset($class)) { - $class = get_called_class(); - } - - return parent::model($class); - - } - /* * Creates a (nested) array of this objects' attributes and relations. */ @@ -196,7 +132,7 @@ public function toArray($related = true, $exclude = []) * @return void * @link http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.sleep */ - function __wakeup() + public function __wakeup() { // Re-attach behaviors. $this->attachBehaviors($this->behaviors()); @@ -236,8 +172,38 @@ public function __sleep() * which is a value of any type other than a resource. * @since 5.4.0 */ - function jsonSerialize() + public function jsonSerialize() { return $this->attributes; } + + /** + * @return array List of relation names that contain only dependent records. + */ + public function dependentRelations() { + return []; + } + + /** + * Returns the static model of the specified AR class. + * The model returned is a static instance of the AR class. + * It is provided for invoking class-level methods (something similar to static class methods.) + * + * EVERY derived AR class must override this method as follows, + *
+     * public static function model($className=__CLASS__)
+     * {
+     *     return parent::model($className);
+     * }
+     * 
+ * + * @param string $className active record class name. + * @return static active record model instance. + */ + public static function model($className = null) + { + return parent::model(!isset($className) ? get_called_class() :$className); + } + + } \ No newline at end of file diff --git a/protected/models/Condition.php b/protected/models/Condition.php index 48a9fd6ac71..dfaafed9742 100644 --- a/protected/models/Condition.php +++ b/protected/models/Condition.php @@ -1,183 +1,65 @@ getTableAlias(); - return array( - 'questions' => array(self::HAS_ONE, 'ls\models\Question', '', - 'on' => "$alias.cqid = questions.qid", - ), - ); - } - +{ - public function deleteRecords($condition=FALSE) - { - $criteria = new CDbCriteria; - if( $condition != FALSE ) - { - if( is_array($condition) ) - { - foreach($condition as $column=>$value) - { - $criteria->addCondition("$column='$value'"); - } - } - else - { - $criteria->addCondition($condition); - } - } - - return $this->deleteAll($criteria); - } - - - /** - * Updates the group ID for all conditions - * - * @param integer $iSurveyID - * @param integer $iQuestionID - * @param integer $iOldGroupID - * @param integer $iNewGroupID - */ - public function updateCFieldName($iSurveyID, $iQuestionID, $iOldGroupID, $iNewGroupID) - { - $oResults=$this->findAllByAttributes(array('cqid'=>$iQuestionID)); - foreach ($oResults as $oRow) - { - - $cfnregs=''; - if (preg_match('/'.$surveyid."X".$iOldGroupID."X".$iQuestionID."(.*)/", $oRow->cfieldname, $cfnregs) > 0) - { - $newcfn=$surveyid."X".$iNewGroupID."X".$iQuestionID.$cfnregs[1]; - $c2query="UPDATE ".db_table_name('conditions') - ." SET cfieldname='{$newcfn}' WHERE cid={$oRow->cid}"; - - Yii::app()->db->createCommand($c2query)->query(); - } - } - } - - - - public function insertRecords($data, $update=FALSE, $condition=FALSE) - { - $record = new self; - foreach ($data as $k => $v) - { - $v = str_replace(array("'", '"'), '', $v); - $record->$k = $v; - } - - if( $update ) - { - $criteria = new CdbCriteria; - if( is_array($condition) ) - { - foreach($condition as $column=>$value) - { - $criteria->addCondition("$column='$value'"); - } - } - else - { - $criteria->where = $condition; - } - - return $record->updateAll($data,$criteria); - } - else - return $record->save(); - } - - function getScenarios($qid) - { - - $scenarioquery = "SELECT DISTINCT scenario FROM ".$this->tableName()." WHERE qid=".$qid." ORDER BY scenario"; + /** + * Returns the setting's table name to be used by the model + * + * @access public + * @return string + */ + public function tableName() + { + return '{{conditions}}'; + } - return Yii::app()->db->createCommand($scenarioquery)->query(); - } - - function getSomeConditions($fields, $condition, $order, $group){ - $record = Yii::app()->db->createCommand() - ->select($fields) - ->from($this->tableName()) - ->where($condition); + /** + * Defines the relations for this model + * + * @access public + * @return array + */ + public function relations() + { + return [ + 'question' => [self::BELONGS_TO, Question::class, 'qid'], + // The question the condition is targeting. + 'targetQuestion' => [self::BELONGS_TO, Question::class, 'cqid'], + ]; + } - if( $order != NULL ) - { - $record->order($order); - } - if( $group != NULL ) - { - $record->group($group); + /** + * Updates the group ID for all conditions + * + * @param integer $iSurveyID + * @param integer $iQuestionID + * @param integer $iOldGroupID + * @param integer $iNewGroupID + */ + public function updateCFieldName($iSurveyID, $iQuestionID, $iOldGroupID, $iNewGroupID) + { + $oResults = $this->findAllByAttributes(array('cqid' => $iQuestionID)); + foreach ($oResults as $oRow) { + + $cfnregs = ''; + if (preg_match('/' . $surveyid . "X" . $iOldGroupID . "X" . $iQuestionID . "(.*)/", $oRow->cfieldname, + $cfnregs) > 0 + ) { + $newcfn = $surveyid . "X" . $iNewGroupID . "X" . $iQuestionID . $cfnregs[1]; + $c2query = "UPDATE " . db_table_name('conditions') + . " SET cfieldname='{$newcfn}' WHERE cid={$oRow->cid}"; + + Yii::app()->db->createCommand($c2query)->query(); } - - return $record->query(); - } - - function getConditionsQuestions($distinctrow,$deqrow,$scenariorow,$surveyprintlang) - { - $conquery="SELECT cid, cqid, q.title, q.question, value, q.type, cfieldname " - ."FROM {{conditions}} c, {{questions}} q " - ."WHERE c.cqid=q.qid " - ."AND c.cqid=:distinctrow " - ."AND c.qid=:deqrow " - ."AND c.scenario=:scenariorow " - ."AND language=:surveyprintlang "; - return Yii::app()->db->createCommand($conquery) - ->bindParam(":distinctrow", $distinctrow, PDO::PARAM_INT) - ->bindParam(":deqrow", $deqrow, PDO::PARAM_INT) - ->bindParam(":scenariorow", $scenariorow, PDO::PARAM_INT) - ->bindParam(":surveyprintlang", $surveyprintlang, PDO::PARAM_STR) - ->query(); } } + +} diff --git a/protected/models/Question.php b/protected/models/Question.php index efd31cd64b3..6cfd32250c2 100644 --- a/protected/models/Question.php +++ b/protected/models/Question.php @@ -367,17 +367,11 @@ public function relations() $alias = $this->getTableAlias(); return array( - 'groups' => array( - self::HAS_ONE, - 'ls\models\QuestionGroup', - '', - 'on' => "$alias.gid = groups.gid AND $alias.language = groups.language" - ), - 'parent' => array(self::BELONGS_TO, 'ls\models\Question', 'parent_qid'), - 'subQuestions' => array(self::HAS_MANY, 'ls\models\Question', 'parent_qid'), + 'parent' => array(self::BELONGS_TO, self::class, 'parent_qid'), + 'subQuestions' => array(self::HAS_MANY, self::class, 'parent_qid'), 'questionAttributes' => [self::HAS_MANY, QuestionAttribute::class, 'qid', 'index' => 'attribute'], - 'group' => [self::BELONGS_TO, 'ls\models\QuestionGroup', 'gid'], - 'survey' => [self::BELONGS_TO, 'ls\models\Survey', 'sid'], + 'group' => [self::BELONGS_TO, QuestionGroup::class, 'gid'], + 'survey' => [self::BELONGS_TO, Survey::class, 'sid'], // Conditions this question has. 'conditions' => [self::HAS_MANY, Condition::class, 'qid'], // Conditions other questions have where this question is the target. @@ -1013,31 +1007,6 @@ public function dependentRelations() ]; } - /** - * Deletes this record and all dependent records. - * @throws CDbException - */ - public function deleteDependent() - { - if (App()->db->getCurrentTransaction() == null) { - $transaction = App()->db->beginTransaction(); - } - foreach ($this->dependentRelations() as $relation) { - /** @var CActiveRecord $record */ - foreach ($this->$relation as $record) { - if (method_exists($record, 'deleteDependent')) { - $record->deleteDependent(); - } else { - $record->delete(); - } - } - } - $this->delete(); - - if (isset($transaction)) { - $transaction->commit(); - } - } /** * Returns the question attributes that do use i18n. diff --git a/protected/models/QuestionGroup.php b/protected/models/QuestionGroup.php index 460361068b7..2f7211ea091 100644 --- a/protected/models/QuestionGroup.php +++ b/protected/models/QuestionGroup.php @@ -147,31 +147,6 @@ public function dependentRelations() } - /** - * Deletes this record and all dependent records. - * @throws CDbException - */ - public function deleteDependent() - { - if (App()->db->getCurrentTransaction() == null) { - $transaction = App()->db->beginTransaction(); - } - foreach ($this->dependentRelations() as $relation) { - /** @var CActiveRecord $record */ - foreach ($this->$relation as $record) { - if (method_exists($record, 'deleteDependent')) { - $record->deleteDependent(); - } else { - $record->delete(); - } - } - } - $this->delete(); - - if (isset($transaction)) { - $transaction->commit(); - } - } public function isRelevant(\ls\interfaces\iResponse $response) { diff --git a/protected/models/Quota.php b/protected/models/Quota.php index f044d06edab..51cd0a12b05 100644 --- a/protected/models/Quota.php +++ b/protected/models/Quota.php @@ -5,19 +5,6 @@ class Quota extends ActiveRecord { - /** - * 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 setting's table name to be used by the model * @@ -37,8 +24,6 @@ public function tableName() */ public function relations() { - $alias = $this->getTableAlias(); - return [ 'languagesettings' => [self::HAS_MANY, QuotaLanguageSetting::class, 'quotals_quota_id'] ]; @@ -62,19 +47,6 @@ public function rules() } - function deleteQuota($condition = false, $recursive = true) - { - if ($recursive == true) { - $oResult = Quota::model()->findAllByAttributes($condition); - foreach ($oResult as $aRow) { - QuotaLanguageSetting::model()->deleteAllByAttributes(array('quotals_quota_id' => $aRow['id'])); - QuotaMember::model()->deleteAllByAttributes(array('quota_id' => $aRow['id'])); - } - } - - Quota::model()->deleteAllByAttributes($condition); - } - /** * Returns the relations that map to dependent records. @@ -87,31 +59,5 @@ public function dependentRelations() 'languagesettings', ]; } - - /** - * Deletes this record and all dependent records. - * @throws CDbException - */ - public function deleteDependent() - { - if (App()->db->getCurrentTransaction() == null) { - $transaction = App()->db->beginTransaction(); - } - foreach ($this->dependentRelations() as $relation) { - /** @var CActiveRecord $record */ - foreach ($this->$relation as $record) { - if (method_exists($record, 'deleteDependent')) { - $record->deleteDependent(); - } else { - $record->delete(); - } - } - } - $this->delete(); - - if (isset($transaction)) { - $transaction->commit(); - } - } } diff --git a/protected/models/Survey.php b/protected/models/Survey.php index a4c375c9922..c530f2cd11d 100644 --- a/protected/models/Survey.php +++ b/protected/models/Survey.php @@ -1017,16 +1017,19 @@ public function getTranslatedFields() */ public function setTranslatedFields($value) { + $languageSettings = $this->languagesettings; foreach ($value as $language => $fields) { - if (!isset($this->languagesettings[$language])) { - $this->languagesettings[$language] = $languageSetting = new SurveyLanguageSetting(); + if (!isset($languageSettings[$language])) { + $languageSettings[$language] = $languageSetting = new SurveyLanguageSetting(); + $languageSetting->surveyls_survey_id = $this->primaryKey; $languageSetting->surveyls_language = $language; } else { - $languageSetting = $this->languagesettings[$language]; + $languageSetting = $languageSettings[$language]; } $languageSetting->attributes = $fields; $languageSetting->save(); } + $this->languagesettings = $languageSettings; } /** @@ -1059,41 +1062,4 @@ public function dependentRelations() ]; } - /** - * Deletes this record and all dependent records. - * @throws CDbException - */ - public function deleteDependent() - { - if (App()->db->getCurrentTransaction() == null) { - $transaction = App()->db->beginTransaction(); - } - foreach ($this->dependentRelations() as $relation) { - /** @var CActiveRecord $record */ - - $config = $this->relations()[$relation]; - if (method_exists($config[1], 'deleteDependent')) { - foreach ($this->$relation as $record) { - $record->deleteDependent(); - } - } else { - // Delete all records in the relation. - if ($config[0] == \CHasManyRelation::class && !isset($config['on']) && is_string($config[2])) { - $class = $config[1]; - $class::model()->deleteAllByAttributes([ - $config[2] => $this->primaryKey - ]); - } else { - throw new \Exceptiion("dont know what to do!"); - } - } - } - $this->delete(); - - if (isset($transaction)) { - $transaction->commit(); - } - } - - } diff --git a/protected/models/SurveyLanguageSetting.php b/protected/models/SurveyLanguageSetting.php index a763ea409e0..0906d335d1b 100644 --- a/protected/models/SurveyLanguageSetting.php +++ b/protected/models/SurveyLanguageSetting.php @@ -76,8 +76,6 @@ public function tableName() */ public function relations() { - $alias = $this->getTableAlias(); - return [ 'survey' => [self::BELONGS_TO, Survey::class, 'surveyls_survey_id'], 'owner' => [self::BELONGS_TO, User::class, '', 'on' => 'survey.owner_id = owner.uid'], @@ -92,6 +90,7 @@ public function relations() public function rules() { return [ + ['surveyls_survey_id', \CExistValidator::class, 'className' => Survey::class, 'attributeName' => 'sid'], ['surveyls_email_invite_subj', 'lsdefault'], ['surveyls_email_invite', 'lsdefault'], ['surveyls_email_remind_subj', 'lsdefault'],