1 |
<? |
2 |
/* |
3 |
* Phormation |
4 |
* - A library of PHP code to make development of database-driven |
5 |
* html forms easy and quick |
6 |
* |
7 |
* Copyright (C) 2000 Jason D. Hildebrand |
8 |
* PeaceWorks Computer Consulting |
9 |
* |
10 |
* jason@peaceworks.ca |
11 |
* |
12 |
* This program is free software; you can redistribute it and/or modify |
13 |
* it under the terms of the GNU General Public License as published by |
14 |
* the Free Software Foundation; either version 2 of the License, or |
15 |
* (at your option) any later version. |
16 |
* |
17 |
* This program is distributed in the hope that it will be useful, |
18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 |
* GNU General Public License for more details. |
21 |
* |
22 |
* You should have received a copy of the GNU General Public License |
23 |
* along with this program; if not, write to the Free Software |
24 |
* Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA. |
25 |
*/ |
26 |
|
27 |
function validate_all_changes( $fields, $names, $datatypes, $values, $sql, $errors, $numforms ) |
28 |
{ |
29 |
$result = true; |
30 |
$errors = array(); |
31 |
$values = array(); |
32 |
for( $i=0; $i < $numforms; $i++ ) { |
33 |
$result = $result & validatechanges( $fields, $names, set_varprefix( $datatypes, "ph_$i" ), |
34 |
&$values[$i], &$sql[$i], &$errors[$i] ); |
35 |
} |
36 |
return $result; |
37 |
} |
38 |
|
39 |
function commit_all_changes( $fields, $names, $datatypes, $values, $tablename, $tablekey, $record_id, $sql, $opts, $numforms ) |
40 |
{ |
41 |
for( $i=0; $i < $numforms; $i++ ) { |
42 |
commitchanges( $fields, $names, set_varprefix( $datatypes, "ph_$i" ), |
43 |
&$values[$i], $tablename, $tablekey, $record_id, &$sql[$i], $opts ); |
44 |
} |
45 |
} |
46 |
|
47 |
|
48 |
function create_all_forms( $fields, $names, $datatypes, $values, $record_id, $errors, $url, $opts, $numforms ) |
49 |
{ |
50 |
global $FORMBGCOLOUR, $PHP_SELF, $key; |
51 |
|
52 |
//$numfields = count( $fields ); |
53 |
|
54 |
// create some javascript stuff we need, and an HTML form |
55 |
?> |
56 |
<form name="mainform" enctype="multipart/form-data" |
57 |
action="<? echo add_var_to_url( $url["onsubmit"], "record_id=$record_id") ?>" method="post" > |
58 |
<? |
59 |
echo '<table border="0" cellspacing="1" cellpadding="0" bgcolor="'. $FORMBGCOLOUR . '">'; |
60 |
|
61 |
echo '<tr><TD></td><td>' . $opts["numforms_prompt"] . '<select name="numforms" onChange="mainform.refresh.value=\'redraw\'; mainform.submit()">'; |
62 |
for( $i = 1; $i <= $opts["maxforms"]; $i++ ) { |
63 |
if( $i == $numforms ) { |
64 |
echo '<option selected value="' . $i . '">' . $i . '</option>'; |
65 |
} else { |
66 |
echo '<option value="' . $i . '">' . $i . '</option>'; |
67 |
} |
68 |
} |
69 |
echo '</select><input type="submit" name="ph_action" value="Update form"></td></tr>'; |
70 |
|
71 |
// now create a input field for each DB field |
72 |
for( $j = 0; $j < $numforms; $j++ ) { |
73 |
echo '<tr><td><BR><BR><B><font size="+2">' . $opts["item_heading"] . ' #' . ($j+1) . ':</font></B></td></tr>'; |
74 |
$newdatatypes = set_varprefix( $datatypes, "ph_$j" ); |
75 |
for( $i = 0; !empty( $fields[ $i ] ); $i++ ) { |
76 |
|
77 |
$type = ""; |
78 |
parse_str( $newdatatypes[ $i ] ); |
79 |
|
80 |
// the functions to create widgets are named maketext, makedate, |
81 |
// etc. |
82 |
$func = "make_" . $type . "_widget"; |
83 |
|
84 |
// call the function to create the widget |
85 |
|
86 |
//echo "\n\n<BR>Pretty Name:" . $names[$i] . "<BR>\n"; |
87 |
//echo "Value: " . $values[$j][$i] . "<BR>\n"; |
88 |
//echo "Field: " . $fields[$i] . "<BR>\n"; |
89 |
//echo "Datatype: " . $datatypes[$i] . "<BR>\n"; |
90 |
|
91 |
$func( $names[$i], $values[$j][$i], $fields[$i], $errors[$j][$i], $newdatatypes[$i] ); |
92 |
} |
93 |
} |
94 |
|
95 |
// finish the form |
96 |
?> |
97 |
</table> |
98 |
<input type = "hidden" name="refresh" value ="" > |
99 |
<? |
100 |
|
101 |
// save variables from GET call |
102 |
while (list($name, $val) = each($GLOBALS[HTTP_GET_VARS])) { |
103 |
echo "<input type=\"hidden\" name=\"$name\" value=\"$val\">\n"; |
104 |
} |
105 |
|
106 |
echo '<input type = "submit" name="ph_action" value = "' . $opts["submittext"] . '" >'; |
107 |
if( empty( $opts["nocancel"] ) ) { |
108 |
echo '<input type = "submit" name="ph_action" value = "' . $opts["canceltext"] . '" >'; |
109 |
} |
110 |
echo '</form>'; |
111 |
} |
112 |
|
113 |
function abort_all_changes( $fields, $names, $datatypes, $values, $tablename, |
114 |
$tablekey, $record_id, $sql, $numforms ) |
115 |
|
116 |
{ |
117 |
for( $i=0; $i < $numforms; $i++ ) { |
118 |
abortchanges( $fields, $names, set_varprefix( $datatypes, "ph_$i" ), |
119 |
&$values[$i], $tablename, $tablekey, $record_id, $sql[$i] ); |
120 |
} |
121 |
} |
122 |
|
123 |
|
124 |
function load_all_defaults( $datatypes, $numforms ) |
125 |
{ |
126 |
$values = array(); |
127 |
for( $i=0; $i < $numforms; $i++ ) { |
128 |
$values[$i] = loaddefaults( $datatypes ); |
129 |
} |
130 |
return $values; |
131 |
} |
132 |
|
133 |
function multiform_main( $tablename, $tablekey, $fields, $names, $datatypes, $record_id, $url, $opts, $numforms ) |
134 |
{ |
135 |
global $ph_action, $refresh, $BODYTAGS; |
136 |
|
137 |
|
138 |
defaults( &$url, &$opts ); |
139 |
|
140 |
if( ! empty( $refresh ) ) { |
141 |
$ph_action = $refresh; // this is probably refresh or redraw |
142 |
$refresh = ""; |
143 |
if( $opts["calltwice"] ) { |
144 |
return; |
145 |
} |
146 |
} |
147 |
if( $ph_action == "Update form" ) { |
148 |
$ph_action = "redraw"; |
149 |
if( $opts["calltwice"] ) { |
150 |
return; |
151 |
} |
152 |
} |
153 |
|
154 |
if( $ph_action == "validate" || $ph_action == $opts["submittext"] ) { |
155 |
$errors = array(); |
156 |
$result = validate_all_changes( $fields, $names, $datatypes, &$values, &$sql, &$errors, $numforms ); |
157 |
if( $result ) { |
158 |
// if everything was ok, save the data |
159 |
commit_all_changes( $fields, $names, $datatypes, &$values, $tablename, $tablekey, $record_id, &$sql, $opts, $numforms ); |
160 |
|
161 |
// now jump to somewhere else |
162 |
header( "Location: " . $url["aftersubmit"] ); |
163 |
$ph_action = "do_nothing"; |
164 |
return; |
165 |
} else { |
166 |
// we need to refresh the form |
167 |
$ph_action = "refresh"; |
168 |
global $ph_values, $ph_errors; |
169 |
$ph_values = $values; |
170 |
$ph_errors = $errors; |
171 |
if( $opts["calltwice"] ) { |
172 |
return; |
173 |
} |
174 |
} |
175 |
} |
176 |
|
177 |
|
178 |
if( $ph_action == "" ) { |
179 |
if( function_exists( "html_before_form" ) ) { |
180 |
html_before_form(); |
181 |
} |
182 |
$values = load_all_defaults( $datatypes, $numforms ); |
183 |
create_all_forms( $fields, $names, $datatypes, $values, $record_id, $errors, $url, $opts, $numforms ); |
184 |
if( function_exists( "html_after_form" ) ) { |
185 |
html_after_form(); |
186 |
} |
187 |
} |
188 |
|
189 |
|
190 |
if( $ph_action == "refresh" || $ph_action == "redraw" ) { |
191 |
global $ph_values; |
192 |
global $ph_errors; |
193 |
if( isset( $ph_values ) ) { |
194 |
$values = $ph_values; |
195 |
$errors = $ph_errors; |
196 |
} else { |
197 |
$result = validate_all_changes( $fields, $names, $datatypes, &$values, &$sql, &$errors, $numforms ); |
198 |
} |
199 |
if( function_exists( "html_before_form" ) ) { |
200 |
html_before_form(); |
201 |
} |
202 |
if( $ph_action == "redraw" ) { |
203 |
// for redrawing we don't want to display errors, so clear the array |
204 |
$errors = array(); |
205 |
} |
206 |
create_all_forms( $fields, $names, $datatypes, $values, $record_id, $errors, $url, $opts, $numforms ); |
207 |
if( function_exists( "html_after_form" ) ) { |
208 |
html_after_form(); |
209 |
} |
210 |
} |
211 |
|
212 |
if( $ph_action == "cancel" || $ph_action == $opts["canceltext"] ) { |
213 |
abort_all_changes( $fields, $names, $datatypes, &$values, $tablename, $tablekey, $record_id, $sql, $numforms ); |
214 |
header( "Location: " . $url["aftercancel"] ); |
215 |
return; |
216 |
} |
217 |
} |
218 |
?> |