forked from yii2mod/yii2-sweet-alert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlert.php
144 lines (123 loc) · 3.28 KB
/
Alert.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace dzero1\alert;
use Yii;
use yii\base\InvalidConfigException;
use yii\bootstrap\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
/**
* Alert widget renders a message from session flash or custom messages.
*
* @package dzero1\alert
*/
class Alert extends Widget
{
/**
* Info type of the alert
*/
const TYPE_INFO = 'info';
/**
* Error type of the alert
*/
const TYPE_ERROR = 'error';
/**
* Success type of the alert
*/
const TYPE_SUCCESS = 'success';
/**
* Warning type of the alert
*/
const TYPE_WARNING = 'warning';
/**
* Input type of the alert
*/
const TYPE_INPUT = 'input';
/**
* @var string the type of the alert to be displayed. One of the `TYPE_` constants.
* Defaults to `TYPE_SUCCESS`
*/
public $type = self::TYPE_SUCCESS;
/**
* All the flash messages stored for the session are displayed and removed from the session
* Defaults to true.
*
* @var bool
*/
public $useSessionFlash = true;
/**
* @var bool If set to true, the user can dismiss the modal by clicking outside it.
* Defaults to true.
*/
public $allowOutsideClick = true;
/**
* @var int Auto close timer of the modal. Set in ms (milliseconds).
* Default - 2,5 second
*/
public $timer = 2500;
/**
* @var string customer alert callback
*/
public $callback = 'function() {}';
/**
* Initializes the widget
*/
public function init()
{
parent::init();
if ($this->useSessionFlash) {
$session = Yii::$app->getSession();
$flashes = $session->getAllFlashes();
foreach ($flashes as $type => $data) {
$data = (array) $data;
foreach ($data as $message) {
$this->options['type'] = $type;
$this->options['title'] = $message;
}
$session->removeFlash($type);
}
} else {
if (!$this->hasTitle()) {
throw new InvalidConfigException("The 'title' option is required.");
}
}
}
/**
* @return string|void
*/
public function run()
{
$this->registerAssets();
}
/**
* Register client assets
*/
protected function registerAssets()
{
//if ($this->hasTitle()) {
$view = $this->getView();
AlertAsset::register($view);
$js = "sweetAlert({$this->getOptions()}, {$this->callback});";
$view->registerJs($js, $view::POS_END);
//}
}
/**
* Get plugin options
*
* @return string
*/
protected function getOptions()
{
$this->options['allowOutsideClick'] = ArrayHelper::getValue($this->options, 'allowOutsideClick', $this->allowOutsideClick);
$this->options['timer'] = ArrayHelper::getValue($this->options, 'timer', $this->timer);
$this->options['type'] = ArrayHelper::getValue($this->options, 'type', $this->type);
return Json::encode($this->options);
}
/**
* @return bool
*/
private function hasTitle()
{
$title = ArrayHelper::getValue($this->options, 'title');
return !empty($title);
}
}