1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\backgroundtasks\models; |
4
|
|
|
|
5
|
|
|
use app\backgroundtasks\traits\SearchModelTrait; |
6
|
|
|
use app\modules\user\models\User; |
7
|
|
|
use yii\data\ActiveDataProvider; |
8
|
|
|
use yii\db\ActiveRecord; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This is the model class for table "backgroundtasks_notify_message". |
12
|
|
|
* |
13
|
|
|
* @property integer $id |
14
|
|
|
* @property integer $task_id |
15
|
|
|
* @property string $result_status |
16
|
|
|
* @property string $result |
17
|
|
|
*/ |
18
|
|
|
class NotifyMessage extends ActiveRecord |
19
|
|
|
{ |
20
|
|
|
use SearchModelTrait; |
21
|
|
|
|
22
|
|
|
public $name; |
23
|
|
|
public $username; |
24
|
|
|
|
25
|
|
|
const STATUS_SUCCESS = 'SUCCESS'; |
26
|
|
|
const STATUS_FAULT = 'FAULT'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
|
public static function tableName() |
32
|
|
|
{ |
33
|
|
|
return '{{%backgroundtasks_notify_message}}'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function getStatuses() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
self::STATUS_SUCCESS => 'success', |
40
|
|
|
self::STATUS_FAULT => 'fault', |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
|
|
public function rules() |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
|
|
[['task_id', 'result_status', 'result'], 'required', 'except' => 'search'], |
51
|
|
|
[['task_id'], 'integer'], |
52
|
|
|
[['result_status', 'result'], 'string'], |
53
|
|
|
[['name', 'username'], 'safe'], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
|
public function attributeLabels() |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
'id' => 'ID', |
64
|
|
|
'task_id' => 'Task ID', |
65
|
|
|
'result_status' => 'Result Status', |
66
|
|
|
'result' => 'Result', |
67
|
|
|
'ts' => 'Received at', |
68
|
|
|
'name' => 'Task', |
69
|
|
|
'username' => 'Initiator', |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Task relation |
75
|
|
|
* @return \yii\db\ActiveQueryInterface |
76
|
|
|
*/ |
77
|
|
|
public function getTask() |
78
|
|
|
{ |
79
|
|
|
return $this->hasOne(Task::className(), ['id' => 'task_id']); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function scenarios() |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
|
|
'default' => ['id', 'task_id', 'result_status', 'result', 'ts'], |
86
|
|
|
'search' => ['ts', 'name', 'username', 'result_status'], |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Search tasks |
92
|
|
|
* @param $params |
93
|
|
|
* @param bool $withUser |
94
|
|
|
* @return ActiveDataProvider |
95
|
|
|
*/ |
96
|
|
|
public function search($params, $withUser = false) |
97
|
|
|
{ |
98
|
|
|
$query = self::find(); |
99
|
|
|
$query->joinWith(['task', 'task.initiatorUser']); |
100
|
|
|
|
101
|
|
|
$dataProvider = new ActiveDataProvider( |
102
|
|
|
[ |
103
|
|
|
'query' => $query, |
104
|
|
|
'pagination' => [ |
105
|
|
|
'pageSize' => 10, |
106
|
|
|
], |
107
|
|
|
'sort' => [ |
108
|
|
|
'defaultOrder' => [ |
109
|
|
|
'ts' => SORT_DESC, |
110
|
|
|
] |
111
|
|
|
], |
112
|
|
|
] |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
$dataProvider->sort->attributes['name'] = [ |
116
|
|
|
'asc' => [Task::tableName().'.name' => SORT_ASC], |
117
|
|
|
'desc' => [Task::tableName().'.name' => SORT_DESC], |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
$dataProvider->sort->attributes['username'] = [ |
121
|
|
|
'asc' => [\app\modules\user\models\User::tableName().'.username' => SORT_ASC], |
122
|
|
|
'desc' => [User::tableName().'.username' => SORT_DESC], |
123
|
|
|
]; |
124
|
|
|
|
125
|
|
|
if ($withUser) { |
126
|
|
|
$query->andWhere([Task::tableName().'.initiator' => \Yii::$app->user->id]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (!($this->load($params) && $this->validate())) { |
130
|
|
|
return $dataProvider; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$this->addCondition($query, User::tableName(), 'username', true); |
134
|
|
|
$this->addCondition($query, $this->tableName(), 'ts', true); |
135
|
|
|
$this->addCondition($query, Task::tableName(), 'name', true); |
136
|
|
|
$this->addCondition($query, $this->tableName(), 'result_status'); |
137
|
|
|
|
138
|
|
|
return $dataProvider; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.