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 "device". |
10
|
|
|
* |
11
|
|
|
* @property int $id |
12
|
|
|
* @property string $name |
13
|
|
|
* @property string $description |
14
|
|
|
* @property string $last_auth |
15
|
|
|
* @property bool $enabled |
16
|
|
|
* @property int $lastId |
17
|
|
|
* @property DeviceHasScreen[] $deviceHasScreens |
18
|
|
|
* @property Screen[] $screens |
19
|
|
|
*/ |
20
|
|
|
class Device extends \yii\db\ActiveRecord |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public static function tableName() |
26
|
|
|
{ |
27
|
|
|
return 'device'; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function rules() |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
[['name'], 'required'], |
37
|
|
|
[['last_auth'], 'safe'], |
38
|
|
|
[['enabled'], 'boolean'], |
39
|
|
|
[['name'], 'string', 'max' => 64], |
40
|
|
|
[['description'], 'string', 'max' => 1024], |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function attributeLabels() |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
|
|
'id' => Yii::t('app', 'ID'), |
51
|
|
|
'name' => Yii::t('app', 'Name'), |
52
|
|
|
'description' => Yii::t('app', 'Description'), |
53
|
|
|
'last_auth' => Yii::t('app', 'Last connection'), |
54
|
|
|
'enabled' => Yii::t('app', 'Enabled'), |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Update last_auth field to indicate screen last connexion. |
60
|
|
|
*/ |
61
|
|
|
public function setAuthenticated() |
62
|
|
|
{ |
63
|
|
|
$this->last_auth = new Expression('NOW()'); |
64
|
|
|
$this->save(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get last inserted AUTO_INCREMENT id from database. |
69
|
|
|
* |
70
|
|
|
* @return int last insert id |
71
|
|
|
*/ |
72
|
|
|
public function getLastId() |
73
|
|
|
{ |
74
|
|
|
return $this->getDb()->getLastInsertID(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return \yii\db\ActiveQuery |
79
|
|
|
*/ |
80
|
|
|
public function getDeviceHasScreens() |
81
|
|
|
{ |
82
|
|
|
return $this->hasMany(DeviceHasScreen::class, ['device_id' => 'id']); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return \yii\db\ActiveQuery |
87
|
|
|
*/ |
88
|
|
|
public function getScreens() |
89
|
|
|
{ |
90
|
|
|
return $this->hasMany(Screen::class, ['id' => 'screen_id'])->viaTable('device_has_screen', ['device_id' => 'id']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Check if device still has rights to view screen. |
95
|
|
|
* |
96
|
|
|
* @param int $id screen id |
97
|
|
|
* |
98
|
|
|
* @return bool can view screen |
99
|
|
|
*/ |
100
|
|
|
public function canViewScreen($id) |
101
|
|
|
{ |
102
|
|
|
foreach ($this->screens as $s) { |
103
|
|
|
if ($id === $s->id) { |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get next screen for this device. |
113
|
|
|
* |
114
|
|
|
* @param int $currentScreenId |
115
|
|
|
* |
116
|
|
|
* @return \app\models\Screen|null next screen |
117
|
|
|
*/ |
118
|
|
|
public function getNextScreen($currentScreenId = null) |
119
|
|
|
{ |
120
|
|
|
// No screen available, bail out |
121
|
|
|
if (count($this->screens) === 0) { |
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$firstScreen = $this->screens[0]; |
126
|
|
|
if ($currentScreenId === null) { |
127
|
|
|
return $firstScreen; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$foundCurrent = false; |
131
|
|
|
foreach ($this->screens as $s) { |
132
|
|
|
if ($foundCurrent) { |
133
|
|
|
return $s; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if ($currentScreenId === $s->id) { |
137
|
|
|
$foundCurrent = true; // Pick next screen after current |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// Return first screen if reached end of array |
142
|
|
|
return $currentScreenId != $firstScreen->id ? $firstScreen : null; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|