Source for file WizardStepContainer.class.php

Documentation is available at WizardStepContainer.class.php

  1. <?php
  2. /**
  3. * @since Jul 20, 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: WizardStepContainer.class.php,v 1.12 2007/09/19 14:04:51 adamfranco Exp $
  10. */
  11.  
  12. /**
  13. * This is a special {@link WizardComponent} that will keep track of {@link WizardStep}s
  14. * and switch from one to the other.
  15. *
  16. * @since Jul 20, 2005
  17. * @package polyphony.wizard.components
  18. *
  19. * @copyright Copyright &copy; 2005, Middlebury College
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  21. *
  22. * @version $Id: WizardStepContainer.class.php,v 1.12 2007/09/19 14:04:51 adamfranco Exp $
  23. */
  24. class WizardStepContainer extends WizardComponent {
  25. var $_currStep;
  26. var $_steps;
  27. var $_stepNames;
  28. /**
  29. * Constructor
  30. * @access public
  31. * @return void
  32. */
  33. function WizardStepContainer () {
  34. $this->_currStep = 0;
  35. $this->_steps = array();
  36. $this->_stepNames = array();
  37. }
  38. /**
  39. * Adds a new {@link WizardStep} to this container.
  40. * @param string $name A reference/ID name for this wizard.
  41. * @param ref object $step
  42. * @access public
  43. * @return ref object The new step object.
  44. */
  45. function addStep ($name, $step) {
  46. ArgumentValidator::validate($step, ExtendsValidatorRule::getRule("WizardStep"), true);
  47. $name = preg_replace("/[^a-zA-Z0-9:_\-]/", "_", $name);
  48. $this->_steps[] =$step;
  49. $this->_stepNames[] = $name;
  50. $step->setParent($this);
  51. return $step;
  52. }
  53. /**
  54. * Returns the current step number (starting at zero).
  55. * @access public
  56. * @return integer
  57. */
  58. function getCurrentStep () {
  59. return $this->_currStep;
  60. }
  61.  
  62. /**
  63. * Gets the short-name associated with the current step.
  64. * @access public
  65. * @return string
  66. */
  67. function getCurrentStepName () {
  68. if (!count($this->_steps)) return null;
  69. return $this->_stepNames[$this->_currStep];
  70. }
  71. /**
  72. * Sets the step to the one given by $name.
  73. * @param string $name
  74. * @access public
  75. * @return void
  76. */
  77. function setStep ($name, $context=array()) {
  78. $name = preg_replace("/[^a-zA-Z0-9:_-]/", "_", $name);
  79. $ind = array_search($name, $this->_stepNames);
  80. if ($ind !== false) {
  81. $oldStep = $this->_currStep;
  82. $this->_currStep = $ind;
  83. $wizard =$this->getWizard();
  84. if(!is_null($oldStep)){
  85. $context['from'] = $this->_stepNames[$oldStep];
  86. $context['to'] =$this->_stepNames[$this->_currStep];
  87. $wizard->triggerLater("edu.middlebury.polyphony.wizard.step_changed", $this, $context);
  88. }
  89. }else{
  90. throwError(new Error("No such step '".$name."'","WizardStepContainer",true));
  91. }
  92. }
  93. /**
  94. * Set the step to the one given by key
  95. *
  96. * @param integer $key
  97. * @return void
  98. * @access public
  99. * @since 4/2/07
  100. */
  101. function setStepByKey ($key, $context=array()) {
  102. $name = $this->_stepNames[$key];
  103. $this->setStep($name, $context);
  104. }
  105. /**
  106. * Returns an array of steps keyed by step name/id.
  107. * @access public
  108. * @return ref array
  109. */
  110. function getSteps () {
  111. return $this->_steps;
  112. }
  113. /**
  114. * Returns the step referenced by $name.
  115. * @param string $name
  116. * @access public
  117. * @return ref object
  118. */
  119. function getStep ($name) {
  120. $name = preg_replace("/[^a-zA-Z0-9:_-]/", "_", $name);
  121. $key = array_search($name, $this->_stepNames);
  122. if($key !== false) return $this->_steps[$key];
  123. return ($null = null);
  124. }
  125. /**
  126. * Goes to the next step, if possible.
  127. * @access public
  128. * @return void
  129. */
  130. function nextStep () {
  131. $num = count($this->_steps);
  132. if ($this->_currStep != $num - 1) {
  133. $this->_currStep++;
  134. }
  135. $wizard =$this->getWizard();
  136. $wizard->triggerLater("edu.middlebury.polyphony.wizard.step_changed", $this, array(
  137. 'from'=>$this->_stepNames[$this->_currStep-1], 'to'=>$this->_stepNames[$this->_currStep]));
  138. }
  139. /**
  140. * Goes to the previous step, if possible.
  141. * @access public
  142. * @return void
  143. */
  144. function previousStep () {
  145. $num = count($this->_steps);
  146. if ($this->_currStep != 0) {
  147. $this->_currStep--;
  148. }
  149. $wizard =$this->getWizard();
  150. $wizard->triggerLater("edu.middlebury.polyphony.wizard.step_changed", $this, array(
  151. 'from'=>$this->_stepNames[$this->_currStep+1], 'to'=>$this->_stepNames[$this->_currStep]));
  152. }
  153. /**
  154. * Returns if this StepContainer has a next step.
  155. * @access public
  156. * @return boolean
  157. */
  158. function hasNext () {
  159. return ($this->_currStep != count($this->_steps) - 1) && (count($this->_steps) != 0);
  160. }
  161. /**
  162. * Returns if this StepContainer has a previous step.
  163. * @access public
  164. * @return boolean
  165. */
  166. function hasPrevious () {
  167. return $this->_currStep != 0;
  168. }
  169. /**
  170. * Returns true if this component (and all child components if applicable) have valid values.
  171. * By default, this will just return TRUE.
  172. *
  173. * In this implementation, we will go through our steps and validate each one in turn. The first
  174. * one that doesn't validate, we will move to that step so that the person can enter the information
  175. * that is required.
  176. * @access public
  177. * @return boolean
  178. */
  179. function validate () {
  180. $children =$this->getSteps();
  181. foreach (array_keys($children) as $step) {
  182. if (!$children[$step]->validate()) {
  183. $this->setStep($step);
  184. return false;
  185. }
  186. }
  187. return true;
  188. }
  189. /**
  190. * Tells the wizard component to update itself - this may include getting
  191. * form post data or validation - whatever this particular component wants to
  192. * do every pageload.
  193. * @param string $fieldName The field name to use when outputting form data or
  194. * similar parameters/information.
  195. * @access public
  196. * @return boolean - TRUE if everything is OK
  197. */
  198. function update ($fieldName) {
  199. for($i = 0; $i < count($this->_steps); $i++) {
  200. $this->_steps[$i]->update($fieldName."_".$this->_stepNames[$i]);
  201. }
  202. // set the current step if necessary
  203. if ($step = RequestContext::value("wizardSkipToStep")) {
  204. $this->setStep($step);
  205. }
  206. }
  207. /**
  208. * Returns the values of wizard-components. Should return an array if children are involved,
  209. * otherwise a whatever type of object is expected.
  210. * @access public
  211. * @return mixed
  212. */
  213. function getAllValues () {
  214. $array = array();
  215. for($i = 0; $i < count($this->_steps); $i++) {
  216. $array[$this->_stepNames[$i]] = $this->_steps[$i]->getAllValues();
  217. }
  218. return $array;
  219. }
  220. /**
  221. * Returns a block of XHTML-valid code that contains markup for this specific
  222. * component.
  223. * @param string $fieldName The field name to use when outputting form data or
  224. * similar parameters/information.
  225. * @access public
  226. * @return string
  227. */
  228. function getMarkup ($fieldName) {
  229. // first we have to check our current step
  230. if (count($this->_steps)) {
  231. $theStep =$this->_steps[$this->_currStep];
  232. $theName = $this->_stepNames[$this->_currStep];
  233. $markup = $theStep->getMarkup($fieldName."_".$theName);
  234. return $markup;
  235. }
  236. $error = dgettext("polyphony", "WIZARD ERROR: no steps were added to this WizardStepContainer!");
  237. return "<span style='color: red; font-weight: 900;'>$error</span>";
  238. }
  239. }
  240.  
  241. ?>

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