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 namespace foundation\helpers;
  9 
 10 use foundation\enum\Enum;
 11 use foundation\exception\InvalidConfigException;
 12 
 13 /**
 14  * Nav Helper renders different type of navigation markups.
 15  *
 16  * @author Antonio Ramirez <amigo.cobos@gmail.com>
 17  * @package foundation\helpers
 18  */
 19 class Nav
 20 {
 21 
 22     /**
 23      * Generates a side nav
 24      *
 25      * @param array $items the link items to display as a side nav. The items have the following format:
 26      * <pre>
 27      * array('label'=>'Item', 'url'=>'#', 'linkOptions'=>array(), 'itemOptions'=>array())
 28      * </pre>
 29      * @param array $htmlOptions
 30      * @return string
 31      * @see http://foundation.zurb.com/docs/components/side-nav.html
 32      */
 33     public static function side($items, $htmlOptions = array())
 34     {
 35         ArrayHelper::addValue('class', Enum::NAV_SIDE, $htmlOptions);
 36         ob_start();
 37         echo \CHtml::openTag('ul', $htmlOptions);
 38         foreach ($items as $item) {
 39 
 40             if (is_string($item)) // treat as divider
 41             echo \CHtml::tag('li', array('class' => Enum::NAV_DIVIDER));
 42             else {
 43                 $itemOptions = ArrayHelper::getValue($item, 'itemOptions', array());
 44                 $linkOptions = ArrayHelper::getValue($item, 'linkOptions', array());
 45                 $label       = ArrayHelper::getValue($item, 'label', 'Item');
 46                 $url         = ArrayHelper::getValue($item, 'url', '#');
 47 
 48                 echo \CHtml::openTag('li', $itemOptions);
 49                 echo \CHtml::link($label, $url, $linkOptions);
 50                 echo \CHtml::closeTag('li');
 51             }
 52         }
 53         echo \CHtml::closeTag('ul');
 54 
 55         return ob_get_clean();
 56     }
 57 
 58     /**
 59      * Generates a foundation sub nav
 60      *
 61      * @param array $items the link items to display as a side nav. The items have the following format:
 62      * <pre>
 63      * array('label'=>'Item', 'url'=>'#', 'linkOptions'=>array(), 'itemOptions'=>array())
 64      * </pre>
 65      * @param array $htmlOptions
 66      * @return string
 67      * @see http://foundation.zurb.com/docs/components/sub-nav.html
 68      */
 69     public static function sub($items, $htmlOptions = array())
 70     {
 71         ArrayHelper::addValue('class', Enum::NAV_SUB, $htmlOptions);
 72         ob_start();
 73         echo \CHtml::openTag('dl', $htmlOptions);
 74         foreach ($items as $item) {
 75             $itemOptions = ArrayHelper::getValue($item, 'itemOptions', array());
 76             $linkOptions = ArrayHelper::getValue($item, 'linkOptions', array());
 77             $label       = ArrayHelper::getValue($item, 'label', 'Item');
 78             $url         = ArrayHelper::getValue($item, 'url', '#');
 79             echo \CHtml::openTag('dt', $itemOptions);
 80             echo \CHtml::link($label, $url, $linkOptions);
 81             echo \CHtml::closeTag('dt');
 82         }
 83         echo \CHtml::closeTag('dl');
 84 
 85         return ob_get_clean();
 86     }
 87 
 88     /**
 89      * Renders menu items. It differs from [[Dropdown]] widget as it is factorial to render multi-level dropdown menus.
 90      * @param array $items the items to render
 91      * @param bool $encodeLabels whether to encode link labels or not
 92      * @return string the resulting dropdown element.
 93      * @throws \foundation\exception\InvalidConfigException
 94      */
 95     public static function dropdown($items, $encodeLabels = true)
 96     {
 97         $li = array();
 98         foreach ($items as $item) {
 99             if (is_string($item)) {
100                 $li[] = $item;
101                 continue;
102             }
103             if (!isset($item['label'])) {
104                 throw new InvalidConfigException("The 'label' option is required.");
105             }
106             $label       = $encodeLabels ? \CHtml::encode($item['label']) : $item['label'];
107             $options     = ArrayHelper::getValue($item, 'options', array());
108             $items       = ArrayHelper::getValue($item, 'items');
109             $url         = \CHtml::normalizeUrl(ArrayHelper::getValue($item, 'url', '#'));
110             $linkOptions = ArrayHelper::getValue($item, 'linkOptions', array());
111 
112             if (ArrayHelper::getValue($item, 'active')) {
113                 ArrayHelper::addValue('class', 'active', $options);
114             }
115 
116             if ($items !== null) {
117                 ArrayHelper::addValue('class', 'has-dropdown', $options);
118                 if (is_array($items)) {
119                     $items = static::dropdown($items, $encodeLabels);
120                 }
121             }
122             $li[] = \CHtml::tag('li', $options, \CHtml::link($label, $url, $linkOptions) . $items);
123         }
124 
125         return \CHtml::tag('ul', array('class' => 'dropdown'), implode("\n", $li));
126     }
127 }
YiiFoundation API documentation generated by ApiGen 2.8.0