From 0a9359ce183f9c98173461d01a5720653d392dce Mon Sep 17 00:00:00 2001 From: Shannon Date: Fri, 29 Apr 2011 11:54:23 -0700 Subject: [PATCH] Adding a convenience method to QListControl to add an array of item, or key value pairs like those create by Type tables --- .../qcodo/_core/qform/QListControl.class.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/includes/qcodo/_core/qform/QListControl.class.php b/includes/qcodo/_core/qform/QListControl.class.php index df09720b..6dec67e6 100644 --- a/includes/qcodo/_core/qform/QListControl.class.php +++ b/includes/qcodo/_core/qform/QListControl.class.php @@ -94,6 +94,31 @@ public function AddItemAt($intIndex, QListItem $objListItem) { $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 {