1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: class QCheckBoxLegacyColumn extends QDataGridLegacyColumn
13: {
14: protected $objDataGrid;
15: protected $blnHtmlEntities = false;
16: protected $chkSelectAll;
17: protected $colIndex = -1;
18: protected $objCheckboxCallback = null;
19: protected $strCheckboxCallbackFunc = null;
20: protected $strPrimaryKey = 'Id';
21:
22: 23: 24: 25: 26: 27: 28:
29: public function __construct($strName = '', QDataGridLegacy $dataGrid, $objOverrideParameters = null)
30: {
31: $this->objDataGrid = $dataGrid;
32:
33: $arrParentArgs = func_get_args();
34:
35:
36: $arrParentArgs[1] = '<?=$_COLUMN->chkSelected_Render($_ITEM) ?>';
37: if (version_compare(PHP_VERSION, '5.1.6', '>='))
38: return call_user_func_array(array($this, 'QDataGridLegacyColumn::__construct'), $arrParentArgs);
39: else
40: {
41: $parent_class=get_parent_class($this);
42: return call_user_func_array(array($parent_class, '__construct'), $arrParentArgs);
43: }
44: }
45:
46: 47: 48: 49: 50: 51:
52: protected function GetColIndex()
53: {
54:
55: if($this->colIndex == -1)
56: {
57: $columns = $this->objDataGrid->GetAllColumns();
58: foreach($columns as $index=>$col)
59:
60: if($col === $this)
61: $this->colIndex = $index;
62: }
63: return $this->colIndex;
64: }
65:
66:
67: 68: 69: 70: 71: 72: 73: 74:
75: public function SetCheckboxCallback($objParent, $strFuncName)
76: {
77: $this->objCheckboxCallback = $objParent;
78: $this->strCheckboxCallbackFunc = $strFuncName;
79: }
80:
81:
82: public function chkSelectAll_Render($blnWithLabel = false) {
83: $colIndex = $this->GetColIndex();
84:
85: $controlId = 'chkSelectAll' . $colIndex . $this->objDataGrid->ControlId ;
86:
87: $this->chkSelectAll = $this->objDataGrid->GetChildControl($controlId);
88:
89: if(null === $this->chkSelectAll) {
90:
91: $this->chkSelectAll = new QCheckBox($this->objDataGrid, $controlId);
92: $this->chkSelectAll->Name = QApplication::Translate('Select All');
93:
94: $colIndex = $this->GetColIndex();
95: $strControlIdStart = 'chkSelect' . $colIndex.$this->objDataGrid->ControlId.'n';
96: $strControlIdStartLen = strlen($strControlIdStart);
97:
98:
99:
100:
101:
102: $strJavascript = "var datagrid = document.getElementById('{$this->objDataGrid->ControlId}');var selectAll = document.getElementById('{$this->chkSelectAll->ControlId}');var childInputs = datagrid.getElementsByTagName('input');for(var i = 0; i < childInputs.length; i++){var subid = childInputs[i].id.substring($strControlIdStartLen, 0);if(subid == '$strControlIdStart')childInputs[i].checked = selectAll.checked;}";
103:
104: $this->chkSelectAll->AddAction(new QClickEvent(), new QJavaScriptAction($strJavascript));
105: }
106:
107: $strOutput = $this->chkSelectAll->Render(false);
108:
109: if ($blnWithLabel) {
110: $strOutput = QHtml::RenderTag('label', null, $this->strName . ' ' . $strOutput);
111: }
112:
113: return $strOutput;
114: }
115:
116: public function chkSelected_Render($_ITEM) {
117: $intId = $_ITEM->{$this->strPrimaryKey};
118: $colIndex = $this->GetColIndex();
119: $strControlId = 'chkSelect' . $colIndex . $this->objDataGrid->ControlId . 'n' . $intId;
120:
121:
122: $chkSelected = $this->objDataGrid->GetChildControl($strControlId);
123: if (!$chkSelected) {
124: $chkSelected = new QCheckBox($this->objDataGrid, $strControlId);
125:
126: if(null !== $this->objCheckboxCallback && null !== $this->strCheckboxCallbackFunc)
127: {
128: $funcName = $this->strCheckboxCallbackFunc;
129: $this->objCheckboxCallback->$funcName($_ITEM, $chkSelected);
130: }
131:
132:
133: $chkSelected->ActionParameter = $intId.','.($chkSelected->Checked?1:0);
134: }
135: return $chkSelected->Render(false);
136: }
137:
138: 139: 140: 141: 142: 143: 144:
145: public function chkIdToItemId ($strCheckId) {
146: $colIndex = $this->GetColIndex();
147: $strControlIdPrefix = 'chkSelect' . $colIndex . $this->objDataGrid->ControlId . 'n';
148:
149: $len = strlen ($strControlIdPrefix);
150: return substr ($strCheckId, $len);
151: }
152:
153: 154: 155: 156: 157: 158: 159: 160: 161:
162: public function GetSelectedItems($strClass, $blnIndex = true, $objClauses = null)
163: {
164: $itemIds = $this->GetSelectedIds();
165:
166:
167: $idQQNode = QQN::$strClass()->{$this->strPrimaryKey};
168: $conditions = QQ::In($idQQNode, $itemIds);
169: $items = call_user_func(array($strClass, 'QueryArray'), $conditions, $objClauses);
170:
171:
172: if($blnIndex)
173: {
174: $newitems = array();
175: foreach($items as $item)
176: $newitems[$item->{$this->strPrimaryKey}] = $item;
177: return $newitems;
178: }
179:
180: return $items;
181: }
182:
183: 184: 185: 186: 187: 188:
189: public function GetSelectedIds()
190: {
191:
192: $childControls = $this->objDataGrid->GetChildControls();
193:
194: $colIndex = $this->GetColIndex();
195: $strSubId = 'chkSelect' . $colIndex.$this->objDataGrid->ControlId .'n';
196:
197: $itemIds = array();
198: foreach ($childControls as $objControl) {
199:
200: if ($objControl instanceof QCheckBox && substr($objControl->ControlId, 0, strlen($strSubId)) == $strSubId) {
201: if ($objControl->Checked) {
202: $arrParams = explode(',', $objControl->ActionParameter);
203: $id = $arrParams[0];
204: $itemIds[$id] = $id;
205: }
206: }
207: }
208: return $itemIds;
209: }
210:
211:
212: 213: 214: 215: 216: 217: 218:
219: public function GetChangedIds($blnRemember = false)
220: {
221:
222: $childControls = $this->objDataGrid->GetChildControls();
223:
224: $colIndex = $this->GetColIndex();
225: $strSubId = 'chkSelect' . $colIndex.$this->objDataGrid->ControlId .'n';
226:
227: $itemIds = array();
228: foreach ($childControls as $objControl)
229: {
230:
231: if($objControl instanceof QCheckBox && substr($objControl->ControlId, 0, strlen($strSubId)) == $strSubId)
232: {
233: $arrParams = explode(',',$objControl->ActionParameter);
234: $id = $arrParams[0];
235: $wasChecked = $arrParams[1] == 1;
236: if($wasChecked != $objControl->Checked)
237: $itemIds[$id] = $objControl->Checked;
238: if($blnRemember)
239: $objControl->ActionParameter = $id.','.($objControl->Checked?1:0);
240: }
241: }
242:
243: return $itemIds;
244: }
245:
246: 247: 248: 249: 250: 251:
252: public function AcceptChanges()
253: {
254: $this->GetChangedIds(true);
255: }
256:
257: public function SetSelectAllCheckbox($value)
258: {
259: $colIndex = $this->GetColIndex();
260:
261: $controlId = 'chkSelectAll' . $colIndex.$this->objDataGrid->ControlId ;
262: $checkbox = $this->objDataGrid->GetChildControl($controlId);
263: if(null === $checkbox)
264: throw new exception('Select All Checkbox not found');
265: $checkbox->Checked = $value;
266: }
267:
268: public function SetCheckbox($itemId, $value)
269: {
270: $colIndex = $this->GetColIndex();
271: $controlId = 'chkSelect' . $colIndex.$this->objDataGrid->ControlId .'n'.$itemId;
272: $checkbox = $this->objDataGrid->GetChildControl($controlId);
273: if(null === $checkbox)
274: return;
275: $checkbox->Checked = $value;
276: }
277:
278: public function __get($strName) {
279: switch ($strName) {
280: case "PrimaryKey":
281: return $this->strPrimaryKey;
282: default:
283: try {
284: return parent::__get($strName);
285: } catch (QCallerException $objExc) {
286: $objExc->IncrementOffset();
287: throw $objExc;
288: }
289: }
290: }
291:
292: 293: 294: 295: 296: 297: 298: 299: 300: 301:
302: public function __set($strName, $mixValue) {
303: switch ($strName) {
304:
305:
306:
307: case 'PrimaryKey':
308: 309: 310: 311: 312:
313: try {
314: return ($this->strPrimaryKey = QType::Cast($mixValue, QType::String));
315: } catch (QCallerException $objExc) {
316: $objExc->IncrementOffset();
317: throw $objExc;
318: }
319: default:
320: try {
321: return parent::__set($strName, $mixValue);
322: } catch (QCallerException $objExc) {
323: $objExc->IncrementOffset();
324: throw $objExc;
325: }
326: }
327: }
328:
329: }