-
Notifications
You must be signed in to change notification settings - Fork 11
/
Toastr.php
105 lines (93 loc) · 3.21 KB
/
Toastr.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
<?php
/**
* @copyright Copyright © Odai Alali 2014
* @package yii2-toastr
* @version 0.1-dev
*/
namespace odaialali\yii2toastr;
use odaialali\yii2toastr\ToastrAsset;
use odaialali\yii2toastr\ToastrCustomAsset;
use yii\helpers\Json;
/**
* Description of Toastr
*
* @author Odai Alali <[email protected]>
*/
class Toastr extends \yii\base\Widget{
const TYPE_ERROR = 'error';
const TYPE_INFO = 'info';
const TYPE_SUCCESS = 'success';
const TYPE_WARNING = 'warning';
/*
* @var boolean use custom style in asset ToastrCustomAsset
*/
public $customStyle = true;
public $toastType;
public $title;
public $message;
public $options = [];
protected $jsonOptions = [];
public function init() {
parent::init();
if(empty($this->toastType)){
$this->toastType = self::TYPE_INFO;
}
if(empty($this->title)){
$this->title = false;
}
if($this->customStyle){
ToastrCustomAsset::register($this->getView());
}else{
ToastrAsset::register($this->getView());
}
}
public function run() {
parent::run();
$this->registerNotification();
}
protected function initJsOptions(){
//if(isset)
$value_arr = array();
$replace_keys = array();
if(isset($this->options['onclick']) && $this->options['onclick'] != null){
$value_arr[] = $this->options['onclick'];
$this->options['onclick'] = '%onclick%';
$replace_keys[] = '"%onclick%"';
}
if(isset($this->options['onShown']) && $this->options['onShown'] != null){
$value_arr[] = $this->options['onShown'];
$this->options['onShown'] = '%onShown%';
$replace_keys[] = '"%onShown%"';
}
if(isset($this->options['onHidden']) && $this->options['onHidden'] != null){
$value_arr[] = $this->options['onHidden'];
$this->options['onHidden'] = '%onHidden%';
$replace_keys[] = '"%onHidden%"';
}
$this->jsonOptions = empty($this->options) ? '[]' : Json::encode($this->options);
$this->jsonOptions = str_replace($replace_keys, $value_arr, $this->jsonOptions);
}
protected function registerNotification(){
$view = $this->getView();
if ($this->options !== false) {
$this->initJsOptions();
switch($this->toastType){
case 'error':
$js = "toastr.error('".$this->message."', '".$this->title."', ".$this->jsonOptions.")";
break;
case 'warning':
$js = "toastr.warning('".$this->message."', '".$this->title."', ".$this->jsonOptions.")";
break;
case 'info':
$js = "toastr.info('".$this->message."', '".$this->title."', ".$this->jsonOptions.")";
break;
case 'success':
$js = "toastr.success('".$this->message."', '".$this->title."', ".$this->jsonOptions.")";
break;
}
if(isset($js)){
$view->registerJs($js);
}
}
}
}