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 |
echo '<input type = "submit" name="ph_action" value = "' . $opts["submittext"] . '" >'; |
101 |
if( empty( $opts["nocancel"] ) ) { |
102 |
echo '<input type = "submit" name="ph_action" value = "' . $opts["canceltext"] . '" >'; |
103 |
} |
104 |
echo '</form>'; |
105 |
} |
106 |
|
107 |
function abort_all_changes( $fields, $names, $datatypes, $values, $tablename, |
108 |
$tablekey, $record_id, $sql, $numforms ) |
109 |
|
110 |
{ |
111 |
for( $i=0; $i < $numforms; $i++ ) { |
112 |
abortchanges( $fields, $names, set_varprefix( $datatypes, "ph_$i" ), |
113 |
&$values[$i], $tablename, $tablekey, $record_id, $sql[$i] ); |
114 |
} |
115 |
} |
116 |
|
117 |
|
118 |
function load_all_defaults( $datatypes, $numforms ) |
119 |
{ |
120 |
$values = array(); |
121 |
for( $i=0; $i < $numforms; $i++ ) { |
122 |
$values[$i] = loaddefaults( $datatypes ); |
123 |
} |
124 |
return $values; |
125 |
} |
126 |
|
127 |
function multiform_main( $tablename, $tablekey, $fields, $names, $datatypes, $record_id, $url, $opts, $numforms ) |
128 |
{ |
129 |
global $ph_action, $refresh, $BODYTAGS; |
130 |
|
131 |
|
132 |
defaults( &$url, &$opts ); |
133 |
|
134 |
if( ! empty( $refresh ) ) { |
135 |
$ph_action = $refresh; // this is probably refresh or redraw |
136 |
$refresh = ""; |
137 |
if( $opts["calltwice"] ) { |
138 |
return; |
139 |
} |
140 |
} |
141 |
if( $ph_action == "Update form" ) { |
142 |
$ph_action = "redraw"; |
143 |
if( $opts["calltwice"] ) { |
144 |
return; |
145 |
} |
146 |
} |
147 |
|
148 |
if( $ph_action == "validate" || $ph_action == $opts["submittext"] ) { |
149 |
$errors = array(); |
150 |
$result = validate_all_changes( $fields, $names, $datatypes, &$values, &$sql, &$errors, $numforms ); |
151 |
if( $result ) { |
152 |
// if everything was ok, save the data |
153 |
commit_all_changes( $fields, $names, $datatypes, &$values, $tablename, $tablekey, $record_id, &$sql, $opts, $numforms ); |
154 |
|
155 |
// now jump to somewhere else |
156 |
header( "Location: " . $url["aftersubmit"] ); |
157 |
$ph_action = "do_nothing"; |
158 |
return; |
159 |
} else { |
160 |
// we need to refresh the form |
161 |
$ph_action = "refresh"; |
162 |
global $ph_values, $ph_errors; |
163 |
$ph_values = $values; |
164 |
$ph_errors = $errors; |
165 |
if( $opts["calltwice"] ) { |
166 |
return; |
167 |
} |
168 |
} |
169 |
} |
170 |
|
171 |
|
172 |
if( $ph_action == "" ) { |
173 |
if( function_exists( "html_before_form" ) ) { |
174 |
html_before_form(); |
175 |
} |
176 |
$values = load_all_defaults( $datatypes, $numforms ); |
177 |
create_all_forms( $fields, $names, $datatypes, $values, $record_id, $errors, $url, $opts, $numforms ); |
178 |
if( function_exists( "html_after_form" ) ) { |
179 |
html_after_form(); |
180 |
} |
181 |
} |
182 |
|
183 |
|
184 |
if( $ph_action == "refresh" || $ph_action == "redraw" ) { |
185 |
global $ph_values; |
186 |
global $ph_errors; |
187 |
if( isset( $ph_values ) ) { |
188 |
$values = $ph_values; |
189 |
$errors = $ph_errors; |
190 |
} else { |
191 |
$result = validate_all_changes( $fields, $names, $datatypes, &$values, &$sql, &$errors, $numforms ); |
192 |
} |
193 |
if( function_exists( "html_before_form" ) ) { |
194 |
html_before_form(); |
195 |
} |
196 |
if( $ph_action == "redraw" ) { |
197 |
// for redrawing we don't want to display errors, so clear the array |
198 |
$errors = array(); |
199 |
} |
200 |
create_all_forms( $fields, $names, $datatypes, $values, $record_id, $errors, $url, $opts, $numforms ); |
201 |
if( function_exists( "html_after_form" ) ) { |
202 |
html_after_form(); |
203 |
} |
204 |
} |
205 |
|
206 |
if( $ph_action == "cancel" || $ph_action == $opts["canceltext"] ) { |
207 |
abort_all_changes( $fields, $names, $datatypes, &$values, $tablename, $tablekey, $record_id, $sql, $numforms ); |
208 |
header( "Location: " . $url["aftercancel"] ); |
209 |
return; |
210 |
} |
211 |
} |
212 |
?> |