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.
147 lines
3.7 KiB
147 lines
3.7 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' => 'bar-danger']
|
||
|
* ]);
|
||
12 years ago
|
*
|
||
|
* // striped
|
||
11 years ago
|
* echo Progress::widget([
|
||
12 years ago
|
* 'percent' => 70,
|
||
11 years ago
|
* 'barOptions' => ['class' => 'bar-warning'],
|
||
|
* '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' => 'bar-success'],
|
||
|
* 'options' => ['class' => 'active progress-striped']
|
||
|
* ]);
|
||
12 years ago
|
*
|
||
12 years ago
|
* // stacked bars
|
||
11 years ago
|
* echo Progress::widget([
|
||
|
* 'bars' => [
|
||
|
* ['percent' => 30, 'options' => ['class' => 'bar-danger']],
|
||
|
* ['percent' => 30, 'label' => 'test', 'options' => ['class' => 'bar-success']],
|
||
|
* ['percent' => 35, 'options' => array['class' => 'bar-warning']],
|
||
|
* ]
|
||
|
* ]);
|
||
12 years ago
|
* ```
|
||
|
* @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
|
||
|
*/
|
||
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";
|
||
11 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
|
{
|
||
12 years ago
|
Html::addCssClass($options, 'bar');
|
||
12 years ago
|
$options['style'] = "width:{$percent}%";
|
||
|
return Html::tag('div', $label, $options);
|
||
|
}
|
||
|
}
|