1 <?php
2 /**
3 * TbProgress class file.
4 * @author Christoffer Niska <ChristofferNiska@gmail.com>
5 * @copyright Copyright © Christoffer Niska 2011-
6 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
7 * @package bootstrap.widgets
8 * @since 0.9.10
9 */
10
11 /**
12 * Bootstrap progress bar widget.
13 * @see http://twitter.github.com/bootstrap/components.html#progress
14 */
15 class TbProgress extends CWidget
16 {
17 // Progress bar types.
18 const TYPE_INFO = 'info';
19 const TYPE_SUCCESS = 'success';
20 const TYPE_WARNING = 'warning';
21 const TYPE_DANGER = 'danger';
22
23 /**
24 * @var string the bar type. Valid values are 'info', 'success', and 'danger'.
25 */
26 public $type;
27 /**
28 * @var boolean indicates whether the bar is striped.
29 */
30 public $striped = false;
31 /**
32 * @var boolean indicates whether the bar is animated.
33 */
34 public $animated = false;
35 /**
36 * @var integer the amount of progress in percent.
37 */
38 public $percent = 0;
39 /**
40 * @var array the HTML attributes for the widget container.
41 */
42 public $htmlOptions = array();
43
44 /**
45 * Initializes the widget.
46 */
47 public function init()
48 {
49 $classes = array('progress');
50
51 $validTypes = array(self::TYPE_INFO, self::TYPE_SUCCESS, self::TYPE_WARNING, self::TYPE_DANGER);
52
53 if (isset($this->type) && in_array($this->type, $validTypes))
54 $classes[] = 'progress-'.$this->type;
55
56 if ($this->striped)
57 $classes[] = 'progress-striped';
58
59 if ($this->animated)
60 $classes[] = 'active';
61
62 if (!empty($classes))
63 {
64 $classes = implode(' ', $classes);
65 if (isset($this->htmlOptions['class']))
66 $this->htmlOptions['class'] .= ' '.$classes;
67 else
68 $this->htmlOptions['class'] = $classes;
69 }
70
71 if ($this->percent < 0)
72 $this->percent = 0;
73 else if ($this->percent > 100)
74 $this->percent = 100;
75 }
76
77 /**
78 * Runs the widget.
79 */
80 public function run()
81 {
82 echo CHtml::openTag('div', $this->htmlOptions);
83 echo '<div class="bar" style="width: '.$this->percent.'%;"></div>';
84 echo '</div>';
85 }
86 }
87