Completed
Push — master ( 9a2843...3c69ee )
by Jeff
03:06
created

Screen::setTemplate()   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
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use yii\db\Expression;
7
8
/**
9
 * This is the model class for table "screen".
10
 *
11
 * @property int $id
12
 * @property string $name
13
 * @property string $description
14
 * @property int $template_id
15
 * @property int $duration
16
 * @property string $last_changes
17
 * @property DeviceHasScreen[] $deviceHasScreens
18
 * @property Device[] $devices
19
 * @property ScreenTemplate $template
20
 * @property ScreenHasFlow[] $screenHasFlows
21
 * @property Flow[] $flows
22
 */
23
class Screen extends \yii\db\ActiveRecord
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public static function tableName()
29
    {
30
        return 'screen';
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function rules()
37
    {
38
        return [
39
            [['name', 'template_id'], 'required'],
40
            [['duration', 'template_id'], 'integer'],
41
            [['last_changes', 'template'], 'safe'],
42
            [['name'], 'string', 'max' => 64],
43
            [['description'], 'string', 'max' => 1024],
44
            [['template_id'], 'exist', 'skipOnError' => true, 'targetClass' => ScreenTemplate::className(), 'targetAttribute' => ['template_id' => 'id']],
45
        ];
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function attributeLabels()
52
    {
53
        return [
54
            'id' => Yii::t('app', 'ID'),
55
            'name' => Yii::t('app', 'Name'),
56
            'description' => Yii::t('app', 'Description'),
57
            'template_id' => Yii::t('app', 'Template'),
58
            'duration' => Yii::t('app', 'Duration'),
59
            'last_changes' => Yii::t('app', 'Last Changes'),
60
        ];
61
    }
62
63
    /**
64
     * Loop through parent flows to build a flows tree.
65
     *
66
     * @return array flows with all parents
67
     */
68
    public function allFlows()
69
    {
70
        $ret = [];
71
72
        // Initialize main loop
73
        $newFlows = $this->flows;
74
        while (count($newFlows)) {
75
            // Append latest loop parents
76
            $ret = array_merge($ret, $newFlows);
77
78
            $parents = [];
79
            foreach ($newFlows as $flow) {
80
                if ($flow->parent_id != null) {
81
                    $f = $flow->parent;
82
                    if ($f) {
83
                        $parents[] = $f;
84
                    }
85
                }
86
            }
87
            // Prepare next loop, will merge if necessary later
88
            $newFlows = $parents;
89
        }
90
91
        return $ret;
92
    }
93
94
    /**
95
     * Update last_modified field to force screen reload.
96
     */
97
    public function setModified()
98
    {
99
        $this->last_changes = new Expression('NOW()');
100
        $this->save();
101
    }
102
103
    /**
104
     * @return \yii\db\ActiveQuery
105
     */
106
    public function getDeviceHasScreens()
107
    {
108
        return $this->hasMany(DeviceHasScreen::className(), ['screen_id' => 'id']);
109
    }
110
111
    /**
112
     * @return \yii\db\ActiveQuery
113
     */
114
    public function getDevices()
115
    {
116
        return $this->hasMany(Device::className(), ['id' => 'device_id'])->viaTable('device_has_screen', ['screen_id' => 'id']);
117
    }
118
119
    /**
120
     * @return \yii\db\ActiveQuery
121
     */
122
    public function getTemplate()
123
    {
124
        return $this->hasOne(ScreenTemplate::className(), ['id' => 'template_id']);
125
    }
126
127
    /**
128
     * @return \yii\db\ActiveQuery
129
     */
130
    public function getScreenHasFlows()
131
    {
132
        return $this->hasMany(ScreenHasFlow::className(), ['screen_id' => 'id']);
133
    }
134
135
    /**
136
     * @return \yii\db\ActiveQuery
137
     */
138
    public function getFlows()
139
    {
140
        return $this->hasMany(Flow::className(), ['id' => 'flow_id'])->viaTable('screen_has_flow', ['screen_id' => 'id']);
141
    }
142
}
143