1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace roaresearch\yii2\workflow\models; |
4
|
|
|
|
5
|
|
|
use roaresearch\yii2\rmdb\SoftDeleteActiveQuery; |
6
|
|
|
use yii\db\ActiveQuery; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Model class for table `{{%workflow}}` |
10
|
|
|
* |
11
|
|
|
* @property integer $id |
12
|
|
|
* @property string $name |
13
|
|
|
* |
14
|
|
|
* @property Stage[] $stages |
15
|
|
|
*/ |
16
|
|
|
class Workflow extends \roaresearch\yii2\rmdb\models\PersistentEntity |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string full class name of the model to be used for the relation |
20
|
|
|
* `getStages()`. |
21
|
|
|
*/ |
22
|
|
|
protected $stageClass = Stage::class; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
25 |
|
public static function tableName() |
28
|
|
|
{ |
29
|
25 |
|
return '{{%workflow}}'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritdoc |
34
|
|
|
*/ |
35
|
19 |
|
protected function attributeTypecast(): ?array |
36
|
|
|
{ |
37
|
19 |
|
return parent::attributeTypecast() + ['id' => 'integer']; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
2 |
|
public function rules() |
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
2 |
|
[['name'], 'required'], |
47
|
|
|
[['name'], 'string', 'min' => 6], |
48
|
|
|
[['name'], 'unique'], |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
3 |
|
public function attributeLabels() |
56
|
|
|
{ |
57
|
3 |
|
return array_merge([ |
58
|
3 |
|
'id' => 'ID', |
59
|
|
|
'name' => 'Workflow name', |
60
|
3 |
|
], parent::attributeLabels()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return SoftDeleteActiveQuery |
65
|
|
|
*/ |
66
|
3 |
|
public function getStages(): SoftDeleteActiveQuery |
67
|
|
|
{ |
68
|
3 |
|
return $this->hasMany($this->stageClass, ['workflow_id' => 'id']) |
|
|
|
|
69
|
3 |
|
->inverseOf('workflow'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return ActiveQuery |
74
|
|
|
*/ |
75
|
1 |
|
public function getDetailStages(): ActiveQuery |
76
|
|
|
{ |
77
|
1 |
|
$query = $this->getStages(); |
78
|
1 |
|
$query->multiple = false; |
79
|
|
|
|
80
|
1 |
|
return $query->select([ |
81
|
1 |
|
'workflow_id', |
82
|
|
|
'totalStages' => 'count(distinct id)', |
83
|
1 |
|
])->asArray() |
84
|
1 |
|
->inverseOf(null) |
85
|
1 |
|
->groupBy('workflow_id'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
1 |
|
public function getTotalStages(): int |
92
|
|
|
{ |
93
|
1 |
|
return (int)$this->detailStages['totalStages']; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritdoc |
98
|
|
|
*/ |
99
|
19 |
|
protected function softDeleteBehaviorConfig(): array |
100
|
|
|
{ |
101
|
19 |
|
return parent::softDeleteBehaviorConfig() + [ |
102
|
19 |
|
'allowDeleteCallback' => function ($record) { |
103
|
1 |
|
return !$record->getStages()->exists(); |
104
|
19 |
|
}, |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|