setCurrentStep ($initial_step); } function run () { $this->onStart (); $this->restoreEncodedState (); $this->moveToStep ($this->getCurrentStep ()); if ($this->hasView ()) { $view = $this->getView (); $this->getForm () ->add ('hidden', 'walker', 'value='.$this->getEncodedState ()) ->set ('walker', 'element_open', '') ; $view->form = $this->getForm (); echo $view->render (); } } function moveToStep ($step, $return_to = null) { $this->resetView (); $this->setCurrentStep ($step); $this->resetForm (); if ($this->hasReturnTo ()) { $step = $this->getReturnTo (); $this->unsetReturnTo (); } elseif (isset ($return_to)) { $this->setReturnTo ($return_to); } call_user_func (array ($this, $step)); } function setView ($view) { $this->view = $view; return $view; } function getView () { return $this->view; } function hasView () { return isset ($this->view); } private function resetView () { $this->view = null; } function setCurrentStep ($current_step) { $this->setPersistent ('step', $current_step); } function getCurrentStep () { return $this->persistents['step']; } function hasCurrentStep () { return isset ($this->persistents['step']); } function setForm (Walker_Form $form) { $form->setWalker ($this); $this->form = $form; } function getForm () { return $this->form; } private function resetForm () { $form = new Walker_Form ($this->getCurrentStep ()); $this->setForm ($form); } protected function setPersistent ($name, $value = null) { if (!isset ($value)) { $form = $this->getForm (); switch ($form->$name->type) { case 'checkbox': $value = $form->$name->checked; break; default: $value = $form->$name->value; break; } } $this->persistents[$name] = $value; } protected function unsetPersistent ($name) { unset ($this->persistents[$name]); } protected function getPersistent ($name, $default = null) { if (!isset ($this->persistents[$name])) { return $default; } else { return $this->persistents[$name]; } } protected function restorePersistent ($name, $default = null) { $value = $this->getPersistent ($name, $default); $form = $this->getForm (); switch ($form->$name->type) { case 'checkbox': $form->$name->checked = $value; break; default: $form->$name->value = $value; break; } return $value; } protected function getPersistents () { return $this->persistents; } private function restoreEncodedState () { if (array_key_exists ('walker', $_POST) && isset ($_POST['walker'])) { $result = $_POST['walker']; if (is_scalar ($result)) { $result = base64_decode ($result, true); $result = $this->getCypher ()->decrypt ($result); if ($result !== false) { $result = @unserialize ($result); if ($result !== false and is_array ($result)) { foreach ($result as $key => $value) { $this->setPersistent ($key, $value); } } } } } } protected function setReturnTo ($return_to) { $this->setPersistent ('return_to', $return_to); } protected function unsetReturnTo () { $this->unsetPersistent ('return_to'); } protected function getReturnTo () { return $this->getPersistent ('return_to'); } protected function hasReturnTo () { $return_to = $this->getPersistent ('return_to'); return isset ($return_to); } private function getEncodedState () { return base64_encode ($this->getCypher ()->encrypt (serialize ($this->getPersistents ()))); } private function getCypher () { $key = Kohana::config ('config.cypher'); if (empty ($key)) { throw new Exception_InvalidIniFile ('No cypher parameter in the configuration file.'); } return new Rms_Blowfish ($key); } protected function onStart () { } }