1: <?php
2: /**
3: * QHListItem.class.php contains the QHListItem class
4: * @package Controls
5: */
6:
7: /**
8: * Represents an item in a hierarchical item list. Uses the QListItemManager trait to manage the interface for adding
9: * sub-items.
10: *
11: * @package Controls
12: * @property string $Anchor If set, the anchor text to print in the href= string when drawing as an anchored item.
13: */
14: class QHListItem extends QListItemBase {
15:
16: /** Allows items to have sub items, and manipulate them with the same interface */
17: use QListItemManager;
18:
19: ///////////////////////////
20: // Private Member Variables
21: ///////////////////////////
22: /** @var string|null if this has an anchor, what to redirect to. Could be javascript or a page. */
23: protected $strAnchor;
24: /** @var string|null a custom tag to draw the item with.*/
25: protected $strTag;
26: /** @var QTagStyler for styling the subtag if needed. */
27: protected $objSubTagStyler;
28:
29:
30: /////////////////////////
31: // Methods
32: /////////////////////////
33: /**
34: * Creates a QListItem
35: *
36: * @param string $strName is the displayed Name or Text of the Item
37: * @param string|null $strValue is any text that represents the value of the ListItem (e.g. maybe a DB Id)
38: * @param string|null $strAnchor is an href anchor that will be associated with item
39: *
40: * @throws Exception|QCallerException
41: */
42: public function __construct($strName, $strValue = null, $strAnchor = null) {
43: parent::__construct ($strName, $strValue);
44: $this->strAnchor = $strAnchor;
45: }
46:
47: /**
48: * Add an item by a QHListItem or a name,value pair
49: * @param string|QHListItem $mixListItemOrName
50: * @param string|null $strValue
51: * @param null|string $strAnchor
52: */
53: public function AddItem($mixListItemOrName, $strValue = null, $strAnchor = null) {
54: if (gettype($mixListItemOrName) == QType::Object) {
55: $objListItem = QType::Cast($mixListItemOrName, "QHListItem");
56: }
57: else {
58: $objListItem = new QHListItem($mixListItemOrName, $strValue, $strAnchor);
59: }
60:
61: $this->AddListItem ($objListItem);
62: }
63:
64: /**
65: * Adds an array of items, or an array of key=>value pairs.
66: * @param array $objItemArray An array of QHListItems or key=>val pairs to be sent to contructor.
67: */
68: public function AddItems($objItemArray) {
69: if (!$objItemArray) return;
70:
71: if (!is_object(reset($objItemArray))) {
72: foreach ($objItemArray as $key=>$val) {
73: $this->AddItem ($key, $val);
74: }
75: } else {
76: $this->AddListItems ($objItemArray);
77: }
78: }
79:
80: /**
81: * Returns a QTagStyler for styling the sub tag.
82: * @return QTagStyler
83: */
84: public function GetSubTagStyler() {
85: if (!$this->objSubTagStyler) {
86: $this->objSubTagStyler = new QTagStyler();
87: }
88: return $this->objSubTagStyler;
89: }
90:
91: /////////////////////////
92: // Public Properties: GET
93: /////////////////////////
94: /**
95: * PHP magic method
96: * @param string $strName
97: *
98: * @return mixed
99: * @throws Exception|QCallerException
100: */
101: public function __get($strName) {
102: switch ($strName) {
103: case "Anchor": return $this->strAnchor;
104: case "Tag": return $this->strTag;
105:
106: default:
107: try {
108: return parent::__get($strName);
109: } catch (QCallerException $objExc) {
110: $objExc->IncrementOffset();
111: throw $objExc;
112: }
113: }
114: }
115:
116: /////////////////////////
117: // Public Properties: SET
118: /////////////////////////
119: /**
120: * PHP magic method
121: * @param string $strName
122: * @param string $mixValue
123: *
124: * @return mixed
125: * @throws Exception|QCallerException|QInvalidCastException
126: */
127: public function __set($strName, $mixValue) {
128: switch ($strName) {
129: case "Anchor":
130: try {
131: $this->strAnchor = QType::Cast($mixValue, QType::String);
132: break;
133: } catch (QInvalidCastException $objExc) {
134: $objExc->IncrementOffset();
135: throw $objExc;
136: }
137: case "Tag":
138: try {
139: $this->strTag = QType::Cast($mixValue, QType::String);
140: break;
141: } catch (QInvalidCastException $objExc) {
142: $objExc->IncrementOffset();
143: throw $objExc;
144: }
145:
146: default:
147: try {
148: parent::__set($strName, $mixValue);
149: } catch (QCallerException $objExc) {
150: $objExc->IncrementOffset();
151: throw $objExc;
152: }
153: break;
154: }
155: }
156: }