1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace dameter\abstracts\models; |
4
|
|
|
|
5
|
|
|
use dameter\abstracts\DActiveRecord; |
6
|
|
|
use dameter\abstracts\interfaces\Translatable; |
7
|
|
|
use yii\helpers\ArrayHelper; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class BaseSurvey |
11
|
|
|
* |
12
|
|
|
* @property int $survey_id |
13
|
|
|
* @property string $name Survey name. Primarily meant for back-end usage. |
14
|
|
|
* @property int $language_id base language id |
15
|
|
|
* |
16
|
|
|
* @property BaseQuestion[] $questions |
17
|
|
|
* @property Language[] $languages Survey base Language |
18
|
|
|
* @property Language $language |
19
|
|
|
* @property Condition[] $conditions All survey conditions |
20
|
|
|
* |
21
|
|
|
* @package dameter\abstracts\models |
22
|
|
|
* @author Tõnis Ormisson <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class BaseSurvey extends DActiveRecord implements Translatable |
25
|
|
|
{ |
26
|
|
|
/** @var Language $runLanguage The language survey is being filled */ |
27
|
|
|
public $runLanguage; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function init() |
33
|
|
|
{ |
34
|
|
|
parent::init(); |
35
|
|
|
if (empty($this->runLanguage)) { |
36
|
|
|
$this->runLanguage = $this->language; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public static function tableName() |
44
|
|
|
{ |
45
|
|
|
return "{{survey}}"; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public static function primaryKey() |
52
|
|
|
{ |
53
|
|
|
return ["survey_id"]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function rules() |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
[['name'], 'string', 'max' => 254], |
63
|
|
|
[['language_id'], 'integer'], |
64
|
|
|
[['language_id'], 'exist', 'skipOnError' => true, 'targetClass' => Language::class, 'targetAttribute' => ['language_id' => 'language_id']], |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function getLanguage() |
72
|
|
|
{ |
73
|
|
|
return $this->hasMany(Language::class); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return \yii\db\ActiveQuery |
78
|
|
|
* @throws \yii\base\NotSupportedException |
79
|
|
|
*/ |
80
|
|
|
public function getLanguages() |
81
|
|
|
{ |
82
|
|
|
$relations = $this->surveyLanguages(); |
83
|
|
|
$ids = ArrayHelper::getColumn($relations, SurveyLanguage::primaryKeySingle()); |
84
|
|
|
return Language::find()->andWhere(['in', Language::primaryKeySingle(), $ids]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public function getQuestions() |
91
|
|
|
{ |
92
|
|
|
return $this->hasMany(BaseQuestion::class); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function getConditions() |
99
|
|
|
{ |
100
|
|
|
return $this->hasMany(Condition::class); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return SurveyLanguage[] |
106
|
|
|
* @throws \yii\base\NotSupportedException |
107
|
|
|
*/ |
108
|
|
|
private function surveyLanguages() |
109
|
|
|
{ |
110
|
|
|
return SurveyLanguage::getChildren($this); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function getTexts() |
117
|
|
|
{ |
118
|
|
|
$query = $this->hasMany(SurveyText::class, ['parent_id' => static::primaryKeySingle()]); |
119
|
|
|
return $query->andWhere(['language_id' => $this->runLanguage->primaryKey])->indexBy(Language::primaryKeySingle()); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: