Source for file Wizard.abstract.php

Documentation is available at Wizard.abstract.php

  1. <?php
  2. /**
  3. *
  4. * @package polyphony.wizard
  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: Wizard.abstract.php,v 1.18 2007/09/19 14:04:50 adamfranco Exp $
  10. */
  11.  
  12. /*
  13. * Require our needed classes
  14. */
  15.  
  16. require_once(HARMONI."/GUIManager/Components/Block.class.php");
  17. require_once(POLYPHONY."/main/library/Wizard/WizardComponentWithChildren.abstract.php");
  18. require_once(POLYPHONY."/main/library/Wizard/Components/WUpdateListener.class.php");
  19.  
  20. /**
  21. * The Wizard class provides a system for posting, retrieving, and
  22. * validating user input over a series of steps, as well as maintianing
  23. * the submitted values over a series of steps, until the wizard is saved.
  24. * The wizard is designed to be called from within a single action. The values
  25. * of its state allow its steps to work as "sub-actions".
  26. *
  27. * The only method left to implement for classes that extend is getMarkup().
  28. *
  29. * @package polyphony.wizard
  30. *
  31. * @copyright Copyright &copy; 2005, Middlebury College
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  33. *
  34. * @version $Id: Wizard.abstract.php,v 1.18 2007/09/19 14:04:50 adamfranco Exp $
  35. * @author Gabe Schine
  36. * @abstract
  37. */
  38.  
  39. class Wizard extends WizardComponentWithChildren/*, EventTrigger*/ {
  40. var $_formName = 'wizard';
  41. var $_id = 'w';
  42. /**
  43. * Tells this wizard to update itself and run any events that have happened.
  44. * @return void
  45. * @access public
  46. */
  47. function go() {
  48. $this->update($this->_id);
  49. }
  50. /**
  51. * Tells the wizard component to update itself - this may include getting
  52. * form post data or validation - whatever this particular component wants to
  53. * do every pageload.
  54. * @param string $fieldName The field name to use when outputting form data or
  55. * similar parameters/information.
  56. * @access public
  57. * @return boolean - TRUE if everything is OK
  58. */
  59. function update ($fieldName) {
  60. // get all the steps we have here and update them
  61. $children =$this->getChildren();
  62. $ok = true;
  63. foreach (array_keys($children) as $key) {
  64. if (!$children[$key]->update($fieldName."_".$key)) {
  65. $ok = false;
  66. }
  67. }
  68. $this->triggerEvent("edu.middlebury.polyphony.wizard.update", $this);
  69. foreach (array_keys($this->_eventsLater) as $key) {
  70. $info =$this->_eventsLater[$key];
  71. $this->triggerEvent($info[0], $info[1], $info[2]);
  72. unset($this->_eventsLater[$key]);
  73. }
  74. return $ok;
  75. }
  76.  
  77. /**
  78. * Returns the top-level {@link Wizard} in which this component resides.
  79. * @access public
  80. * @return ref object
  81. */
  82. function getWizard () {
  83. return $this;
  84. }
  85. /**
  86. * Sets this Wizard's ID string. It is optional but must be used to allow multiple Wizards
  87. * to be displayed on one page to differentiate fields and save/cancel requests.
  88. * @param string $id must be alpha-numeric with _ or -.
  89. * @access public
  90. * @return void
  91. */
  92. function setIdString ($id) {
  93. $this->_id = $id;
  94. }
  95. /**
  96. * Returns the ID string.
  97. * @access public
  98. * @return string
  99. */
  100. function getIdString () {
  101. return $this->_id;
  102. }
  103. /**
  104. * Returns a layout of content for the current Wizard-state
  105. * @return ref object Layout
  106. */
  107. function getLayout () {
  108. $markup = $this->getMarkup($this->_id);
  109. $obj = new Block($markup, WIZARD_BLOCK);
  110. return $obj;
  111. }
  112. /**
  113. * Sets this component's parent (probably a {@link Wizard} object) so that it can
  114. * have access to its parent's information, if needed.
  115. * @param ref object
  116. * @access public
  117. * @return void
  118. */
  119. function setParent ($parent) {
  120. // do nothing - this component should never have a parent
  121. }
  122. /**
  123. * Returns the name (id) of the form in which the wizard is contained.
  124. * @access public
  125. * @return string
  126. */
  127. function getWizardFormName () {
  128. return $this->_id . "_form";
  129. }
  130. var $_eventsLater = array();
  131. /**
  132. * Triggers this event after the update has occured.
  133. * @param string $event
  134. * @param ref object $source
  135. * @param optional array $context
  136. * @access public
  137. * @return void
  138. */
  139. function triggerLater ($event, $source, $context = null) {
  140. $newArray = array();
  141. $newArray[0] = $event;
  142. $newArray[1] =$source;
  143. $newArray[2] = $context;
  144. $this->_eventsLater[] =$newArray;
  145. }
  146. /**
  147. * Returns a block of XHTML-valid code that contains markup for this specific
  148. * component.
  149. * @param string $fieldName The field name to use when outputting form data or
  150. * similar parameters/information.
  151. * @access public
  152. * @return string
  153. */
  154. function getMarkup ($fieldName) {
  155. $done = isset($GLOBALS["__wizardJSDone"]);
  156. if ($done) return '';
  157. // we are going to output a bunch of javascript to handle wizard
  158. // validation, etc.
  159. $javascript = <<< END
  160.  
  161. function addWizardRule(elementID, rule, errorID, displayError) {
  162. var element = getWizardElement(elementID);
  163. element._ruleCheck = rule;
  164. element._ruleErrorID = errorID;
  165.  
  166. var errEl = getWizardElement(errorID);
  167. // errEl.style.position = "absolute";
  168. errEl.style.visibility = (displayError?"visible":"hidden");
  169. //if(element._ruleCheck){
  170. // alert(element.name);
  171. //}
  172. }
  173.  
  174. function getWizardElement(id) {
  175. if (document.layers) return document.layers[id];
  176. if (document.all) return document.all[id];
  177. return document.getElementById(id);
  178. }
  179.  
  180. function validateWizard(form) {
  181. // alert('checking...');
  182. if (form._ignoreValidation) return true;
  183. var elements = form.elements;
  184. var ok = true;
  185. for(var i = 0; i < form.length; i++) {
  186. var el = elements[i];
  187.  
  188. //alert(el.name);
  189. //getWizardElement(el.name);
  190. // var al = "hello "+el._ruleErrorID+" world "+elementID+" "+ rule;
  191. // alert(al);
  192. if (el._ruleCheck) {
  193. var errID = el._ruleErrorID;
  194. var errDiv = getWizardElement(errID);
  195. if (!el._ruleCheck(el)) {
  196. ok = false;
  197. // show the error div
  198. errDiv.style.visibility = "visible";
  199. } else {
  200. errDiv.style.visibility = "hidden";
  201. }
  202. }
  203. }
  204. return ok;
  205. }
  206.  
  207. function ignoreValidation(form) {
  208. // alert("ignoring...");
  209. form._ignoreValidation = true;
  210. }
  211.  
  212. function submitWizard(form) {
  213. // alert("Submit!");
  214. if (validateWizard(form)) form.submit();
  215. }
  216.  
  217. END;
  218.  
  219. $m = "<script type='text/javascript'>\n";
  220. $m .= "/*<![CDATA[*/\n" . $javascript . "\n/*]]>*/\n</script>\n";
  221. $GLOBALS["__wizardJSDone"] = true;
  222. return $m;
  223. }
  224. // ------------------------------
  225. // Utility static methods
  226. // ---------------------------------
  227. /**
  228. * Returns the parsed string from text including [[tags]].
  229. * The second parameter holds a hashtable of components to ask for content for
  230. * each of the [[tags]].
  231. * @param string $text
  232. * @param ref array $components
  233. * @param optional string $prepend Optionally prepends the string passed to the field names.
  234. * @access public
  235. * @return string
  236. * @static
  237. */
  238. function parseText($text, $components, $prepend = '')
  239. {
  240. // first get all of the tags we have to work with
  241. preg_match_all("/\[{2}[^\[]*\]{2}/", $text, $matches);
  242. // to though each of the matches and get the required string
  243. $workingText = $text;
  244. foreach ($matches[0] as $match) {
  245. if (preg_match("/\[{2}fieldname:([^|]*)\]{2}/", $match, $parts)) {
  246. $propName = $parts[1];
  247. // fieldname:xxxx
  248. if (isset($components[$propName])) {
  249. $markup = $prepend.$propName;
  250. } else {
  251. $msg = sprintf(dgettext("polyphony", "WIZARD ERROR: could not find a component to match with fieldname:<i>%s</i>!"), $propName);
  252. $markup = "<span style='color: red; font-weight: 900;'>$msg</span>";
  253. }
  254. } else if (preg_match("/\[{2}([^|]*)\]{2}/", $match, $parts)) {
  255. $propName = $parts[1];
  256. if (isset($components[$propName])) {
  257. $markup = $components[$propName]->getMarkup($prepend.$propName);
  258. } else {
  259. $msg = sprintf(dgettext("polyphony", "WIZARD ERROR: could not find a component to match with <i>%s</i>!"), $propName);
  260. $markup = "<span style='color: red; font-weight: 900;'>$msg</span>";
  261. }
  262. } else {
  263. $msg = sprintf(dgettext("polyphony", "WIZARD ERROR: could not find a component name within the tag <i>%s</i>!"), $match);
  264. $markup = "<span style='color: red; font-weight: 900;'>$msg</span>";
  265. }
  266. $workingText = str_replace($match, $markup, $workingText);
  267. }
  268. return $workingText;
  269. }
  270. /**
  271. * Returns a block of javascript that will add a validation command to the form when submitting.
  272. * @param string $elementID The ID of the form element.
  273. * @param ref object $rule A {@link WECRule} for error checking.
  274. * @param string $errDivID The ID of a div tag that will be displayed if the element doesn't validate.
  275. * @param optional boolean $displayError Defaults to FALSE, but allows you to display the error on
  276. * pageload, if we need to notify the user immediately of an error.
  277. * @access public
  278. * @return string
  279. * @static
  280. */
  281. function getValidationJavascript ($elementID, $rule, $errDivID, $displayError = false) {
  282. $elementID = str_replace("'", "\\'", $elementID);
  283. $errDivID = str_replace("'", "\\'", $errDivID);
  284. $checkFunc = $rule->generateJavaScript();
  285. $m = "<script type='text/javascript'>\n" .
  286. "/*<![CDATA[*/\n" .
  287. "addWizardRule('$elementID', $checkFunc, '$errDivID', ".($displayError?"true":"false").");" .
  288. "/*]]>*/\n" .
  289. "</script>\n";
  290. return $m;
  291. }
  292. //-----------------------------------
  293. // EventTrigger methods
  294. // -----------------------------------
  295. /**
  296. * @var array $_eventListeners
  297. * @access private
  298. */
  299. var $_eventListeners = array();
  300. /**
  301. * Adds an {@link EventListener} to be triggered whenever an event is thrown.
  302. * @param string $eventType The string event type for which this {@link EventListener} is listening (example: "edu.middlebury.harmoni.action_executed")
  303. * @param ref object $eventListener the {@link EventListener} object.
  304. * @access public
  305. * @return ref object
  306. */
  307. function addEventListener ($eventListener) {
  308. ArgumentValidator::validate($eventListener, HasMethodsValidatorRule::getRule("handleEvent"), true);
  309. $this->_eventListeners[] =$eventListener;
  310. }
  311. /**
  312. * Notifies all of the {@link EventListener}s that have been added that an event has
  313. * occured.
  314. * @param string $eventType The event type string.
  315. * @param ref object $source The source object of this event.
  316. * @param optional array $context An array of contextual parameters for the specific event. The content of the array will depend on the event.
  317. * @access public
  318. * @return void
  319. */
  320. function triggerEvent ($eventType, $source, $context = null) {
  321. $list =$this->_eventListeners;
  322. foreach (array_keys($list) as $key) {
  323. $list[$key]->handleEvent($eventType, $source, $context);
  324. }
  325. }
  326. }
  327.  
  328. ?>

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