Progress.php 3.77 KB
Newer Older
Antonio Ramirez committed
1 2 3 4 5 6 7 8 9 10
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\bootstrap;

use yii\base\InvalidConfigException;
Qiang Xue committed
11
use yii\helpers\ArrayHelper;
Antonio Ramirez committed
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
use yii\helpers\Html;

/**
 * Progress renders a bootstrap progress bar component.
 *
 * For example,
 *
 * ```php
 * // default with label
 * echo Progress::widget(array(
 *     'percent' => 60,
 *     'label' => 'test',
 * ));
 *
 * // styled
 * echo Progress::widget(array(
 *     'percent' => 65,
 *     'barOptions' => array('class' => 'bar-danger')
 * ));
 *
 * // striped
 * echo Progress::widget(array(
 *     'percent' => 70,
 *     'barOptions' => array('class' => 'bar-warning'),
 *     'options' => array('class' => 'progress-striped')
 * ));
 *
 * // striped animated
 * echo Progress::widget(array(
 *     'percent' => 70,
 *     'barOptions' => array('class' => 'bar-success'),
 *     'options' => array('class' => 'active progress-striped')
 * ));
 *
Qiang Xue committed
46
 * // stacked bars
Antonio Ramirez committed
47
 * echo Progress::widget(array(
Qiang Xue committed
48
 *     'bars' => array(
Antonio Ramirez committed
49
 *         array('percent' => 30, 'options' => array('class' => 'bar-danger')),
50
 *         array('percent' => 30, 'label' => 'test', 'options' => array('class' => 'bar-success')),
Antonio Ramirez committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
 *         array('percent' => 35, 'options' => array('class' => 'bar-warning'))
 *     )
 * ));
 * ```
 * @see http://twitter.github.io/bootstrap/components.html#progress
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
 * @since 2.0
 */
class Progress extends Widget
{
	/**
	 * @var string the button label
	 */
	public $label;
	/**
	 * @var integer the amount of progress as a percentage.
	 */
	public $percent = 0;
	/**
	 * @var array the HTML attributes of the
	 */
	public $barOptions = array();
	/**
Qiang Xue committed
74 75
	 * @var array a set of bars that are stacked together to form a single progress bar.
	 * Each bar is an array of the following structure:
Antonio Ramirez committed
76 77
	 *
	 * ```php
Qiang Xue committed
78 79 80 81 82 83 84 85
	 * array(
	 *     // required, the amount of progress as a percentage.
	 *     'percent' => 30,
	 *     // optional, the label to be displayed on the bar
	 *     'label' => '30%',
	 *     // optional, array, additional HTML attributes for the bar tag
	 *     'options' => array(),
	 * )
Antonio Ramirez committed
86
	 */
Qiang Xue committed
87
	public $bars;
Antonio Ramirez committed
88 89 90 91 92 93 94 95 96


	/**
	 * Initializes the widget.
	 * If you override this method, make sure you call the parent implementation first.
	 */
	public function init()
	{
		parent::init();
97
		Html::addCssClass($this->options, 'progress');
Antonio Ramirez committed
98 99 100 101 102 103 104 105 106 107
	}

	/**
	 * Renders the widget.
	 */
	public function run()
	{
		echo Html::beginTag('div', $this->options) . "\n";
		echo $this->renderProgress() . "\n";
		echo Html::endTag('div') . "\n";
108
		BootstrapAsset::register($this->getView());
Antonio Ramirez committed
109 110 111
	}

	/**
Qiang Xue committed
112
	 * Renders the progress.
Antonio Ramirez committed
113
	 * @return string the rendering result.
Qiang Xue committed
114
	 * @throws InvalidConfigException if the "percent" option is not set in a stacked progress bar.
Antonio Ramirez committed
115 116 117
	 */
	protected function renderProgress()
	{
Qiang Xue committed
118 119
		if (empty($this->bars)) {
			return $this->renderBar($this->percent, $this->label, $this->barOptions);
Antonio Ramirez committed
120 121
		}
		$bars = array();
Qiang Xue committed
122 123 124
		foreach ($this->bars as $bar) {
			$label = ArrayHelper::getValue($bar, 'label', '');
			if (!isset($bar['percent'])) {
Antonio Ramirez committed
125 126
				throw new InvalidConfigException("The 'percent' option is required.");
			}
Qiang Xue committed
127 128
			$options = ArrayHelper::getValue($bar, 'options', array());
			$bars[] = $this->renderBar($bar['percent'], $label, $options);
Antonio Ramirez committed
129 130 131 132
		}
		return implode("\n", $bars);
	}

Qiang Xue committed
133 134 135 136 137 138 139 140 141
	/**
	 * Generates a bar
	 * @param int $percent the percentage of the bar
	 * @param string $label, optional, the label to display at the bar
	 * @param array $options the HTML attributes of the bar
	 * @return string the rendering result.
	 */
	protected function renderBar($percent, $label = '', $options = array())
	{
142
		Html::addCssClass($options, 'bar');
Qiang Xue committed
143 144 145 146
		$options['style'] = "width:{$percent}%";
		return Html::tag('div', $label, $options);
	}
}