1 |
dpavlin |
1.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 |
ravilov |
1.3 |
echo "<tr><td colspan=\"3\"><hr></td></tr>"; |
71 |
dpavlin |
1.1 |
|
72 |
|
|
// now create a input field for each DB field |
73 |
|
|
for( $j = 0; $j < $numforms; $j++ ) { |
74 |
|
|
echo '<tr><td><BR><BR><B><font size="+2">' . $opts["item_heading"] . ' #' . ($j+1) . ':</font></B></td></tr>'; |
75 |
|
|
$newdatatypes = set_varprefix( $datatypes, "ph_$j" ); |
76 |
|
|
for( $i = 0; !empty( $fields[ $i ] ); $i++ ) { |
77 |
|
|
|
78 |
|
|
$type = ""; |
79 |
|
|
parse_str( $newdatatypes[ $i ] ); |
80 |
|
|
|
81 |
|
|
// the functions to create widgets are named maketext, makedate, |
82 |
|
|
// etc. |
83 |
|
|
$func = "make_" . $type . "_widget"; |
84 |
|
|
|
85 |
|
|
// call the function to create the widget |
86 |
|
|
|
87 |
|
|
//echo "\n\n<BR>Pretty Name:" . $names[$i] . "<BR>\n"; |
88 |
|
|
//echo "Value: " . $values[$j][$i] . "<BR>\n"; |
89 |
|
|
//echo "Field: " . $fields[$i] . "<BR>\n"; |
90 |
|
|
//echo "Datatype: " . $datatypes[$i] . "<BR>\n"; |
91 |
|
|
|
92 |
|
|
$func( $names[$i], $values[$j][$i], $fields[$i], $errors[$j][$i], $newdatatypes[$i] ); |
93 |
|
|
} |
94 |
ravilov |
1.3 |
echo "<tr><td colspan=\"2\"><hr></td></tr>"; |
95 |
dpavlin |
1.1 |
} |
96 |
|
|
|
97 |
|
|
// finish the form |
98 |
|
|
?> |
99 |
|
|
</table> |
100 |
|
|
<input type = "hidden" name="refresh" value ="" > |
101 |
|
|
<? |
102 |
dpavlin |
1.2 |
|
103 |
|
|
// save variables from GET call |
104 |
|
|
while (list($name, $val) = each($GLOBALS[HTTP_GET_VARS])) { |
105 |
|
|
echo "<input type=\"hidden\" name=\"$name\" value=\"$val\">\n"; |
106 |
|
|
} |
107 |
|
|
|
108 |
dpavlin |
1.1 |
echo '<input type = "submit" name="ph_action" value = "' . $opts["submittext"] . '" >'; |
109 |
|
|
if( empty( $opts["nocancel"] ) ) { |
110 |
|
|
echo '<input type = "submit" name="ph_action" value = "' . $opts["canceltext"] . '" >'; |
111 |
|
|
} |
112 |
|
|
echo '</form>'; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
function abort_all_changes( $fields, $names, $datatypes, $values, $tablename, |
116 |
|
|
$tablekey, $record_id, $sql, $numforms ) |
117 |
|
|
|
118 |
|
|
{ |
119 |
|
|
for( $i=0; $i < $numforms; $i++ ) { |
120 |
|
|
abortchanges( $fields, $names, set_varprefix( $datatypes, "ph_$i" ), |
121 |
|
|
&$values[$i], $tablename, $tablekey, $record_id, $sql[$i] ); |
122 |
|
|
} |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
|
126 |
|
|
function load_all_defaults( $datatypes, $numforms ) |
127 |
|
|
{ |
128 |
|
|
$values = array(); |
129 |
|
|
for( $i=0; $i < $numforms; $i++ ) { |
130 |
|
|
$values[$i] = loaddefaults( $datatypes ); |
131 |
|
|
} |
132 |
|
|
return $values; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
function multiform_main( $tablename, $tablekey, $fields, $names, $datatypes, $record_id, $url, $opts, $numforms ) |
136 |
|
|
{ |
137 |
|
|
global $ph_action, $refresh, $BODYTAGS; |
138 |
|
|
|
139 |
|
|
|
140 |
|
|
defaults( &$url, &$opts ); |
141 |
|
|
|
142 |
|
|
if( ! empty( $refresh ) ) { |
143 |
|
|
$ph_action = $refresh; // this is probably refresh or redraw |
144 |
|
|
$refresh = ""; |
145 |
|
|
if( $opts["calltwice"] ) { |
146 |
|
|
return; |
147 |
|
|
} |
148 |
|
|
} |
149 |
|
|
if( $ph_action == "Update form" ) { |
150 |
|
|
$ph_action = "redraw"; |
151 |
|
|
if( $opts["calltwice"] ) { |
152 |
|
|
return; |
153 |
|
|
} |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
if( $ph_action == "validate" || $ph_action == $opts["submittext"] ) { |
157 |
|
|
$errors = array(); |
158 |
|
|
$result = validate_all_changes( $fields, $names, $datatypes, &$values, &$sql, &$errors, $numforms ); |
159 |
|
|
if( $result ) { |
160 |
|
|
// if everything was ok, save the data |
161 |
|
|
commit_all_changes( $fields, $names, $datatypes, &$values, $tablename, $tablekey, $record_id, &$sql, $opts, $numforms ); |
162 |
|
|
|
163 |
|
|
// now jump to somewhere else |
164 |
|
|
header( "Location: " . $url["aftersubmit"] ); |
165 |
|
|
$ph_action = "do_nothing"; |
166 |
|
|
return; |
167 |
|
|
} else { |
168 |
|
|
// we need to refresh the form |
169 |
|
|
$ph_action = "refresh"; |
170 |
|
|
global $ph_values, $ph_errors; |
171 |
|
|
$ph_values = $values; |
172 |
|
|
$ph_errors = $errors; |
173 |
|
|
if( $opts["calltwice"] ) { |
174 |
|
|
return; |
175 |
|
|
} |
176 |
|
|
} |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
|
180 |
|
|
if( $ph_action == "" ) { |
181 |
|
|
if( function_exists( "html_before_form" ) ) { |
182 |
|
|
html_before_form(); |
183 |
|
|
} |
184 |
|
|
$values = load_all_defaults( $datatypes, $numforms ); |
185 |
|
|
create_all_forms( $fields, $names, $datatypes, $values, $record_id, $errors, $url, $opts, $numforms ); |
186 |
|
|
if( function_exists( "html_after_form" ) ) { |
187 |
|
|
html_after_form(); |
188 |
|
|
} |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
|
192 |
|
|
if( $ph_action == "refresh" || $ph_action == "redraw" ) { |
193 |
|
|
global $ph_values; |
194 |
|
|
global $ph_errors; |
195 |
|
|
if( isset( $ph_values ) ) { |
196 |
|
|
$values = $ph_values; |
197 |
|
|
$errors = $ph_errors; |
198 |
|
|
} else { |
199 |
|
|
$result = validate_all_changes( $fields, $names, $datatypes, &$values, &$sql, &$errors, $numforms ); |
200 |
|
|
} |
201 |
|
|
if( function_exists( "html_before_form" ) ) { |
202 |
|
|
html_before_form(); |
203 |
|
|
} |
204 |
|
|
if( $ph_action == "redraw" ) { |
205 |
|
|
// for redrawing we don't want to display errors, so clear the array |
206 |
|
|
$errors = array(); |
207 |
|
|
} |
208 |
|
|
create_all_forms( $fields, $names, $datatypes, $values, $record_id, $errors, $url, $opts, $numforms ); |
209 |
|
|
if( function_exists( "html_after_form" ) ) { |
210 |
|
|
html_after_form(); |
211 |
|
|
} |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
if( $ph_action == "cancel" || $ph_action == $opts["canceltext"] ) { |
215 |
|
|
abort_all_changes( $fields, $names, $datatypes, &$values, $tablename, $tablekey, $record_id, $sql, $numforms ); |
216 |
|
|
header( "Location: " . $url["aftercancel"] ); |
217 |
|
|
return; |
218 |
|
|
} |
219 |
|
|
} |
220 |
|
|
?> |