1: <?php
2: 3: 4: 5: 6: 7: 8:
9:
10: Yii::import('bootstrap.behaviors.TbWidget');
11:
12: 13: 14: 15:
16: class TbAlert extends CWidget
17: {
18: 19: 20:
21: public $alerts;
22: 23: 24:
25: public $closeText = TbHtml::CLOSE_TEXT;
26: 27: 28:
29: public $block = true;
30: 31: 32:
33: public $fade = true;
34: 35: 36:
37: public $events = array();
38: 39: 40:
41: public $htmlOptions = array();
42:
43: 44: 45:
46: public function init()
47: {
48: $this->attachBehavior('TbWidget', new TbWidget);
49: $this->copyId();
50: if (is_string($this->alerts)) {
51: $colors = explode(' ', $this->alerts);
52: } else {
53: if (!isset($this->alerts)) {
54: $colors = array(
55: TbHtml::ALERT_COLOR_SUCCESS,
56: TbHtml::ALERT_COLOR_WARNING,
57: TbHtml::ALERT_COLOR_INFO,
58: TbHtml::ALERT_COLOR_ERROR
59: );
60: }
61: }
62: if (isset($colors)) {
63: $this->alerts = array();
64: foreach ($colors as $color) {
65: $this->alerts[$color] = array();
66: }
67: }
68: }
69:
70: 71: 72:
73: public function run()
74: {
75:
76: $user = Yii::app()->getUser();
77: if (count($user->getFlashes(false)) == 0) {
78: return;
79: }
80: echo TbHtml::openTag('div', $this->htmlOptions);
81: foreach ($this->alerts as $color => $alert) {
82: if (isset($alert['visible']) && !$alert['visible']) {
83: continue;
84: }
85:
86: if ($user->hasFlash($color)) {
87: $htmlOptions = TbArray::popValue('htmlOptions', $alert, array());
88: TbArray::defaultValue('closeText', $this->closeText, $htmlOptions);
89: TbArray::defaultValue('block', $this->block, $htmlOptions);
90: TbArray::defaultValue('fade', $this->fade, $htmlOptions);
91: echo TbHtml::alert($color, $user->getFlash($color), $htmlOptions);
92: }
93: }
94: echo '</div>';
95: $this->registerEvents("#{$this->htmlOptions['id']} > .alert", $this->events);
96: }
97: }