Source for file WRadioList.class.php

Documentation is available at WRadioList.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: WRadioList.class.php,v 1.12 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 lists of input type='radio' items of a specific group.
  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: WRadioList.class.php,v 1.12 2007/09/19 14:04:51 adamfranco Exp $
  24. */
  25. class WRadioList
  26. extends WizardComponent
  27. {
  28.  
  29. var $_eachPre;
  30. var $_eachPost;
  31. var $_pre;
  32. var $_post;
  33. var $_value;
  34. var $_onchange;
  35. var $_style = null;
  36. var $_items = array();
  37. var $_extendedHtml = array();
  38. /**
  39. * Virtual constructor - creates this object with the specified layout.
  40. * @param string $pre A string to prepend onto the markup block (ex, "<ul>")
  41. * @param string $eachPre A string to put at the beginning of each of the
  42. * elements (ex, "<li>")
  43. * @param string $eachPost A string to put at the end of each of the
  44. * elements (ex, "</li>")
  45. * @param string $post A string to tack onto the end of the block (ex, "</ul>")
  46. * @access public
  47. * @return ref object
  48. * @static
  49. */
  50. function withLayout ($pre, $eachPre, $eachPost, $post, $class='WRadioList') {
  51. $obj = new $class();
  52. $obj->_pre = $pre;
  53. $obj->_post = $post;
  54. $obj->_eachPre = $eachPre;
  55. $obj->_eachPost = $eachPost;
  56. return $obj;
  57. }
  58. /**
  59. * Constructor
  60. * @access public
  61. * @return WRadioList
  62. */
  63. function WRadioList () {
  64. $this->_pre = $this->_post = '';
  65. $this->_eachPost = "\n<br/>";
  66. }
  67. /**
  68. * sets the CSS style for the labels of the radio buttons.
  69. * @param string $style
  70. * @access public
  71. * @return void
  72. */
  73. function setStyle ($style) {
  74. $this->_style = $style;
  75. }
  76. /**
  77. * Sets the value of this radio button group.
  78. * @param string $value
  79. * @access public
  80. * @return void
  81. */
  82. function setValue ($value) {
  83. $this->_value = $value;
  84. }
  85. /**
  86. * Add commands to the javascript onchange attribute.
  87. * @param string $commands
  88. * @access public
  89. * @return void
  90. */
  91. function addOnChange($commands) {
  92. $this->_onchange .= " ".$commands;
  93. }
  94. /**
  95. * Adds a radio option to this list.
  96. * @param string $value The short value that represents the displayed text.
  97. * @param optional string $displayText The text to show to the end user. Defaults to $value.
  98. * @param optional string $extendedHtml Text to add to the item after the display text.
  99. * @access public
  100. * @return void
  101. */
  102. function addOption ($value, $displayText = null, $extendedHtml = null) {
  103. if ($displayText == null) $displayText = $value;
  104. $this->_items[$value] = $displayText;
  105. $this->_extendedHtml[$value] = $extendedHtml;
  106. }
  107. /**
  108. * Tells the wizard component to update itself - this may include getting
  109. * form post data or validation - whatever this particular component wants to
  110. * do every pageload.
  111. * @param string $fieldName The field name to use when outputting form data or
  112. * similar parameters/information.
  113. * @access public
  114. * @return boolean - TRUE if everything is OK
  115. */
  116. function update ($fieldName) {
  117. $val = RequestContext::value($fieldName);
  118. if ($val) $this->_value = $val;
  119. }
  120. /**
  121. * Returns the values of wizard-components. Should return an array if children are involved,
  122. * otherwise a whatever type of object is expected.
  123. * @access public
  124. * @return mixed
  125. */
  126. function getAllValues () {
  127. return $this->_value;
  128. }
  129. /**
  130. * Returns a block of XHTML-valid code that contains markup for this specific
  131. * component.
  132. * @param string $fieldName The field name to use when outputting form data or
  133. * similar parameters/information.
  134. * @access public
  135. * @return string
  136. */
  137. function getMarkup ($fieldName) {
  138. $name = RequestContext::name($fieldName);
  139. $m = $this->_pre;
  140. $style = '';
  141. if ($this->_style) $style = " style=\"".addslashes($this->_style)."\"";
  142. $s = array();
  143. $ids = array();
  144. foreach (array_keys($this->_items) as $key) {
  145. $ids[] = $name . "_" . addslashes($key);
  146. }
  147. foreach (array_keys($this->_items) as $key) {
  148. $disp = $this->_items[$key];
  149. $extendedHtml = $this->_extendedHtml[$key];
  150. $checked = $this->_value==$key?" checked='checked'":"";
  151. $val = htmlspecialchars($key, ENT_QUOTES);
  152. $javascript = '';
  153. $id = $name . "_" . addslashes($key);
  154. $others = array_diff(array($id), $ids);
  155. $javascript .= "document.getElementById('$id').checked = true; ";
  156. foreach ($others as $otherId) {
  157. $javascript .= "document.getElementById('$otherId').checked = false; ";
  158. }
  159. if ($this->_onchange)
  160. $javascript .= " ".str_replace("\"", "\\\"", $this->_onchange)."; ";
  161. $s[] = "<label onmousedown=\"$javascript\" style='cursor: pointer;'$style><input type='radio' name='$name' id='$id' value='$val'$checked /> $disp</label>".$extendedHtml;
  162. }
  163. $m .= $this->_eachPre;
  164. $m .= implode($this->_eachPost.$this->_eachPre, $s);
  165. $m .= $this->_eachPost;
  166. $m .= $this->_post;
  167. return $m;
  168. }
  169. }
  170.  
  171. ?>

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