Source for file WSelectOrNew.class.php

Documentation is available at WSelectOrNew.class.php

  1. <?php
  2. /**
  3. * @since 4/28/06
  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: WSelectOrNew.class.php,v 1.8 2007/09/19 14:04:51 adamfranco Exp $
  10. */
  11.  
  12. /**
  13. * <##>
  14. *
  15. * @since 4/28/06
  16. * @package polyphony.wizard.components
  17. *
  18. * @copyright Copyright &copy; 2005, Middlebury College
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  20. *
  21. * @version $Id: WSelectOrNew.class.php,v 1.8 2007/09/19 14:04:51 adamfranco Exp $
  22. */
  23. class WSelectOrNew
  24. extends WizardComponentWithChildren
  25. //implements ErrorCheckingWizardComponent
  26.  
  27. {
  28.  
  29. var $_select;
  30. var $_new;
  31. var $_label;
  32. var $_errorRule = null;
  33. var $_errorMessage = null;
  34. var $_errorStyle = "color: red;";
  35. var $_showError;
  36. /**
  37. * Constructor
  38. *
  39. * @param <##>
  40. * @return <##>
  41. * @access public
  42. * @since 4/28/06
  43. */
  44. function WSelectOrNew () {
  45. $this->_select = new WSelectList;
  46. $this->_select->setParent($this);
  47. $this->_new = new WTextField;
  48. $this->_new->setParent($this);
  49. $this->_init();
  50. }
  51. /**
  52. * Initialize our fields
  53. *
  54. * @return void
  55. * @access public
  56. * @since 6/2/06
  57. */
  58. function _init () {
  59. $this->_select->addOption('__NEW_VALUE__', (dgettext("polyphony", "* New Value *")));
  60. $this->_select->setValue('__NEW_VALUE__');
  61. }
  62. /**
  63. * Set the value of the input component
  64. *
  65. * @param string $value
  66. * @access public
  67. * @return void
  68. * @since 10/21/05
  69. */
  70. function setValue ($value) {
  71. if ($this->_select->isOption($value)) {
  72. $this->_select->setValue($value);
  73. $this->_new->setValue('');
  74. } else {
  75. $this->_select->setValue('__NEW_VALUE__');
  76. $this->_new->setValue($value);
  77. }
  78. }
  79. /**
  80. * Sets if this component will be enabled or disabled.
  81. * @param boolean $enabled
  82. * @param boolean $sticky If true, future calls to setEnabled without sticky
  83. * will have no effect.
  84. * @access public
  85. * @return void
  86. */
  87. function setEnabled ($enabled, $sticky = false) {
  88. $this->_select->setEnabled($enabled, $sticky);
  89. $this->_new->setEnabled($enabled, $sticky);
  90. }
  91. /**
  92. * Set the input component
  93. *
  94. * @param object WComponent $input
  95. * @return object WComponent
  96. * @access public
  97. * @since 10/20/05
  98. */
  99. function setSelectComponent ( $input ) {
  100. ArgumentValidator::validate($input,
  101. ExtendsValidatorRule::getRule("WizardComponent"));
  102. ArgumentValidator::validate($input,
  103. HasMethodsValidatorRule::getRule("addOnChange"));
  104. $this->_select =$input;
  105. $this->_select->setParent($this);
  106. $this->_init();
  107. return $this->_select;
  108. }
  109. /**
  110. * Set the input component
  111. *
  112. * @param object WComponent $input
  113. * @return object WComponent
  114. * @access public
  115. * @since 10/20/05
  116. */
  117. function setNewComponent ( $input ) {
  118. ArgumentValidator::validate($input,
  119. ExtendsValidatorRule::getRule("WizardComponent"));
  120. $this->_new =$input;
  121. $this->_new->setParent($this);
  122. return $this->_new;
  123. }
  124. /**
  125. * Adds an option to this list.
  126. * @param string $value The short value that represents the displayed text.
  127. * @param string $displayText The text to show to the end user.
  128. * @param string $styles Any styles to pass into the menu option.
  129. * @access public
  130. * @return void
  131. */
  132. function addOption ($value, $displayText, $styles=null) {
  133. $this->_select->addOption($value, $displayText, $styles);
  134. }
  135. /**
  136. * $this is a shallow copy, subclasses should override to copy fields as
  137. * necessary to complete the full copy.
  138. *
  139. * @return object
  140. * @access public
  141. * @since 7/11/05
  142. */
  143. function postCopy () {
  144. $this->_select =$this->_select->copy();
  145. $this->_new =$this->_new->copy();
  146. return $this;
  147. }
  148. /**
  149. * Sets the label for this checkbox element.
  150. * @param string $label;
  151. * @access public
  152. * @return void
  153. */
  154. function setLabel ($label) {
  155. $this->_label = $label;
  156. }
  157. /**
  158. * Returns true if this component (and all child components if applicable) have valid values.
  159. * By default, this will just return TRUE. Validate should be called usually before a save event
  160. * is handled, to make sure everything went smoothly.
  161. * @access public
  162. * @return boolean
  163. */
  164. function validate () {
  165. if($this->isUsingNewValue()){
  166. $rule =$this->getErrorRule();
  167. if (!$rule) return true;
  168. $err = $rule->checkValue($this);
  169. return $err;
  170. }else{
  171. return $this->_select->validate();
  172. }
  173. }
  174. /**
  175. * Tells the wizard component to update itself - this may include getting
  176. * form post data or validation - whatever this particular component wants to
  177. * do every pageload.
  178. * @param string $fieldName The field name to use when outputting form data or
  179. * similar parameters/information.
  180. * @access public
  181. * @return boolean - TRUE if everything is OK
  182. */
  183. function update ($fieldName) {
  184. $this->_select->update($fieldName."_select");
  185. $this->_new->update($fieldName."_new");
  186. if ($this->isUsingNewValue() && $this->_new->getAllValues())
  187. $this->setValue($this->_new->getAllValues());
  188. }
  189. /**
  190. * Returns the values of wizard-components. Should return an array if children are involved,
  191. * otherwise a whatever type of object is expected.
  192. * @access public
  193. * @return mixed
  194. */
  195. function getAllValues () {
  196. if ($this->isUsingNewValue())
  197. return $this->_new->getAllValues();
  198. else
  199. return $this->_select->getAllValues();
  200. }
  201. /**
  202. * Return true if we should be using the new value rather than the select
  203. *
  204. * @return boolean
  205. * @access public
  206. * @since 6/2/06
  207. */
  208. function isUsingNewValue () {
  209. if ($this->_select->isStartingDisplay())
  210. return false;
  211. if ($this->_select->getAllValues() == '__NEW_VALUE__')
  212. return true;
  213. if ($this->_select->getAllValues() == '')
  214. return true;
  215. return false;
  216. }
  217. /**
  218. * Sets this element's regular expression. Its value must match this to be considered valid.
  219. * @param string $regex
  220. * @access public
  221. * @return void
  222. */
  223. function setErrorRule ($rule) {
  224. $this->_errorRule =$rule;
  225. }
  226. /**
  227. * Sets the text to be displayed if an error occurs.
  228. * @param string $text
  229. * @access public
  230. * @return void
  231. */
  232. function setErrorText ($text) {
  233. $this->_errorMessage = $text;
  234. }
  235. /**
  236. * Sets the CSS style of the error text.
  237. * @param string $style
  238. * @access public
  239. * @return void
  240. */
  241. function setErrorStyle ($style) {
  242. $this->_errorStyle = $style;
  243. }
  244. /**
  245. * Returns the error {@link WECRule}.
  246. * @access public
  247. * @return ref object
  248. */
  249. function getErrorRule () {
  250. return $this->_errorRule;
  251. }
  252. /**
  253. * Returns the error text.
  254. * @access public
  255. * @return string
  256. */
  257. function getErrorText () {
  258. return $this->_errorMessage;
  259. }
  260. /**
  261. * Returns the error text CSS style.
  262. * @access public
  263. * @return string
  264. */
  265. function getErrorStyle () {
  266. return $this->_errorStyle;
  267. }
  268. /**
  269. * Returns a block of XHTML-valid code that contains markup for this specific
  270. * component.
  271. * @param string $fieldName The field name to use when outputting form data or
  272. * similar parameters/information.
  273. * @access public
  274. * @return string
  275. */
  276. function getMarkup ($fieldName) {
  277. $m = "";
  278. $m .= "\n\t<div title='".$this->_label."' style='vertical-align: top; padding: 0px; margin: 0px;'>";
  279. $newId = RequestContext::name($fieldName."_new_container");
  280. if ($this->isUsingNewValue())
  281. $display = " display: block;";
  282. else
  283. $display = " display: none;";
  284. $this->_select->addOnChange("var newField = document.get_element_by_id('$newId'); if (this.value == '__NEW_VALUE__') { newField.style.display = 'block'; } else { newField.style.display = 'none'; }");
  285. $m .= "\n\t\t".$this->_select->getMarkup($fieldName."_select");
  286. $m .= "\n\t</div>";
  287. $m .= "\n\t<div id='$newId' style='padding: 0px; margin: 0px; $display'>";
  288. $m .= "\n\t\t".$this->_new->getMarkup($fieldName."_new");
  289. $m .= "\n\t</div>";
  290. $errText = $this->getErrorText();
  291. $errRule =$this->getErrorRule();
  292. $errStyle = $this->getErrorStyle();
  293. //$todo fix the darn validation
  294. if ($errText && $errRule) {
  295. //$m .= "\n\t\t<span id='".$fieldName."_error' style=\"padding-left: 10px; $errStyle\">&laquo; $errText</span>";
  296. //$m .= Wizard::getValidationJavascript($fieldName."_new", $errRule, $fieldName."_error", !$this->validate());
  297. }
  298. return $m;
  299. }
  300.  
  301. }
  302.  
  303. ?>

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