Progress.php 4.12 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
use yii\helpers\Html;

/**
 * Progress renders a bootstrap progress bar component.
 *
 * For example,
 *
 * ```php
 * // default with label
Alexander Makarov committed
21
 * echo Progress::widget([
Antonio Ramirez committed
22 23
 *     'percent' => 60,
 *     'label' => 'test',
Alexander Makarov committed
24
 * ]);
Antonio Ramirez committed
25 26
 *
 * // styled
Alexander Makarov committed
27
 * echo Progress::widget([
Antonio Ramirez committed
28
 *     'percent' => 65,
29
 *     'barOptions' => ['class' => 'progress-bar-danger']
Alexander Makarov committed
30
 * ]);
Antonio Ramirez committed
31 32
 *
 * // striped
Alexander Makarov committed
33
 * echo Progress::widget([
Antonio Ramirez committed
34
 *     'percent' => 70,
35
 *     'barOptions' => ['class' => 'progress-bar-warning'],
Alexander Makarov committed
36 37
 *     'options' => ['class' => 'progress-striped']
 * ]);
Antonio Ramirez committed
38 39
 *
 * // striped animated
Alexander Makarov committed
40
 * echo Progress::widget([
Antonio Ramirez committed
41
 *     'percent' => 70,
42
 *     'barOptions' => ['class' => 'progress-bar-success'],
Alexander Makarov committed
43 44
 *     'options' => ['class' => 'active progress-striped']
 * ]);
Antonio Ramirez committed
45
 *
Qiang Xue committed
46
 * // stacked bars
Alexander Makarov committed
47 48
 * echo Progress::widget([
 *     'bars' => [
49 50 51
 *         ['percent' => 30, 'options' => ['class' => 'progress-bar-danger']],
 *         ['percent' => 30, 'label' => 'test', 'options' => ['class' => 'progress-bar-success']],
 *         ['percent' => 35, 'options' => ['class' => 'progress-bar-warning']],
Alexander Makarov committed
52 53
 *     ]
 * ]);
Antonio Ramirez committed
54
 * ```
55
 * @see http://getbootstrap.com/components/#progress
Antonio Ramirez committed
56
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
57
 * @author Alexander Makarov <sam@rmcreative.ru>
Antonio Ramirez committed
58 59 60 61 62 63 64 65 66 67 68 69 70
 * @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;
	/**
71
	 * @var array the HTML attributes of the bar
Antonio Ramirez committed
72
	 */
Alexander Makarov committed
73
	public $barOptions = [];
Antonio Ramirez committed
74
	/**
Qiang Xue committed
75 76
	 * @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
77 78
	 *
	 * ```php
Alexander Makarov committed
79
	 * [
Qiang Xue committed
80 81 82 83 84
	 *     // 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
Alexander Makarov committed
85 86
	 *     'options' => [],
	 * ]
Antonio Ramirez committed
87
	 */
Qiang Xue committed
88
	public $bars;
Antonio Ramirez committed
89 90 91 92 93 94 95 96 97


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

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

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

Qiang Xue committed
134 135 136 137 138 139 140
	/**
	 * 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.
	 */
Alexander Makarov committed
141
	protected function renderBar($percent, $label = '', $options = [])
Qiang Xue committed
142
	{
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
		$defaultOptions = [
			'role' => 'progressbar',
			'aria-valuenow' => $percent,
			'aria-valuemin' => 0,
			'aria-valuemax' => 100,
			'style' => "width:{$percent}%",
		];
		$options = array_merge($defaultOptions, $options);
		Html::addCssClass($options, 'progress-bar');

		$out = Html::beginTag('div', $options);
		$out .= $label;
		$out .= Html::tag('span', \Yii::t('yii', '{percent}% Complete', ['percent' => $percent]), [
			'class' => 'sr-only'
		]);
		$out .= Html::endTag('div');
		return $out;
Qiang Xue committed
160 161
	}
}