/[corp_html]/back/phormation/widgets.php
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /back/phormation/widgets.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.24 - (show annotations)
Wed Sep 19 16:51:08 2001 UTC (22 years, 7 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
Changes since 1.23: +6 -2 lines
fix strganih widgeta

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
28 include( "$phormationdir/date_fcns.php" );
29 include( "$phormationdir/misc_fcns.php" );
30 include( "$phormationdir/mywidgets.php" );
31
32
33 function make_text_widget( $name, $value, $fieldname, $errortext, $params )
34 {
35 $value = trim( $value );
36 // get all of the datatype parameters into local variables
37 // e.g. type, height, width, length, etc.
38 parse_str( $params );
39
40 if( empty( $length ) ) {
41 echo "Program error: Must specify length of field $name!!<BR>";
42 return;
43 }
44 if( empty( $height) ) {
45 $height = 1;
46 }
47 if( empty( $width ) ) {
48 $width = $length;
49 }
50 if ($name) {
51 echo "<TR>";
52 if( $required ) {
53 $star = '<font class="requiredstar"> *</font>';
54 }
55 echo "<TD class=\"widgetlabel\" valign=top>$name$star</TD>";
56 }
57 if( $height <= 1 ) {
58 if ($name) echo '<TD>';
59 echo '<input type="text" name="' . $varprefix . $fieldname . '"';
60 echo ' value="' . ($phescape ? HTMLSpecialChars( $value ) : $value) . '" size="' . $width . '"';
61 if ($maxlength) echo ' maxlength="' . $length . '"';
62 echo '>';
63 if ($name) echo '</TD>';
64 } else {
65 if (!$wrap) $wrap = "off";
66 if ($name) echo '<TD>';
67 echo '<textarea name="' . $varprefix . $fieldname . '" rows="' . $height .'" cols="' . $width . '" wrap="' . $wrap . '">';
68 echo ($phescape ? HTMLSpecialChars( $value ) : $value);
69 echo "</textarea>";
70 if ($name) echo '</TD>';
71 }
72 if( $name && $errortext != "" ) {
73 echo "<TD>$errortext</TD>";
74 }
75 if ($name) echo "</TR>\n";
76 }
77
78 function validate_text_widget( $name, $value, $fieldname, $params, $sql, $errortext )
79 {
80 // get all of the datatype parameters into local variables
81 // e.g. type, height, width, length, etc.
82 parse_str( $params );
83
84 if( empty( $length ) ) {
85 echo "Program error: Must specify length of field $name!!<BR>";
86 return;
87 }
88 if( empty( $height) ) {
89 $height = 1;
90 }
91 if( empty( $width ) ) {
92 $width = $length;
93 }
94
95 if( empty( $maxlength ) ) {
96 $maxlength = -1;
97 }
98
99 // get the field value
100 $varname = $varprefix . $fieldname;
101 global $$varname;
102 $value = stripslashes( $$varname );
103
104 if( $required && $value == "" ) {
105 $errortext = "This field is required!";
106 return( false );
107 }
108
109 if( $maxlength >= 0 && strlen( $value ) > $maxlength ) {
110 $errortext = "This field is too long (it is " . strlen( $value ) . " characters, but the maximum is $length characters)";
111 return( false );
112 }
113
114 // put the value into the SQL strings
115 if( $value == "" ) {
116 insertvalue( $fieldname, "NULL", &$sql );
117 } else {
118 if( $noescapes ) {
119 insertvalue( $fieldname, "'" . $value . "'", &$sql );
120 } else {
121 insertvalue( $fieldname, "'" . addslashes( $value ) . "'", &$sql );
122 }
123 if( $soundex_column ) {
124 insertvalue( $soundex_column, "'" . soundex( $value ) . "'", &$sql );
125 }
126 if( $metaphone_column ) {
127 insertvalue( $metaphone_column, "'" . metaphone( $value ) . "'", &$sql );
128 }
129 }
130 return( true ) ;
131 }
132
133 function make_email_widget( $name, $value, $fieldname, $errortext, $params )
134 {
135 $value = trim( $value );
136 // get all of the datatype parameters into local variables
137 // e.g. type, height, width, length, etc.
138 parse_str( $params );
139
140 if( empty( $length ) ) {
141 echo "Program error: Must specify length of field $name!!<BR>";
142 return;
143 }
144 if( empty( $width ) ) {
145 $width = $length;
146 }
147 echo "<TR>";
148 if( $required ) {
149 $star = '<font class="requiredstar"> *</font>';
150 }
151 echo "<TD class=\"widgetlabel\">$name$star</TD>";
152 echo '<TD><input type="text" name="' . $varprefix . $fieldname . '"';
153 echo ' value="' . $phescape ? HTMLSpecialChars( $value ) : $value . '" size="' . $width . '" maxlength="' . $length . '"></TD>';
154 if( $errortext != "" ) {
155 echo "<TD>$errortext</TD>";
156 }
157 echo "</TR>\n";
158 }
159
160 function validate_email_widget( $name, $value, $fieldname, $params, $sql, $errortext )
161 {
162 // get all of the datatype parameters into local variables
163 // e.g. type, height, width, length, etc.
164 parse_str( $params );
165
166 // get the field value
167 $varname = $varprefix . $fieldname;
168 global $$varname;
169 $value = stripslashes( $$varname );
170
171 if( $required && $value == "" ) {
172 $errortext = "This field is required!";
173 return( false );
174 }
175
176 if( strlen( $value ) > $length ) {
177 $errortext = "This field is too long (it is " . strlen( $value ) . " characters, but the maximum is $length characters)";
178 return( false );
179 }
180
181
182 if( $dns && !empty( $value ) ) {
183 $pos = strpos( $value, "@" );
184 $host = substr( $value, $pos + 1 );
185 if( ! checkdnsrr( $host, "ANY" ) ) {
186 $errortext = "Please enter a valid email address.";
187 return( false );
188 }
189 } else if( !empty( $value) && ! ereg( "[^@]+@[^\.]+(\.[^\.]+)+", $value ) ) {
190 $errortext = "Please enter a valid email address.";
191 return( false );
192 }
193
194 // put the value into the SQL strings
195 if( $value == "" ) {
196 insertvalue( $fieldname, "NULL", &$sql );
197 } else {
198 if( $noescapes ) {
199 insertvalue( $fieldname, "'" . $value . "'", &$sql );
200 } else {
201 insertvalue( $fieldname, "'" . addslashes( $value ) . "'", &$sql );
202 }
203 if( $soundex_column ) {
204 insertvalue( $soundex_column, "'" . soundex( $value ) . "'", &$sql );
205 }
206 if( $metaphone_column ) {
207 insertvalue( $metaphone_column, "'" . metaphone( $value ) . "'", &$sql );
208 }
209 }
210 return( true ) ;
211 }
212
213
214 function make_password_widget( $name, $value, $fieldname, $errortext, $params )
215 {
216 $value = trim( $value );
217 // get all of the datatype parameters into local variables
218 // e.g. type, height, width, length, etc.
219 parse_str( $params );
220
221 if( empty( $length ) ) {
222 echo "Program error: Must specify length of field $name!!<BR>";
223 return;
224 }
225 if( empty( $height) ) {
226 $height = 1;
227 }
228 if( empty( $width ) ) {
229 $width = $length;
230 }
231 echo "<TR>";
232 if( $required ) {
233 $star = '<font class="requiredstar"> *</font>';
234 }
235 echo "<TD class=\"widgetlabel\">$name$star</TD>";
236
237 echo '<TD><input type="password" name="' . $varprefix . $fieldname . '"';
238 echo ' value="' . $phescape ? HTMLSpecialChars( $value ) : $value . '" size="' . $width . '" maxlength="' . $length . '"></TD>';
239
240 if( $errortext != "" ) {
241 echo "<TD>$errortext</TD>";
242 }
243 echo "</TR>\n";
244 if( $verify ) {
245 echo "<TR>";
246 if( $required ) {
247 $star = '<font class="requiredstar"> *</font>';
248 }
249 echo "<TD class=\"widgetlabel\">$name again$star<BR>(for verification)</TD>";
250
251 echo '<TD><input type="password" name="' . $varprefix . $fieldname . '_2"';
252 echo ' value="' . $phescape ? HTMLSpecialChars( $value ) : $value . '" size="' . $width . '" maxlength="' . $length . '"></TD>';
253 echo "</TR>\n";
254 }
255 }
256
257 function validate_password_widget( $name, $value, $fieldname, $params, $sql, $errortext )
258 {
259 // get all of the datatype parameters into local variables
260 // e.g. type, height, width, length, etc.
261 parse_str( $params );
262
263 if( empty( $length ) ) {
264 echo "Program error: Must specify length of field $name!!<BR>";
265 return;
266 }
267 if( empty( $height) ) {
268 $height = 1;
269 }
270 if( empty( $width ) ) {
271 $width = $length;
272 }
273
274 // get the field value
275 $varname = $varprefix . $fieldname;
276 global $$varname;
277 $value = stripslashes( $$varname );
278
279 if( $required && $value == "" ) {
280 $errortext = "This field is required!";
281 return( false );
282 }
283
284 if( strlen( $value ) > $length ) {
285 $errortext = "This field is too long (it is " . strlen( $value ) . " characters, but the maximum is $length characters)";
286 return( false );
287 }
288
289 if( $verify ) {
290 $varname2 = $varname . "_2";
291 global $$varname2;
292 $value2 = $$varname2;
293 if( $value != $value2 ) {
294 $errortext = "The passwords do not match, please re-enter them.";
295 $value = "";
296 return( false );
297 }
298 }
299
300 // put the value into the SQL strings
301 if( $value == "" ) {
302 insertvalue( $fieldname, "NULL", &$sql );
303 } else {
304 if( $noescapes ) {
305 insertvalue( $fieldname, "'" . $value . "'", &$sql );
306 } else {
307 insertvalue( $fieldname, "'" . addslashes( $value ) . "'", &$sql );
308 }
309 }
310 return( true ) ;
311 }
312
313 function make_select_widget( $name, $value, $fieldname, $errortext, $params )
314 {
315 // get all of the datatype parameters into local variables
316 // e.g. type, height, width, length, etc.
317 parse_str( $params );
318
319 if ($name) echo "<TR>";
320 if( $required ) {
321 $star = '<font class="requiredstar"> *</font>';
322 }
323 if ($name) echo "<TD class=\"widgetlabel\">$name$star</TD>";
324 if ($name) echo '<TD>';
325 if (!$map) $map = array();
326 $var = $varprefix.$fieldname; global $$var;
327 $tmp = $$var; if (is_array($tmp)) $tmp = $tmp[0];
328 $val = $tmp;
329 if (!$val) $val = $value;
330 if (is_array($val)) $val = $val[0];
331 $val = trim( $val );
332 if( $selecttype == "horizontal_radio" ) {
333 reset( $map );
334 for( ; ; ) {
335 $string = stripslashes( current( $map ) );
336 if( $val == key( $map ) ) {
337 echo '<input type="radio" name="' . $var . '" value="' . key($map) . '" CHECKED>' . $string;
338 } else {
339 echo '<input type="radio" name="' . $var . '" value="' . key($map) . '">' . $string;
340 }
341 $rc = next( $map );
342 if( ! $rc ) {
343 break;
344 }
345 echo "&nbsp;";
346 }
347 } else if( $selecttype == "vertical_radio" ) {
348 reset( $map );
349 for( ; ; ) {
350 $string = stripslashes( current( $map ) );
351 if( $val == key( $map ) ) {
352 echo '<input type="radio" name="' . $var . '" value="' . key($map) . '" CHECKED>' . $string . "<BR>";
353 } else {
354 echo '<input type="radio" name="' . $var . '" value="' . key($map) . '">' . $string . "<BR>";
355 }
356 $rc = next( $map );
357 if( ! $rc ) {
358 break;
359 }
360 }
361 } else if( $selecttype == "dropdown" ) {
362 echo '<select name="' . $varprefix . $fieldname . '"';
363 if ($dorefresh) {
364 echo ' onchange="mainform.refresh.value=\'redraw\';';
365 if ($noid) echo 'mainform.record_id.value=\'\';';
366 echo 'mainform.submit();"';
367 }
368 echo '>';
369 reset( $map );
370 for( ; ; ) {
371 $string = stripslashes( current( $map ) );
372 if( $val == key($map) ) {
373 echo '<option selected value="' . key($map) . '">' . $string . '</option>';
374 } else {
375 echo '<option value="' . key($map) . '">' . $string . '</option>';
376 }
377 echo "&nbsp;";
378 $rc = next( $map );
379 if( ! $rc ) {
380 break;
381 }
382 }
383 echo '</select>';
384 if ($dorefresh) echo '<input type="submit" name="refresh" value="Refresh">';
385 }
386 if ($name) echo "</TD>";
387
388 if( $name && $errortext != "" ) {
389 echo "<TD>$errortext</TD>";
390 }
391 if ($name) echo "</TR>\n";
392 }
393
394 function validate_select_widget( $name, $value, $fieldname, $params, $sql, $errortext )
395 {
396 // get all of the datatype parameters into local variables
397 // e.g. type, height, width, length, etc.
398 parse_str( $params );
399
400 // get the field value
401 $varname = $varprefix . $fieldname;
402 global $$varname;
403 $value = $$varname;
404
405 if( $required && $value == "" ) {
406 $errortext = "This field is required!";
407 return( false );
408 }
409
410 // put the value into the SQL strings
411 if( $value == "" ) {
412 insertvalue( $fieldname, "NULL", &$sql );
413 } else {
414 // check if the value is an int or a string
415 $n = intval($value);
416 if( "$n" == $value ) {
417 // the value is an int
418 insertvalue( $fieldname, $value, &$sql );
419 } else {
420 // the value is a string
421 insertvalue( $fieldname, "'" . $value . "'", &$sql );
422 }
423 }
424 return( true ) ;
425 }
426
427 function make_help_widget( $name, $value, $fieldname, $errortext, $params )
428 {
429 global $FORMBGCOLOUR;
430
431 $value = trim( $value );
432 // get all of the datatype parameters into local variables
433 // e.g. type, height, width, length, etc.
434 parse_str( $params );
435
436 echo '</table>';
437 echo stripslashes( $html );
438 echo '<table border="0" cellspacing="1" cellpadding="0" bgcolor="'. $FORMBGCOLOUR . '">';
439 }
440
441 function validate_help_widget( $name, $value, $fieldname, $params, $sql, $errortext )
442 {
443 return true;
444 }
445
446 function make_readonly_widget( $name, $value, $fieldname, $errortext, $params )
447 {
448 $value = trim( $value );
449 // get all of the datatype parameters into local variables
450 // e.g. type, height, width, length, etc.
451 parse_str( $params );
452
453 echo "<tr>";
454 echo "<TD class=\"widgetlabel\">$name</TD>";
455 echo "<td>&nbsp;" ;
456 echo $phescape ? HTMLSpecialChars( $value ) : $value;
457 echo "</td></tr>";
458 make_hidden_widget( $name, $value, $fieldname, $errortext, $params );
459 }
460
461 function validate_readonly_widget( $name, $value, $fieldname, $params, $sql, $errortext )
462 {
463 // get all of the datatype parameters into local variables
464 // e.g. type, height, width, length, etc.
465 parse_str( $params );
466
467 $rc = validate_hidden_widget( $name, &$value, $fieldname, $params, &$sql, &$errortext );
468 return( $rc );
469 }
470
471 function make_hidden_widget( $name, $value, $fieldname, $errortext, $params )
472 {
473 $value = trim( $value );
474 // get all of the datatype parameters into local variables
475 // e.g. type, height, width, length, etc.
476 parse_str( $params );
477
478 echo '<tr><td><input type="hidden" name="' . $varprefix . $fieldname . '"';
479 echo ' value="';
480 echo $phescape ? HTMLSpecialChars( $value ) : $value;
481 echo '"></td></tr>';
482 }
483
484 function validate_hidden_widget( $name, $value, $fieldname, $params, $sql, $errortext )
485 {
486 // get all of the datatype parameters into local variables
487 // e.g. type, height, width, length, etc.
488 parse_str( $params );
489
490 // get the field value
491 $varname = $varprefix . $fieldname;
492 global $$varname;
493 $value = $$varname;
494
495 // put the value into the SQL strings
496 if( $value == "" ) {
497 insertvalue( $fieldname, "NULL", &$sql );
498 } else {
499 if( $noescapes ) {
500 insertvalue( $fieldname, "'" . $value . "'", &$sql );
501 } else {
502 insertvalue( $fieldname, "'" . addslashes( $value ) . "'", &$sql );
503 }
504 }
505 return( true ) ;
506 }
507
508 function make_date_widget( $name, $value, $fieldname, $errortext, $params )
509 {
510 global $full_months;
511
512 parse_str( $params );
513 if( empty( $yearspan) ) {
514 $yearspan = 5;
515 }
516 $value = trim( $value );
517 if( $value == "" || $value == "NULL" ) {
518 $array["day"] = "";
519 $array["month"] = -1;
520 $array["year"] = -1;
521 } elseif ( strtolower($value) == "now" ) {
522 $array["day"] = date("j");
523 $array["month"] = date("n");
524 $array["year"] = date("Y");
525 } else {
526 $array = parse_date( $value );
527 }
528
529 if( $required ) {
530 $star = '<font class="requiredstar"> *</font>';
531 }
532 ?>
533 <TR>
534 <TD class="widgetlabel"><? echo $name . $star ?></TD>
535 <TD>
536 <? //echo $array["day"] . " " . $array["month"] . " " . $array["year"] ?>
537 <select name="<? echo $varprefix . $fieldname . "_month" ?>">
538 <?
539 if( $array["month"] == -1 ) {
540 echo '<option selected value="NULL">--------</option>';
541 } else {
542 echo '<option value="NULL">--------</option>';
543 }
544 for( $i = 1; $i <= 12; $i++ ) {
545 if( $i == $array["month"] ) {
546 echo '<option selected value="' . $i . '">' . $full_months[$i] . '</option>';
547 } else {
548 echo '<option value="' . $i . '">' . $full_months[$i] . '</option>';
549 }
550 }
551 ?>
552 </select>
553 <input type="text" name="<? echo $varprefix . $fieldname . "_day"?>" value="<? echo $array["day"] ?>" size="2" maxlength="2">
554 <?
555 if( $allyears ) {
556 if( $array["year"] == -1 ) {
557 $array["year"] = "";
558 }
559 echo '<input type="text" name="' . $varprefix . $fieldname . '_year"';
560 echo ' value="' . $array["year"] . '" size="4" maxlength="4">';
561 } else {
562 echo '<select name="' . $varprefix . $fieldname . '_year">';
563 $year = (int) date( "Y", time() );
564 $year -= floor( $yearspan / 2 );
565 if( $array["year"] == -1 ) {
566 echo '<option selected value="NULL">----</option>';
567 } else {
568 echo '<option value="NULL">----</option>';
569 }
570 for( $i = 0; $i < $yearspan; $i++ ) {
571 if( $year == $array["year"] ) {
572 // make the proper year selected
573 echo '<option selected value="' . $year . '" >' . $year . '</option>';
574 } else {
575 echo '<option value="' . $year . '" >' . $year . '</option>';
576 }
577
578 $year += 1;
579 }
580 echo '</select>';
581 }
582 ?>
583 </TD>
584 <?
585 if( $errortext != "" ) {
586 echo "<TD>$errortext</TD>";
587 }
588 echo "</TR>";
589 }
590
591 function validate_date_widget( $name, $value, $fieldname, $params, $sql, $errortext )
592 {
593 // get all of the datatype parameters into local variables
594 // e.g. type, height, width, length, etc.
595 parse_str( $params );
596
597 // get the field value
598 $varname = $varprefix . $fieldname . "_year";
599 global $$varname;
600 $year = $$varname;
601 $varname = $varprefix . $fieldname . "_month";
602 global $$varname;
603 $month = $$varname;
604 $varname = $varprefix . $fieldname . "_day";
605 global $$varname;
606 $day = $$varname;
607
608 $day = trim( $day );
609 //echo "Year = $year<BR>";
610 //echo "Month = $month<BR>";
611 //echo "Day = $day<BR>";
612
613 if( $year == "NULL" && $month == "NULL" && $day == "" ) {
614 $value = "NULL";
615 } else if( $year == "" || $year == "NULL" || $month == "NULL" || $day == "" ) {
616 $value = "NULL";
617 $errortext = "The year/month/day components must all be set or all left blank.";
618 return( false );
619 } else {
620 $value = $year . "-" . $month . "-" . $day;
621 $gooddate = checkdate( $month, $day, $year );
622 if( ! $gooddate ) {
623 $errortext = "$value is not a valid date.";
624 //$value = "NULL";
625 return( false );
626 }
627 }
628
629 if( $required && $value == "NULL" ) {
630 $errortext = "This field is required!";
631 return( false );
632 }
633
634 // put the value into the SQL strings
635 if( $value == "NULL" ) {
636 insertvalue( $fieldname, "NULL", &$sql );
637 } else {
638 insertvalue( $fieldname, "'$value'", &$sql );
639 }
640 return( true ) ;
641 }
642
643
644
645 // this "widget" assumes that the time is stored in a text
646 // field in the database and looks like "10:30 p.m."
647 function make_timetext_widget( $name, $value, $fieldname, $errortext, $params )
648 {
649 trim( $value );
650 if( $value == "" || $value == "NULL" ) {
651 $time = "";
652 $mode = "";
653 } else {
654 $time = trim( substr( $value, 0, 5 ) );
655 $mode = trim( substr( $value, 5, 5 ) );
656 }
657 if( $required ) {
658 $star = '<font class="requiredstar"> *</font>';
659 }
660 echo "<TR>";
661 echo "<TD class=\"widgetlabel\">$name$star</TD>";
662 echo '<TD><input type="text" name="' . $varprefix . $fieldname . '"';
663 echo 'value="' . $time . '" size="5" maxlength="5">';
664 echo '<select name="' . $varprefix . $fieldname . "_mode" . '">';
665 if( $mode == "" ) {
666 echo '<option selected value="NULL">----</option>';
667 } else {
668 echo '<option value="NULL">----</option>';
669 }
670 if( $mode == "a.m." ) {
671 echo '<option selected value="am">a.m.</option>';
672 } else {
673 echo '<option value="am">a.m.</option>';
674 }
675 if( $mode == "p.m." ) {
676 echo '<option selected value="pm">p.m.</option>';
677 } else {
678 echo '<option value="pm">p.m.</option>';
679 }
680 echo "</select></TD>";
681 if( $errortext != "" ) {
682 echo "<TD>$errortext</TD>";
683 }
684 echo "</TR>\n";
685 }
686
687 function validate_timetext_widget( $name, $value, $fieldname, $params, $sql, $errortext )
688 {
689 // get all of the datatype parameters into local variables
690 // e.g. type, height, width, length, etc.
691 parse_str( $params );
692
693 // get the field value
694 $varname = $varprefix . $fieldname;
695 global $$varname;
696 $time = $$varname;
697 $varname = $varprefix . $fieldname . "_mode";
698 global $$varname;
699 $mode = $$varname;
700
701 $time = trim( $time );
702
703 if( $mode == "NULL" && $time == "" ) {
704 $value = "NULL";
705 } else if( $mode == "NULL" || $time == "" ) {
706 $errortext = "The time/mode components must all be set or all left blank.";
707 return( false );
708 } else {
709 $value = $time . " ";
710 if( $mode == "am" ) {
711 $value .= "a.m.";
712 } else {
713 $value .= "p.m.";
714 }
715 }
716
717 // TODO: make sure the time is valid
718
719 if( $required && $value == "NULL" ) {
720 $errortext = "This field is required!";
721 return( false );
722 }
723
724 // put the value into the SQL strings
725 if( $value == "NULL" ) {
726 insertvalue( $fieldname, "NULL", &$sql );
727 } else {
728 insertvalue( $fieldname, "'$value'", &$sql );
729 }
730 return( true ) ;
731 }
732
733 function make_fkey_widget( $name, $value, $fieldname, $errortext, $params )
734 {
735 global $conn;
736
737 // get all of the datatype parameters into local variables
738 // e.g. type, height, width, length, etc.
739 parse_str( $params );
740
741 if( $required ) {
742 $star = '<font class="requiredstar"> *</font>';
743 }
744 echo "<TR>";
745 echo "<TD class=\"widgetlabel\">$name$star</TD>";
746 echo "<TD>";
747
748 if (! $query) {
749 $query = "select $pkey, $desc from $table";
750 } else {
751 $query = stripslashes($query);
752 }
753 $result = dbi_exec( $conn, $query );
754 if( ! $result ) {
755 echo "Error executing database query<P>";
756 return;
757 }
758 echo '<select name="' . $varprefix . $fieldname . '">';
759 $numrows = dbi_numrows( $result );
760 for( $i = 0; $i < $numrows; $i++ ) {
761 $array = dbi_fetch_array( $result, $i );
762 if( $array[$pkey] == $value ) {
763 echo '<option selected value="' . $array[$pkey] . '">' . $array[$desc] . '</option>';
764 } else {
765 echo '<option value="' . $array[$pkey] . '">' . $array[$desc] . '</option>';
766 }
767 }
768 echo '</select></td>';
769 if( $errortext != "" ) {
770 echo "<TD>$errortext</TD>";
771 }
772 echo '</tr>';
773 }
774
775 function validate_fkey_widget( $name, $value, $fieldname, $params, $sql, $errortext )
776 {
777 // get all of the datatype parameters into local variables
778 // e.g. type, height, width, length, etc.
779 parse_str( $params );
780
781 // get the field value
782 $varname = $varprefix . $fieldname;
783 global $$varname;
784 $value = $$varname;
785
786 // put the value into the SQL strings
787 insertvalue( $fieldname, $value, &$sql );
788 return( true ) ;
789 }
790
791 function make_boolean_widget( $name, $value, $fieldname, $errortext, $params )
792 {
793 // get all of the datatype parameters into local variables
794 // e.g. type, height, width, length, etc.
795 parse_str( $params );
796
797 echo "<TR>";
798 if( $required ) {
799 $star = '<font class="requiredstar"> *</font>';
800 }
801 echo "<TD class=\"widgetlabel\">$name$star</TD>";
802 if( $value == "1" || $value == "t" ) {
803 $checked = "CHECKED";
804 } else {
805 $checked = "";
806 }
807 echo '<TD><input type="checkbox" name="' . $varprefix . $fieldname . '" value="1"' . $checked . '></TD>';
808 if( $errortext != "" ) {
809 echo "<TD>$errortext</TD>";
810 }
811 echo "</TR>\n";
812 }
813
814 function validate_boolean_widget( $name, $value, $fieldname, $params, $sql, $errortext )
815 {
816 // get all of the datatype parameters into local variables
817 // e.g. type, height, width, length, etc.
818 parse_str( $params );
819
820 // get the field value
821 $varname = $varprefix . $fieldname;
822 global $$varname;
823 $value = $$varname;
824
825 global $DB;
826 if( $DB == "pgsql" ) {
827 $true="'true'"; $false="'false'";
828 } else {
829 $true="1"; $false="0";
830 }
831
832 // put the value into the SQL strings
833 if ($value == 1 || $value == "t") {
834 insertvalue( $fieldname, $true, &$sql );
835 } else {
836 insertvalue( $fieldname, $false, &$sql );
837 }
838 return( true ) ;
839 }
840
841 function make_creditexpiry_widget( $name, $value, $fieldname, $errortext, $params )
842 {
843 $value = trim( $value );
844
845 // get all of the datatype parameters into local variables
846 // e.g. type, height, width, length, etc.
847 parse_str( $params );
848
849 $month = substr( $value, 0, 2 );
850 $year = substr( $value, 3, 2 );
851
852 echo "<TR>";
853 if( $required ) {
854 $star = '<font class="requiredstar"> *</font>';
855 }
856 echo "<TD class=\"widgetlabel\">$name$star";
857 echo '<BR><font size="-1">(in MM/YY format,<BR> as it appears on your card)</font></TD>';
858 echo '<TD>';
859 echo '<input type="text" name="' . $varprefix . $fieldname . '_month"';
860 echo ' value="' . $month . '" size="2" maxlength="2">';
861 echo ' / ';
862 echo '<input type="text" name="' . $varprefix . $fieldname . '_year"';
863 echo ' value="' . $year . '" size="2" maxlength="2">';
864 echo '</TD>';
865 if( $errortext != "" ) {
866 echo "<TD>$errortext</TD>";
867 }
868 echo "</TR>\n";
869 }
870
871 function validate_creditexpiry_widget( $name, $value, $fieldname, $params, $sql, $errortext )
872 {
873 // get all of the datatype parameters into local variables
874 // e.g. type, height, width, length, etc.
875 parse_str( $params );
876
877 // get the field value
878 $varname = $varprefix . $fieldname . "_month";
879 global $$varname;
880 $month = $$varname;
881
882 $varname = $varprefix . $fieldname . "_year";
883 global $$varname;
884 $year = $$varname;
885
886 if( $required && ( empty( $month ) || empty( $year ) ) ) {
887 $errortext = "Please fill in the month and the year!";
888 return( false );
889 }
890
891 $value = "$month/$year";
892
893 // put the value into the SQL strings
894 if( $value == "" ) {
895 insertvalue( $fieldname, "NULL", &$sql );
896 } else {
897 insertvalue( $fieldname, "'" . $value . "'", &$sql );
898 }
899 return( true ) ;
900 }
901
902
903 //BEGIN phone_widget
904
905 function make_phone_widget( $name, $value, $dbfield, $errortext, $params )
906 {
907 // the following function takes the parameter string (which is the
908 // "type" string passed in to form_main, with some variables added by
909 // Phormation), and creates local a variable containing the parameter
910 // for each key/value pair in the string (it's a PHP function)
911 parse_str( $params );
912
913 // if the value is empty, initialize our variables to empty
914 if( $value == "" ) {
915 $areacode = "";
916 $prefix = "";
917 $number = "";
918 } else {
919 // otherwise chunk up the string into the three parts
920 // the string may contain spaces (see the validate function)
921 // so after we split it up we trim the spaces.
922 $areacode = trim( substr( $value, 0 , 3 ) );
923 $prefix = trim( substr( $value, 3 , 3 ) );
924 $number = trim( substr( $value, 6 , 4 ) );
925 }
926
927 if( $required ) {
928 $star = '<font class="requiredstar"> *</font>';
929 }
930
931 // output the widget label
932 ?>
933 <TR>
934 <TD class="widgetlabel"><? echo $name . $star ?></TD>
935 <?
936
937 echo "<TD>";
938
939 // make the area code field. The Phormation convention is that input
940 // names are made up of the variables $varprefix and $dbfield, followed
941 // by some suffix if the widget uses multiple input fields.
942 // $varprefix is part of the "params" string passed to the widget, and
943 // must be used so that the widget will work properly in multiforms.
944 // (so that the input names don't collide)
945 echo '(';
946 echo '<input type="text" size="3" maxlength="3" name="' . $varprefix . $dbfield . '_areacode" ';
947 echo 'value="' . $areacode . '">';
948
949 echo ')&nbsp;-&nbsp;';
950
951 // likewise create the prefix field
952 echo '<input type="text" size="3" maxlength="3" name="' . $varprefix . $dbfield . '_prefix" ';
953 echo 'value="' . $prefix . '">';
954
955 echo '&nbsp;-&nbsp;';
956
957 // and the number field
958 echo '<input type="text" size="4" maxlength="4" name="' . $varprefix . $dbfield . '_number" ';
959 echo 'value="' . $number . '">';
960
961 echo "</TD>";
962
963 // display an error if there is one
964 if( $errortext != "" ) {
965 echo "<TD>$errortext</TD>";
966 }
967 echo "</TR>";
968 }
969
970 function validate_phone_widget( $name, $value, $dbfield, $params, $sql, $errortext )
971 {
972 // get all of the datatype parameters into local variables
973 // e.g. type, height, width, length, etc.
974 parse_str( $params );
975
976 // construct the names of the input fields, bring
977 // the variables into the local scope
978 $varname = $varprefix . $dbfield . "_areacode";
979 global $$varname;
980 $areacode = $$varname;
981 $varname = $varprefix . $dbfield . "_prefix";
982 global $$varname;
983 $prefix = $$varname;
984 $varname = $varprefix . $dbfield . "_number";
985 global $$varname;
986 $number = $$varname;
987
988 // now do some checking on the input. Make sure no component is
989 // empty or has less than the required number of digits
990
991 if( $areacode == "" && $prefix == "" && $number == "" ) {
992 $value = "";
993 } else if( $areacode == "" || strspn( $areacode, "0123456789" ) < 3 ) {
994 // if areacode is missing or too short, prompt the user to enter it
995 $value = " " . str_pad( $prefix, 3 ) . str_pad( $number, 4 );
996 $errortext = "Please enter the area code.";
997 return( false );
998 } else if( $prefix == "" || strspn( $prefix, "0123456789" ) < 3 ) {
999 // if prefix or number is missing, prompt the user
1000 $value = $areacode . " " . str_pad( $number, 4 );
1001 $errortext = "Please enter your full phone number.";
1002 return( false );
1003 } else if( $number == "" || strspn( $number, "0123456789" ) < 4 ) {
1004 // if prefix or number is missing, prompt the user
1005 $value = $areacode . str_pad( $prefix, 3 ) . " ";
1006 $errortext = "Please enter your full phone number.";
1007 return( false );
1008 } else {
1009 $value = $areacode . $prefix . $number;
1010 }
1011
1012 // if the number was completely empty but the field is required,
1013 // then return an error
1014 if( $required && $value == "" ) {
1015 $errortext = "This field is required!";
1016 return( false );
1017 }
1018
1019 // if we're here then we validated ok, so store the phone number
1020 //
1021 if( $value == "" ) {
1022 insertvalue( $dbfield, "NULL", &$sql );
1023 } else {
1024 insertvalue( $dbfield, "'$value'", &$sql );
1025 }
1026 return( true ) ;
1027 }
1028
1029 //END phone_widget
1030
1031
1032 function make_file_widget( $name, $value, $fieldname, $errortext, $params )
1033 {
1034 // get all of the datatype parameters into local variables
1035 // e.g. type, height, width, length, etc.
1036 parse_str( $params );
1037
1038 $filefield = strtok( $fieldname, "," ); // the filename is stored in this field
1039 $originalname = strtok( "," ); // the original (as uploaded) filename is stored here
1040
1041 //echo "<TD>" . $filefield . "</TD>";
1042 //echo "<TD>" . $value[$filefield] . "</TD>";
1043 $value[$filefield] = trim( $value[$filefield] );
1044 $value[$originalname] = trim( $value[$originalname] );
1045
1046 // create a global variable which holds the current filename (and full
1047 // path). This file might be a temp file or already be in the target directory
1048 $currentfile = $varprefix . $filefield . "_current";
1049 global $$currentfile;
1050
1051 // the stored file, if set, is the full path of the file that was
1052 // previously stored. We have to remember the filename so that we can
1053 // delete it if it is replaced by an uploaded file
1054 $storedfile = $varprefix . $filefield . "_stored";
1055 global $$storedfile;
1056 global $record_id;
1057
1058 if( $$currentfile == "" && $value[$filefield] != "" ) {
1059 $$currentfile = $targetdir . "/" . $value[$filefield];
1060 }
1061
1062 if( empty( $noclear ) ) {
1063 ?>
1064 <script type="text/javascript" language="JavaScript">
1065 <!-- // begin
1066 function clear<? echo $filefield ?>()
1067 {
1068 document.mainform.<? echo $varprefix . $filefield . "_clear" ?>.value = 'clear';
1069 document.mainform.refresh.value = 'refresh';
1070 document.mainform.submit();
1071 // return false;
1072 }
1073 // end -->
1074 </script>
1075 <?
1076 }
1077
1078 echo "<TR>";
1079 if( $required ) {
1080 $star = '<font class="requiredstar"> *</font>';
1081 }
1082 echo "<TD class=\"widgetlabel\">$name$star";
1083 //echo "Currentfile = " . $$currentfile;
1084 echo "</TD>";
1085 echo '<td>';
1086 echo '<table><tr>';
1087 if( $imagemode && ( dirname( $$currentfile ) == $targetdir ) ) {
1088 // image name must be encoded, since it may contain spaces or
1089 // other nasties which browsers don't like
1090 $imgref = $serverdir . "/" . rawurlencode( basename( $$currentfile ));
1091 echo '<td><img height="60" src="' . $imgref . '"></td>';
1092 } else if( $imagemode && ! empty( $value[$originalname] ) ) {
1093 echo '<td>' . $value[$originalname] . '<BR> has been uploaded.</TD>';
1094 }
1095 echo '<td>';
1096 if( $showfilename ) {
1097 echo 'Current File: ';
1098 if( $value[$originalname] == "" ) {
1099 echo 'none';
1100 } else {
1101 echo $value[$originalname];
1102 }
1103 }
1104 echo '<BR>';
1105 echo '<input type="file" name="' .$varprefix . $filefield . '">';
1106 echo '<input type="hidden" name="' .$varprefix . $filefield . '_current" value="' . $$currentfile . '">';
1107 echo '<input type="hidden" name="' .$varprefix . $filefield . '_oldfilename" value="' . $value[$filefield] . '">';
1108 echo '<input type="hidden" name="' .$varprefix . $filefield . '_oldoriginalname" value="' . $value[$originalname] . '">';
1109 echo '<input type="hidden" name="' .$varprefix . $filefield . '_stored" value="' . $$storedfile . '">';
1110 if( empty( $noclear ) ) {
1111 echo '<input type="hidden" name="' .$varprefix . $filefield . '_clear" value="">';
1112 echo '&nbsp;&nbsp;<input type = "button" name="action" value = "Clear" onClick="clear' . $filefield . '()" >';
1113 }
1114 echo '</TD>';
1115 echo "</tr></table>";
1116 echo "</td>";
1117 if( $errortext != "" ) {
1118 echo "<TD>$errortext</TD>";
1119 }
1120 echo "</tr>";
1121 }
1122
1123 function validate_file_widget( $name, $value, $fieldname, $params, $sql, $errortext )
1124 {
1125 global $HTTP_POST_FILES;
1126
1127 parse_str( $params );
1128
1129 $filefield = strtok( $fieldname, "," ); // the filename is stored in this field
1130 $originalname = strtok( "," ); // the original (as uploaded) filename is stored here
1131
1132 // get the field value
1133 $varname = $varprefix . $filefield;
1134 global $$varname;
1135 $tmpfilename = $$varname;
1136 $varname = $varprefix . $filefield . "_name";
1137 global $$varname;
1138 $filename = basename( $$varname );
1139 $currentfile = $varprefix . $filefield . "_current";
1140 $storedfile = $varprefix . $filefield . "_stored";
1141 global $$currentfile, $$storedfile;
1142 $varname = $varprefix . $filefield . "_oldoriginalname";
1143 global $$varname;
1144 $oldoriginalname = $$varname;
1145 $varname = $varprefix . $filefield . "_oldfilename";
1146 global $$varname;
1147 $oldfilename = $$varname;
1148 $varname = $varprefix . $filefield . "_clear";
1149 global $$varname;
1150 $clear = $$varname;
1151 $varname = $varprefix . $filefield . "_size";
1152 global $$varname;
1153 $filesize = $$varname;
1154
1155 $value = array();
1156
1157 if( !empty( $max_file_size) && $filesize > $max_file_size ) {
1158 $errortext = "File too large!<BR>File cannot be larger than " . floor($max_file_size / 1024) . "k.";
1159 $tmpfilename = "none";
1160 return( false );
1161 }
1162
1163 if( $clear == "clear" ) {
1164 // if the current file is a tmp file...
1165 if( $$currentfile != "" && dirname( $$currentfile ) != $targetdir ) {
1166 // then delete it
1167 if( is_file_in_tmp( $$currentfile ) ) {
1168 unlink( $$currentfile );
1169 }
1170 } else {
1171 // otherwise remember to delete the stored file
1172 $$storedfile = $$currentfile;
1173 }
1174 $$currentfile = "";
1175 $value[$filefield] = "";
1176 $value[$originalname] = "";
1177 return( false );
1178 }
1179 if( $tmpfilename != "none" && $tmpfilename != "" ) {
1180 $succ = @rename( $tmpfilename, $tmpfilename . ".bak" );
1181 if( ! $succ ) {
1182 //echo "Error renaming uploaded file $tmpfilename to $tmpfilename.bak";
1183 }
1184 //echo "currentfile " . $$currentfile . "<BR>";
1185 //echo "dirname " . dirname( $$currentfile ) . "<BR>";
1186 //echo "targetdir " . $targetdir . "<BR>";
1187
1188 // here we delete the previous current file, if it's not in the target directory
1189 if( $$currentfile != "" && dirname( $$currentfile ) != $targetdir ) {
1190 unlink( $$currentfile );
1191 } else {
1192 $$storedfile = $$currentfile;
1193 }
1194
1195 $$currentfile = $tmpfilename . ".bak";
1196
1197 // here we call the filter, which does any processing it needs
1198 // to on the current file, and also returns the target filename
1199 // for this file
1200 if( function_exists( $process_hook ) ) {
1201 $value[$filefield] = $process_hook( $processparm, $tmpfilename . ".bak", $filename, $targetdir );
1202 } else {
1203 $value[$filefield] = $filename;
1204 }
1205 $value[$originalname] = $filename;
1206
1207 } else {
1208 $value[$filefield] = $oldfilename;
1209 $value[$originalname] = $oldoriginalname;
1210 }
1211
1212 //$errortext = $tmpfilename;
1213 if( $required && ( $value[$filefield] == "none" || $value[$filefield] == "" ) ) {
1214 $errortext = "Please select a file";
1215 return( false );
1216 }
1217
1218 // put the value into the SQL strings
1219 if( $value != "none" ) {
1220 if( $noescapes ) {
1221 insertvalue( $filefield, "'" . $value[$filefield] . "'", &$sql );
1222 if ($originalname != $filefield)
1223 insertvalue( $originalname, "'" . $value[$originalname] . "'", &$sql );
1224 } else {
1225 insertvalue( $filefield, "'" . addslashes( $value[$filefield] ) . "'", &$sql );
1226 if ($originalname != $filefield)
1227 insertvalue( $originalname, "'" . addslashes( $value[$originalname] ) . "'", &$sql );
1228 }
1229 }
1230 return( true ) ;
1231 }
1232
1233 function commit_file_widget( $name, $value, $fieldname, $params, $record_id )
1234 {
1235 parse_str( $params );
1236
1237 $filefield = strtok( $fieldname, "," ); // the filename is stored in this field
1238 $originalname = strtok( "," ); // the original (as uploaded) filename is stored here
1239
1240 $currentfile = $varprefix . $filefield . "_current";
1241 $storedfile = $varprefix . $filefield . "_stored";
1242 global $$currentfile, $$storedfile;
1243
1244 //echo "In COMMIT:<BR>";
1245 //echo "filename = " . $value[$filefield] . "<BR>";
1246 //echo "currentfile = " . $$currentfile . "<BR>";
1247 //echo "storedfile = " . $$storedfile . "<BR>";
1248
1249 // delete the storedfile (if any) first, since if the new file has the
1250 // same name we must do this before copying the new file to the same
1251 // location (otherwise we copy the new file and then delete it)
1252 if( ! empty( $$storedfile ) ) {
1253 $file = $targetdir . "/" . basename( $$storedfile );
1254 //echo "Deleting " . $file . "<BR>";
1255 unlink( $file );
1256 }
1257 if( $$currentfile != "" && dirname( $$currentfile ) != $targetdir ) {
1258 if( is_file_in_tmp( $$currentfile ) ) {
1259
1260 $rc = copy( $$currentfile, $targetdir . "/" . $value[$filefield] );
1261 unlink( $$currentfile );
1262 }
1263 }
1264 }
1265
1266 function abort_file_widget( $name, $value, $fieldname, $params, $record_id )
1267 {
1268 parse_str( $params );
1269
1270 $filefield = strtok( $fieldname, "," ); // the filename is stored in this field
1271 $originalname = strtok( "," ); // the original (as uploaded) filename is stored here
1272
1273 $currentfile = $varprefix . $filefield . "_current";
1274 $storedfile = $varprefix . $filefield . "_stored";
1275 global $$currentfile, $$storedfile;
1276
1277 //echo "In ABORT:<BR>";
1278 //echo "filename = " . $value[$filefield] . "<BR>";
1279 //echo "currentfile = " . $$currentfile . "<BR>";
1280 //echo "storedfile = " . $$storedfile . "<BR>";
1281
1282 if( $$currentfile != "" && dirname( $$currentfile ) != $targetdir ) {
1283 if( is_file_in_tmp( $$currentfile ) ) {
1284 unlink( $$currentfile );
1285 }
1286 }
1287 }
1288
1289 function is_file_in_tmp( $filename )
1290 {
1291 // first figure out the tmp dir used by PHP to store uploaded files
1292 $tmpdir = get_cfg_var( "upload_tmp_dir" );
1293 if( empty( $tmpdir ) ) {
1294 $tmpdir = getenv( "TMPDIR" );
1295 }
1296 if( empty( $tmpdir ) ) {
1297 $tmpdir = "/tmp";
1298 }
1299
1300 // trim any trailing slash
1301 if( $tmpdir[strlen($tmpdir) - 1] == '/' ) {
1302 $tmpdir = substr( $tmpdir, 0, strlen($tmpdir) - 1 );
1303 }
1304
1305 $tmpfile = $tmpdir . "/" . basename( $filename );
1306 return( $tmpfile == $filename );
1307 }
1308 ?>

  ViewVC Help
Powered by ViewVC 1.1.26