1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This is the model class for table "flow". |
9
|
|
|
* |
10
|
|
|
* @property int $id |
11
|
|
|
* @property string $name |
12
|
|
|
* @property string $description |
13
|
|
|
* @property int $parent_id |
14
|
|
|
* @property Content[] $contents |
15
|
|
|
* @property Flow $parent |
16
|
|
|
* @property Flow[] $flows |
17
|
|
|
* @property ScreenHasFlow[] $screenHasFlows |
18
|
|
|
* @property Screen[] $screens |
19
|
|
|
* @property UserHasFlow[] $userHasFlows |
20
|
|
|
* @property User[] $users |
21
|
|
|
*/ |
22
|
|
|
class Flow extends \yii\db\ActiveRecord |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public static function tableName() |
28
|
|
|
{ |
29
|
|
|
return 'flow'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function rules() |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
return [ |
38
|
|
|
[['name'], 'required'], |
39
|
|
|
[['parent_id', 'id'], 'integer'], |
40
|
|
|
[['name'], 'string', 'max' => 64], |
41
|
|
|
[['description'], 'string', 'max' => 1024], |
42
|
|
|
[['parent'], 'safe'], |
43
|
|
|
[['parent_id'], 'exist', 'skipOnError' => true, 'targetClass' => self::class, 'targetAttribute' => ['parent_id' => 'id']], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function attributeLabels() |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
'id' => Yii::t('app', 'ID'), |
54
|
|
|
'name' => Yii::t('app', 'Name'), |
55
|
|
|
'description' => Yii::t('app', 'Description'), |
56
|
|
|
'parent_id' => Yii::t('app', 'Parent'), |
57
|
|
|
'parent' => Yii::t('app', 'Parent flow'), |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return \yii\db\ActiveQuery |
63
|
|
|
*/ |
64
|
|
|
public function getContents() |
65
|
|
|
{ |
66
|
|
|
return $this->hasMany(Content::class, ['flow_id' => 'id']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return \yii\db\ActiveQuery |
71
|
|
|
*/ |
72
|
|
|
public function getParent() |
73
|
|
|
{ |
74
|
|
|
return $this->hasOne(self::class, ['id' => 'parent_id']); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set parent_id with flow. |
79
|
|
|
* |
80
|
|
|
* @param \app\models\Flow $flow parent flow |
81
|
|
|
*/ |
82
|
|
|
public function setParent($flow) |
83
|
|
|
{ |
84
|
|
|
$this->parent_id = $flow->id; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return \yii\db\ActiveQuery |
89
|
|
|
*/ |
90
|
|
|
public function getChildren() |
91
|
|
|
{ |
92
|
|
|
return $this->hasMany(self::class, ['parent_id' => 'id']); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return \yii\db\ActiveQuery |
97
|
|
|
*/ |
98
|
|
|
public function getScreenHasFlows() |
99
|
|
|
{ |
100
|
|
|
return $this->hasMany(ScreenHasFlow::class, ['flow_id' => 'id']); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return \yii\db\ActiveQuery |
105
|
|
|
*/ |
106
|
|
|
public function getScreens() |
107
|
|
|
{ |
108
|
|
|
return $this->hasMany(Screen::class, ['id' => 'screen_id'])->viaTable('screen_has_flow', ['flow_id' => 'id']); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return \yii\db\ActiveQuery |
113
|
|
|
*/ |
114
|
|
|
public function getUserHasFlows() |
115
|
|
|
{ |
116
|
|
|
return $this->hasMany(UserHasFlow::class, ['flow_id' => 'id']); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return \yii\db\ActiveQuery |
121
|
|
|
*/ |
122
|
|
|
public function getUsers() |
123
|
|
|
{ |
124
|
|
|
return $this->hasMany(User::class, ['username' => 'user_username'])->viaTable('user_has_flow', ['flow_id' => 'id']); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Build a query for a specific user, allowing to see only authorized flows. |
129
|
|
|
* |
130
|
|
|
* @param \yii\web\User $user |
131
|
|
|
* |
132
|
|
|
* @return \yii\db\ActiveQuery |
133
|
|
|
*/ |
134
|
|
|
public static function availableQuery($user) |
135
|
|
|
{ |
136
|
|
View Code Duplication |
if ($user->can('setFlowContent')) { |
|
|
|
|
137
|
|
|
return self::find(); |
138
|
|
|
} elseif ($user->can('setOwnFlowContent') && $user->identity instanceof \app\models\User) { |
139
|
|
|
return self::find()->joinWith(['users'])->where(['username' => $user->identity->username]); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Check if a specific user is allowed to see this flow. |
145
|
|
|
* |
146
|
|
|
* @param \yii\web\User $user |
147
|
|
|
* |
148
|
|
|
* @return bool can see |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
public function canView($user) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
if ($user->can('setFlowContent')) { |
153
|
|
|
return true; |
154
|
|
|
} |
155
|
|
|
if ($user->can('setOwnFlowContent') && in_array($user->identity, $this->users)) { |
156
|
|
|
return true; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.