/[corp_html]/back/phormation/mywidgets.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

Annotation of /back/phormation/mywidgets.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (hide annotations)
Fri Sep 7 12:55:43 2001 UTC (22 years, 8 months ago) by ravilov
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +48 -39 lines
Bugfixes in mydata widget.

1 ravilov 1.1 <?php
2     // --- RA:
3    
4 ravilov 1.10 function make_autodate_widget($name, $value, $fieldname, $errortext, $params) {
5     }
6     function validate_autodate_widget($name, $value, $fieldname, $params, $sql, $errortext) {
7     global $record_id;
8     parse_str($params);
9     if ($orig) $orig = $record_id ? true : false;
10     if (!$orig) insertvalue($fieldname, "NOW()", &$sql);
11     return true;
12     }
13    
14 ravilov 1.2 function make_myselect_widget($name, $value, $fieldname, $errortext, $params) {
15     global $conn;
16 ravilov 1.1 parse_str($params);
17 ravilov 1.4 $tmp = array();
18 ravilov 1.2 if ($table && $id && $caption) {
19 ravilov 1.9 $id2 = ereg_replace("^.*\.([^\.]+)$", "\\1", $id);
20     $caption2 = ereg_replace("^.*\.([^\.]+)$", '\\1', $caption);
21     $sql = "SELECT $id".($id==$id2?"":" AS $id2").", $caption".($caption==$caption2?"":" AS $caption2")." FROM $table";
22     if ($conditions) $sql .= " WHERE ($conditions)";
23     $sql .= " ORDER BY $caption";
24     while (ereg('\{([^\},]+)(,([^\}]+))?\}', $sql, $regs)) {
25     if (!$regs[1]) break;
26     $varname = $varprefix.$regs[1]; global $$varname;
27     $val = $$varname;
28     if (!$val) $val = $regs[3];
29     $sql = ereg_replace('\{'.$regs[1].'(,[^\}]+)?\}', $val, $sql);
30     }
31     $res = dbi_exec($conn, $sql);
32 ravilov 1.8 $empty = array();
33     $non_empty = array();
34 ravilov 1.2 for ($i = 0; $i < dbi_numrows($res); $i++) {
35     $ret = dbi_fetch_array($res, $i);
36 ravilov 1.9 list($val, $cap) = array($ret[$id2], $ret[$caption2]);
37 ravilov 1.4 $val = urlencode($val);
38     $cap = urlencode($cap);
39 ravilov 1.8 if ($cap) $non_empty[$val] = $cap;
40     else if ($defcap) $empty[$val] = $defcap;
41 ravilov 1.2 }
42 ravilov 1.8 while (list($val, $var) = each($empty))
43     array_push($tmp, "map[$val]=$var");
44     while (list($val, $var) = each($non_empty))
45     array_push($tmp, "map[$val]=$var");
46 ravilov 1.2 }
47 ravilov 1.4 return make_select_widget($name, $value, eregi_replace("[^A-Za-z0-9]", "_", $fieldname),
48     $errortext, $params."&".implode("&", $tmp));
49 ravilov 1.2 }
50     function validate_myselect_widget($name, $value, $fieldname, $params, $sql, $errortext) {
51     $fname = eregi_replace("[^A-Za-z0-9]", "_", $fieldname);
52 ravilov 1.3 return validate_select_widget($name, $value, $fname, $params, &$sql, $errortext);
53 ravilov 1.1 }
54    
55 ravilov 1.9 $scanned = array();
56     function load_level($id, $caption, $table, $level, $n, &$ret) {
57     global $conn, $scanned;
58     $indent = "";
59     for ($i = 0; $i < $n; $i++)
60     for ($j = 0; $j < 6; $j++) $indent .= "&nbsp;";
61     $indent .= "+&nbsp;";
62     $res = dbi_exec($conn, "SELECT $id, $caption FROM $table WHERE ($id LIKE '$level%') AND ($id <> '$level') ORDER BY $caption");
63     for ($i = 0; $i < dbi_numrows($res); $i++) {
64     $row = dbi_fetch_array($res, $i);
65     list($val, $cap) = $row;
66     if ($scanned[$val]) continue;
67     $scanned[$val] = true;
68     $val = urlencode($val);
69     $cap = urlencode($indent.$cap);
70     array_push($ret, "map[$val]=$cap");
71     load_level($id, $caption, $table, $val, $n + 1, $ret);
72     }
73     }
74     function make_mylevel_widget($name, $value, $fieldname, $errortext, $params) {
75     global $conn, $scanned;
76     parse_str($params);
77     $scanned = array();
78     $tmp = array();
79     if ($table && $id && $caption) {
80     $res = dbi_exec($conn, "SELECT $id, $caption FROM $table ORDER BY $caption");
81     for ($i = 0; $i < dbi_numrows($res); $i++) {
82     $ret = dbi_fetch_array($res, $i);
83     list($val, $cap) = array($ret[$id], $ret[$caption]);
84     if ($scanned[$val]) continue;
85     $scanned[$val] = true;
86     $val = urlencode($val);
87     $cap = urlencode($cap);
88     array_push($tmp, "map[$val]=$cap");
89     load_level($id, $caption, $table, $val, 1, $tmp);
90     }
91     }
92     return make_select_widget($name, $value, eregi_replace("[^A-Za-z0-9]", "_", $fieldname),
93     $errortext, $params."&".implode("&", $tmp));
94     }
95     function validate_mylevel_widget($name, $value, $fieldname, $params, $sql, $errortext) {
96     $fname = eregi_replace("[^A-Za-z0-9]", "_", $fieldname);
97     return validate_select_widget($name, $value, $fname, $params, &$sql, $errortext);
98     }
99    
100 ravilov 1.2 function make_mydata_widget($name, $value, $fieldname, $errortext, $params) {
101 ravilov 1.6 global $conn, $record_id;
102 ravilov 1.1 parse_str($params);
103 ravilov 1.2 $fieldname = explode(",", $fieldname);
104     if (!is_array($value)) $value = array($value);
105     if (!$query) {
106     echo "Property error: need SQL query!\n";
107     return;
108     }
109     $mainfield = explode(",", $mainfield);
110 ravilov 1.6 if (count($mainfield) != 2) {
111     echo "Property error: need the main field with its ID!\n";
112 ravilov 1.2 return;
113     }
114 ravilov 1.9 $flds2 = explode(",", $flds);
115     if (count($flds2) < 1) {
116 ravilov 1.2 echo "Property error: need at least one field!\n";
117     return;
118     }
119 ravilov 1.9 $flds = array();
120     for ($i = 0; $i < count($flds2); $i++) {
121     list($nm, $tp) = explode("/", $flds2[$i]);
122     if (!$tp) $tp = "text";
123     $flds[$nm] = $tp;
124     }
125 ravilov 1.2 $star = $required ? "<FONT CLASS=\"requiredstar\"> *</FONT>" : "";
126 ravilov 1.1 echo "<TR>";
127 ravilov 1.2 echo "<TD CLASS=\"widgetlabel\" VALIGN=\"top\">$name$star</TD>";
128     echo "<TD><TABLE BORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"1\">";
129 ravilov 1.3 while (ereg('\{([^\},]+)(,([^\}]+))?\}', $query, $regs)) {
130     if (!$regs[1]) break;
131 ravilov 1.11 $varname = $varprefix.$regs[1];
132     global $$varname;
133 ravilov 1.3 $val = $$varname;
134     if (!$val) $val = $regs[3];
135     $query = ereg_replace('\{'.$regs[1].'(,[^\}]+)?\}', $val, $query);
136     }
137 ravilov 1.5 $ids = array();
138 ravilov 1.11 if ($query) {
139     $res = dbi_exec($conn, $query);
140     for ($i = 0; $i < dbi_numrows($res); $i++) {
141     $ret = dbi_fetch_array($res, $i);
142     echo "<TR";
143     if ($ret["id"] && 0) # FIXME:
144     echo ' STYLE="font-weight:bold"';
145     echo ">";
146     echo "<TD>".$ret[$mainfield[1]]."</TD>";
147     reset($flds);
148     while (list($k, $v) = each($flds)) {
149     $foo = $ret[$k];
150     if ($map[$foo] && $v == "select")
151     $foo = $map[$foo];
152     echo "<TD>&nbsp;".$foo."</TD>";
153     }
154     echo "</TR>";
155     if ($ret["id"]) array_push($ids, $ret["id"]);
156 ravilov 1.9 }
157 ravilov 1.11 echo "<TR><TD COLSPAN=\"".(count($flds) + 1)."\"><HR WIDTH=\"100%\"></TD></TR>";
158 ravilov 1.1 }
159 ravilov 1.11 $mains = 0;
160 ravilov 1.2 if ($table && $id && $caption) {
161     $res = dbi_exec($conn, "SELECT $id, $caption FROM $table ORDER BY $caption");
162     $tmp = array();
163     for ($i = 0; $i < dbi_numrows($res); $i++) {
164     $ret = dbi_fetch_array($res, $i);
165 ravilov 1.6 if (!$record_id && count(preg_grep("/^".
166     $ret[$id]."\$/", $ids)) > 0) continue;
167 ravilov 1.11 if ($mains <= 0) echo "<TR><TD><SELECT NAME=\"".$varprefix.$mainfield[0]."\">";
168     $mains++;
169 ravilov 1.5 if ($ret[$id] == $sif) continue;
170 ravilov 1.2 echo "<OPTION VALUE=\"".$ret[$id]."\"";
171 ravilov 1.3 $val = $value[$mainfield[0]];
172     $gvar = $varprefix.$mainfield[0];
173     global $$gvar; $gval = $$gvar;
174     if ($gval) $val = $gval;
175     if (trim($val) == $ret[$id]) echo " SELECTED";
176 ravilov 1.2 echo ">".$ret[$caption]."</OPTION>";
177     }
178 ravilov 1.1 }
179 ravilov 1.11 if ($mains > 0) {
180     echo "</SELECT></TD>";
181     $s = $length ? " SIZE=\"$length\"" : "";
182     reset($flds);
183     while (list($k, $v) = each($flds)) {
184     $val = $value[$k];
185     $gvar = $varprefix.$k;
186     global $$gvar; $gval = $$gvar;
187     if ($gval) $val = $gval;
188     echo "<TD>";
189     if ($v == "text") make_text_widget("", $val, $k, "", "varprefix=$varprefix&length=$length");
190     if ($v == "select") {
191     $m2 = array();
192     reset($map);
193     while (list($kk, $vv) = each($map)) array_push($m2, "map[$kk]=$vv");
194     $m2 = implode("&", $m2);
195     make_select_widget("", $val, $k, "", "varprefix=$varprefix&selecttype=dropdown&$m2");
196     }
197     echo "</TD>";
198 ravilov 1.9 }
199 ravilov 1.11 } else echo "<TR><TD COLSPAN=\"".(count($flds) + 1)."\"><I>Sorry, all available values of this attribute are already linked.</I></TD>";
200 ravilov 1.2 echo "</TR></TABLE></TD>";
201     if ($errortext != "") echo "<TD VALIGN=\"bottom\">$errortext</TD>";
202 ravilov 1.1 echo "</TR>\n";
203     }
204 ravilov 1.2 function validate_mydata_widget($name, $value, $fieldname, $params, $sql, $errortext) {
205 ravilov 1.1 parse_str($params);
206 ravilov 1.3 $mainfield = explode(",", $mainfield);
207 ravilov 1.9 $flds2 = explode(",", $flds);
208     $flds = array();
209     for ($i = 0; $i < count($flds2); $i++) {
210     list($k, $t) = explode("/", $flds2[$i]);
211     if (!$t) $t = "text";
212     $flds[$k] = $t;
213     }
214 ravilov 1.3 $emp = false;
215 ravilov 1.9 $vars = array($mainfield[0]);
216     reset($flds);
217     while (list($k, $v) = each($flds)) array_push($vars, $k);
218 ravilov 1.3 for ($i = 0; $i < count($vars); $i++) {
219     $varname = $varprefix.$vars[$i];
220     global $$varname; $var = $$varname;
221     if (empty($var)) $emp = true;
222 ravilov 1.1 }
223 ravilov 1.9 if ($emp && $required) {
224 ravilov 1.2 $errortext = "This field is required!";
225 ravilov 1.1 return false;
226 ravilov 1.7 }
227     $numeric = explode(",", $numeric);
228     $proportion = explode(",", $proportion);
229 ravilov 1.9 reset($flds);
230     $i = 0;
231     while (list($k, $v) = each($flds)) {
232     $var = $varprefix.$k; global $$var;
233 ravilov 1.7 $val = $$var;
234     $tmp = (string)doubleval($val);
235     if ($proportion[$i]) $numeric[$i] = true;
236     if ($numeric[$i] && $val != $tmp) {
237     $errortext = "This field must be numeric!";
238     return false;
239     }
240 ravilov 1.9 $i++;
241 ravilov 1.7 }
242 ravilov 1.9 $i = 0;
243     while (list($k, $v) = each($flds)) {
244     $val = $$k;
245 ravilov 1.7 if ($proportion[$i] && ($tmp < 0 || $tmp > 1)) {
246     $errortext = "This field must be in range 0 - 1!";
247     return false;
248     }
249 ravilov 1.9 $i++;
250 ravilov 1.1 }
251 ravilov 1.3 for ($i = 0; $i < count($vars); $i++) {
252     $varname = $varprefix.$vars[$i];
253     $val = $$varname;
254 ravilov 1.9 reset($map);
255 ravilov 1.11 while (list($k, $v) = each($map))
256     if ($val == $v) $val = $k;
257 ravilov 1.9 if (!isset($val)) $val = "NULL";
258 ravilov 1.11 $tmp = intval($val);
259     if ($tmp."" != $val."") $val = "'$val'";
260 ravilov 1.9 insertvalue($vars[$i], "$val", &$sql);
261 ravilov 1.3 }
262 ravilov 1.1 return true;
263     }
264    
265     // --- DbP:
266    
267     function make_myfile_widget( $name, $value, $fieldname, $errortext, $params )
268     {
269     // get all of the datatype parameters into local variables
270     // e.g. type, height, width, length, etc.
271     parse_str( $params );
272    
273     $filefield = $fieldname; // the filename is stored in this field
274    
275     //echo "<TD>" . $filefield . "</TD>";
276     //echo "<TD>" . $value[$filefield] . "</TD>";
277     $value = trim( $value );
278    
279     // create a global variable which holds the current filename (and full
280     // path). This file might be a temp file or already be in the target directory
281     $currentfile = $varprefix . $filefield . "_current";
282     global $$currentfile;
283    
284     // the stored file, if set, is the full path of the file that was
285     // previously stored. We have to remember the filename so that we can
286     // delete it if it is replaced by an uploaded file
287     $storedfile = $varprefix . $filefield . "_stored";
288     global $$storedfile;
289     global $record_id;
290    
291     if( $$currentfile == "" && $value != "" ) {
292     $$currentfile = $targetdir . "/" . $value;
293     }
294    
295     if( empty( $noclear ) ) {
296     ?>
297     <script language="JavaScript">
298     <!--
299     function clear<? echo $filefield ?>()
300     {
301     document.mainform.<? echo $varprefix . $filefield . "_clear" ?>.value = 'clear';
302     document.mainform.refresh.value = 'refresh';
303     document.mainform.submit();
304     //return false;
305     }
306     // -->
307     </script>
308     <?
309     }
310    
311     echo "<TR>";
312     if( $required ) {
313     $star = '<font class="requiredstar"> *</font>';
314     }
315     echo "<TD class=\"widgetlabel\">$name$star";
316     //echo "Currentfile = " . $$currentfile;
317     echo "</TD>";
318     echo '<td>';
319     echo '<table><tr>';
320     if( $imagemode && ( dirname( $$currentfile ) == $targetdir ) ) {
321     // image name must be encoded, since it may contain spaces or
322     // other nasties which browsers don't like
323     $imgref = $serverdir . "/" . rawurlencode( basename( $$currentfile ));
324     echo '<td><img height="60" src="' . $imgref . '"></td>';
325     } else if( $imagemode && ! empty( $value ) ) {
326     echo '<td>' . $value . '<BR> has been uploaded.</TD>';
327     }
328     echo '<td>';
329     if( $showfilename ) {
330     echo 'Current File: ';
331     if( $value == "" ) {
332     echo 'none';
333     } else {
334     echo $value;
335     }
336     }
337     echo '<BR>';
338     echo '<input type="file" name="' .$varprefix . $filefield . '">';
339     echo '<input type="hidden" name="' .$varprefix . $filefield . '_current" value="' . $$currentfile . '">';
340     echo '<input type="hidden" name="' .$varprefix . $filefield . '_oldfilename" value="' . $value . '">';
341     echo '<input type="hidden" name="' .$varprefix . $filefield . '_stored" value="' . $$storedfile . '">';
342     if( empty( $noclear ) ) {
343     echo '<input type="hidden" name="' .$varprefix . $filefield . '_clear" value="">';
344     echo '&nbsp;&nbsp;<input type = "button" name="action" value = "Clear" onClick="clear' . $filefield . '()" >';
345     }
346     echo '</TD>';
347     echo "</tr></table>";
348     echo "</td>";
349     if( $errortext != "" ) {
350     echo "<TD>$errortext</TD>";
351     }
352     echo "</tr>";
353     }
354    
355     function validate_myfile_widget( $name, $value, $fieldname, $params, $sql, $errortext )
356     {
357     global $HTTP_POST_FILES;
358    
359     parse_str( $params );
360    
361     $filefield = $fieldname; // the filename is stored in this field
362    
363     // get the field value
364     $varname = $varprefix . $filefield;
365     global $$varname;
366     $tmpfilename = $$varname;
367     $varname = $varprefix . $filefield . "_name";
368     global $$varname;
369     $filename = basename( $$varname );
370     $currentfile = $varprefix . $filefield . "_current";
371     $storedfile = $varprefix . $filefield . "_stored";
372     global $$currentfile, $$storedfile;
373     $varname = $varprefix . $filefield . "_oldoriginalname";
374     global $$varname;
375     $oldoriginalname = $$varname;
376     $varname = $varprefix . $filefield . "_oldfilename";
377     global $$varname;
378     $oldfilename = $$varname;
379     $varname = $varprefix . $filefield . "_clear";
380     global $$varname;
381     $clear = $$varname;
382     $varname = $varprefix . $filefield . "_size";
383     global $$varname;
384     $filesize = $$varname;
385    
386     $value = array();
387    
388     if( !empty( $max_file_size) && $filesize > $max_file_size ) {
389     $errortext = "File too large!<BR>File cannot be larger than " . floor($max_file_size / 1024) . "k.";
390     $tmpfilename = "none";
391     return( false );
392     }
393    
394     if( $clear == "clear" ) {
395     // if the current file is a tmp file...
396     if( $$currentfile != "" && dirname( $$currentfile ) != $targetdir ) {
397     // then delete it
398     if( is_file_in_tmp( $$currentfile ) ) {
399     unlink( $$currentfile );
400     }
401     } else {
402     // otherwise remember to delete the stored file
403     $$storedfile = $$currentfile;
404     }
405     $$currentfile = "";
406     $value = "";
407     return( false );
408     }
409     if( $tmpfilename != "none" && $tmpfilename != "" ) {
410     $succ = @rename( $tmpfilename, $tmpfilename . ".bak" );
411     if( ! $succ ) {
412     //echo "Error renaming uploaded file $tmpfilename to $tmpfilename.bak";
413     }
414     //echo "currentfile " . $$currentfile . "<BR>";
415     //echo "dirname " . dirname( $$currentfile ) . "<BR>";
416     //echo "targetdir " . $targetdir . "<BR>";
417    
418     // here we delete the previous current file, if it's not in the target directory
419     if( $$currentfile != "" && dirname( $$currentfile ) != $targetdir ) {
420     unlink( $$currentfile );
421     } else {
422     $$storedfile = $$currentfile;
423     }
424    
425     $$currentfile = $tmpfilename . ".bak";
426    
427     // here we call the filter, which does any processing it needs
428     // to on the current file, and also returns the target filename
429     // for this file
430     if( function_exists( $process_hook ) ) {
431     $value = $process_hook( $processparm, $tmpfilename . ".bak", $filename, $targetdir );
432     } else {
433     $value = $filename;
434     }
435    
436     } else {
437     $value = $oldfilename;
438     }
439    
440     //$errortext = $tmpfilename;
441     if( $required && ( $value == "none" || $value == "" ) ) {
442     $errortext = "Please select a file";
443     return( false );
444     }
445    
446     // put the value into the SQL strings
447     if( $value != "none" ) {
448     if( $noescapes ) {
449     insertvalue( $filefield, "'" . $value . "'", &$sql );
450     } else {
451     insertvalue( $filefield, "'" . addslashes( $value ) . "'", &$sql );
452     }
453     }
454     return( true ) ;
455     }
456    
457     function commit_myfile_widget( $name, $value, $fieldname, $params, $record_id )
458     {
459     parse_str( $params );
460    
461     $filefield = $fieldname; // the filename is stored in this field
462    
463     $currentfile = $varprefix . $filefield . "_current";
464     $storedfile = $varprefix . $filefield . "_stored";
465     global $$currentfile, $$storedfile;
466    
467     //echo "In COMMIT:<BR>";
468     //echo "filename = " . $value[$filefield] . "<BR>";
469     //echo "currentfile = " . $$currentfile . "<BR>";
470     //echo "storedfile = " . $$storedfile . "<BR>";
471    
472     // delete the storedfile (if any) first, since if the new file has the
473     // same name we must do this before copying the new file to the same
474     // location (otherwise we copy the new file and then delete it)
475     if( ! empty( $$storedfile ) ) {
476     $file = $targetdir . "/" . basename( $$storedfile );
477     //echo "Deleting " . $file . "<BR>";
478     unlink( $file );
479     }
480     if( $$currentfile != "" && dirname( $$currentfile ) != $targetdir ) {
481     if( is_file_in_tmp( $$currentfile ) ) {
482    
483     $rc = copy( $$currentfile, $targetdir . "/" . $value );
484     unlink( $$currentfile );
485     }
486     }
487     }
488    
489     function abort_myfile_widget( $name, $value, $fieldname, $params, $record_id )
490     {
491     parse_str( $params );
492    
493     $filefield = $fieldname; // the filename is stored in this field
494    
495     $currentfile = $varprefix . $filefield . "_current";
496     $storedfile = $varprefix . $filefield . "_stored";
497     global $$currentfile, $$storedfile;
498    
499     //echo "In ABORT:<BR>";
500     //echo "filename = " . $value[$filefield] . "<BR>";
501     //echo "currentfile = " . $$currentfile . "<BR>";
502     //echo "storedfile = " . $$storedfile . "<BR>";
503    
504     if( $$currentfile != "" && dirname( $$currentfile ) != $targetdir ) {
505     if( is_file_in_tmp( $$currentfile ) ) {
506     unlink( $$currentfile );
507     }
508     }
509     }
510    
511     //-----------------------------------------------------------
512    
513     function make_showpic_widget( $name, $value, $fieldname, $errortext, $params )
514     {
515     $value = trim( $value );
516     // get all of the datatype parameters into local variables
517     // e.g. type, height, width, length, etc.
518     parse_str( $params );
519    
520     if( empty( $length ) ) {
521     echo "Program error: Must specify length of field $name!!<BR>";
522     return;
523     }
524     echo "<TR>";
525     echo "<TD class=\"widgetlabel\">$name</TD>";
526 ravilov 1.9 echo "<TD><img src=\"$dir/$value\" alt=\"".($phescape ? HTMLSpecialChars( $value ) : $value)."\"></TD>";
527 ravilov 1.1 echo "</TR>\n";
528     }
529    
530     function validate_showpic_widget( ) {
531     return(true);
532     }
533     ?>

  ViewVC Help
Powered by ViewVC 1.1.26