Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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