1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* TbAlert class file.
|
4
|
|
|
* @author Christoffer Niska <[email protected]>
|
5
|
|
|
* @copyright Copyright © Christoffer Niska 2011-
|
6
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
|
7
|
|
|
* @package bootstrap.widgets
|
8
|
|
|
*/
|
9
|
|
|
|
10
|
|
|
/**
|
11
|
|
|
* Bootstrap alert widget.
|
12
|
|
|
* @see http://twitter.github.com/bootstrap/javascript.html#alerts
|
13
|
|
|
*/
|
14
|
|
|
class TbAlert extends CWidget
|
15
|
|
|
{
|
16
|
|
|
// Alert types.
|
17
|
|
|
const TYPE_SUCCESS = 'success';
|
18
|
|
|
const TYPE_INFO = 'info';
|
19
|
|
|
const TYPE_WARNING = 'warning';
|
20
|
|
|
const TYPE_ERROR = 'error';
|
21
|
|
|
const TYPE_DANGER = 'danger'; // same as error
|
22
|
|
|
|
23
|
|
|
/**
|
24
|
|
|
* @var array the alerts configurations.
|
25
|
|
|
*/
|
26
|
|
|
public $alerts;
|
27
|
|
|
/**
|
28
|
|
|
* @var string|boolean the close link text. If this is set false, no close link will be displayed.
|
29
|
|
|
*/
|
30
|
|
|
public $closeText = '×';
|
31
|
|
|
/**
|
32
|
|
|
* @var boolean indicates whether the alert should be an alert block. Defaults to 'true'.
|
33
|
|
|
*/
|
34
|
|
|
public $block = true;
|
35
|
|
|
/**
|
36
|
|
|
* @var boolean indicates whether alerts should use transitions. Defaults to 'true'.
|
37
|
|
|
*/
|
38
|
|
|
public $fade = true;
|
39
|
|
|
/**
|
40
|
|
|
* @var string[] the Javascript event handlers.
|
41
|
|
|
*/
|
42
|
|
|
public $events = array();
|
43
|
|
|
/**
|
44
|
|
|
* @var array the HTML attributes for the widget container.
|
45
|
|
|
*/
|
46
|
|
|
public $htmlOptions = array();
|
47
|
|
|
|
48
|
|
|
/**
|
49
|
|
|
* Initializes the widget.
|
50
|
|
|
*/
|
51
|
|
|
public function init()
|
52
|
|
|
{
|
53
|
|
|
if (!isset($this->htmlOptions['id']))
|
54
|
|
|
$this->htmlOptions['id'] = $this->getId();
|
55
|
|
|
|
56
|
|
|
if (is_string($this->alerts))
|
57
|
|
|
$this->alerts = array($this->alerts);
|
58
|
|
|
|
59
|
|
|
// Display all alert types by default.
|
60
|
|
|
if (!isset($this->alerts))
|
61
|
|
|
$this->alerts = array(self::TYPE_SUCCESS, self::TYPE_INFO, self::TYPE_WARNING, self::TYPE_ERROR, self::TYPE_DANGER);
|
62
|
|
|
}
|
63
|
|
|
|
64
|
|
|
/**
|
65
|
|
|
* Runs the widget.
|
66
|
|
|
*/
|
67
|
|
|
public function run()
|
68
|
|
|
{
|
69
|
|
|
$id = $this->htmlOptions['id'];
|
70
|
|
|
|
71
|
|
|
echo CHtml::openTag('div', $this->htmlOptions);
|
72
|
|
|
|
73
|
|
|
foreach ($this->alerts as $type => $alert)
|
74
|
|
|
{
|
75
|
|
|
if (is_string($alert))
|
76
|
|
|
{
|
77
|
|
|
$type = $alert;
|
78
|
|
|
$alert = array();
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
if (isset($alert['visible']) && $alert['visible'] === false)
|
82
|
|
|
continue;
|
83
|
|
|
|
84
|
|
|
if (Yii::app()->user->hasFlash($type))
|
85
|
|
|
{
|
86
|
|
|
$classes = array('alert in');
|
87
|
|
|
|
88
|
|
|
if (!isset($alert['block']))
|
89
|
|
|
$alert['block'] = $this->block;
|
90
|
|
|
|
91
|
|
|
if ($alert['block'] === true)
|
92
|
|
|
$classes[] = 'alert-block';
|
93
|
|
|
|
94
|
|
|
if (!isset($alert['fade']))
|
95
|
|
|
$alert['fade'] = $this->fade;
|
96
|
|
|
|
97
|
|
|
if ($alert['fade'] === true)
|
98
|
|
|
$classes[] = 'fade';
|
99
|
|
|
|
100
|
|
|
$validTypes = array(self::TYPE_SUCCESS, self::TYPE_INFO, self::TYPE_WARNING, self::TYPE_ERROR, self::TYPE_DANGER);
|
101
|
|
|
|
102
|
|
|
if (in_array($type, $validTypes))
|
103
|
|
|
$classes[] = 'alert-'.$type;
|
104
|
|
|
|
105
|
|
|
if (!isset($alert['htmlOptions']))
|
106
|
|
|
$alert['htmlOptions'] = array();
|
107
|
|
|
|
108
|
|
|
$classes = implode(' ', $classes);
|
109
|
|
|
if (isset($alert['htmlOptions']['class']))
|
110
|
|
|
$alert['htmlOptions']['class'] .= ' '.$classes;
|
111
|
|
|
else
|
112
|
|
|
$alert['htmlOptions']['class'] = $classes;
|
113
|
|
|
|
114
|
|
|
echo CHtml::openTag('div', $alert['htmlOptions']);
|
115
|
|
|
|
116
|
|
|
if ($this->closeText !== false && !isset($alert['closeText']))
|
117
|
|
|
$alert['closeText'] = $this->closeText;
|
118
|
|
|
else
|
119
|
|
|
$alert['closeText'] = false;
|
120
|
|
|
|
121
|
|
|
if ($alert['closeText'] !== false)
|
122
|
|
|
echo '<a class="close" data-dismiss="alert">'.$alert['closeText'].'</a>';
|
123
|
|
|
|
124
|
|
|
echo Yii::app()->user->getFlash($type);
|
125
|
|
|
|
126
|
|
|
echo '</div>';
|
127
|
|
|
}
|
128
|
|
|
}
|
129
|
|
|
|
130
|
|
|
echo '</div>';
|
131
|
|
|
|
132
|
|
|
$selector = "#{$id} .alert";
|
133
|
|
|
|
134
|
|
|
/** @var CClientScript $cs */
|
135
|
|
|
$cs = Yii::app()->getClientScript();
|
136
|
|
|
$cs->registerScript(__CLASS__.'#'.$id, "jQuery('{$selector}').alert().animate({opacity: 0.5}, 5000).fadeOut('fast')");
|
137
|
|
|
|
138
|
|
|
foreach ($this->events as $name => $handler)
|
139
|
|
|
{
|
140
|
|
|
$handler = CJavaScript::encode($handler);
|
141
|
|
|
$cs->registerScript(__CLASS__.'#'.$id.'_'.$name, "jQuery('{$selector}').on('{$name}', {$handler});");
|
142
|
|
|
}
|
143
|
|
|
}
|
144
|
|
|
}
|
145
|
|
|
|