-
Notifications
You must be signed in to change notification settings - Fork 48
/
NotificationsModule.php
95 lines (81 loc) · 2.64 KB
/
NotificationsModule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace machour\yii2\notifications;
use Yii;
use Exception;
use machour\yii2\notifications\models\Notification;
use yii\base\Module;
use yii\db\Expression;
class NotificationsModule extends Module
{
/**
* @var string The controllers namespace
*/
public $controllerNamespace = 'machour\yii2\notifications\controllers';
/**
* @var Notification The notification class defined by the application
*/
public $notificationClass;
/**
* @var boolean Whether notification can be duplicated (same user_id, key, and key_id) or not
*/
public $allowDuplicate = false;
/**
* @var string Database created_at field format
*/
public $dbDateFormat = 'Y-m-d H:i:s';
/**
* @var callable|integer The current user id
*/
public $userId;
/**
* @var callable|integer The current user id
*/
public $expirationTime = 0;
/**
* @inheritdoc
*/
public function init() {
if (is_callable($this->userId)) {
$this->userId = call_user_func($this->userId);
}
parent::init();
if (Yii::$app instanceof \yii\console\Application) {
$this->controllerNamespace = 'machour\yii2\notifications\commands';
}
}
/**
* Creates a notification
*
* @param Notification $notification The notification class
* @param string $key The notification key
* @param integer $user_id The user id that will get the notification
* @param string $key_id The key unique id
* @param string $type The notification type
* @return bool Returns TRUE on success, FALSE on failure
* @throws Exception
*/
public static function notify($notification, $key, $user_id, $key_id = null, $type = Notification::TYPE_DEFAULT)
{
if (!in_array($key, $notification::$keys)) {
throw new Exception("Not a registered notification key: $key");
}
if (!in_array($type, Notification::$types)) {
throw new Exception("Unknown notification type: $type");
}
/** @var Notification $instance */
$instance = $notification::findOne(['user_id' => $user_id, 'key' => $key, 'key_id' => (string)$key_id]);
if (!$instance || \Yii::$app->getModule('notifications')->allowDuplicate) {
$instance = new $notification([
'key' => $key,
'type' => $type,
'seen' => 0,
'flashed' => 0,
'user_id' => $user_id,
'key_id' => (string)$key_id,
'created_at' => new Expression('NOW()'),
]);
return $instance->save();
}
return true;
}
}