Completed
Push — master ( c06f3c...0ec0d0 )
by Fabio
11:03
created

TActiveListControlAdapter   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 28
c 2
b 1
f 0
lcom 1
cbo 3
dl 0
loc 130
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A canUpdateClientSide() 0 4 1
A clearSelection() 0 15 3
A updateListItems() 0 12 4
B setSelectedIndex() 0 18 6
C setSelectedIndices() 0 25 8
A setSelectedValue() 0 9 2
A setSelectedValues() 0 13 4
1
<?php
2
/**
3
 * TActiveListControlAdapter class file.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
6
 * @link https://github.com/pradosoft/prado
7
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
8
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9
 * @package System.Web.UI.ActiveControls
10
 */
11
12
/**
13
 * Load active control adapter.
14
 */
15
Prado::using('System.Web.UI.ActiveControls.TActiveControlAdapter');
16
Prado::using('System.Web.UI.WebControls.TListControl');
17
18
/**
19
 * TActiveListControlAdapter class.
20
 *
21
 * Adapte the list controls to allows the selections on the client-side to be altered
22
 * during callback response.
23
 *
24
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
25
 * @package System.Web.UI.ActiveControls
26
 * @since 3.1
27
 */
28
class TActiveListControlAdapter extends TActiveControlAdapter implements IListControlAdapter
29
{
30
	/**
31
	 * @return boolean true if can update client-side attributes.
32
	 */
33
	protected function canUpdateClientSide()
34
	{
35
		return $this->getControl()->getActiveControl()->canUpdateClientSide();
36
	}
37
38
	/**
39
	 * Selects an item based on zero-base index on the client side.
40
	 * @param integer the index (zero-based) of the item to be selected
41
	 */
42
	public function setSelectedIndex($index)
43
	{
44
		if($this->canUpdateClientSide())
45
		{
46
			$this->updateListItems();
47
			// if a prompt is set, we mimic the postback behaviour of not counting it
48
			// in the index. We assume the prompt is _always_ the first item (Issue #368)
49
			$promptValue=$this->getControl()->getPromptValue();
50
			if($promptValue==='')
51
				$promptValue=$this->getControl()->getPromptText();
52
			if($promptValue!=='')
53
				$index++;
54
55
			if($index >= 0 && $index <= $this->getControl()->getItemCount())
56
			$this->getPage()->getCallbackClient()->select(
57
					$this->getControl(), 'Index', $index);
58
		}
59
	}
60
61
	/**
62
	 * Selects a list of item based on zero-base indices on the client side.
63
	 * @param array list of index of items to be selected
64
	 */
65
	public function setSelectedIndices($indices)
66
	{
67
		if($this->canUpdateClientSide())
68
		{
69
			$this->updateListItems();
70
			$n = $this->getControl()->getItemCount();
71
72
			$promptValue=$this->getControl()->getPromptValue();
73
			if($promptValue==='')
74
				$promptValue=$this->getControl()->getPromptText();
75
76
			$list = array();
77
			foreach($indices as $index)
78
			{
79
				$index = intval($index);
80
				if($promptValue!=='')
81
					$index++;
82
				if($index >= 0 && $index <= $n)
83
					$list[] = $index;
84
			}
85
			if(count($list) > 0)
86
				$this->getPage()->getCallbackClient()->select(
87
					$this->getControl(), 'Indices', $list);
88
		}
89
	}
90
91
	/**
92
	 * Sets selection by item value on the client side.
93
	 * @param string the value of the item to be selected.
94
	 */
95
	public function setSelectedValue($value)
96
	{
97
		if($this->canUpdateClientSide())
98
		{
99
			$this->updateListItems();
100
			$this->getPage()->getCallbackClient()->select(
101
					$this->getControl(), 'Value', $value);
102
		}
103
	}
104
105
	/**
106
	 * Sets selection by a list of item values on the client side.
107
	 * @param array list of the selected item values
108
	 */
109
	public function setSelectedValues($values)
110
	{
111
		if($this->canUpdateClientSide())
112
		{
113
			$this->updateListItems();
114
			$list = array();
115
			foreach($values as $value)
116
				$list[] = $value;
117
			if(count($list) > 0)
118
				$this->getPage()->getCallbackClient()->select(
119
					$this->getControl(), 'Values', $list);
120
		}
121
	}
122
123
    /**
124
     * Clears all existing selections on the client side.
125
     */
126
    public function clearSelection()
127
    {
128
		if($this->canUpdateClientSide())
129
		{
130
			$this->updateListItems();
131
			if($this->getControl() instanceof TActiveDropDownList)
132
			{
133
				// clearing a TActiveDropDownList's selection actually doesn't select the first item;
134
				// we mimic the postback behaviour selecting it (Issue #368)
135
				$this->getPage()->getCallbackClient()->select($this->getControl(), 'Index', 0);
136
			} else {
137
				$this->getPage()->getCallbackClient()->select($this->getControl(), 'Clear');
138
			}
139
		}
140
    }
141
142
	/**
143
	 * Update the client-side list options.
144
	 */
145
	public function updateListItems()
146
	{
147
		if($this->canUpdateClientSide())
148
		{
149
			$items = $this->getControl()->getItems();
150
			if($items instanceof TActiveListItemCollection
151
				&& $items->getListHasChanged())
152
			{
153
				$items->updateClientSide();
154
			}
155
		}
156
	}
157
}
158
159
/**
160
 * TActiveListItemCollection class.
161
 *
162
 * Allows TActiveDropDownList and TActiveListBox to add new options
163
 * during callback response. New options can only be added <b>after</b> the
164
 * {@link TControl::onLoad OnLoad} event.
165
 *
166
 * The {@link getListHasChanged ListHasChanged} property is true when the
167
 * list items has changed. The control responsible for the list needs to
168
 * repopulate the client-side options.
169
 *
170
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
171
 * @package System.Web.UI.ActiveControls
172
 * @since 3.1
173
 */
174
class TActiveListItemCollection extends TListItemCollection
175
{
176
	/**
177
	 * @var IActiveControl control instance.
178
	 */
179
	private $_control;
180
	/**
181
	 * @var boolean true if list items were changed.
182
	 */
183
	private $_hasChanged=false;
184
185
	/**
186
	 * @return boolean true if active controls can update client-side and
187
	 * the onLoad event has already been raised.
188
	 */
189
	protected function canUpdateClientSide()
190
	{
191
		return $this->getControl()->getActiveControl()->canUpdateClientSide()
192
				&& $this->getControl()->getHasLoaded();
193
	}
194
195
	/**
196
	 * @param IActiveControl a active list control.
197
	 */
198
	public function setControl(IActiveControl $control)
199
	{
200
		$this->_control = $control;
201
	}
202
203
	/**
204
	 * @return IActiveControl active control using the collection.
205
	 */
206
	public function getControl()
207
	{
208
		return $this->_control;
209
	}
210
211
	/**
212
	 * @return boolean true if the list has changed after onLoad event.
213
	 */
214
	public function getListHasChanged()
215
	{
216
		return $this->_hasChanged;
217
	}
218
219
	/**
220
	 * Update client-side list items.
221
	 */
222
	public function updateClientSide()
223
	{
224
		$client = $this->getControl()->getPage()->getCallbackClient();
225
		$client->setListItems($this->getControl(), $this);
226
		$this->_hasChanged=false;
227
	}
228
229
	/**
230
	 * Inserts an item into the collection.
231
	 * The new option is added on the client-side during callback.
232
	 * @param integer the location where the item will be inserted.
233
	 * The current item at the place and the following ones will be moved backward.
234
	 * @param TListItem the item to be inserted.
235
	 * @throws TInvalidDataTypeException if the item being inserted is neither a string nor TListItem
236
	 */
237
	public function insertAt($index, $value)
238
	{
239
		parent::insertAt($index, $value);
240
		if($this->canUpdateClientSide())
241
			$this->_hasChanged = true;
242
	}
243
244
	/**
245
	 * Removes an item from at specified index.
246
	 * @param int zero based index.
247
	 */
248
	public function removeAt($index)
249
	{
250
		parent::removeAt($index);
251
		if($this->canUpdateClientSide())
252
			$this->_hasChanged = true;
253
	}
254
}
255
256