1 <?php
2 3 4 5 6 7
8 namespace foundation\widgets;
9
10 use foundation\exception\InvalidConfigException;
11 use foundation\helpers\ArrayHelper;
12 use foundation\helpers\Html;
13 use foundation\enum\Enum;
14 use foundation\exception\InvalidConfigurationException;
15
16 17 18 19 20 21 22 23
24 class Alert extends base\Widget
25 {
26 27 28
29 public $config;
30 31 32
33 public $closeText = '×';
34 35 36
37 public $events = array();
38
39
40 41 42 43
44 public function init()
45 {
46 if (is_string($this->config)) {
47 throw new InvalidConfigException('"config" must be of type array.');
48 }
49 $this->assets = array(
50 'js' => YII_DEBUG ? 'foundation/foundation.alerts.js' : 'foundation.min.js'
51 );
52
53 if (!isset($this->config)) {
54 $this->config = array(
55 Enum::COLOR_REGULAR => array(),
56 Enum::COLOR_ALERT => array(),
57 Enum::COLOR_SECONDARY => array(),
58 Enum::COLOR_SUCCESS => array(),
59 );
60 }
61 $this->htmlOptions = ArrayHelper::defaultValue('id', $this->getId(), $this->htmlOptions);
62 parent::init();
63 }
64
65 66 67
68 public function run()
69 {
70 echo $this->renderAlert();
71 }
72
73 74 75 76
77 public function renderAlert()
78 {
79 $user = \Yii::app()->user;
80 if (count($user->getFlashes(false)) == 0) {
81 return;
82 }
83 $alerts = array();
84 foreach ($this->config as $style => $config) {
85 if ($user->hasFlash($style)) {
86 $options = ArrayHelper::getValue($config, 'htmlOptions', array());
87 Html::addCssClass($options, $style);
88 $close = ArrayHelper::getValue($config, 'close', '×');
89 $alerts[] = \foundation\helpers\Alert::alert($user->getFlash($style), $options, $close);
90 }
91 }
92 $this->registerEvents("#{$this->htmlOptions['id']} > .alert-box", $this->events);
93
94 return \CHtml::tag('div', $this->htmlOptions, implode("\n", $alerts));
95 }
96 }
97