/
QListControl.class.php
359 lines (334 loc) · 11.8 KB
/
QListControl.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
/**
* Abstract object which is extended by anything which involves lists of
* selectable items. (e.g. QListBox, QCheckBoxList, QRadioButtonList and
* QTreeNav)
* It contains a private objItemsArray which contains an array of QListItems.
*
* @property-read integer $ItemCount is the current count of QListItems in
* the control.
*
* @property integer $SelectedIndex is the index # of the control that is
* selected. "-1" means that nothing is selected. If multiple items are
* selected, it will return the lowest index # of all QListItems that are
* currently selected. SETTING SelectedIndex will obviously select that
* specific ListItem, but it will also automatically UNSELECT ALL OTHER
* currently selected QListItems (if applicable).
*
* @property QListItem[] $SelectedIndexes simply returns an array of selected
* QListItems
*
* @property QListItem $SelectedName simply returns
* ListControl::SelectedItem->Name, or null if nothing is selected
*
* @property QListItem[] $SelectedNames simply returns an array of selected
* QListItems
*
* @property QListItem $SelectedValue simply returns
* ListControl::SelectedItem->Value, or null if nothing is selected.
*
* @property QListItem[] $SelectedValues returns an array of selected
* QListItems (if any).
*
* @property-read QListItem $SelectedItem returns the ListItem object,
* itself, that is selected (or the ListItem with the lowest index # of a
* QListItems that are currently selected if multiple items are selected).
* It will return null if nothing is selected.
*
* @property-read QListItem[] $SelectedItems returns an array of selected
* QListItems (if any).
*/
abstract class QListControl extends QControl {
///////////////////////////
// Private Member Variables
///////////////////////////
// MISC
/**
* @var QListItem[] $objItemsArray
*/
protected $objItemsArray = array();
//////////
// Methods
//////////
/**
* Allows you to add a QListItem to the QListControl at the end of the
* private objItemsArray.
*
* @param mixed $mixListItemOrName
* @param string $strValue
* @param boolean $blnSelected
* @param string $strItemGroup
* @param string $strOverrideParameters
*/
public function AddItem($mixListItemOrName, $strValue = null, $blnSelected = null, $strItemGroup = null, $strOverrideParameters = null) {
$this->blnModified = true;
if (gettype($mixListItemOrName) == QType::Object)
$objListItem = QType::Cast($mixListItemOrName, "QListItem");
elseif ($strOverrideParameters)
// The OverrideParameters can only be included if they are not null, because OverrideAttributes in QBaseClass can't except a NULL Value
$objListItem = new QListItem($mixListItemOrName, $strValue, $blnSelected, $strItemGroup, $strOverrideParameters);
else
$objListItem = new QListItem($mixListItemOrName, $strValue, $blnSelected, $strItemGroup);
array_push($this->objItemsArray, $objListItem);
}
// Used if you want to add a LIstItem at a specific location in objItemsArray
public function AddItemAt($intIndex, QListItem $objListItem) {
$this->blnModified = true;
try {
$intIndex = QType::Cast($intIndex, QType::Integer);
} catch (QInvalidCastException $objExc) {
$objExc->IncrementOffset();
throw $objExc;
}
if (($intIndex < 0) ||
($intIndex > count($this->objItemsArray)))
throw new QIndexOutOfRangeException($intIndex, "AddItemAt()");
for ($intCount = count($this->objItemsArray); $intCount > $intIndex; $intCount--) {
$this->objItemsArray[$intCount] = $this->objItemsArray[$intCount - 1];
}
$this->objItemsArray[$intIndex] = $objListItem;
}
// Use this to add an array of items, or an array of key=>value pairs. Convenient for adding a list from a type table.
// When passing key=>val pairs, mixSelectedValues can be an array, or just a single value to compare against to indicate what is selected
public function AddItems (array $mixItemArray, $mixSelectedValues = null, $strItemGroup = null, $strOverrideParameters = null) {
$this->blnModified = true;
try {
$mixItemArray = QType::Cast($mixItemArray, QType::ArrayType);
} catch (QInvalidCastException $objExc) {
$objExc->IncrementOffset();
throw $objExc;
}
foreach ($mixItemArray as $val=>$item) {
$blnSelected = false;
if ($mixSelectedValues) {
if (gettype($mixSelectedValues) == QType::ArrayType) {
$blnSelected = in_array ($val, $mixSelectedValues);
}
else {
$blnSelected = ($val == $mixSelectedValues);
}
}
$this->AddItem ($item, $val, $blnSelected, $strItemGroup, $strOverrideParameters);
};
}
// Gets the ListItem at a specific location in objItemsArray
public function GetItem($intIndex) {
try {
$intIndex = QType::Cast($intIndex, QType::Integer);
} catch (QInvalidCastException $objExc) {
$objExc->IncrementOffset();
throw $objExc;
}
if (($intIndex < 0) ||
($intIndex >= count($this->objItemsArray)))
throw new QIndexOutOfRangeException($intIndex, "GetItem()");
return $this->objItemsArray[$intIndex];
}
/**
* This will return an array of ALL the QQListItems associated with this QListControl.
* Please note that while each individual item can be altered, altering the array, itself,
* will not affect any change on the QListControl. So existing QQListItems may be modified,
* but to add / remove items from the QListControl, you should use AddItem() and RemoveItem().
* @return QListItem[]
*/
public function GetAllItems() {
return $this->objItemsArray;
}
// Removes all the items in objItemsArray
public function RemoveAllItems() {
$this->blnModified = true;
$this->objItemsArray = array();
}
// Removes a specific ListItem at a specific location in objItemsArray
public function RemoveItem($intIndex) {
$this->blnModified = true;
try {
$intIndex = QType::Cast($intIndex, QType::Integer);
} catch (QInvalidCastException $objExc) {
$objExc->IncrementOffset();
throw $objExc;
}
if (($intIndex < 0) ||
($intIndex > (count($this->objItemsArray) - 1)))
throw new QIndexOutOfRangeException($intIndex, "RemoveItem()");
for ($intCount = $intIndex; $intCount < count($this->objItemsArray) - 1; $intCount++) {
$this->objItemsArray[$intCount] = $this->objItemsArray[$intCount + 1];
}
$this->objItemsArray[$intCount] = null;
unset($this->objItemsArray[$intCount]);
}
/////////////////////////
// Public Properties: GET
/////////////////////////
public function __get($strName) {
switch ($strName) {
case "ItemCount":
if ($this->objItemsArray)
return count($this->objItemsArray);
else
return 0;
case "SelectedIndex":
for ($intIndex = 0; $intIndex < count($this->objItemsArray); $intIndex++) {
if ($this->objItemsArray[$intIndex]->Selected)
return $intIndex;
}
return -1;
case "SelectedName":
for ($intIndex = 0; $intIndex < count($this->objItemsArray); $intIndex++) {
if ($this->objItemsArray[$intIndex]->Selected)
return $this->objItemsArray[$intIndex]->Name;
}
return null;
case "SelectedValue":
for ($intIndex = 0; $intIndex < count($this->objItemsArray); $intIndex++) {
if ($this->objItemsArray[$intIndex]->Selected)
return $this->objItemsArray[$intIndex]->Value;
}
return null;
case "SelectedItem":
for ($intIndex = 0; $intIndex < count($this->objItemsArray); $intIndex++) {
if ($this->objItemsArray[$intIndex]->Selected)
return $this->objItemsArray[$intIndex];
}
return null;
case "SelectedItems":
$objToReturn = array();
for ($intIndex = 0; $intIndex < count($this->objItemsArray); $intIndex++) {
if ($this->objItemsArray[$intIndex]->Selected)
array_push($objToReturn, $this->objItemsArray[$intIndex]);
// $objToReturn[count($objToReturn)] = $this->objItemsArray[$intIndex];
}
return $objToReturn;
case "SelectedNames":
$strNamesArray = array();
for ($intIndex = 0; $intIndex < count($this->objItemsArray); $intIndex++) {
if ($this->objItemsArray[$intIndex]->Selected)
array_push($strNamesArray, $this->objItemsArray[$intIndex]->Name);
// $strNamesArray[count($strNamesArray)] = $this->objItemsArray[$intIndex]->Name;
}
return $strNamesArray;
case "SelectedValues":
$objToReturn = array();
for ($intIndex = 0; $intIndex < count($this->objItemsArray); $intIndex++) {
if ($this->objItemsArray[$intIndex]->Selected)
array_push($objToReturn, $this->objItemsArray[$intIndex]->Value);
// $objToReturn[count($objToReturn)] = $this->objItemsArray[$intIndex]->Value;
}
return $objToReturn;
default:
try {
return parent::__get($strName);
} catch (QCallerException $objExc) {
$objExc->IncrementOffset();
throw $objExc;
}
}
}
/////////////////////////
// Public Properties: SET
/////////////////////////
public function __set($strName, $mixValue) {
$this->blnModified = true;
switch ($strName) {
case "SelectedIndex":
try {
$mixValue = QType::Cast($mixValue, QType::Integer);
} catch (QInvalidCastException $objExc) {
$objExc->IncrementOffset();
throw $objExc;
}
// Special Case
if ($mixValue == -1)
$mixValue = null;
if (($mixValue < 0) ||
($mixValue > (count($this->objItemsArray) - 1)))
throw new QIndexOutOfRangeException($mixValue, "SelectedIndex");
for ($intIndex = 0; $intIndex < count($this->objItemsArray); $intIndex++)
if ($mixValue == $intIndex)
$this->objItemsArray[$intIndex]->Selected = true;
else
$this->objItemsArray[$intIndex]->Selected = false;
return $mixValue;
break;
case "SelectedName":
foreach ($this->objItemsArray as $objItem)
if ($objItem->Name == $mixValue)
$objItem->Selected = true;
else
$objItem->Selected = false;
return $mixValue;
break;
case "SelectedValue":
foreach ($this->objItemsArray as $objItem)
if ($objItem->Value == $mixValue)
$objItem->Selected = true;
else
$objItem->Selected = false;
return $mixValue;
break;
case "SelectedNames":
try {
$mixValue = QType::Cast($mixValue, QType::ArrayType);
} catch (QInvalidCastException $objExc) {
$objExc->IncrementOffset();
throw $objExc;
}
foreach ($this->objItemsArray as $objItem) {
$objItem->Selected = false;
foreach ($mixValue as $mixName) {
if ($objItem->Name == $mixName) {
$objItem->Selected = true;
break;
}
}
}
return $mixValue;
break;
case "SelectedValues":
try {
$mixValue = QType::Cast($mixValue, QType::ArrayType);
} catch (QInvalidCastException $objExc) {
$objExc->IncrementOffset();
throw $objExc;
}
foreach ($this->objItemsArray as $objItem) {
$objItem->Selected = false;
foreach ($mixValue as $mixName) {
if ($objItem->Value == $mixName) {
$objItem->Selected = true;
break;
}
}
}
return $mixValue;
break;
case "SelectedIndexes":
try {
$intIndexArray = QType::Cast($mixValue, QType::ArrayType);
} catch (QInvalidCastException $objExc) {
$objExc->IncrementOffset();
throw $objExc;
}
//First remove all indexes
$this->SelectedIndex = -1;
//Assign selected
foreach ($intIndexArray as $intIndex){
if ($this->objItemsArray[$intIndex]){
$this->objItemsArray[$intIndex]->Selected = true;
} else {
throw new QIndexOutOfRangeException($intIndex, "SelectedIndexes");
}
}
break;
default:
try {
parent::__set($strName, $mixValue);
break;
} catch (QCallerException $objExc) {
$objExc->IncrementOffset();
throw $objExc;
}
}
}
}
?>