YiiFoundation
  • Namespace
  • Class
  • Tree

Namespaces

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

Classes

  • Alert
  • ArrayHelper
  • Button
  • Foundation
  • Html
  • Icon
  • Nav
  • Panel
  • Pricing
  • Progress
  • Typo
  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 
  9 namespace foundation\helpers;
 10 
 11 use foundation\widgets\Dropdown;
 12 use foundation\enum\Enum;
 13 
 14 /**
 15  * Button helper renders different foundation buttons
 16  *
 17  * @author Antonio Ramirez <amigo.cobos@gmail.com>
 18  * @package foundation\helpers
 19  */
 20 class Button
 21 {
 22     /**
 23      * Generates a button
 24      * @param string $label
 25      * @param array $htmlOptions
 26      * @return string the generated button.
 27      */
 28     public static function button($label = 'button', $htmlOptions = array())
 29     {
 30         $htmlOptions = ArrayHelper::defaultValue('name', \CHtml::ID_PREFIX . \CHtml::$count++, $htmlOptions);
 31         Html::clientChange('click', $htmlOptions);
 32         return static::btn('button', $label, $htmlOptions);
 33     }
 34 
 35     /**
 36      * Generates a link button.
 37      * @param string $label the button label text.
 38      * @param array $htmlOptions the HTML attributes for the button.
 39      * @return string the generated button.
 40      */
 41     public static function link($label = 'submit', $htmlOptions = array())
 42     {
 43         ArrayHelper::addValue('class', 'button', $htmlOptions);
 44         return static::btn('a', $label, $htmlOptions);
 45     }
 46 
 47     /**
 48      * Generates a group of link buttons
 49      * @param array $buttons the link buttons to render in a group
 50      * @param array $htmlOptions
 51      * @return string
 52      * @see http://foundation.zurb.com/docs/components/button-groups.html
 53      */
 54     public static function group($buttons, $htmlOptions = array())
 55     {
 56         ArrayHelper::addValue('class', 'button-group', $htmlOptions);
 57 
 58         ob_start();
 59         echo \CHtml::openTag('ul', $htmlOptions);
 60         foreach ($buttons as $button) {
 61             echo $button;
 62         }
 63         echo \CHtml::closeTag('ul');
 64 
 65         return ob_get_clean();
 66     }
 67 
 68     /**
 69      * Generates a button bar by wrapping multiple button groups into a button-bar
 70      * @param array $groups the button group to wrap
 71      * @param array $htmlOptions
 72      * @return string
 73      */
 74     public static function bar($groups, $htmlOptions = array())
 75     {
 76         ArrayHelper::addValue('class', 'button-bar', $htmlOptions);
 77 
 78         ob_start();
 79         echo \CHtml::openTag('div', $htmlOptions);
 80         foreach ($groups as $group) {
 81             echo $group;
 82         }
 83         echo \CHtml::closeTag('div');
 84         
 85         return ob_get_clean();
 86     }
 87 
 88     /**
 89      * Generates a button.
 90      * @param string $tag the HTML tag.
 91      * @param string $label the button label text.
 92      * @param array $htmlOptions additional HTML attributes.
 93      * @return string the generated button.
 94      */
 95     public static function btn($tag, $label, $htmlOptions = array())
 96     {
 97         ArrayHelper::addValue('class', 'button', $htmlOptions);
 98 
 99         $icon = ArrayHelper::removeValue($htmlOptions, 'icon');
100         if (isset($icon))
101             $label = Icon::icon($icon) . '&nbsp;' . $label;
102 
103         return \CHtml::tag($tag, $htmlOptions, $label);
104     }
105 
106     /**
107      * Generates a dropdown button. Important: This method does not formats your list items into links, you have to pass
108      * them as link tags. For a more flexible solution use the widget.
109      *
110      * @param string $label
111      * @param array $list of link items to be displayed on the dropdown menu
112      * @param array $htmlOptions the HTML attributes. It allows a special attribute `dropHtmlOptions` to set your
113      * custom attributes to the dropdown list.
114      * @return string
115      */
116     public static function dropdown($label, $list, $htmlOptions = array())
117     {
118         $id = Enum::ID_PREFIX . '-' . Html::$count++;
119 
120         ArrayHelper::addValue('data-dropdown', $id, $htmlOptions);
121         ArrayHelper::addValue('class', Enum::DROPDOWN, $htmlOptions);
122 
123         $dropHtmlOptions                          = ArrayHelper::removeValue($htmlOptions, 'dropHtmlOptions', array());
124         $dropHtmlOptions['id']                    = $id;
125         $dropHtmlOptions['data-dropdown-content'] = '';
126 
127         ArrayHelper::addValue('class', Enum::DROPDOWN_LIST, $dropHtmlOptions);
128 
129         ob_start();
130         echo static::link($label, $htmlOptions);
131         echo Dropdown::display(
132             array(
133                 'items'       => $list,
134                 'htmlOptions' => $dropHtmlOptions
135             )
136         );
137         return ob_get_clean();
138     }
139 
140     /**
141      * Generates a split button.
142      * Important: Currently split buttons require the dropdown list items to be rendered before the body tag, so the
143      * dropdown list hides on document.click. You will have to click again on dropdown to hide the list.
144      * Please, use the button widget for better performance. At the moment, Foundation uses a hack until its new release 4.2
145      * @param $label
146      * @param $list
147      * @param array $htmlOptions
148      * @return string
149      */
150     public static function split($label, $list, $htmlOptions = array())
151     {
152         $id = Enum::ID_PREFIX . '-' . ++Html::$count;
153 
154         ArrayHelper::addValue('class', Enum::DROPDOWN_SPLIT, $htmlOptions);
155 
156         $label .= ' <span data-dropdown="' . $id . '"></span>';
157 
158         $dropHtmlOptions       = ArrayHelper::removeValue($htmlOptions, 'dropHtmlOptions', array());
159         $dropHtmlOptions['id'] = $id;
160 
161         ArrayHelper::addValue('class', Enum::DROPDOWN_LIST, $dropHtmlOptions);
162 
163         ob_start();
164         echo '<div style="position:relative">'; // hack
165         echo static::link($label, $htmlOptions);
166         echo Dropdown::display(
167             array(
168                 'items'       => $list,
169                 'htmlOptions' => $dropHtmlOptions
170             )
171         );
172         echo '</div>';
173         return ob_get_clean();
174     }
175 }
176 
YiiFoundation API documentation generated by ApiGen 2.8.0