forked from digitv/yii2sockets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YiiNodeSocketFrameGrowl.php
249 lines (219 loc) · 5.09 KB
/
YiiNodeSocketFrameGrowl.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
namespace digitv\yii2sockets;
use Yii;
use yii\base\Component;
use yii\helpers\Url;
use yii\web\IdentityInterface;
/**
* Message class basic
* @var array|string $_body
* @var string $_sessionId
* @var string $_channel
* @var string $_callback
* @var integer $_userId
* @var boolean $_broadcast
* @var string $_text
* @var string $_title
* @var string $_icon
* @var string $_url
* @var string $_url_target
* @var string $_type
* @var integer $_time
* @var integer $_allow_dismiss
*/
class YiiNodeSocketFrameGrowl extends YiiNodeSocketFrameBasic {
const MESSAGE_TYPE_SUCCESS = 'success';
const MESSAGE_TYPE_INFO = 'info';
const MESSAGE_TYPE_DANGER = 'danger';
const MESSAGE_TYPE_WARNING = 'warning';
protected $_text;
protected $_title;
protected $_icon;
protected $_url;
protected $_url_target = '_self';
protected $_type = self::MESSAGE_TYPE_SUCCESS;
protected $_time = 5000;
protected $_allow_dismiss = 1;
/**
* @param string $text
* @return $this
*/
public function setText($text) {
$this->_text = $text;
return $this;
}
/**
* @param string $title
* @return $this
*/
public function setTitle($title) {
$this->_title = $title;
return $this;
}
/**
* @param string $type
* @return $this
*/
public function setType($type) {
$this->_type = $type;
return $this;
}
/**
* @param string|array $url
* @return $this
*/
public function setUrl($url) {
if(is_array($url)) $url = Url::to($url, true);
$this->_url = $url;
return $this;
}
/**
* @param string $url_target
* @return $this
*/
public function setUrlTarget($url_target) {
if(!in_array($url_target, ['_blank', '_self', '_top'])) $url_target = '_self';
$this->_url_target = $url_target;
return $this;
}
/**
* @param string $icon
* @return $this
*/
public function setIcon($icon) {
$this->_icon = $icon;
return $this;
}
/**
* @return $this
*/
public function noIcon() {
$this->_icon = null;
return $this;
}
/**
* @return $this
*/
public function defaultIcon() {
$this->_icon = 'fa fa-envelope';
return $this;
}
/**
* @param integer $time
* @return $this
*/
public function setTime($time) {
$this->_time = $time;
return $this;
}
/**
* @return $this
*/
public function allowDismiss() {
$this->_allow_dismiss = 1;
return $this;
}
/**
* @return $this
*/
public function disallowDismiss() {
$this->_allow_dismiss = 0;
return $this;
}
/**
* @return string|null
*/
public function getText() {
return $this->_text;
}
/**
* @return string|null
*/
public function getTitle() {
return $this->_title;
}
/**
* @return string|null
*/
public function getType() {
return $this->_type;
}
/**
* @return string|null
*/
public function getUrl() {
return $this->_url;
}
/**
* @return string
*/
public function getUrlTarget() {
return $this->_url_target;
}
/**
* @return string|null
*/
public function getIcon() {
return $this->_icon;
}
/**
* @return integer
*/
public function getTime() {
return $this->_time;
}
/**
* Validate input
* @return bool
*/
protected function validate() {
$this->checkType();
if(!$this->getCallback()) $this->setCallback('notifyFrameCallback');
$this->composeOptions();
$valid = parent::validate();
return $valid;
}
/**
* Get data array for YiiNodeSocket
* @return array
*/
protected function composeData() {
$data = parent::composeData();
return $data;
}
protected function composeOptions() {
$options = [];
$settings = [];
$optionsMap = [
'message' => '_text',
'title' => '_title',
'icon' => '_icon',
'url' => '_url',
'target' => '_url_target',
];
$settingsMap = [
'type' => '_type',
'delay' => '_time',
'allow_dismiss' => '_allow_dismiss',
];
foreach ($optionsMap as $mapName => $fieldName) {
$options[$mapName] = $this->{$fieldName};
}
foreach ($settingsMap as $mapName => $fieldName) {
$settings[$mapName] = $this->{$fieldName};
}
$this->setBody([
'options' => $options,
'settings' => $settings,
]);
}
protected function checkType() {
$types = [
self::MESSAGE_TYPE_SUCCESS,
self::MESSAGE_TYPE_INFO,
self::MESSAGE_TYPE_DANGER,
self::MESSAGE_TYPE_WARNING,
];
if(!in_array($this->_type, $types)) $this->setType(self::MESSAGE_TYPE_SUCCESS);
}
}