/lib/form/select.php

https://github.com/jarednipper/HSU-common-code · PHP · 169 lines · 97 code · 8 blank · 64 comment · 16 complexity · 2909003a233d4f77fa4691997d8b9b52 MD5 · raw file

  1. <?php
  2. require_once('HTML/QuickForm/select.php');
  3. /**
  4. * HTML class for a select type element
  5. *
  6. * @author Jamie Pratt
  7. * @access public
  8. */
  9. class MoodleQuickForm_select extends HTML_QuickForm_select{
  10. /**
  11. * html for help button, if empty then no help
  12. *
  13. * @var string
  14. */
  15. var $_helpbutton='';
  16. var $_hiddenLabel=false;
  17. function MoodleQuickForm_select($elementName=null, $elementLabel=null, $options=null, $attributes=null) {
  18. parent::HTML_QuickForm_select($elementName, $elementLabel, $options, $attributes);
  19. }
  20. function setHiddenLabel($hiddenLabel){
  21. $this->_hiddenLabel = $hiddenLabel;
  22. }
  23. function toHtml(){
  24. if ($this->_hiddenLabel){
  25. $this->_generateId();
  26. return '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
  27. $this->getLabel().'</label>'.parent::toHtml();
  28. } else {
  29. return parent::toHtml();
  30. }
  31. }
  32. /**
  33. * Automatically generates and assigns an 'id' attribute for the element.
  34. *
  35. * Currently used to ensure that labels work on radio buttons and
  36. * checkboxes. Per idea of Alexander Radivanovich.
  37. * Overriden in moodleforms to remove qf_ prefix.
  38. *
  39. * @access private
  40. * @return void
  41. */
  42. function _generateId()
  43. {
  44. static $idx = 1;
  45. if (!$this->getAttribute('id')) {
  46. $this->updateAttributes(array('id' => 'id_'. substr(md5(microtime() . $idx++), 0, 6)));
  47. }
  48. } // end func _generateId
  49. /**
  50. * set html for help button
  51. *
  52. * @access public
  53. * @param array $help array of arguments to make a help button
  54. * @param string $function function name to call to get html
  55. */
  56. function setHelpButton($helpbuttonargs, $function='helpbutton'){
  57. if (!is_array($helpbuttonargs)){
  58. $helpbuttonargs=array($helpbuttonargs);
  59. }else{
  60. $helpbuttonargs=$helpbuttonargs;
  61. }
  62. //we do this to to return html instead of printing it
  63. //without having to specify it in every call to make a button.
  64. if ('helpbutton' == $function){
  65. $defaultargs=array('', '', 'moodle', true, false, '', true);
  66. $helpbuttonargs=$helpbuttonargs + $defaultargs ;
  67. }
  68. $this->_helpbutton=call_user_func_array($function, $helpbuttonargs);
  69. }
  70. /**
  71. * get html for help button
  72. *
  73. * @access public
  74. * @return string html for help button
  75. */
  76. function getHelpButton(){
  77. return $this->_helpbutton;
  78. }
  79. /**
  80. * Removes an OPTION from the SELECT
  81. *
  82. * @param string $value Value for the OPTION to remove
  83. * @since 1.0
  84. * @access public
  85. * @return void
  86. */
  87. function removeOption($value)
  88. {
  89. $key=array_search($value, $this->_values);
  90. if ($key!==FALSE and $key!==null) {
  91. unset($this->_values[$key]);
  92. }
  93. foreach ($this->_options as $key=>$option){
  94. if ($option['attr']['value']==$value){
  95. unset($this->_options[$key]);
  96. // we must reindex the options because the ugly code in quickforms' select.php expects that keys are 0,1,2,3... !?!?
  97. $this->_options = array_merge($this->_options);
  98. return;
  99. }
  100. }
  101. } // end func removeOption
  102. /**
  103. * Removes all OPTIONs from the SELECT
  104. *
  105. * @param string $value Value for the OPTION to remove
  106. * @since 1.0
  107. * @access public
  108. * @return void
  109. */
  110. function removeOptions()
  111. {
  112. $this->_options = array();
  113. } // end func removeOption
  114. /**
  115. * Slightly different container template when frozen. Don't want to use a label tag
  116. * with a for attribute in that case for the element label but instead use a div.
  117. * Templates are defined in renderer constructor.
  118. *
  119. * @return string
  120. */
  121. function getElementTemplateType(){
  122. if ($this->_flagFrozen){
  123. return 'static';
  124. } else {
  125. return 'default';
  126. }
  127. }
  128. /**
  129. * We check the options and return only the values that _could_ have been
  130. * selected. We also return a scalar value if select is not "multiple"
  131. */
  132. function exportValue(&$submitValues, $assoc = false)
  133. {
  134. if (empty($this->_options)) {
  135. return $this->_prepareValue(null, $assoc);
  136. }
  137. $value = $this->_findValue($submitValues);
  138. if (is_null($value)) {
  139. $value = $this->getValue();
  140. }
  141. $value = (array)$value;
  142. $cleaned = array();
  143. foreach ($value as $v) {
  144. foreach ($this->_options as $option) {
  145. if ((string)$option['attr']['value'] === (string)$v) {
  146. $cleaned[] = (string)$option['attr']['value'];
  147. break;
  148. }
  149. }
  150. }
  151. if (empty($cleaned)) {
  152. return $this->_prepareValue(null, $assoc);
  153. }
  154. if ($this->getMultiple()) {
  155. return $this->_prepareValue($cleaned, $assoc);
  156. } else {
  157. return $this->_prepareValue($cleaned[0], $assoc);
  158. }
  159. }
  160. }
  161. ?>