]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/UI/WebControls/TLabel.php
6ffd025bed053f9a63442db3d6648ca073d428ec
[bacula/bacula] / gui / baculum / framework / Web / UI / WebControls / TLabel.php
1 <?php
2 /**
3  * TLabel class file.
4  *
5  * @author Qiang Xue <qiang.xue@gmail.com>
6  * @link http://www.pradosoft.com/
7  * @copyright Copyright &copy; 2005-2014 PradoSoft
8  * @license http://www.pradosoft.com/license/
9  * @package System.Web.UI.WebControls
10  */
11
12 /**
13  * TLabel class
14  *
15  * TLabel displays a piece of text on a Web page.
16  * Use {@link setText Text} property to set the text to be displayed.
17  * TLabel will render the contents enclosed within its component tag
18  * if {@link setText Text} is empty.
19  * To use TLabel as a form label, associate it with a control by setting the
20  * {@link setForControl ForControl} property.
21  * The associated control must be locatable within the label's naming container.
22  * If the associated control is not visible, the label will not be rendered, either.
23  *
24  * Note, {@link setText Text} will NOT be encoded for rendering.
25  * Make sure it does not contain dangerous characters that you want to avoid.
26  *
27  * @author Qiang Xue <qiang.xue@gmail.com>
28  * @package System.Web.UI.WebControls
29  * @since 3.0
30  */
31 class TLabel extends TWebControl implements IDataRenderer
32 {
33         private $_forControl='';
34
35         /**
36          * @return string tag name of the label, returns 'label' if there is an associated control, 'span' otherwise.
37          */
38         protected function getTagName()
39         {
40                 return ($this->getForControl()==='')?'span':'label';
41         }
42
43         /**
44          * Adds attributes to renderer.
45          * @param THtmlWriter the renderer
46          * @throws TInvalidDataValueException if associated control cannot be found using the ID
47          */
48         protected function addAttributesToRender($writer)
49         {
50                 if($this->_forControl!=='')
51                         $writer->addAttribute('for',$this->_forControl);
52                 parent::addAttributesToRender($writer);
53         }
54
55         /**
56          * Renders the label.
57          * It overrides the parent implementation by checking if an associated
58          * control is visible or not. If not, the label will not be rendered.
59          * @param THtmlWriter writer
60          */
61         public function render($writer)
62         {
63                 if(($aid=$this->getForControl())!=='')
64                 {
65                         if($control=$this->findControl($aid))
66                         {
67                                 if($control->getVisible(true))
68                                 {
69                                         $this->_forControl=$control->getClientID();
70                                         parent::render($writer);
71                                 }
72                         }
73                         else
74                                 throw new TInvalidDataValueException('label_associatedcontrol_invalid',$aid);
75                 }
76                 else
77                         parent::render($writer);
78         }
79
80         /**
81          * Renders the body content of the label.
82          * @param THtmlWriter the renderer
83          */
84         public function renderContents($writer)
85         {
86                 if(($text=$this->getText())==='')
87                         parent::renderContents($writer);
88                 else
89                         $writer->write($text);
90         }
91
92         /**
93          * @return string the text value of the label
94          */
95         public function getText()
96         {
97                 return $this->getViewState('Text','');
98         }
99
100         /**
101          * @param string the text value of the label
102          */
103         public function setText($value)
104         {
105                 $this->setViewState('Text',$value,'');
106         }
107
108         /**
109          * Returns the text value of the label.
110          * This method is required by {@link IDataRenderer}.
111          * It is the same as {@link getText()}.
112          * @return string the text value of the label
113          * @see getText
114          * @since 3.1.0
115          */
116         public function getData()
117         {
118                 return $this->getText();
119         }
120
121         /**
122          * Sets the text value of the label.
123          * This method is required by {@link IDataRenderer}.
124          * It is the same as {@link setText()}.
125          * @param string the text value of the label
126          * @see setText
127          * @since 3.1.0
128          */
129         public function setData($value)
130         {
131                 $this->setText($value);
132         }
133
134         /**
135          * @return string the associated control ID
136          */
137         public function getForControl()
138         {
139                 return $this->getViewState('ForControl','');
140         }
141
142         /**
143          * Sets the ID of the control that the label is associated with.
144          * The control must be locatable via {@link TControl::findControl} using the ID.
145          * @param string the associated control ID
146          */
147         public function setForControl($value)
148         {
149                 $this->setViewState('ForControl',$value,'');
150         }
151 }
152