|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace dominus77\noty; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\base\Widget; |
|
7
|
|
|
use yii\helpers\Json; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Noty |
|
11
|
|
|
* @package dominus77\noty |
|
12
|
|
|
*/ |
|
13
|
|
|
class NotyWidget extends Widget |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Themes |
|
17
|
|
|
*/ |
|
18
|
|
|
const THEME_BOOTSTRAP_3 = 'bootstrap-v3'; |
|
19
|
|
|
const THEME_BOOTSTRAP_4 = 'bootstrap-v4'; |
|
20
|
|
|
const THEME_LIGHT = 'light'; |
|
21
|
|
|
const THEME_METROUI = 'metroui'; |
|
22
|
|
|
const THEME_MINT = 'mint'; |
|
23
|
|
|
const THEME_NEST = 'nest'; |
|
24
|
|
|
const THEME_RELAX = 'relax'; |
|
25
|
|
|
const THEME_SEMANTICUI = 'semanticui'; |
|
26
|
|
|
const THEME_SUNSET = 'sunset'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Types |
|
30
|
|
|
*/ |
|
31
|
|
|
const TYPE_ALERT = 'alert'; |
|
32
|
|
|
const TYPE_SUCCESS = 'success'; |
|
33
|
|
|
const TYPE_WARNING = 'warning'; |
|
34
|
|
|
const TYPE_DANGER = 'error'; |
|
35
|
|
|
const TYPE_ERROR = 'error'; |
|
36
|
|
|
const TYPE_INFO = 'info'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
private $typeMap = [ |
|
42
|
|
|
'alert' => self::TYPE_ALERT, |
|
43
|
|
|
'success' => self::TYPE_SUCCESS, |
|
44
|
|
|
'warning' => self::TYPE_WARNING, |
|
45
|
|
|
'danger' => self::TYPE_DANGER, |
|
46
|
|
|
'error' => self::TYPE_ERROR, |
|
47
|
|
|
'info' => self::TYPE_INFO, |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var array |
|
52
|
|
|
*/ |
|
53
|
|
|
public $typeOptions = []; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
public $options = [ |
|
59
|
|
|
'progressBar' => true, |
|
60
|
|
|
'timeout' => false, |
|
61
|
|
|
'layout' => 'topRight', |
|
62
|
|
|
'dismissQueue' => true, |
|
63
|
|
|
'theme' => self::THEME_RELAX, |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
|
|
/** @var yii\web\View */ |
|
67
|
|
|
private $_view; |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @inheritdoc |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function init() |
|
73
|
|
|
{ |
|
74
|
1 |
|
parent::init(); |
|
75
|
1 |
|
$this->_view = $this->getView(); |
|
|
|
|
|
|
76
|
1 |
|
NotyAsset::register($this->_view); |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return string|void |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function run() |
|
83
|
|
|
{ |
|
84
|
1 |
|
$session = Yii::$app->session; |
|
85
|
1 |
|
$flashes = $session->getAllFlashes(); |
|
86
|
|
|
|
|
87
|
1 |
|
foreach ($flashes as $type => $flash) { |
|
88
|
1 |
|
if (!isset($this->typeMap[$type])) { |
|
89
|
|
|
continue; |
|
90
|
|
|
} |
|
91
|
1 |
|
$typeAlert = $this->typeMap[$type]; |
|
92
|
1 |
|
$options = array_merge($this->options, $this->typeOptions[$typeAlert] ?? []); |
|
93
|
|
|
|
|
94
|
1 |
|
foreach ((array)$flash as $i => $message) { |
|
95
|
1 |
|
$options['type'] = $typeAlert; |
|
96
|
1 |
|
$options['text'] = $message; |
|
97
|
1 |
|
$optionsEncoded = Json::encode($options); |
|
98
|
1 |
|
$this->_view->registerJs("new Noty($optionsEncoded).show();"); |
|
99
|
|
|
} |
|
100
|
1 |
|
$session->removeFlash($type); |
|
101
|
|
|
} |
|
102
|
1 |
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|