$path, 'title' => (($name) ? $name : $module . ' wizard'), 'callback' => $callback, 'weight' => -5, 'access' => $access); foreach (array_keys($processes) as $op) { //TODO: specify a mechanism to allow an alternate tree creation schema $links[] = array('path' => $path .'/'. $op, 'title' => $processes[$op], 'callback' => $callback, 'weight' => $i++, 'access' => $access); } } return $links; } /** * This function seperates the default wizard menu construction. * this allows us to change the location of the wizard, and other important things */ function wizard_callback() { $path = trim(str_replace(arg(0) . "/" . arg(1) , "", $_GET["q"]), "/"); $module = arg(0); $proc = arg(1); return wizard_page($module, $proc, $path); } function _wizard_next($wizards_array, $path_present) { $paths = array_keys($wizards_array); if ($path_present) { $paths = array_keys($wizards_array); $paths_flipped = array_flip($paths); $index = $paths_flipped[$path_present]; return $paths[$index + 1]; } else { return $paths[0]; } } /** * Generate a page for the wizard. */ function wizard_page($module, $proc, $path, $mount = "") { if ($path == $_POST['edit']['wizard_history']) { $_SESSION['wizard'][$module][$path] = ($_SESSION['wizard'][$module][$path] == null) ? array() : $_SESSION['wizard'][$module][$path]; $_SESSION["wizard"][$module][$path] = array_merge($_SESSION["wizard"][$module][$path], (array)$_POST["edit"]); } $screens = module_invoke($module, "wizards", $proc); $mount = ($mount != null) ? trim($mount, "/") : "$module/$proc"; $process_page = $module . "_process_" . $proc; if (isset($_POST["wizard_back"])) { drupal_goto("$mount/" . $_POST["edit"]["wizard_previous"]); } if (isset($_POST["wizard_forward"])) { $wizard_next = _wizard_next($screens, $path); //if ($_POST["edit"]["wizard_next"]) { // drupal_goto("$mount/" . $_POST["edit"]["wizard_next"]); //} if ($wizard_next) { drupal_goto("$mount/" . $wizard_next); } else { $_SESSION["wizard"]["completed"][$module] = true; drupal_goto($mount); } } //Where we are coming from after submission $form .= form_hidden("wizard_history", $path); $screenpath = array_keys($screens); //completed the form, unless told otherwise $completed = true; if (empty($path)) { if ($_SESSION["wizard"]["completed"][$module]) { //form has been completed //Run through all screens, only calling the execute op, if it validates foreach ($screenpath as $screen) { $nextkey++; $defaults = wizard_invoke_screen($module, $screen, $edit, "defaults"); $defaults = is_array($defaults) ? $defaults : array(); $_SESSION["wizard"][$module][$screen] = (is_array($_SESSION["wizard"][$module][$screen])) ? $_SESSION["wizard"][$module][$screen] : array(); $edit = array_merge($defaults, $_SESSION["wizard"][$module][$screen]); if (wizard_invoke_screen($module, $screen, $edit, "validate") != true) { // Validation failed, go back to screen and try again drupal_goto("$mount/$screen"); } else { // Call the execute operation, making the database modifications wizard_invoke_screen($module, $screen, $edit, "execute"); } } if (function_exists($process_page)) { call_user_func($process_page, "execute"); $form .= call_user_func($process_page, "completed"); } else { $form .= t("
Thank you for completing this wizard, you may continue
"); } unset($_SESSION["wizard"]["completed"][$module]); } else { // display form introduction $completed = false; $nextkey = 0; if (function_exists($process_page)) { $form .= call_user_func($process_page, "introduction"); } else { $form .= t("Welcome to this wizard. please continue
"); } } $output = $form; } else { $nextkey = 0; foreach ($screenpath as $screen) { $nextkey++; $defaults = wizard_invoke_screen($module, $screen, $edit, "defaults"); $defaults = is_array($defaults) ? $defaults : array(); $_SESSION["wizard"][$module][$screen] = (($_SESSION["wizard"][$module][$screen]) != NULL) ? $_SESSION["wizard"][$module][$screen] : array(); $edit = array_merge($defaults, $_SESSION["wizard"][$module][$screen]); if ($path == $screen) { $form .= wizard_invoke_screen($module, $screen, $edit, "form"); $completed = false; break; // could be dangerous } elseif (wizard_invoke_screen($module, $screen, $edit, "validate") != true) { if ($path != $screen) { drupal_goto("$mount/$screen"); } $form .= wizard_invoke_screen($module, $screen, $edit, "form"); $completed = false; break; // could be dangerous } } } // // Display the buttons at the bottom of the page // $previous = ($nextkey - 2); if (!$completed) { if ($previous >= 0) { $buttons .= form_button("Back", "wizard_back"); $buttons .= form_hidden("wizard_previous", $screenpath[$previous]); } $buttons .= form_button("Next", "wizard_forward"); $buttons .= form_hidden("wizard_next", $screenpath[$nextkey]); $form .= $buttons; $output = form($form, "post"); } print theme("page", $output, $screens[$path]); } function wizard_invoke_screen($module, $path, $edit, $op) { $split = explode("/", str_replace("#","", $path)); for ($x = sizeof($split); $x >= 0; $x--) { $func = implode("_", array_merge( array($module . "_wizard"), array_slice($split, 0, $x))); if (function_exists($func)) { return $func($path, $edit ,$op); } } } ?>