Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TListBox 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: TListBox.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI.WebControls
11
 */
12
 
13
/**
14
 * Includes TListControl class
15
 */
16
Prado::using('System.Web.UI.WebControls.TListControl');
17
 
18
/**
19
 * TListBox class
20
 *
21
 * TListBox displays a list box on a Web page that allows single or multiple selection.
22
 * The list box allows multiple selections if {@link setSelectionMode SelectionMode}
23
 * is TListSelectionMode::Multiple. It takes single selection only if Single.
24
 * The property {@link setRows Rows} specifies how many rows of options are visible
25
 * at a time. See {@link TListControl} for inherited properties.
26
 *
27
 * Since v3.0.3, TListBox starts to support optgroup. To specify an option group for
28
 * a list item, set a Group attribute with it,
29
 * <code>
30
 *  $listitem->Attributes->Group="Group Name";
31
 *  // or <com:TListItem Attributes.Group="Group Name" .../> in template
32
 * </code>
33
 *
34
 * @author Qiang Xue <qiang.xue@gmail.com>
35
 * @version $Id: TListBox.php 2541 2008-10-21 15:05:13Z qiang.xue $
36
 * @package System.Web.UI.WebControls
37
 * @since 3.0
38
 */
39
class TListBox extends TListControl implements IPostBackDataHandler, IValidatable
40
{
41
	private $_dataChanged=false;
42
	private $_isValid=true;
43
 
44
	/**
45
	 * Adds attribute name-value pairs to renderer.
46
	 * This method overrides the parent implementation with additional list box specific attributes.
47
	 * @param THtmlWriter the writer used for the rendering purpose
48
	 */
49
	protected function addAttributesToRender($writer)
50
	{
51
		$rows=$this->getRows();
52
		$writer->addAttribute('size',"$rows");
53
		if($this->getSelectionMode()===TListSelectionMode::Multiple)
54
			$writer->addAttribute('name',$this->getUniqueID().'[]');
55
		else
56
			$writer->addAttribute('name',$this->getUniqueID());
57
		parent::addAttributesToRender($writer);
58
	}
59
 
60
	/**
61
	 * Gets the name of the javascript class responsible for performing postback for this control.
62
	 * This method overrides the parent implementation.
63
	 * @return string the javascript class name
64
	 */
65
	protected function getClientClassName()
66
	{
67
		return 'Prado.WebUI.TListBox';
68
	}
69
 
70
	/**
71
	 * Registers the list control to load post data on postback.
72
	 * This method overrides the parent implementation.
73
	 * @param mixed event parameter
74
	 */
75
	public function onPreRender($param)
76
	{
77
		parent::onPreRender($param);
78
		if($this->getEnabled(true))
79
			$this->getPage()->registerRequiresPostData($this);
80
	}
81
 
82
	/**
83
	 * Loads user input data.
84
	 * This method is primarly used by framework developers.
85
	 * @param string the key that can be used to retrieve data from the input data collection
86
	 * @param array the input data collection
87
	 * @return boolean whether the data of the component has been changed
88
	 */
89
	public function loadPostData($key,$values)
90
	{
91
		if(!$this->getEnabled(true))
92
			return false;
93
		$this->ensureDataBound();
94
		$selections=isset($values[$key])?$values[$key]:null;
95
		if($selections!==null)
96
		{
97
			$items=$this->getItems();
98
			if($this->getSelectionMode()===TListSelectionMode::Single)
99
			{
100
				$selection=is_array($selections)?$selections[0]:$selections;
101
				$index=$items->findIndexByValue($selection,false);
102
				if($this->getSelectedIndex()!==$index)
103
				{
104
					$this->setSelectedIndex($index);
105
					return $this->_dataChanged=true;
106
				}
107
				else
108
					return false;
109
			}
110
			if(!is_array($selections))
111
				$selections=array($selections);
112
			$list=array();
113
			foreach($selections as $selection)
114
				$list[]=$items->findIndexByValue($selection,false);
115
			$list2=$this->getSelectedIndices();
116
			$n=count($list);
117
			$flag=false;
118
			if($n===count($list2))
119
			{
120
				sort($list,SORT_NUMERIC);
121
				for($i=0;$i<$n;++$i)
122
				{
123
					if($list[$i]!==$list2[$i])
124
					{
125
						$flag=true;
126
						break;
127
					}
128
				}
129
			}
130
			else
131
				$flag=true;
132
			if($flag)
133
			{
134
				$this->setSelectedIndices($list);
135
				$this->_dataChanged=true;
136
			}
137
			return $flag;
138
		}
139
		else if($this->getSelectedIndex()!==-1)
140
		{
141
			$this->clearSelection();
142
			return $this->_dataChanged=true;
143
		}
144
		else
145
			return false;
146
	}
147
 
148
	/**
149
	 * Raises postdata changed event.
150
	 * This method is required by {@link IPostBackDataHandler} interface.
151
	 * It is invoked by the framework when {@link getSelectedIndices SelectedIndices} property
152
	 * is changed on postback.
153
	 * This method is primarly used by framework developers.
154
	 */
155
	public function raisePostDataChangedEvent()
156
	{
157
		if($this->getAutoPostBack() && $this->getCausesValidation())
158
			$this->getPage()->validate($this->getValidationGroup());
159
		$this->onSelectedIndexChanged(null);
160
	}
161
 
162
	/**
163
	 * Returns a value indicating whether postback has caused the control data change.
164
	 * This method is required by the IPostBackDataHandler interface.
165
	 * @return boolean whether postback has caused the control data change. False if the page is not in postback mode.
166
	 */
167
	public function getDataChanged()
168
	{
169
		return $this->_dataChanged;
170
	}
171
 
172
	/**
173
	 * @return boolean whether this control allows multiple selection
174
	 */
175
	protected function getIsMultiSelect()
176
	{
177
		return $this->getSelectionMode()===TListSelectionMode::Multiple;
178
	}
179
 
180
	/**
181
	 * @return integer the number of rows to be displayed in the list control
182
	 */
183
	public function getRows()
184
	{
185
		return $this->getViewState('Rows', 4);
186
	}
187
 
188
	/**
189
	 * @param integer the number of rows to be displayed in the list control
190
	 */
191
	public function setRows($value)
192
	{
193
		$value=TPropertyValue::ensureInteger($value);
194
		if($value<=0)
195
			$value=4;
196
		$this->setViewState('Rows', $value, 4);
197
	}
198
 
199
	/**
200
	 * @return TListSelectionMode the selection mode (Single, Multiple). Defaults to TListSelectionMode::Single.
201
	 */
202
	public function getSelectionMode()
203
	{
204
		return $this->getViewState('SelectionMode', TListSelectionMode::Single);
205
	}
206
 
207
	/**
208
	 * @param TListSelectionMode the selection mode
209
	 */
210
	public function setSelectionMode($value)
211
	{
212
		$this->setViewState('SelectionMode',TPropertyValue::ensureEnum($value,'TListSelectionMode'),TListSelectionMode::Single);
213
	}
214
 
215
	/**
216
	 * Returns the value to be validated.
217
	 * This methid is required by IValidatable interface.
218
	 * @return mixed the value of the property to be validated.
219
	 */
220
	public function getValidationPropertyValue()
221
	{
222
		return $this->getSelectedValue();
223
	}
224
 
225
	/**
226
	 * Returns true if this control validated successfully.
227
	 * Defaults to true.
228
	 * @return bool wether this control validated successfully.
229
	 */
230
	public function getIsValid()
231
	{
232
	    return $this->_isValid;
233
	}
234
	/**
235
	 * @param bool wether this control is valid.
236
	 */
237
	public function setIsValid($value)
238
	{
239
	    $this->_isValid=TPropertyValue::ensureBoolean($value);
240
	}
241
}
242
 
243
 
244
/**
245
 * TListSelectionMode class.
246
 * TListSelectionMode defines the enumerable type for the possible selection modes of a {@link TListBox}.
247
 *
248
 * The following enumerable values are defined:
249
 * - Single: single selection
250
 * - Multiple: allow multiple selection
251
 *
252
 * @author Qiang Xue <qiang.xue@gmail.com>
253
 * @version $Id: TListBox.php 2541 2008-10-21 15:05:13Z qiang.xue $
254
 * @package System.Web.UI.WebControls
255
 * @since 3.0.4
256
 */
257
class TListSelectionMode extends TEnumerable
258
{
259
	const Single='Single';
260
	const Multiple='Multiple';
261
}
262