Source for file WSelectList.class.php

Documentation is available at WSelectList.class.php

  1. <?php
  2. /**
  3. * @since Jul 21, 2005
  4. * @package polyphony.wizard.components
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: WSelectList.class.php,v 1.19 2007/09/19 14:04:51 adamfranco Exp $
  10. */
  11.  
  12. require_once(POLYPHONY.'/main/library/Wizard/WizardComponent.abstract.php');
  13.  
  14. /**
  15. * This class allows for the creation of select lists.
  16. *
  17. * @since Jul 21, 2005
  18. * @package polyphony.wizard.components
  19. *
  20. * @copyright Copyright &copy; 2005, Middlebury College
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  22. *
  23. * @version $Id: WSelectList.class.php,v 1.19 2007/09/19 14:04:51 adamfranco Exp $
  24. */
  25. class WSelectList
  26. extends WizardComponent
  27. {
  28.  
  29. var $_value;
  30. var $_style = null;
  31. var $_onchange = '';
  32. var $_items = array();
  33. var $_styles = array();
  34. var $_disabled = array();
  35. /**
  36. * Constructor
  37. * @access public
  38. * @return WSelectList
  39. */
  40. function WSelectList () {
  41. // do nothing
  42. }
  43. /**
  44. * sets the CSS style for the labels of the radio buttons.
  45. * @param string $style
  46. * @access public
  47. * @return void
  48. */
  49. function setStyle ($style) {
  50. $this->_style = $style;
  51. }
  52. /**
  53. * Sets the text of the field to display until the user enters the field.
  54. * @param string $text
  55. * @access public
  56. * @return void
  57. */
  58. function setStartingDisplayText ($text) {
  59. if (!$this->isOption('_starting_display')) {
  60. $this->addOption('_starting_display', $text);
  61. }
  62. $this->setValue('_starting_display');
  63. }
  64. /**
  65. * Return True if the select list is on the starting display, rather than
  66. * a real value
  67. *
  68. * @return boolean
  69. * @access public
  70. * @since 8/3/06
  71. */
  72. function isStartingDisplay () {
  73. if ($this->_value == '_starting_display')
  74. return true;
  75. else
  76. return false;
  77. }
  78. /**
  79. * Sets the value of this radio button group.
  80. * @param string $value
  81. * @access public
  82. * @return void
  83. */
  84. function setValue ($value) {
  85. // ArgumentValidator::validate($value, StringValidatorRule::getRule());
  86. if (is_object($value))
  87. $this->_value = $value->asString();
  88. else
  89. $this->_value = $value;
  90. }
  91. /**
  92. * Adds an option to this list.
  93. * @param string $value The short value that represents the displayed text.
  94. * @param string $displayText The text to show to the end user.
  95. * @param string $styles Any styles to pass into the menu option.
  96. * @access public
  97. * @return void
  98. */
  99. function addOption ($value, $displayText, $styles = null) {
  100. $this->_items[$value] = $displayText;
  101. $this->_styles[$value] = $styles;
  102. }
  103. /**
  104. * Add a disabled option to this list.
  105. *
  106. * @param string $value The short value that represents the displayed text.
  107. * @param string $displayText The text to show to the end user.
  108. * @param string $styles Any styles to pass into the menu option.
  109. * @return void
  110. * @access public
  111. * @since 4/3/07
  112. */
  113. function addDisabledOption ($value, $displayText, $styles = null) {
  114. $this->addOption($value, $displayText, $styles);
  115. $this->_disabled[$value] = true;
  116. }
  117. /**
  118. * Answer true if the value passed is a valid option
  119. *
  120. * @param string $value
  121. * @return boolean
  122. * @access public
  123. * @since 4/28/06
  124. */
  125. function isOption ($value) {
  126. $rule = StringValidatorRule::getRule();
  127. if ($rule->check($value))
  128. return array_key_exists($value, $this->_items);
  129. else
  130. return array_key_exists('', $this->_items);
  131. }
  132. /**
  133. * Tells the wizard component to update itself - this may include getting
  134. * form post data or validation - whatever this particular component wants to
  135. * do every pageload.
  136. * @param string $fieldName The field name to use when outputting form data or
  137. * similar parameters/information.
  138. * @access public
  139. * @return boolean - TRUE if everything is OK
  140. */
  141. function update ($fieldName) {
  142. $val = RequestContext::value($fieldName);
  143. if ($val !== false && $val !== null)
  144. $this->_value = $val;
  145. }
  146. /**
  147. * Returns the values of wizard-components. Should return an array if children are involved,
  148. * otherwise a whatever type of object is expected.
  149. * @access public
  150. * @return mixed
  151. */
  152. function getAllValues () {
  153. if ($this->_value == '_starting_display')
  154. return null;
  155. else
  156. return $this->_value;
  157. }
  158. /**
  159. * Add commands to the javascript onchange attribute.
  160. * @param string $commands
  161. * @access public
  162. * @return void
  163. */
  164. function addOnChange($commands) {
  165. $this->_onchange .= " ".$commands;
  166. }
  167. /**
  168. * Add a confirmation question that will be present in a javascript 'confirm'
  169. * dialog onchange press.
  170. *
  171. * @param string $confirmText
  172. * @return void
  173. * @access public
  174. * @since 6/7/06
  175. */
  176. function addConfirm ($confirmText) {
  177. if (!isset($this->_confirms))
  178. $this->_confirms = array();
  179. $this->_confirms[] = $confirmText;
  180. }
  181. /**
  182. * Returns a block of XHTML-valid code that contains markup for this specific
  183. * component.
  184. * @param string $fieldName The field name to use when outputting form data or
  185. * similar parameters/information.
  186. * @access public
  187. * @return string
  188. */
  189. function getMarkup ($fieldName) {
  190. $name = RequestContext::name($fieldName);
  191. $style = '';
  192. if ($this->_style) $style = " style=\"".addslashes($this->_style)."\"";
  193.  
  194. $m = "<select name='$name' $style ";
  195. if (($this->_onchange || (isset($this->_confirms) && count($this->_confirms)))
  196. && $this->isEnabled())
  197. {
  198. $m .= "\n\t\t\t\tonchange=\"";
  199. if (isset($this->_confirms) && count($this->_confirms)) {
  200. $m .= "var confirmed = (confirm('";
  201. $m .= implode("') && confirm('", $this->_confirms);
  202. $m .= "'));";
  203. } else {
  204. $m .= "var confirmed = true; ";
  205. }
  206. $m .= " if (confirmed) { ";
  207. $m .= str_replace("\"", "\\\"", $this->_onchange);
  208. $m .= " } else { ";
  209. $m .= " this.value = '".htmlspecialchars($this->_value, ENT_QUOTES)."';";
  210. $m .= " }\"";
  211. }
  212. if (!$this->isEnabled())
  213. $m .= "\n\t\t\t\tdisabled=\"disabled\"";
  214. $m .= ">\n";
  215. foreach (array_keys($this->_items) as $key) {
  216. $disp = $this->_items[$key];
  217. $selected = $this->_value==$key?" selected='selected'":"";
  218. $val = htmlspecialchars($key, ENT_QUOTES);
  219. if (!is_null($this->_styles[$key]))
  220. $style = " style='".$this->_styles[$key]."'";
  221. else
  222. $style = '';
  223. $m .= "<option value='".$val."'".$selected;
  224. if (isset($this->_disabled[$val]) && $this->_disabled[$val]) {
  225. $m .= " disabled='disabled'";
  226. }
  227. $m .= $style.">".htmlspecialchars($disp)."</option>\n";
  228. }
  229. if ($this->_value && !$this->isOption($this->_value)) {
  230. $m .= "<option value='".htmlspecialchars($this->_value, ENT_QUOTES)."' selected='selected'>"._("(Current value, '").htmlspecialchars($this->_value)." "._("', is not in allowed list.)")."</option>\n";
  231. }
  232. $m .= "</select>\n";
  233. return $m;
  234. }
  235. }
  236.  
  237. ?>

Documentation generated on Wed, 19 Sep 2007 10:28:18 -0400 by phpDocumentor 1.3.0RC3