YiiFoundation
  • Namespace
  • Class
  • Tree

Namespaces

  • foundation
    • enum
    • exception
    • helpers
    • widgets
      • base
  • PHP

Classes

  • Alert
  • Breadcrumbs
  • Clearing
  • Dropdown
  • FlexVideo
  • Interchange
  • JoyRide
  • Magellan
  • Orbit
  • Pager
  • Reveal
  • Section
  • SwitchButton
  • Tooltip
  • TopBar
 1 <?php
 2 /**
 3  * @copyright Copyright &copy; 2amigOS! Consulting Group 2013-
 4  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 5  * @package foundation.components
 6  * @version 1.0.0
 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  * Alert renders a Foundation style alert from messages stored on user session variables -named Flash messages.
18  *
19  * @see http://foundation.zurb.com/docs/components/alert-boxes.html
20  *
21  * @author Antonio Ramirez <amigo.cobos@gmail.com>
22  * @package foundation\widgets
23  */
24 class Alert extends base\Widget
25 {
26     /**
27      * @var array the alerts configurations (style=>config)
28      */
29     public $config;
30     /**
31      * @var string|boolean the close link text. If this is set false, no close link will be displayed.
32      */
33     public $closeText = '&times;';
34     /**
35      * @var string[] the JavaScript event configuration (name=>handler).
36      */
37     public $events = array();
38 
39 
40     /**
41      * Initializes the widget
42      * @throws \foundation\exception\InvalidConfigException
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      * Renders the widget
67      */
68     public function run()
69     {
70         echo $this->renderAlert();
71     }
72 
73     /**
74      * Renders the alert
75      * @return string the rendering result
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', '&times;');
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 
YiiFoundation API documentation generated by ApiGen 2.8.0