-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPNotify.php
86 lines (69 loc) · 1.85 KB
/
PNotify.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
<?php
namespace iutbay\yii2pnotify;
use yii\web\AssetBundle;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
/**
* PNotify Widget.
*
* @author Kevin LEVRON <[email protected]>
*/
class PNotify extends \yii\base\Widget
{
public $title;
public $text;
public $type;
public $notifications = [];
public $clientOptions = [
'styling' => 'fontawesome',
];
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
}
/**
* Renders the widget.
*/
public function run()
{
$this->registerClientScript();
if ($this->text) $this->createNotification([
'title' => $this->title,
'text' => $this->text,
'type' => $this->type,
]);
foreach ($this->notifications as $n) {
$this->createNotification($n);
}
}
/**
* Create notification
* @param [] $n
*/
protected function createNotification($n)
{
$view = $this->getView();
$options = [];
$text = ArrayHelper::getValue($n, 'text');
if ($text) $options['text'] = $text;
else throw new \InvalidArgumentException('Missing text param.');
$title = ArrayHelper::getValue($n, 'title');
if ($title) $options['title'] = $title;
$type = ArrayHelper::getValue($n, 'type');
if ($type) $options['type'] = $type;
$options = array_merge($this->clientOptions, $options);
$options = Json::encode($options);
$view->registerJs("new PNotify({$options});");
}
/**
* Registers the needed JavaScript.
*/
public function registerClientScript()
{
$view = $this->getView();
PNotifyAsset::register($view);
}
}