/includes/qcodo/_core/qform/QListBoxBase.class.php

https://github.com/apselico/qcodo · PHP · 277 lines · 201 code · 38 blank · 38 comment · 44 complexity · 54200fa00f25f86aeb52ab3188388bb0 MD5 · raw file

  1. <?php
  2. // This class will render an HTML DropDown or MultiSelect box <SELECT>.
  3. // It extends ListControl, which has methods to handle the ListItem array.
  4. // * "Rows" specifies how many rows you want to have shown.
  5. // * "SelectionMode" specifies if this is a "Single" or "Multiple" select control.
  6. // Typically, for a standard dropdown, you will want "Rows" set to 1 and "SelectionMode" set to
  7. // SelectionMode::Single (which is the default setting).
  8. abstract class QListBoxBase extends QListControl {
  9. ///////////////////////////
  10. // Private Member Variables
  11. ///////////////////////////
  12. // APPEARANCE
  13. private $intRows = 1;
  14. protected $strLabelForRequired;
  15. protected $strLabelForRequiredUnnamed;
  16. protected $objItemStyle = null;
  17. protected $blnHtmlEntities = true;
  18. // BEHAVIOR
  19. protected $strSelectionMode = QSelectionMode::Single;
  20. // SETTINGS
  21. protected $strJavaScripts = '_core/listbox.js';
  22. //////////
  23. // Methods
  24. //////////
  25. public function __construct($objParentObject, $strControlId = null) {
  26. parent::__construct($objParentObject, $strControlId);
  27. $this->strLabelForRequired = QApplication::Translate('%s is required');
  28. $this->strLabelForRequiredUnnamed = QApplication::Translate('Required');
  29. $this->objItemStyle = new QListItemStyle();
  30. }
  31. public function ParsePostData() {
  32. if (array_key_exists($this->strControlId, $_POST)) {
  33. if (is_array($_POST[$this->strControlId])) {
  34. // Multi-Select, so find them all
  35. for ($intIndex = 0; $intIndex < count($this->objItemsArray); $intIndex++) {
  36. if (array_search($intIndex, $_POST[$this->strControlId]) !== false)
  37. $this->objItemsArray[$intIndex]->Selected = true;
  38. else
  39. $this->objItemsArray[$intIndex]->Selected = false;
  40. }
  41. } else {
  42. // Single-select
  43. for ($intIndex = 0; $intIndex < count($this->objItemsArray); $intIndex++) {
  44. if ($_POST[$this->strControlId] == $intIndex)
  45. $this->objItemsArray[$intIndex]->Selected = true;
  46. else
  47. $this->objItemsArray[$intIndex]->Selected = false;
  48. }
  49. }
  50. } else {
  51. // Multiselect forms with nothing passed via $_POST means that everything was DE selected
  52. if ($this->strSelectionMode == QSelectionMode::Multiple) {
  53. for ($intIndex = 0; $intIndex < count($this->objItemsArray); $intIndex++) {
  54. $this->objItemsArray[$intIndex]->Selected = false;
  55. }
  56. }
  57. }
  58. }
  59. public function GetJavaScriptAction() {
  60. return "onchange";
  61. }
  62. public function GetAttributes($blnIncludeCustom = true, $blnIncludeAction = true) {
  63. $strToReturn = parent::GetAttributes($blnIncludeCustom, $blnIncludeAction);
  64. if ($this->intRows)
  65. $strToReturn .= sprintf('size="%s" ', $this->intRows);
  66. if ($this->strSelectionMode == QSelectionMode::Multiple)
  67. $strToReturn .= 'multiple="multiple" ';
  68. return $strToReturn;
  69. }
  70. protected function GetItemHtml($objItem, $intIndex) {
  71. // The Default Item Style
  72. $objStyle = $this->objItemStyle;
  73. // Apply any Style Override (if applicable)
  74. if ($objItem->ItemStyle) {
  75. $objStyle = $objStyle->ApplyOverride($objItem->ItemStyle);
  76. }
  77. $strToReturn = sprintf('<option value="%s" %s%s>%s</option>',
  78. $intIndex,
  79. ($objItem->Selected) ? 'selected="selected"' : "",
  80. $objStyle->GetAttributes(),
  81. ($this->blnHtmlEntities) ? QApplication::HtmlEntities($objItem->Name) : $objItem->Name
  82. );
  83. return $strToReturn;
  84. }
  85. protected function GetControlHtml() {
  86. $strStyle = $this->GetStyleAttributes();
  87. if ($strStyle)
  88. $strStyle = sprintf('style="%s"', $strStyle);
  89. $strToReturn = sprintf('<select name="%s%s" id="%s" %s%s>',
  90. $this->strControlId,
  91. ($this->strSelectionMode == QSelectionMode::Multiple) ? "[]" : "",
  92. $this->strControlId,
  93. $this->GetAttributes(),
  94. $strStyle);
  95. $strCurrentGroup = null;
  96. if (is_array($this->objItemsArray)) {
  97. for ($intIndex = 0; $intIndex < $this->ItemCount; $intIndex++) {
  98. $objItem = $this->objItemsArray[$intIndex];
  99. // Figure Out Groups (if applicable)
  100. if (!is_null($objItem->ItemGroup)) {
  101. // We've got grouping -- are we in a new or same group?
  102. if (is_null($strCurrentGroup))
  103. // New Group
  104. $strToReturn .= '<optgroup label="' . QApplication::HtmlEntities($objItem->ItemGroup) . '">';
  105. else if ($strCurrentGroup != $objItem->ItemGroup)
  106. // Different Group
  107. $strToReturn .= '</optgroup><optgroup label="' . QApplication::HtmlEntities($objItem->ItemGroup) . '">';
  108. $strCurrentGroup = $objItem->ItemGroup;
  109. // We've got no (or no more) grouping
  110. } else {
  111. if (!is_null($strCurrentGroup)) {
  112. // End the current group
  113. $strToReturn .= '</optgroup>';
  114. $strCurrentGroup = null;
  115. }
  116. }
  117. $strToReturn .= $this->GetItemHtml($objItem, $intIndex);
  118. }
  119. if (!is_null($strCurrentGroup))
  120. $strToReturn .= '</optgroup>';
  121. }
  122. $strToReturn .= '</select>';
  123. // If MultiSelect and if NOT required, add a "Reset" button to deselect everything
  124. if (($this->strSelectionMode == QSelectionMode::Multiple) && (!$this->blnRequired) && ($this->blnEnabled) && ($this->blnVisible))
  125. $strToReturn .= $this->GetResetButtonHtml();
  126. return $strToReturn;
  127. }
  128. // For multiple-select based listboxes, you must define the way a "Reset" button should look
  129. abstract protected function GetResetButtonHtml();
  130. public function Validate() {
  131. if ($this->blnRequired) {
  132. if ($this->SelectedIndex == -1) {
  133. if ($this->strName)
  134. $this->strValidationError = sprintf($this->strLabelForRequired, $this->strName);
  135. else
  136. $this->strValidationError = $this->strLabelForRequiredUnnamed;
  137. return false;
  138. }
  139. if (($this->SelectedIndex == 0) && (strlen($this->SelectedValue) == 0)) {
  140. if ($this->strName)
  141. $this->strValidationError = sprintf($this->strLabelForRequired, $this->strName);
  142. else
  143. $this->strValidationError = $this->strLabelForRequiredUnnamed;
  144. return false;
  145. }
  146. }
  147. $this->strValidationError = null;
  148. return true;
  149. }
  150. /////////////////////////
  151. // Public Properties: GET
  152. /////////////////////////
  153. public function __get($strName) {
  154. switch ($strName) {
  155. // APPEARANCE
  156. case "Rows": return $this->intRows;
  157. case "LabelForRequired": return $this->strLabelForRequired;
  158. case "LabelForRequiredUnnamed": return $this->strLabelForRequiredUnnamed;
  159. case "ItemStyle": return $this->objItemStyle;
  160. case "HtmlEntities": return $this->blnHtmlEntities;
  161. // BEHAVIOR
  162. case "SelectionMode": return $this->strSelectionMode;
  163. default:
  164. try {
  165. return parent::__get($strName);
  166. } catch (QCallerException $objExc) {
  167. $objExc->IncrementOffset();
  168. throw $objExc;
  169. }
  170. }
  171. }
  172. /////////////////////////
  173. // Public Properties: SET
  174. /////////////////////////
  175. public function __set($strName, $mixValue) {
  176. $this->blnModified = true;
  177. switch ($strName) {
  178. // APPEARANCE
  179. case "Rows":
  180. try {
  181. $this->intRows = QType::Cast($mixValue, QType::Integer);
  182. break;
  183. } catch (QInvalidCastException $objExc) {
  184. $objExc->IncrementOffset();
  185. throw $objExc;
  186. }
  187. case "LabelForRequired":
  188. try {
  189. $this->strLabelForRequired = QType::Cast($mixValue, QType::String);
  190. break;
  191. } catch (QInvalidCastException $objExc) {
  192. $objExc->IncrementOffset();
  193. throw $objExc;
  194. }
  195. case "LabelForRequiredUnnamed":
  196. try {
  197. $this->strLabelForRequiredUnnamed = QType::Cast($mixValue, QType::String);
  198. break;
  199. } catch (QInvalidCastException $objExc) {
  200. $objExc->IncrementOffset();
  201. throw $objExc;
  202. }
  203. case "HtmlEntities":
  204. try {
  205. $this->blnHtmlEntities = QType::Cast($mixValue, QType::Boolean);
  206. break;
  207. } catch (QInvalidCastException $objExc) {
  208. $objExc->IncrementOffset();
  209. throw $objExc;
  210. }
  211. // BEHAVIOR
  212. case "SelectionMode":
  213. try {
  214. $this->strSelectionMode = QType::Cast($mixValue, QType::String);
  215. break;
  216. } catch (QInvalidCastException $objExc) {
  217. $objExc->IncrementOffset();
  218. throw $objExc;
  219. }
  220. case "ItemStyle":
  221. try {
  222. $this->objItemStyle = QType::Cast($mixValue, "QListItemStyle");
  223. } catch (QInvalidCastException $objExc) {
  224. $objExc->IncrementOffset();
  225. throw $objExc;
  226. }
  227. break;
  228. default:
  229. try {
  230. parent::__set($strName, $mixValue);
  231. } catch (QCallerException $objExc) {
  232. $objExc->IncrementOffset();
  233. throw $objExc;
  234. }
  235. break;
  236. }
  237. }
  238. }
  239. ?>