Source

yii-bootstrap2 / widgets / BootNav.php

Full commit
<?php
/**
 * BootNav class file.
 * @author Christoffer Niska <ChristofferNiska@gmail.com>
 * @copyright  Copyright &copy; Christoffer Niska 2011-
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 */

 Yii::import('bootstrap.widgets.BootWidget');

/**
 * Bootstrap topbar navigation widget with support for dropdown menus.
 * @since 0.9.7
 */
class BootNav extends BootWidget
{
	/**
	 * @var string the topbar type. Defaults to 'normal'. Valid values are 'normal' and 'fluid'.
	 */
	public $type = 'normal';
	/**
	 * @var string the text for the brand.
	 */
	public $brand;
	/**
	 * @var string the URL for the brand link.
	 */
	public $brandUrl;
	/**
	 * @var array the HTML attributes for the brand link.
	 */
	public $brandOptions = array();
	/**
	 * @var string the item template.
	 */
	public $itemTemplate = '{menu}';
	/**
	 * @var array the primary menu items.
	 */
	public $primaryItems = array();
	/**
	 * @var array the secondary menu items.
	 */
	public $secondaryItems = array(); 
	/**
	 * @var array the HTML attributes for the primary menu.
	 */
	public $primaryOptions = array();
	/**
	 * @var array the HTML attributes for the secondary menu.
	 */
	public $secondaryOptions = array();

	/**
	 * Initializes the widget.
	 */
	public function init()
	{
		if (!in_array($this->type, array('normal', 'fluid')))
			throw new CException(__CLASS__.'.type is invalid. Valid values are "normal" and "fluid".');

		if (!isset($this->brand))
			$this->brand = CHtml::encode(Yii::app()->name);

		if (!isset($this->brandUrl))
			$this->brandUrl = Yii::app()->homeUrl;
	}

	/**
	 * Runs the widget.
	 */
	public function run()
	{
		if (isset($this->htmlOptions['class']))
			$this->htmlOptions['class'] .= ' topbar';
		else
			$this->htmlOptions['class'] = 'topbar';

		if (isset($this->brandOptions['class']))
			$this->brandOptions['class'] .= ' brand';
		else
			$this->brandOptions['class'] = 'brand';

		if (isset($this->brandUrl))
			$this->brandOptions['href'] = $this->brandUrl;

		if (isset($this->primaryOptions['class']))
			$this->primaryOptions['class'] .= ' nav';
		else
			$this->primaryOptions['class'] = 'nav';

		if (isset($this->secondaryOptions['class']))
			$this->secondaryOptions['class'] .= ' nav secondary-nav';
		else
			$this->secondaryOptions['class'] = 'nav secondary-nav';

		$cssClass = $this->type === 'normal' ? 'container' : 'container-fluid';

		echo CHtml::openTag('div', $this->htmlOptions);
		echo '<div class="topbar-inner"><div class="'.$cssClass.'">';
		echo CHtml::openTag('a', $this->brandOptions).$this->brand.'</a>';

		if (!empty($this->primaryItems))
		{
			$this->controller->widget('bootstrap.widgets.BootMenu', array(
				'type'=>'',
				'items'=>$this->primaryItems,
				'itemTemplate'=>$this->itemTemplate,
				'encodeLabel'=>false,
				'htmlOptions'=>$this->primaryOptions,
			));
		}

		if (!empty($this->secondaryItems))
		{
			$this->controller->widget('bootstrap.widgets.BootMenu', array(
				'type'=>'',
				'items'=>$this->secondaryItems,
				'itemTemplate'=>$this->itemTemplate,
				'encodeLabel'=>false,
				'htmlOptions'=>$this->secondaryOptions,
			));
		}

		echo '</div></div></div>';
	}
}