Completed
Push — master ( 794470...3fa4d0 )
by Tõnis
02:17
created

BaseSurvey::getTexts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 function rules()
44
    {
45
        return [
46
            [['name'], 'string', 'max' => 254],
47
            [['language_id'], 'integer'],
48
            [['language_id'], 'exist', 'skipOnError' => true, 'targetClass' => Language::class, 'targetAttribute' => ['language_id' => 'language_id']],
49
        ];
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getLanguage()
56
    {
57
        return $this->hasMany(Language::class);
58
    }
59
60
    /**
61
     * @return \yii\db\ActiveQuery
62
     * @throws \yii\base\NotSupportedException
63
     */
64
    public function getLanguages()
65
    {
66
        $relations = $this->surveyLanguages();
67
        $ids = ArrayHelper::getColumn($relations, SurveyLanguage::primaryKeySingle());
68
        return Language::find()->andWhere(['in', Language::primaryKeySingle(), $ids]);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getQuestions()
75
    {
76
        return $this->hasMany(BaseQuestion::class);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getConditions()
83
    {
84
        return $this->hasMany(Condition::class);
85
    }
86
87
88
    /**
89
     * @return SurveyLanguage[]
90
     * @throws \yii\base\NotSupportedException
91
     */
92
    private function surveyLanguages()
93
    {
94
        return SurveyLanguage::getChildren($this);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getTexts()
101
    {
102
        $query = $this->hasMany(SurveyText::class, ['parent_id' => static::primaryKeySingle()]);
103
        return $query->andWhere(['language_id' => $this->runLanguage->primaryKey])->indexBy(Language::primaryKeySingle());
104
    }
105
}