TbProgress.php 2.99 KB
Newer Older
irving's avatar
irving committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
<?php
/**
 * TbProgress 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
 * @package bootstrap.widgets
 * @since 0.9.10
 */

/**
 * Bootstrap progress bar widget.
 * @see http://twitter.github.com/bootstrap/components.html#progress
 */
class TbProgress extends CWidget
{
	// Progress bar types.
	const TYPE_INFO = 'info';
	const TYPE_SUCCESS = 'success';
	const TYPE_WARNING = 'warning';
	const TYPE_DANGER = 'danger';

	/**
	 * @var string the bar type. Valid values are 'info', 'success', and 'danger'.
	 */
	public $type;
	/**
	 * @var boolean indicates whether the bar is striped.
	 */
	public $striped = false;
	/**
	 * @var boolean indicates whether the bar is animated.
	 */
	public $animated = false;
	/**
	 * @var integer the amount of progress in percent.
	 */
	public $percent = 0;
	/**
	 * @var array the HTML attributes for the widget container.
	 */
	public $htmlOptions = array();

	/**
	 * @var array $stacked set to an array of progress bar values to display stacked progress bars
	 * <pre>
	 *  'stacked'=>array(
	 *      array('type' => 'info|success|warning|danger', 'percent'=>'30', 'htmlOptions'=>array('class'=>'custom')),
	 *      array('type' => 'info|success|warning|danger', 'percent'=>'30'),
	 *  )
	 * </pre>
	 * @since 9/21/12 8:14 PM antonio ramirez <antonio@clevertech.biz>
	 */
	public $stacked;

	/**
	 * Initializes the widget.
	 */
	public function init()
	{
		$classes = array('progress');
		if(empty($this->stacked))
		{
			$validTypes = array(self::TYPE_INFO, self::TYPE_SUCCESS, self::TYPE_WARNING, self::TYPE_DANGER);

			if (isset($this->type) && in_array($this->type, $validTypes))
				$classes[] = 'progress-'.$this->type;

			if ($this->striped)
				$classes[] = 'progress-striped';

			if ($this->animated)
				$classes[] = 'active';

			if ($this->percent < 0)
				$this->percent = 0;
			else if ($this->percent > 100)
				$this->percent = 100;
		}

		if (!empty($classes))
		{
			$classes = implode(' ', $classes);
			if (isset($this->htmlOptions['class']))
				$this->htmlOptions['class'] .= ' '.$classes;
			else
				$this->htmlOptions['class'] = $classes;
		}
	}

	/**
	 * Runs the widget.
	 * @since  9/21/12 8:13 PM  antonio ramirez <antonio@clevertech.biz>
	 * Updated to use stacked progress bars
	 */
	public function run()
	{
		echo CHtml::openTag('div', $this->htmlOptions);
		if(empty($this->stacked))
		{
			echo '<div class="bar" style="width: '.$this->percent.'%;"></div>';
		}
		elseif (is_array($this->stacked))
		{
			foreach($this->stacked as $bar)
			{
				$options = isset($bar['htmlOptions'])? $bar['htmlOptions'] : array();
				$options['style'] = 'width: ' . $bar['percent']. '%';
				$options['class'] = 'bar bar-'.$bar['type'];
				echo '<div '.CHtml::renderAttributes($options).'></div>';
			}
		}
		echo '</div>';
	}
}