1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
<?php
##
## Copyright (c) 1999 Internet Images Srl
## Massimiliano Masserelli
##
## Depends on ooh_forms
##
## $Id: tpl_form.inc,v 1.1 1999/07/31 15:57:26 negro Exp $
##
class tpl_form {
var $classname = "tpl_form"; # Used for serialization AND in display()
# to determine the filename of template
# containing html/php code needed to
# actually render the form
var $form_data; # Holds form info (form object), is
# initialized in the init() "constructor".
var $values = array(); # Must be inizialized by a call to the
# init() "constructor".
# All data that this form produces
# for application use shold be
# appended there
var $error = ""; # Contains error messages generated by
# validate() and validate_input() methods.
var $has_defaults = 0; # Flag, form default values were passed
# via set_default_values() method. Should
# not be tampered with by the user.
# This is a sort of constructor for the class. $values
# is an array used to store form values. The class
# writes data into it, but may also use it to obtain
# data from the application
function init($values) {
if (! is_array($values)) {
$this->values = array();
} else {
$this->values = $values;
}
return true;
}
# Init the form object, which will contain all fields info. The
# hidden field form_name is used by other methods to determine
# if form has been submitted by the user.
# You shouldn't override this in descendants, use setup_fields()
# instead.
function setup() {
$this->form_data = new form;
$this->form_data->add_element(array(
"name"=>"form_name",
"value"=>$this->classname,
"type"=>"hidden"
));
$this->setup_fields();
return true;
}
# Override this method in order to provide form fields definition
# that suit your needs.
function setup_fields() {
return true;
}
# Returns an array containing all data submitted by the user for
# the form. This array is intended to be passed to set_defaults_values()
# some time later.
function get_default_values() {
if (! is_object($this->form_data)) {
$this->setup();
}
$fv = array();
for (reset($this->form_data->elements); $elrec = current($this->form_data->elements); next($this->form_data->elements)) {
$el = $elrec["ob"];
$vn = $el->name;
global $$vn;
$fv[$el->name] = $$vn;
}
return $fv;
}
# Restore form defaults from an array as returned by get_default_values()
function set_default_values($fv) {
if (! is_object($this->form_data)) {
$this->setup();
}
if (! is_array($fv)) {
return false;
}
while (list($var, $value) = each($fv)) {
global $$var;
$$var = $value;
}
$this->has_defaults = 1;
return true;
}
# Validates user input. This method should not be overridden in
# descendants. See validate_input() instead. Returns false on
# error and sets $this->error accordingly.
function validate() {
global $form_name;
if (! is_object($this->form_data)) {
$this->setup();
}
if ($form_name == $this->classname) {
$err = $this->form_data->validate("ok");
if ($err == "ok") {
return $this->validate_input();
} else {
$this->error = $err;
return false;
}
} else {
return false;
}
}
# This method should be overridden in descendants, in order to
# provided complex validation methods (i.e. field2 should not be
# empty IF field1 = "other").
# Should return false on error and set $this->error accordingly.
function validate_input() {
$this->error = "";
return true;
}
# Actually display form fields. This method should not be overridden
# in descendants. User should provide a file named as the derived
# class and with ".ihtml" extension.
function display() {
global $sess;
global $form_name;
global $PHP_SELF;
if (! is_object($this->form_data)) {
$this->setup();
}
if (($form_name == $this->classname) || $this->has_defaults) {
$this->form_data->load_defaults();
}
include($this->classname . ".ihtml");
return true;
}
# Process user data. This method should not be overridden by descendants.
# See process_input() and process_default() instead. Returns true on
# success.
function process() {
if ($this->validate()) {
return $this->process_input();
} else {
return $this->process_default();
}
}
# This method should be overridden in descendants. It is executed after
# validation has taken place. The data passed to the form should be
# used to fill $this->values array (eventually after a database lookup
# or whatever).
function process_input() {
return true;
}
# This method should be overridden in descendants. It is executed when
# form validation fails or before presenting the form for the first
# time. Should be used to inhibit form displaying if data can be
# extracted from previous actions, divination, penguin fly watching or
# whatever.
function process_default() {
return false;
}
# This method should not be overridden. It is intended as the main
# interface between the application and the form. Once the form has
# been properly derived to suit designer's needs, applications calls
# $myform->get_values() and obtains an array containing user supplied
# data. Failing that, the application should call $form->display()
# to (re)present the form to the user.
function get_values() {
if ($this->process()) {
return $this->values;
} else {
return false;
}
}
# Sort of "destructor". There's no real need to call it, except maybe
# freeing some memory. May be called from the application, but is
# otherwise not executed. Returns true.
function clear() {
$this->has_defaults = 0;
$this->values = array();
$this->error = "";
unset($this->form_data);
return true;
}
}
?>
|