1: <?php
2: /**
3: * QListItem.class.php contains the QListItem class
4: * @package Controls
5: */
6:
7: /**
8: * Utilized by the {@link QListControl} class which contains a private array of ListItems. Originally these
9: * represented items in a select list, but now represent items in any kind of control that has repetitive items
10: * in it. This includes list controls, menus, drop-downs, and hierarchical lists. This is a general purpose container
11: * for the options in each item. Note that not all the options are used by every control, and we don't do any drawing here.
12: *
13: * @package Controls
14: * @property boolean $Selected is a boolean of whether or not this item is selected or not (do only! use during initialization, otherwise this should be set by the {@link QListControl}!)
15: * @property string $ItemGroup is the group (if any) in which the Item should be displayed
16: * @property string $Label is optional text to display instead of the Name for certain controls.
17: */
18: class QListItem extends QListItemBase implements JsonSerializable {
19:
20: ///////////////////////////
21: // Private Member Variables
22: ///////////////////////////
23: /** @var bool Is the item selected? */
24: protected $blnSelected = false;
25: /** @var null|string Group to which the item belongs, if control supports groups. */
26: protected $strItemGroup = null;
27: /** @var string Label text for the item. */
28: protected $strLabel = null;
29:
30:
31: /////////////////////////
32: // Methods
33: /////////////////////////
34: /**
35: * Creates a QListItem
36: *
37: * @param string $strName is the displayed Name of the Item
38: * @param string $strValue is any text that represents the value of the ListItem (e.g. maybe a DB Id)
39: * @param boolean $blnSelected is a boolean of whether or not this item is selected or not (optional)
40: * @param string $strItemGroup is the group (if any) in which the Item should be displayed
41: * @param array|string $mixOverrideParameters
42: * allows you to override item styles. It is either a string formatted as Property=Value
43: * or an array of the format array(property => value)
44: *
45: * @throws Exception|QCallerException
46: * @return QListItem
47: */
48: public function __construct($strName, $strValue = null, $blnSelected = false, $strItemGroup = null, $mixOverrideParameters = null) {
49: parent::__construct ($strName, $strValue);
50: $this->blnSelected = $blnSelected;
51: $this->strItemGroup = $strItemGroup;
52:
53: // Override parameters get applied here
54: $strOverrideArray = func_get_args();
55: if (count($strOverrideArray) > 4) {
56: throw new QCallerException ("Please provide either a string, or an array, but not multiple parameters");
57: }
58: if ($mixOverrideParameters) {
59: $this->GetStyle()->OverrideAttributes($mixOverrideParameters);
60: }
61: }
62:
63: /**
64: * Returns the details of the control as javascript string. This is customized for the JQuery UI autocomplete. If your
65: * widget requires something else, you will need to subclass and override this.
66: * @return string
67: */
68: public function toJsObject() {
69: $strId = $this->strValue;
70: if (is_null($strId)) {
71: $strId = $this->strId;
72: }
73:
74: $a = array('value' => $this->strName, 'id' => $strId);
75: if ($this->strLabel) {
76: $a['label'] = $this->strLabel;
77: }
78: if ($this->strItemGroup) {
79: $a['category'] = $this->strItemGroup;
80: }
81: return JavaScriptHelper::toJsObject($a);
82: }
83:
84: /**
85: * Returns the details of the control as javascript string. This is customized for the JQuery UI autocomplete. If your
86: * widget requires something else, you will need to subclass and override this.
87: * @return string
88: */
89: public function jsonSerialize() {
90: $strId = $this->strValue;
91: if (!$strId) {
92: $strId = $this->strId;
93: }
94:
95: $a = array('value' => $this->strName, 'id' => $strId);
96: if ($this->strLabel) {
97: $a['label'] = $this->strLabel;
98: }
99: if ($this->strItemGroup) {
100: $a['category'] = $this->strItemGroup;
101: }
102: return $a;
103: }
104:
105:
106:
107:
108: /////////////////////////
109: // Public Properties: GET
110: /////////////////////////
111: /**
112: * PHP magic method
113: * @param string $strName
114: *
115: * @return mixed
116: * @throws Exception|QCallerException
117: */
118: public function __get($strName) {
119: switch ($strName) {
120: case "Selected": return $this->blnSelected;
121: case "ItemGroup": return $this->strItemGroup;
122: case "Label": return $this->strLabel;
123:
124: default:
125: try {
126: return parent::__get($strName);
127: } catch (QCallerException $objExc) {
128: $objExc->IncrementOffset();
129: throw $objExc;
130: }
131: }
132: }
133:
134: /////////////////////////
135: // Public Properties: SET
136: /////////////////////////
137: /**
138: * PHP magic method
139: * @param string $strName
140: * @param string $mixValue
141: *
142: * @return mixed
143: * @throws Exception|QCallerException|QInvalidCastException
144: */
145: public function __set($strName, $mixValue) {
146: switch ($strName) {
147: case "Selected":
148: try {
149: $this->blnSelected = QType::Cast($mixValue, QType::Boolean);
150: break;
151: } catch (QInvalidCastException $objExc) {
152: $objExc->IncrementOffset();
153: throw $objExc;
154: }
155: case "ItemGroup":
156: try {
157: $this->strItemGroup = QType::Cast($mixValue, QType::String);
158: break;
159: } catch (QInvalidCastException $objExc) {
160: $objExc->IncrementOffset();
161: throw $objExc;
162: }
163: case "Label":
164: try {
165: $this->strLabel = QType::Cast($mixValue, QType::String);
166: break;
167: } catch (QInvalidCastException $objExc) {
168: $objExc->IncrementOffset();
169: throw $objExc;
170: }
171: default:
172: try {
173: parent::__set($strName, $mixValue);
174: } catch (QCallerException $objExc) {
175: $objExc->IncrementOffset();
176: throw $objExc;
177: }
178: break;
179: }
180: }
181: }
182: