1<?php
2/**
3 * TbAlert class file.
4 * @author Christoffer Niska <christoffer.niska@gmail.com>
5 * @copyright Copyright &copy; Christoffer Niska 2013-
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 */
14class TbAlert extends CWidget
15{
16    /**
17     * @var array the alerts configurations (style=>config).
18     */
19    public $alerts;
20    /**
21     * @var string|boolean the close link text. If this is set false, no close link will be displayed.
22     */
23    public $closeText = TbHtml::CLOSE_TEXT;
24    /**
25     * @var boolean indicates whether the alert should be an alert block. Defaults to 'true'.
26     */
27    public $block = true;
28    /**
29     * @var boolean indicates whether alerts should use transitions. Defaults to 'true'.
30     */
31    public $fade = true;
32    /**
33     * @var string[] the JavaScript event configuration (name=>handler).
34     */
35    public $events = array();
36    /**
37     * @var array the HTML attributes for the alert container.
38     */
39    public $htmlOptions = array();
40
41    /**
42     * Initializes the widget.
43     */
44    public function init()
45    {
46        $this->attachBehavior('TbWidget', new TbWidget);
47        $this->copyId();
48        if (is_string($this->alerts)) {
49            $colors = explode(' ', $this->alerts);
50        } else {
51            if (!isset($this->alerts)) {
52                $colors = array(
53                    TbHtml::ALERT_COLOR_SUCCESS,
54                    TbHtml::ALERT_COLOR_WARNING,
55                    TbHtml::ALERT_COLOR_INFO,
56                    TbHtml::ALERT_COLOR_DANGER,
57                ); // render all styles by default
58            }
59        }
60        if (isset($colors)) {
61            $this->alerts = array();
62            foreach ($colors as $color) {
63                $this->alerts[$color] = array();
64            }
65        }
66    }
67
68    /**
69     * Runs the widget.
70     */
71    public function run()
72    {
73        /* @var $user CWebUser */
74        $user = Yii::app()->getUser();
75        if (count($user->getFlashes(false)) == 0) {
76            return;
77        }
78        echo TbHtml::openTag('div', $this->htmlOptions);
79        foreach ($this->alerts as $color => $alert) {
80            if (isset($alert['visible']) && !$alert['visible']) {
81                continue;
82            }
83
84            if ($user->hasFlash($color)) {
85                $htmlOptions = TbArray::popValue('htmlOptions', $alert, array());
86                TbArray::defaultValue('closeText', $this->closeText, $htmlOptions);
87                TbArray::defaultValue('block', $this->block, $htmlOptions);
88                TbArray::defaultValue('fade', $this->fade, $htmlOptions);
89                echo TbHtml::alert($color, $user->getFlash($color), $htmlOptions);
90            }
91        }
92        echo '</div>';
93        $this->registerEvents("#{$this->htmlOptions['id']} > .alert", $this->events);
94    }
95}
96