Date: 12/11/00
- Next message: uw: "[phplib-dev] cvs commit"
- Previous message: uw: "[phplib-dev] cvs commit"
- Next in thread: uw: "[phplib-dev] cvs commit"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
From: uw
Date: Mon Dec 11 19:09:29 2000
Modified files:
php-lib/php/form/assistant.inc
Log message:
starting to clean up
Index: php-lib/php/form/assistant.inc
diff -u php-lib/php/form/assistant.inc:1.13 php-lib/php/form/assistant.inc:1.14
--- php-lib/php/form/assistant.inc:1.13 Tue Jun 20 08:40:08 2000
+++ php-lib/php/form/assistant.inc Mon Dec 11 19:08:56 2000
@@ -1,714 +1,719 @@
-<?php
-/**
-* Assistant/wizard that guides a user through several forms
-* <email protected> Ulf Wendel <ulf <email protected>>
-* <email protected> 0.1alpha
-*/
-class assistant extends panemanager {
-
- /**
- * PHPLIB persistance helper, name of this class
- * <email protected> string $classname
- * <email protected> public
- */
- var $classname = "assistant";
-
- /**
- * PHPLIB persistance helper, list of persistant "slots"
- * <email protected> array $persistent_slots
- * <email protected> public
- */
- var $persistent_slots = array("page_errors", "page_values", "lastviewed");
-
- /**
- * Array with status (isValid()) information on each page
- * - must be restored by a session! -
- * <email protected> array $page_errors
- * <email protected> private
- */
- var $page_errors = array();
-
- /**
- * Which page was shown last?
- * - must be restored by a session! -
- * <email protected> string $lastviewed
- * <email protected> private
- */
- var $lastviewed = 0;
-
- /**
- * Which page will be show now?
- * <email protected> string $now
- * <email protected> private
- */
- var $now = -1;
-
- /**
- * Show tabs?
- * <email protected> bool $show_tabs
- * <email protected> private
- * <email protected> show_tabs()
- */
- var $show_tabs = true;
-
- /**
- * Template object
- * <email protected> object $tpl
- * <email protected> private
- */
- var $tpl = "";
-
- /**
- * Path to the template files
- * <email protected> string $template
- * <email protected> private
- */
- var $template = "";
-
- /**
- * How to handle unknowns
- * <email protected> string $unknowns
- * <email protected> private
- */
- var $unknowns = "remove";
-
- /**
- * Template files
- * <email protected> array $template_files
- * <email protected> private
- * <email protected> setTemplateFiles()
- */
- var $template_files = array(
- "main" => "assistant_main.tpl"
- #"tabs" => "assistant_tabs.html"
- );
-
- /**
- * "switching mode"
- * <email protected> enum(strict|weak) $mode = "strict"
- * <email protected> private
- * <email protected> setMode()
- */
- var $mode = "strict";
-
- /**
- * Some labels
- * <email protected> array $labels
- * <email protected> setLabels()
- */
- var $labels = array (
- "next" => "",
- "prev" => "",
- "reset" => ""
- );
-
- /**
- * Nameprefix of all buttons generated by assistant
- * <email protected> string $buttonname
- * <email protected> private
- */
- var $buttonname = "assistant";
-
- /**
- * Show prev- and next-button?
- * <email protected> bool show_prevnext = true
- * <email protected> private
- * <email protected> setPrevNext()
- */
- var $show_prevnext = true;
-
- /**
- * Flag, set if specials
- * <email protected> string $flag_specials
- * <email protected> private
- * <email protected> get(), findPosition()
- */
- var $flag_specials = "";
-
- /**
- * Restores the Assistant. Load the pages with values, sets validation flags etc.
- * <email protected> private
- */
- function restoreAssistant() {
-
- $this->saveFormValues();
- $this->setPageErrors();
- $this->findPosition();
- $this->loadFormValues();
-
- } // end func restoreAssistant
-
-
-
- /**
- * Sets the path to the templates
- * <email protected> string $template
- * <email protected> bool $ok
- * <email protected> Runtime_WrongParam
- * <email protected> public
- */
- function setTemplatePath($template) {
- if (""==$template) {
- $this->error = new exception_runtime_wrongparam("setTemplatePath(), illegal function call", 16, __FILE__, __LINE__);
- return false;
- }
- $this->template = $template;
- return true;
- } // end func setTemplatepath
-
- /**
- * Creates a new template object
- * <email protected> bool $ok
- * <email protected> Runtime
- * <email protected> private
- * <email protected> assistant()
- */
- function newTemplate() {
- if (""==$this->template || 0==count($this->template_files)) {
- $this->error = new exception_runtime("newTemplate(), illegal function call", 15, __FILE__, __LINE__);
- return false;
- }
- unset($this->tpl);
- $this->tpl = new Template($this->template, $this->unknowns);
-
- $allowed = array("main" => true, "tabs" => true);
- reset($this->template_files);
- while (list($name, $file)=each($this->template_files))
- if (isset($allowed[$name]))
- $this->tpl->set_file($name, $file);
-
- $this->tpl->set_block("main", "tab", "tabs");
- return true;
- } // end func newTemplate
-
- /**
- * Set the template files
- * <email protected> array $files
- * <email protected> template_files
- * <email protected> public
- */
- function setTemplateFiles($files) {
- if (!is_array($files))
- $files = array($files);
-
- $allowed = array("main" => true, "tabs" => true);
- reset($files);
- while (list($name, $file)=each($files))
- if (isset($allowed[$name]))
- $this->template_files[$name] = $file;
- } // end func setTemplateFiles
-
- /**
- * return the current page
- * <email protected> string $html
- * <email protected> runtime
- * <email protected> public
- * <email protected> show()
- */
- function get() {
- if (0==count($this->pages)) {
- $this->error = new exception_runtime("assistant -> get(), no pages in assistant", 14, __FILE__, __LINE__);
- return "";
- }
-
- $this->lastviewed = $this->now;
- $this->newTemplate();
-
- $counter = 0;
- reset($this->pages) ;
- while (list($p_id, $data)=each($this->pages)) {
-
- $optional = (isset($data["optional"])) ? true : false;
- $label = (isset($data["tab"])) ? $data["tab"] : $data["name"];
- $error = $this->page_errors[$p_id];
- list($tab, $bgcolor) = $this->getTab($label, $p_id, $optional, $error);
- $replace = array (
- "TAB" => $tab,
- "BGCOLOR" => $bgcolor
- );
-
- $this->tpl->set_var($replace);
- $this->tpl->parse("tabs", "tab", true);
- $counter++;
- }
-
- $data = $this->pages[$this->now];
- $comment = (isset($data["comment"])) ? $data["comment"] : "";
- $obj = $data["object"];
- $obj->restoreSessionHash($this->page_values[$this->now]);
- $form = $obj->get();
-
- $finished = 0;
- reset($this->page_errors);
- while (list($k, $v)=each($this->page_errors))
- if (true == $v)
- $finished++;
-
- list($prev, $next)=$this->getNeighbour($this->now);
- $labels["prev"] = (isset($data["prev"])) ? $data["prev"] : $this->labels["prev"];
- $labels["next"] = (isset($data["next"])) ? $data["next"] : $this->labels["next"];
- if (""!=$labels["prev"])
- $prev = $this->getButton($labels["prev"], $prev);
- if (""!=$labels["next"])
- $next = $this->getButton($labels["next"], $next);
-
- $submit = (isset($data["submit"])) ? $data["submit"] : $data["tab"];
- if (""!=$submit)
- $submit = $this->getButton($submit, $this->now);
-
- $reset = (isset($data["reset"])) ? $data["reset"] : $this->labels["reset"];
- if (""!=$reset)
- $reset = $this->getResetButton($reset);
-
- $finish = "";
- $exit = "";
- $help = "";
- if ($this->isValid()) {
- $finish = $this->getButton($this->finish_button["label"], "", $this->finish_button["name"]);
- }
- if (isset($this->exit["name"])) {
- $exit = $this->getButton($this->exit["label"], "", $this->exit["name"]);
- }
- if (isset($this->help["help"])) {
- $help = $this->getButton($this->help["label"], "", $this->help["name"]);
- }
-
- $replace = array (
- "START_FORM" => $this->form_start,
- "END_FORM" => $this->form_end,
- "FORM" => $form,
- "HELP" => ("help"==$this->flag_specials) ? $this->help["help"] : "",
- "COMMENT" => $comment,
- "TOTAL_PAGES" => count($this->pages),
- "FINISHED_PAGES" => $finished,
- "BUTTON_PREV" => $prev,
- "BUTTON_NEXT" => $next,
- "BUTTON_SUBMIT" => $submit,
- "BUTTON_RESET" => $reset,
- "BUTTON_FINISH" => $finish,
- "BUTTON_EXIT" => $exit,
- "BUTTON_HELP" => $help
- );
- $this->tpl->set_var($replace);
- $this->tpl->parse("out", "main", true);
-
- return $this->tpl->get("out");
- } // end func get()
-
- /**
- * Get the values of the last form
- * <email protected> private
- */
- function saveFormValues() {
- if (""==(string)$this->lastviewed)
- return;
-
- $obj = $this->pages[$this->lastviewed]["object"];
- $this->page_values[$this->lastviewed] = $obj->getSessionHash();
- } // end func saveFormValues()
-
- /**
- * Load a form with values before we display it
- * <email protected> private
- */
- function loadFormValues() {
-
- $obj = $this->pages[$this->now]["object"];
- if ($this->lastviewed != $this->now && isset($this->page_values[$this->now])) {
- $obj->restoreSessionHash($this->page_values[$this->now]);
- $this->pages[$this->now]["object"] = $obj;
- }
- $this->page_errors[$this->now] = ($obj->isValid()) ? true : false;
-
- } // end func loadFormValues()
-
- /**
- * Return the name of the current page
- * <email protected> string $page_name
- * <email protected> public
- * <email protected> isValid(), currentPageID()
- */
- function currentPageName() {
- if (!isset($this->pages[$this->now]["name"]))
- return "";
- return $this->pages[$this->now]["name"];
- } // end func currentPageName
-
- /**
- * Returns the ID of the current page. The first page defined with
- * addPage() call gets the ID 0, the second 1 and so on.
- * If a negative number is returned the help or exit button is pressed.
- * <email protected> int $page_id
- * <email protected> public
- * <email protected> isValid(), currentPageName()s
- */
- function currentPageID() {
- return $this->now;
- } // end func currentPageID
-
- /**
- * Returns the page_id (page__values/page_errors index) of a
- * page name.
- * <email protected> string $page_name
- * <email protected> int $page_id, if -1 the page was not found
- * <email protected> private
- * <email protected> isValid(), getPageValues()
- */
- function PageNametoPageID($page_name) {
- if (""==$page_name) {
- $this->setError(47, "assistant -> PageNametoPageID(), illegal function call");
- return -1;
- }
-
- $found = -1;
- reset($this->pages);
- while (list($p_id, $page)=each($this->pages))
- if ($page_name == $page["name"]) {
- $found = $p_id;
- break;
- }
-
- return $found;
- } // end func PageNametoPageID
-
- /**
- * Are all pages valid, is the requested page valid?
- * <email protected> string $page = "", if empty test all
- * <email protected> bool $ok
- * <email protected> public
- */
- function isValid($page="") {
- if (0==count($this->pages)) {
- $this->setError(12, "assistant -> isValid(), no pages in assistant");
- return false;
- }
-
- if (""!=$page) {
-
- $p_id = $this->PageNametoPageID($page);
- if (-1==$p_id) {
- $this->setError(48, "assistant -> isValid(), unknown page '$page'");
- return false;
- }
-
- if (isset($this->page_errors[$p_id]))
- return $this->page_errors[$p_id];
- return false;
-
- } else {
- reset($this->page_errors);
- while (list($p_id, $valid)=each($this->page_errors))
- if (!$valid)
- return false;
- return true;
- }
- } // end func isValid
-
- /**
- * Find the previous and the next element of the current element
- * <email protected> string $now
- * <email protected> array($prev, $next)
- * <email protected> private
- */
- function getNeighbour($now) {
- if (""==(string)$now) {
- $this->setError(11, "assistant -> getNeighbour(), illegal function call");
- return array("", "");
- }
- if (0==count($this->pages)) {
- $this->setError(12, "assistant -> getNeighbour(), no pages in assistant");
- return array("", "");
- }
-
- $len = count($this->pages)-1;
- $next = $now+1;
- $prev = $now-1;
- if ($next>$len)
- $next = 1;
- if ($prev<0)
- $prev = $len;
- return array($prev, $next);
- } // end func getNeighbour
-
- /**
- * Sets the error flags of all pages
- * <email protected> bool $ok
- * <email protected> private
- */
- function setPageErrors() {
- if (0==count($this->pages)) {
- $this->setError(10, "assistant -> setPageErrors(), no pages in assistant");
- return false;
- }
-
- if (""==(string)$this->lastviewed)
- return true;
-
- if (isset($this->pages[$this->lastviewed]["optional"]))
- return true;
-
- $obj = $this->pages[$this->lastviewed]["object"];
- $obj->restoreSessionHash($this->page_values[$this->lastviewed]);
- $this->page_errors[$this->lastviewed] = ($obj->isValid()) ? true : false;
-
- return true;
- } // end func setPageErrors
-
- /**
- * If you override some page_values in Init() you have to update the
- * page validation flags.
- * <email protected> string $pages name of one page, "" means update all
- * <email protected> array $pages array with page names
- * <email protected> bool $ok
- * <email protected> public
- */
- function updatePageErrors($pages = "") {
-
- if (""==$pages) {
- reset($this->pages);
- while (list($page, $data)=each($this->pages))
- $pages[] = $page;
- }
-
- if (!is_array($pages))
- $pages = array($pages);
-
- if (0==count($pages)) {
- $this->setError(63, "form -> updatePageErrors(), no pages defined?");
- return false;
- }
-
- $ok = true;
- $unknown = "";
-
- reset($pages);
- while (list($k, $page)=each($pages)) {
- if (isset($this->page_values[$page]) ) {
- if (isset($this->pages[$page]["optional"]))
- continue;
- $obj = $this->pages[$page]["object"];
- $obj->restoreSessionHash($this->page_values[$page]);
- $this->page_errors[$page] = ($obj->isValid()) ? true : false;
- } else
- $unknown.= "'$page' ";
- }
-
- if (""!=$unknown) {
- $this->setError(64, "form -> updatePageErrors(), unknown pages: $unknown");
- $ok = false;
- }
-
- return $ok;
- } // end func updatePageErrors
-
- function findSelected() {
-
- if ("POST" == $this->method)
- $data = $GLOBALS["HTTP_POST_VARS"];
- else
- $data = $GLOBALS["HTTP_GET_VARS"];
-
- $this->selected = -1;
-
- if (!is_array($data) || 0==count($data))
- return false;
-
- if (isset($data[$this->help["name"]])) {
- $this->selected = -2;
- return false;
- }
- if (isset($data[$this->exit["name"]])) {
- $this->selected = -3;
- return false;
- }
-
- $len = strlen($this->buttonname);
- reset($data);
- while (list($varname, $v)=each($data))
- if ($this->buttonname == substr($varname, 0, $len)) {
- if (preg_match(sprintf("/%s_([0-9]+)_[xy]{1}/i", $this->buttonname), $varname, $regs))
- $this->selected = (int)$regs[1];
- else
- $this->selected = (int)substr($varname, $len+1);
- }
- return true;
- }
- /**
- * Find the page that was selected by the user
- * <email protected> private
- */
- function findPosition() {
-
- $ok = $this->findSelected();
- if (-1 == $this->selected) {
- $this->now = $this->lastviewed;
- return false;
- }
- if (-2 == $this->selected) {
- $this->now = $this->lastviewed;
- $this->flag_specials = "help";
- return true;
- }
- if (-3 == $this->selected) {
- $this->now = $this->lastviewed;
- $this->flag_specials = "exit";
- }
-
- if ("strict"!=$this->mode) {
- $this->now = $this->selected;
- return;
- }
-
- if ($this->page_errors[$this->selected] && $this->page_errors[0]) {
- $this->now = $this->selected;
- return;
- }
-
- if (!$this->page_errors[$this->lastviewed]) {
- $this->now = $this->lastviewed;
- return;
- }
-
- $this->now = $this->selected;
- } // end func findPosition
-
- /**
- * Returns an array with the names of all pages
- * page_id => name
- * <email protected> array $pagenames
- * <email protected> public
- */
- function getPageNames() {
- if (0==count($this->pages)) {
- $this->setError(9, "assistant -> getPagesNames(), no pages in assistant");
- return array();
- }
-
- $pagenames = array();
- reset($this->pages);
- while (list($p_id, $data)=each($this->pages))
- $pagenames[$p_id] = $data["name"];
-
- return $pagenames;
- } // end func getPageNames
-
- /**
- * Add a page to the assistant
- * <email protected> array $pagedata
- * <email protected> bool $ok
- * <email protected> public
- */
- function addPage($pagedata) {
- if (!is_array($pagedata) || 0==count($pagedata)) {
- $this->setError(2, "assistant -> addPage(), illegal function call");
- return false;
- }
- if (!isset($pagedata["name"]) || ""==$pagedata["name"]) {
- $this->setError(3, "assistant -> addPage(), you must specify a name for you page");
- return false;
- }
- if (""==(string)$pagedata["object"]) {
- $this->setError(40, "assistant -> addPage(), you must specify a template object for this page");
- return false;
- }
-
- if (!is_object($pagedata["object"])) {
- $obj = $pagedata["object"];
- $pagedata["object"] = new $obj;
- }
-
- $this->pages[] = $pagedata;
-
- $p_id = count($this->pages)-1;
- if (!isset($this->page_errors[$p_id])) {
- if (isset($pagedata["optional"]))
- $this->page_errors[$p_id] = true;
- else
- $this->page_errors[$p_id] = false;
- }
-
- return true;
- } // end func addPage
-
- /**
- * Sets the "switching mode". If set to "strict" the
- * user must complete a form before he gets to the
- * next tab. If set to "weak" he's free to switch between the tabs.
- * In both cases he's not allowed to finish the assistant before
- * all forms/tabs that are not declared as optional are without errors
- * <email protected> enum(strict|weak) $mode
- * <email protected> public
- */
- function setMode($mode) {
- if (""==$mode) {
- $this->setError(4, "assistant -> mode(), illegal function call");
- return false;
- }
-
- if ("strict"!=$mode)
- $this->mode = "weak";
- else
- $this->mode = $mode;
-
- return true;
- } // end func mode
-
- /**
- * Sets the label and action for the button that finishes the form
- * <email protected> string $label
- * <email protected> string $action, fallback on $PHP_SELF
- * <email protected> bool $ok
- * <email protected> public
- */
- function setFinish($label, $action="") {
- global $PHP_SELF;
- if (""==$label) {
- $this->setError(5, "assistant -> setFinish(), illegal function call");
- return false;
- }
- if (""==$action)
- $action = $PHP_SELF;
-
- $this->finish_button = array (
- "label" => $label,
- "action" => $PHP_SELF
- );
-
- return true;
- } // end func setFinish
-
- /**
- * Set the labels of some buttons
- * <email protected> string|array $labels
- * <email protected> string $value
- * <email protected> bool $ok
- * <email protected> labels
- * <email protected> public
- */
- function setLabel($labels, $value="") {
- if ((""==$value && ""==$labels) || (!is_array($labels) && ""==$value)) {
- $this->setError(16, "assistant -> setLabel(), illegal function call");
- return false;
- }
-
- $ok = false;
- if (!is_array($labels)) {
- if (isset($this->labels[$labels])) {
- $this->labels[$labels] = $value;
- $ok = true;
- }
- } else {
-
- reset($labels);
- while (list($label, $value)=each($labels))
- if (isset($this->labels[$label])) {
- $this->labels[$label] = $value;
- $ok = true;
- }
- }
-
- return $ok;
- } // end func setLabel
-
-} // end class assistant
+<?php
+/**
+* Assistant/wizard that guides a user through several forms.
+*
+* <email protected> Ulf Wendel <ulf.wendel <email protected>>
+* <email protected> $Id: assistant.inc,v 1.14 2000/12/11 18:08:56 uw Exp $
+*/
+class assistant extends panemanager {
+
+ /**
+ * PHPLIB persistance helper, name of this class.
+ *
+ * <email protected> string $classname
+ * <email protected> public
+ */
+ var $classname = "assistant";
+
+ /**
+ * PHPLIB persistance helper, list of persistant "slots".
+ *
+ * <email protected> array $persistent_slots
+ * <email protected> public
+ */
+ var $persistent_slots = array("page_errors", "page_values", "lastviewed");
+
+ /**
+ * Array with status (isValid()) information of each page.
+ *
+ * - must be restored by a session! -
+ *
+ * <email protected> array $page_errors
+ */
+ var $page_errors = array();
+
+ /**
+ * Which page was shown last?
+ *
+ * - must be restored by a session! -
+ *
+ * <email protected> string $lastviewed
+ */
+ var $lastviewed = 0;
+
+ /**
+ * Which page will be show now?
+ *
+ * <email protected> string $now
+ */
+ var $now = -1;
+
+ /**
+ * Show tabs?
+ *
+ * <email protected> bool $show_tabs
+ * <email protected> show_tabs()
+ */
+ var $show_tabs = true;
+
+ /**
+ * Template object.
+ *
+ * <email protected> object $tpl
+ */
+ var $tpl = "";
+
+ /**
+ * Path to the template files.
+ *
+ * <email protected> string $template
+ */
+ var $template = "";
+
+ /**
+ * How to handle unknowns.
+ *
+ * <email protected> string $unknowns
+ */
+ var $unknowns = "remove";
+
+ /**
+ * Template files.
+ *
+ * <email protected> array $template_files
+ * <email protected> setTemplateFiles()
+ */
+ var $template_files = array(
+ "main" => "assistant_main.tpl"
+ #"tabs" => "assistant_tabs.html"
+ );
+
+ /**
+ * "switching mode"
+ *
+ * <email protected> string
+ * <email protected> setMode()
+ */
+ var $mode = "strict";
+
+ s/**
+ * Some labels.
+ *
+ * <email protected> array $labels
+ * <email protected> setLabels()
+ */
+ var $labels = array (
+ "next" => "",
+ "prev" => "",
+ "reset" => ""
+ );
+
+ /**
+ * Nameprefix of all buttons generated by assistant.
+ *
+ * <email protected> string $buttonname
+ */
+ var $buttonname = "assistant";
+
+ /**
+ * Show prev- and next-button?
+ *
+ * <email protected> bool show_prevnext = true
+ * <email protected> setPrevNext()
+ */
+ var $show_prevnext = true;
+
+ /**
+ * Flag, set if specials.
+ *
+ * <email protected> string $flag_specials
+ * <email protected> get(), findPosition()
+ */
+ var $flag_specials = "";
+
+ /**
+ * Restores the Assistant. Load the pages with values, sets validation flags etc.
+ *s
+ * <email protected> private
+ */
+ function restoreAssistant() {
+
+ $this->saveFormValues();
+ $this->setPageErrors();
+ $this->findPosition();
+ $this->loadFormValues();
+
+ } // end func restoreAssistant
+
+ /**
+ * Sets the path to the templates
+ *
+ * <email protected> string $template
+ * <email protected> bool $ok
+ * <email protected> FormError
+ * <email protected> public
+ */
+ function setTemplatePath($template) {
+ if ("" == $template) {
+ $this->exceptions[] = new FormError("setTemplatePath(), illegal function call",__FILE__, __LINE__);
+ return false;
+ }
+
+ $this->template = $template;
+ return true;
+ } // end func setTemplatepath
+
+ /**
+ * Creates a new template object.
+ *
+ * <email protected> bool $ok
+ * <email protected> FormError
+ * <email protected> private
+ * <email protected> assistant()
+ */
+ function newTemplate() {
+ if ("" == $this->template || 0 == count($this->template_files)) {
+ $this->exceptions[] = new FormError("newTemplate(), illegal function call", __FILE__, __LINE__);
+ return false;
+ }
+
+ unset($this->tpl);
+ $this->tpl = new Template($this->template, $this->unknowns);
+
+ $allowed = array("main" => true, "tabs" => true);
+ reset($this->template_files);
+ while (list($name, $file)=each($this->template_files))
+ if (isset($allowed[$name]))
+ $this->tpl->set_file($name, $file);
+
+ $this->tpl->set_block("main", "tab", "tabs");
+ return true;
+ } // end func newTemplate
+
+ /**
+ * Set the template files
+ *
+ * <email protected> array $files
+ * <email protected> template_files
+ * <email protected> public
+ */
+ function setTemplateFiles($files) {
+ if (!is_array($files))
+ $files = array($files);
+
+ $allowed = array("main" => true, "tabs" => true);
+ reset($files);
+ while (list($name, $file)=each($files))
+ if (isset($allowed[$name]))
+ $this->template_files[$name] = $file;
+ } // end func setTemplateFiles
+
+ /**
+ * Returns the current page.
+ *
+ * <email protected> string $html
+ * <email protected> public
+ * <email protected> show()
+ */
+ function get() {
+
+ $this->lastviewed = $this->now;
+ $this->newTemplate();
+
+ // Tablist
+ $counter = 0;
+ foreach ($this->pages as $p_id => $data) {
+
+ $optional = (isset($data["optional"])) ? true : false;
+ $label = (isset($data["tab"])) ? $data["tab"] : $data["name"];
+ $error = $this->page_errors[$p_id];
+ list($tab, $bgcolor) = $this->getTab($label, $p_id, $optional, $error);
+ $replace = array (
+ "TAB" => $tab,
+ "BGCOLOR" => $bgcolor
+ );
+
+ $this->tpl->set_var($replace);
+ $this->tpl->parse("tabs", "tab", true);
+ $counter++;
+ }
+
+ $data = &$this->pages[$this->now];
+ $comment = (isset($data["comment"])) ? $data["comment"] : "";
+ $data["object"]->restoreSessionHash($this->page_values[$this->now]);
+ $form = $data["object"]->get();
+
+ // number of finished pages
+ $finished = 0;
+ foreach ($this->page_errors as $k => $v)
+ if (true == $v)
+ $finished++;
+
+ // building buttons
+ list($prev, $next)=$this->getNeighbour($this->now);
+ $labels["prev"] = (isset($data["prev"])) ? $data["prev"] : $this->labels["prev"];
+ $labels["next"] = (isset($data["next"])) ? $data["next"] : $this->labels["next"];
+ if ("" != $labels["prev"])
+ $prev = $this->getButton($labels["prev"], $prev);
+ if ("" != $labels["next"])
+ $next = $this->getButton($labels["next"], $next);
+
+ $submit = (isset($data["submit"])) ? $data["submit"] : $data["tab"];
+ if ("" != $submit)
+ $submit = $this->getButton($submit, $this->now);
+
+ $reset = (isset($data["reset"])) ? $data["reset"] : $this->labels["reset"];
+ if ("" != $reset)
+ $reset = $this->getResetButton($reset);
+
+ $finish = "";
+ $exit = "";
+ $help = "";
+
+ if ($this->isValid())
+ $finish = $this->getButton($this->finish_button["label"], "", $this->finish_button["name"]);
+
+ if (isset($this->exit["name"]))
+ $exit = $this->getButton($this->exit["label"], "", $this->exit["name"]);
+
+ if (isset($this->help["help"]))
+ $help = $this->getButton($this->help["label"], "", $this->help["name"]);
+
+ // preparing the template replacements
+
+ $replace = array (
+ "START_FORM" => $this->form_start,
+ "END_FORM" => $this->form_end,
+ "FORM" => $form,
+ "HELP" => ("help" == $this->flag_specials) ? $this->help["help"] : "",
+ "COMMENT" => $comment,
+ "TOTAL_PAGES" => count($this->pages),
+ "FINISHED_PAGES" => $finished,
+ "BUTTON_PREV" => $prev,
+ "BUTTON_NEXT" => $next,
+ "BUTTON_SUBMIT" => $submit,
+ "BUTTON_RESET" => $reset,
+ "BUTTON_FINISH" => $finish,
+ "BUTTON_EXIT" => $exit,
+ "BUTTON_HELP" => $help
+ );
+ $this->tpl->set_var($replace);
+ $this->tpl->parse("out", "main", true);
+
+ return $this->tpl->get("out");
+ } // end func get()
+
+ /**
+ * Get the values of the last form.
+ *
+ * <email protected> private
+ */
+ function saveFormValues() {
+ if ("" == $this->lastviewed)
+ return;
+
+ $this->page_values[$this->lastviewed] = $this->pages[$this->lastviewed]["object"]->getSessionHash();
+ } // end func saveFormValues()
+
+ /**
+ * Load the form with values before we display it.
+ */
+ function loadFormValues() {
+
+ if ($this->lastviewed != $this->now && isset($this->page_values[$this->now]))
+ $this->pages[$this->now]["object"]->restoreSessionHash($this->page_values[$this->now]);
+
+ $this->page_errors[$this->now] = ($this->pages[$this->now]["object"]->isValid()) ? true : false;
+ } // end func loadFormValues()
+
+ /**
+ * Return the name of the current page
+ * <email protected> string $page_name
+ * <email protected> public
+ * <email protected> isValid(), currentPageID()
+ */
+ function currentPageName() {
+ if (!isset($this->pages[$this->now]["name"]))
+ return "";
+ return $this->pages[$this->now]["name"];
+ } // end func currentPageName
+
+ /**
+ * Returns the ID of the current page.
+ *
+ * The first page defined with addPage() call gets the ID 0, the second 1 and so on.
+ * If a negative number is returned the help or exit button is pressed.
+ *
+ * <email protected> int $page_id
+ * <email protected> public
+ * <email protected> isValid(), currentPageName()
+ */
+ function currentPageID() {
+ return $this->now;
+ } // end func currentPageID
+
+ /**
+ * Returns the page_id (page__values/page_errors index) that belongs to a page name.
+ *
+ * <email protected> string $page_name
+ * <email protected> int $page_id, if -1 the page was not found
+ * <email protected> private
+ * <email protected> FormError
+ * <email protected> isValid(), getPageValues()
+ */
+ function PageNametoPageID($page_name) {
+ if ("" == $page_name) {
+ $this->exceptions[] = new FormError("assistant -> PageNametoPageID(), illegal function call", __FILE__, __LINE__);
+ return -1;
+ }
+
+ $found = -1;
+ foreach ($this->pages as $p_id => $page)
+ if ($page_name == $page["name"]) {
+ $found = $p_id;
+ break;
+ }
+
+ return $found;
+ } // end func PageNametoPageID
+
+ /**
+ * Are all pages valid, is the requested page valid?
+ *
+ * <email protected> string $page = "", if empty test all
+ * <email protected> bool $ok
+ * <email protected> public
+ */
+ function isValid($page = "") {
+ if (0 == count($this->pages)) {
+ $this->exceptions[] = new FormError("assistant -> isValid(), no pages in assistant", __FILE__, __LINE__);
+ return false;
+ }
+
+ if ("" != $page) {
+
+ $p_id = $this->PageNametoPageID($page);
+ if (-1 == $p_id) {
+ $this->exceptions[] = new FormError("assistant -> isValid(), unknown page '$page'", __FILE__, __LINE__);
+ return false;
+ }
+
+ if (isset($this->page_errors[$p_id]))
+ return $this->page_errors[$p_id];
+ return false;
+
+ } else {
+
+ foreach ($this->page_errors as $p_id => $value)
+ if (!$valid)
+ return false;
+ return true;
+
+ }
+
+ } // end func isValid
+
+ /**
+ * Find the previous and the next element of the current element.
+ *
+ * <email protected> string $now
+ * <email protected> array($prev, $next)
+ */
+ function getNeighbour($now) {
+ if ("" == (string)$now) {
+ $this->exceptions[] = new FormError(11, "assistant -> getNeighbour(), illegal function call");
+ return array("", "");
+ }
+
+ $len = count($this->pages)-1;
+ $next = $now+1;
+ $prev = $now-1;
+
+ if ($next > $len)
+ $next = 1;
+
+ if ($prev < 0)
+ $prev = $len;
+
+ return array($prev, $next);
+ } // end func getNeighbour
+
+ /**
+ * Sets the error flags of all pages.
+ *
+ * <email protected> bool $ok
+ */
+ function setPageErrors() {
+ if (0 == count($this->pages)) {
+ $this->exceptions[] = new FormError("assistant -> setPageErrors(), no pages in assistant", __FILE__, __LINE__);
+ return false;
+ }
+
+ if ("" == $this->lastviewed)
+ return true;
+
+ if (isset($this->pages[$this->lastviewed]["optional"]))
+ return true;
+
+ $obj = &$this->pages[$this->lastviewed]["object"];
+ $obj->restoreSessionHash($this->page_values[$this->lastviewed]);
+ $this->page_errors[$this->lastviewed] = ($obj->isValid()) ? true : false;
+
+ return true;
+ } // end func setPageErrors
+
+ /**
+ * If you override some page_values in Init() you have to update the page validation flags.
+ *
+ * <email protected> string $pages name of one page, "" means update all
+ * <email protected> array $pages array with page names
+ * <email protected> bool $ok
+ * <email protected> public
+ */
+ function updatePageErrors($pages = "") {
+
+ if ("" == $pages) {
+
+ $pages = array();
+ foreach ($this->pages as $page => $date)
+ $pages[] = $page;
+
+ }
+
+ if (!is_array($pages))
+ $pages = array($pages);
+
+ if (0 == count($pages)) {
+ $this->exceptions[] = new FormError("form -> updatePageErrors(), no pages defined?", __FILE__, __LINE__);
+ return false;
+ }
+
+ $ok = true;
+ $unknown = "";
+
+ foreach ($pages as $k => $page) {
+ if (isset($this->page_values[$page]) ) {
+ if (isset($this->pages[$page]["optional"]))
+ continue;
+ $obj = &$this->pages[$page]["object"];
+ $obj->restoreSessionHash($this->page_values[$page]);
+ $this->page_errors[$page] = ($obj->isValid()) ? true : false;
+ } else
+ $unknown .= "'$page' ";
+ }
+
+ if ("" != $unknown) {
+ $this->exceptions[] = new FormError("form -> updatePageErrors(), unknown pages: $unknown", __FILE__, __LINE__);
+ $ok = false;
+ }
+
+ return $ok;
+ } // end func updatePageErrors
+
+ function findSelected() {
+
+ $data = ("GET" == $this->method) ? &$GLOBALS["HTTP_GET_VARS"] : &$GLOBALS["HTTP_POST_VARS"];
+ $this->selected = -1;
+
+ if (!is_array($data) || 0 == count($data))
+ return false;
+
+ if (isset($data[$this->help["name"]])) {
+ $this->selected = -2;
+ return false;
+ }
+ if (isset($data[$this->exit["name"]])) {
+ $this->selected = -3;
+ return false;
+ }
+
+ $len = strlen($this->buttonname);
+ reset($data);
+ while (list($varname, $v)=each($data))
+ if ($this->buttonname == substr($varname, 0, $len)) {
+ if (preg_match(sprintf("/%s_([0-9]+)_[xy]{1}/i", $this->buttonname), $varname, $regs))
+ $this->selected = (int)$regs[1];
+ else
+ $this->selected = (int)substr($varname, $len+1);
+ }
+ return true;
+ }
+ /**
+ * Find the page that was selected by the user
+ * <email protected> private
+ */
+ function findPosition() {
+
+ $ok = $this->findSelected();
+ if (-1 == $this->selected) {
+ $this->now = $this->lastviewed;
+ return false;
+ }
+ if (-2 == $this->selected) {
+ $this->now = $this->lastviewed;
+ $this->flag_specials = "help";
+ return true;
+ }
+ if (-3 == $this->selected) {
+ $this->now = $this->lastviewed;
+ $this->flag_specials = "exit";
+ }
+
+ if ("strict"!=$this->mode) {
+ $this->now = $this->selected;
+ return;
+ }
+
+ if ($this->page_errors[$this->selected] && $this->page_errors[0]) {
+ $this->now = $this->selected;
+ return;
+ }
+
+ if (!$this->page_errors[$this->lastviewed]) {
+ $this->now = $this->lastviewed;
+ return;
+ }
+
+ $this->now = $this->selected;
+ } // end func findPosition
+
+ /**
+ * Returns an array with the names of all pages
+ * page_id => name
+ * <email protected> array $pagenames
+ * <email protected> public
+ */
+ function getPageNames() {
+ if (0==count($this->pages)) {
+ $this->setError(9, "assistant -> getPagesNames(), no pages in assistant");
+ return array();
+ }
+
+ $pagenames = array();
+ reset($this->pages);
+ while (list($p_id, $data)=each($this->pages))
+ $pagenames[$p_id] = $data["name"];
+
+ return $pagenames;
+ } // end func getPageNames
+
+ /**
+ * Add a page to the assistant
+ * <email protected> array $pagedata
+ * <email protected> bool $ok
+ * <email protected> public
+ */
+ function addPage($pagedata) {
+ if (!is_array($pagedata) || 0==count($pagedata)) {
+ $this->setError(2, "assistant -> addPage(), illegal function call");
+ return false;
+ }
+ if (!isset($pagedata["name"]) || "" == $pagedata["name"]) {
+ $this->setError(3, "assistant -> addPage(), you must specify a name for you page");
+ return false;
+ }
+ if ("" == (string)$pagedata["object"]) {
+ $this->setError(40, "assistant -> addPage(), you must specify a template object for this page");
+ return false;
+ }
+
+ if (!is_object($pagedata["object"])) {
+ $obj = $pagedata["object"];
+ $pagedata["object"] = new $obj;
+ }
+
+ $this->pages[] = $pagedata;
+
+ $p_id = count($this->pages)-1;
+ if (!isset($this->page_errors[$p_id])) {
+ if (isset($pagedata["optional"]))
+ $this->page_errors[$p_id] = true;
+ else
+ $this->page_errors[$p_id] = false;
+ }
+
+ return true;
+ } // end func addPage
+
+ /**
+ * Sets the "switching mode". If set to "strict" the
+ * user must complete a form before he gets to the
+ * next tab. If set to "weak" he's free to switch between the tabs.
+ * In both cases he's not allowed to finish the assistant before
+ * all forms/tabs that are not declared as optional are without errors
+ * <email protected> enum(strict|weak) $mode
+ * <email protected> public
+ */
+ function setMode($mode) {
+ if ("" == $mode) {
+ $this->setError(4, "assistant -> mode(), illegal function call");
+ return false;
+ }
+
+ if ("strict"!=$mode)
+ $this->mode = "weak";
+ else
+ $this->mode = $mode;
+
+ return true;
+ } // end func mode
+
+ /**
+ * Sets the label and action for the button that finishes the form
+ * <email protected> string $label
+ * <email protected> string $action, fallback on $PHP_SELF
+ * <email protected> bool $ok
+ * <email protected> public
+ */
+ function setFinish($label, $action="") {
+ global $PHP_SELF;
+ if ("" == $label) {
+ $this->setError(5, "assistant -> setFinish(), illegal function call");
+ return false;
+ }
+ if ("" == $action)
+ $action = $PHP_SELF;
+
+ $this->finish_button = array (
+ "label" => $label,
+ "action" => $PHP_SELF
+ );
+
+ return true;
+ } // end func setFinish
+
+ /**
+ * Set the labels of some buttons
+ * <email protected> string|array $labels
+ * <email protected> string $value
+ * <email protected> bool $ok
+ * <email protected> labels
+ * <email protected> public
+ */
+ function setLabel($labels, $value="") {
+ if (("" == $value && "" == $labels) || (!is_array($labels) && "" == $value)) {
+ $this->setError(16, "assistant -> setLabel(), illegal function call");
+ return false;
+ }
+
+ $ok = false;
+ if (!is_array($labels)) {
+ if (isset($this->labels[$labels])) {
+ $this->labels[$labels] = $value;
+ $ok = true;
+ }
+ } else {
+
+ reset($labels);
+ while (list($label, $value)=each($labels))
+ if (isset($this->labels[$label])) {
+ $this->labels[$label] = $value;
+ $ok = true;
+ }
+ }
+
+ return $ok;
+ } // end func setLabel
+
+} // end class assistant
?>
---------------------------------------------------------------------
To unsubscribe, e-mail: phplib-dev-unsubscribe <email protected>
For additional commands, e-mail: phplib-dev-help <email protected>
- Next message: uw: "[phplib-dev] cvs commit"
- Previous message: uw: "[phplib-dev] cvs commit"
- Next in thread: uw: "[phplib-dev] cvs commit"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

