1 <?php
2 /**
3 * TbBadge 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 */
9
10 /**
11 * Bootstrap badge widget.
12 * @see http://twitter.github.com/bootstrap/components.html#badges
13 */
14 class TbBadge extends CWidget
15 {
16 // Badge types.
17 const TYPE_SUCCESS = 'success';
18 const TYPE_WARNING = 'warning';
19 const TYPE_IMPORTANT = 'important';
20 const TYPE_INFO = 'info';
21 const TYPE_INVERSE = 'inverse';
22
23 /**
24 * @var string the badge type.
25 * Valid types are 'success', 'warning', 'important', 'info' and 'inverse'.
26 */
27 public $type;
28 /**
29 * @var string the badge text.
30 */
31 public $label;
32 /**
33 * @var boolean whether to encode the label.
34 */
35 public $encodeLabel = true;
36 /**
37 * @var array the HTML attributes for the widget container.
38 */
39 public $htmlOptions = array();
40
41 /**
42 * Initializes the widget.
43 */
44 public function init()
45 {
46 $classes = array('badge');
47
48 $validTypes = array(self::TYPE_SUCCESS, self::TYPE_WARNING, self::TYPE_IMPORTANT, self::TYPE_INFO, self::TYPE_INVERSE);
49
50 if (isset($this->type) && in_array($this->type, $validTypes))
51 $classes[] = 'badge-'.$this->type;
52
53 if (!empty($classes))
54 {
55 $classes = implode(' ', $classes);
56 if (isset($this->htmlOptions['class']))
57 $this->htmlOptions['class'] .= ' '.$classes;
58 else
59 $this->htmlOptions['class'] = $classes;
60 }
61
62 if ($this->encodeLabel === true)
63 $this->label = CHtml::encode($this->label);
64 }
65
66 /**
67 * Runs the widget.
68 */
69 public function run()
70 {
71 echo CHtml::tag('span', $this->htmlOptions, $this->label);
72 }
73 }
74