You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
4.1 KiB
162 lines
4.1 KiB
12 years ago
|
<?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;
|
||
12 years ago
|
use yii\helpers\ArrayHelper;
|
||
12 years ago
|
use yii\helpers\Html;
|
||
|
|
||
|
/**
|
||
|
* Progress renders a bootstrap progress bar component.
|
||
|
*
|
||
|
* For example,
|
||
|
*
|
||
|
* ```php
|
||
|
* // default with label
|
||
11 years ago
|
* echo Progress::widget([
|
||
12 years ago
|
* 'percent' => 60,
|
||
|
* 'label' => 'test',
|
||
11 years ago
|
* ]);
|
||
12 years ago
|
*
|
||
|
* // styled
|
||
11 years ago
|
* echo Progress::widget([
|
||
12 years ago
|
* 'percent' => 65,
|
||
11 years ago
|
* 'barOptions' => ['class' => 'progress-bar-danger']
|
||
11 years ago
|
* ]);
|
||
12 years ago
|
*
|
||
|
* // striped
|
||
11 years ago
|
* echo Progress::widget([
|
||
12 years ago
|
* 'percent' => 70,
|
||
11 years ago
|
* 'barOptions' => ['class' => 'progress-bar-warning'],
|
||
11 years ago
|
* 'options' => ['class' => 'progress-striped']
|
||
|
* ]);
|
||
12 years ago
|
*
|
||
|
* // striped animated
|
||
11 years ago
|
* echo Progress::widget([
|
||
12 years ago
|
* 'percent' => 70,
|
||
11 years ago
|
* 'barOptions' => ['class' => 'progress-bar-success'],
|
||
11 years ago
|
* 'options' => ['class' => 'active progress-striped']
|
||
|
* ]);
|
||
12 years ago
|
*
|
||
12 years ago
|
* // stacked bars
|
||
11 years ago
|
* echo Progress::widget([
|
||
|
* 'bars' => [
|
||
11 years ago
|
* ['percent' => 30, 'options' => ['class' => 'progress-bar-danger']],
|
||
|
* ['percent' => 30, 'label' => 'test', 'options' => ['class' => 'progress-bar-success']],
|
||
|
* ['percent' => 35, 'options' => ['class' => 'progress-bar-warning']],
|
||
11 years ago
|
* ]
|
||
|
* ]);
|
||
12 years ago
|
* ```
|
||
11 years ago
|
* @see http://getbootstrap.com/components/#progress
|
||
12 years ago
|
* @author Antonio Ramirez <amigo.cobos@gmail.com>
|
||
11 years ago
|
* @author Alexander Makarov <sam@rmcreative.ru>
|
||
12 years ago
|
* @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;
|
||
|
/**
|
||
11 years ago
|
* @var array the HTML attributes of the bar
|
||
12 years ago
|
*/
|
||
11 years ago
|
public $barOptions = [];
|
||
12 years ago
|
/**
|
||
12 years ago
|
* @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:
|
||
12 years ago
|
*
|
||
|
* ```php
|
||
11 years ago
|
* [
|
||
12 years ago
|
* // 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
|
||
11 years ago
|
* 'options' => [],
|
||
|
* ]
|
||
12 years ago
|
*/
|
||
12 years ago
|
public $bars;
|
||
12 years ago
|
|
||
|
|
||
|
/**
|
||
|
* Initializes the widget.
|
||
|
* If you override this method, make sure you call the parent implementation first.
|
||
|
*/
|
||
|
public function init()
|
||
|
{
|
||
|
parent::init();
|
||
12 years ago
|
Html::addCssClass($this->options, 'progress');
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Renders the widget.
|
||
|
*/
|
||
|
public function run()
|
||
|
{
|
||
|
echo Html::beginTag('div', $this->options) . "\n";
|
||
|
echo $this->renderProgress() . "\n";
|
||
|
echo Html::endTag('div') . "\n";
|
||
12 years ago
|
BootstrapAsset::register($this->getView());
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Renders the progress.
|
||
12 years ago
|
* @return string the rendering result.
|
||
12 years ago
|
* @throws InvalidConfigException if the "percent" option is not set in a stacked progress bar.
|
||
12 years ago
|
*/
|
||
|
protected function renderProgress()
|
||
|
{
|
||
12 years ago
|
if (empty($this->bars)) {
|
||
|
return $this->renderBar($this->percent, $this->label, $this->barOptions);
|
||
12 years ago
|
}
|
||
11 years ago
|
$bars = [];
|
||
12 years ago
|
foreach ($this->bars as $bar) {
|
||
|
$label = ArrayHelper::getValue($bar, 'label', '');
|
||
|
if (!isset($bar['percent'])) {
|
||
12 years ago
|
throw new InvalidConfigException("The 'percent' option is required.");
|
||
|
}
|
||
11 years ago
|
$options = ArrayHelper::getValue($bar, 'options', []);
|
||
12 years ago
|
$bars[] = $this->renderBar($bar['percent'], $label, $options);
|
||
12 years ago
|
}
|
||
|
return implode("\n", $bars);
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* 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.
|
||
|
*/
|
||
11 years ago
|
protected function renderBar($percent, $label = '', $options = [])
|
||
12 years ago
|
{
|
||
11 years ago
|
$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;
|
||
12 years ago
|
}
|
||
|
}
|