1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\backgroundtasks\controllers; |
4
|
|
|
|
5
|
|
|
use app\backgroundtasks\models\NotifyMessage; |
6
|
|
|
use app\backgroundtasks\models\Task; |
7
|
|
|
use Yii; |
8
|
|
|
use yii\filters\AccessControl; |
9
|
|
|
use yii\filters\VerbFilter; |
10
|
|
|
use yii\web\Controller; |
11
|
|
|
use yii\web\NotFoundHttpException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class NotificationController |
15
|
|
|
* @package app\backgroundtasks\controllers |
16
|
|
|
* @author evgen-d <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class NotificationController extends Controller |
19
|
|
|
{ |
20
|
|
|
public function behaviors() |
21
|
|
|
{ |
22
|
|
|
return [ |
23
|
|
|
'access' => [ |
24
|
|
|
'class' => AccessControl::className(), |
|
|
|
|
25
|
|
|
'rules' => [ |
26
|
|
|
[ |
27
|
|
|
'allow' => true, |
28
|
|
|
'roles' => ['@'], |
29
|
|
|
], |
30
|
|
|
], |
31
|
|
|
], |
32
|
|
|
'verbs' => [ |
33
|
|
|
'class' => VerbFilter::className(), |
|
|
|
|
34
|
|
|
'actions' => [ |
35
|
|
|
], |
36
|
|
|
], |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* View all notifications |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function actionIndex() |
45
|
|
|
{ |
46
|
|
|
if (Yii::$app->getModule('background')->inGroup()) { |
47
|
|
|
$searchModel = new NotifyMessage(['scenario' => 'search']); |
48
|
|
|
$dataProvider = $searchModel->search($_GET); |
49
|
|
|
} else { |
50
|
|
|
$searchModel = new NotifyMessage(['scenario' => 'search']); |
51
|
|
|
$dataProvider = $searchModel->search($_GET, true); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->render( |
55
|
|
|
'index', |
56
|
|
|
[ |
57
|
|
|
'dataProvider' => $dataProvider, |
58
|
|
|
'searchModel' => $searchModel, |
59
|
|
|
] |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* View Notification model if exist |
65
|
|
|
* @param $id |
66
|
|
|
* @return string |
67
|
|
|
* @throws \yii\web\NotFoundHttpException |
68
|
|
|
*/ |
69
|
|
|
public function actionView($id) |
70
|
|
|
{ |
71
|
|
|
if (Yii::$app->getModule('background')->inGroup()) { |
72
|
|
|
/* @var $model NotifyMessage */ |
73
|
|
|
$model = NotifyMessage::find()->joinWith(['task'])->where( |
74
|
|
|
[ |
75
|
|
|
'NotifyMessage.id' => $id, |
76
|
|
|
] |
77
|
|
|
)->one(); |
78
|
|
|
} else { |
79
|
|
|
/* @var $model NotifyMessage */ |
80
|
|
|
$model = NotifyMessage::find()->joinWith(['task'])->where( |
81
|
|
|
[ |
82
|
|
|
NotifyMessage::tableName() . '.id' => $id, |
83
|
|
|
Task::tableName() . '.initiator' => Yii::$app->user->id, |
84
|
|
|
] |
85
|
|
|
)->one(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($model !== null) { |
89
|
|
|
switch ($model->result_status) { |
90
|
|
|
case NotifyMessage::STATUS_SUCCESS: |
91
|
|
|
$class = 'panel-success'; |
92
|
|
|
break; |
93
|
|
|
case NotifyMessage::STATUS_FAULT: |
94
|
|
|
$class = 'panel-danger'; |
95
|
|
|
break; |
96
|
|
|
default: |
97
|
|
|
$class = 'panel-default'; |
98
|
|
|
break; |
99
|
|
|
} |
100
|
|
|
return $this->render( |
101
|
|
|
'view', |
102
|
|
|
[ |
103
|
|
|
'model' => $model, |
104
|
|
|
'class' => $class, |
105
|
|
|
] |
106
|
|
|
); |
107
|
|
|
} else { |
108
|
|
|
throw new NotFoundHttpException('repeated task #' . $id . ' not found'); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Return count of new notifications |
114
|
|
|
* @param $current |
115
|
|
|
* @return mixed |
|
|
|
|
116
|
|
|
*/ |
117
|
|
|
public function actionOnlyNewNotifications($current) |
118
|
|
|
{ |
119
|
|
|
if (Yii::$app->getModule('background')->inGroup()) { |
120
|
|
|
return NotifyMessage::find()->where('UNIX_TIMESTAMP(`ts`) > :current', [':current' => $current])->count(); |
121
|
|
|
} else { |
122
|
|
|
return NotifyMessage::find()->where( |
123
|
|
|
[ |
124
|
|
|
'user_id' => Yii::$app->user->id, |
125
|
|
|
] |
126
|
|
|
)->andWhere('UNIX_TIMESTAMP(`ts`) > :current', [':current' => $current])->count(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
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.