PhpLabWare version 0.3 
/includes/ -> general_inc.php

1  <?php
2 
3  // general_inc.php - functions used by general.php, user-defined tabels
4  // general.php - author: Ethan Garner, Nico Stuurman <nicost@sf.net>
5    /***************************************************************************
6    * Copyright (c) 2002 by Ethan Garner, Nico Stuurman *
7    * ------------------------------------------------------------------------ *
8    * Part of phplabware, a web-driven groupware suite for research labs *
9    * This file contains classes and functions needed in general.php. *
10    * *
11    * This program is free software; you can redistribute it and/or modify it *
12    * under the terms of the GNU General Public License as published by the *
13    * Free Software Foundation; either version 2 of the License, or (at your *
14    * option) any later version. *
15    \**************************************************************************/
16 
17  ////
18  // !Displays information on the record's owner
19  // needs to be called within a table
20  function user_entry($id,$real_tablename) {
21     global $db;
22     $ownerid=get_cell($db,"$real_tablename","ownerid","id","$id");
23     $r=$db->Execute("SELECT firstname,lastname,email FROM users WHERE id=$ownerid");
24     if ($r->fields["email"]) {
25        echo "<tr><th>Submitted by: </th><td><a href='mailto:".$r->fields["email"]."'>";
26        echo $r->fields["firstname"]." ".$r->fields["lastname"]."</a></td>\n";
27     }
28     else {
29        echo "<tr><th>Submitted by: </th><td>".$r->fields["firstname"]." ";
30        echo $r->fields["lastname"] ."</td>\n";
31     }
32     echo "<td>&nbsp;</td>";
33  }
34 
35 
36 
37  ///////////////////////////////////////////////////////////
38  ////
39  // !Prints name and date
40  // Needs to be called within a table
41  function date_entry($id,$real_tablename) {
42     global $db,$system_settings;
43 
44     $date=get_cell($db,$real_tablename,"date","id","$id");
45     $dateformat=get_cell($db,"dateformats","dateformat","id",$system_settings["dateformat"]);
46     $date=date($dateformat,$date);
47     echo "<th>Date entered: </th><td colspan=3>$date</td></tr>\n";
48     if ($lastmodby && $lastmoddate) {
49        echo "<tr>";
50        $r=$db->Execute("SELECT firstname,lastname,email FROM users WHERE id=$lastmodby");
51        if ($r->fields["email"]) {
52           echo "<tr><th>Last modified by: </th><td><a href='mailto:".$r->fields["email"]."'>";
53           echo $r->fields["firstname"]." ".$r->fields["lastname"]."</a></td>\n";
54        }
55        else {
56        echo "<tr><th>Last modified by: </th><td>".$r->fields["firstname"]." ";
57           echo $r->fields["lastname"] ."</td>\n";
58        }
59        echo "<td>&nbsp;</td>";
60        $lastmoddate=date($dateformat,$lastmoddate);
61        echo "<th>Date modified: </th><td colspan=3>$lastmoddate</td></tr>\n";
62     }
63  }
64 
65 
66  ////
67  // !Displays searchbar in table view
68  // For data of type table, recursive calls ar used
69  // The ugly stuff with HTTP_POST_VARS could be done better
70  // it would also be nicer if a string was returned instead of writing directly
71  function searchfield ($db,$tableinfo,$nowfield,$HTTP_POST_VARS,$jscript) {
72     global $USER;
73     $LAYOUT=16;
74     $column=strtok($tableinfo->fields,",");
75     while ($column) {
76        ${$column}=$HTTP_POST_VARS[$column];
77        $column=strtok(",");
78     }
79     if ($nowfield['datatype']== 'link')
80        echo "<td style='width: 10%'>&nbsp;</td>\n";
81     // datatype of column date is text (historical oversight...)
82     elseif ($nowfield['name']=='date')
83        echo "<td style='width: 10%'>&nbsp;</td>\n";
84     // datatype of column ownerid is text (historical oversight...)
85     elseif ($nowfield['name']=='ownerid') {
86         //if ($list) {
87        $rowners=$db->Execute("SELECT ownerid FROM $tableinfo->realname");
88        //$rowners=$db->Execute("SELECT ownerid FROM $tableinfo->realname WHERE $list");
89        while ($rowners && !$rowners->EOF) {
90           $ownerids[]=$rowners->fields[0];
91           $rowners->MoveNext();
92        }
93        if ($ownerids)
94            $ownerlist=implode(',',$ownerids);
95       // }
96        if ($ownerlist) {
97           $rowners2=$db->Execute("SELECT lastname,id FROM users WHERE id IN ($ownerlist)");
98            $text=$rowners2->GetMenu2("$nowfield[name]",${$nowfield[name]},true,false,0,"style='width: 80%' $jscript");
99           echo "<td style='width:10%'>$text</td>\n";
100        }
101        else
102           echo "<td style='width:10%'>&nbsp;</td>\n";
103      }
104      elseif ($nowfield['datatype']=='int' || $nowfield['datatype']=='float' || $nowfield['datatype']=='sequence' || $nowfield['datatype']=='date') {
105     echo " <td style='width: 10%'><input type='text' name='$nowfield[name]' value='".${$nowfield[name]}."'size=8 align='center'></td>\n";
106      }
107      elseif ($nowfield['datatype']== 'text' || $nowfield['datatype']=='file')
108         echo " <td style='width: 25%'><input type='text' name='$nowfield[name]' value='".${$nowfield[name]}."'size=8></td>\n";
109      elseif ($nowfield['datatype']== 'textlong')
110         echo " <td style='width: 10%'><input type='text' name='$nowfield[name]' value='".${$nowfield[name]}."'size=8></td>\n";
111      elseif ($nowfield['datatype']== 'pulldown' || $nowfield['datatype']=='mpulldown') {
112        echo "<td style='width: 10%'>";
113        if ($USER['permissions'] & $LAYOUT) {
114            $jscript2=" onclick='MyWindow=window.open (\"general.php?tablename=".$tableinfo->name."&edit_type=$nowfield[ass_t]&jsnewwindow=true&formname=$formname&selectname=$nowfield[name]".SID."\",\"type\",\"scrollbars,resizable,toolbar,status,menubar,width=600,height=400\");MyWindow.focus()'";
115            echo "<input type='button' name='edit_button' value='Edit $nowfield[label]' $jscript2><br>\n";
116        }
117        $rpull=$db->Execute("SELECT typeshort,id from $nowfield[ass_t] ORDER by sortkey,typeshort");
118        if ($rpull)
119           if ($nowfield['datatype']=='mpulldown')
120              $text=$rpull->GetMenu2("$nowfield[name]",${$nowfield[name]},true,true,0,"style='width: 80%' ");
121           else
122              $text=$rpull->GetMenu2("$nowfield[name]",${$nowfield[name]},true,false,0,"style='width: 80%' $jscript");
123        else
124            $text="&nbsp;";
125        echo "$text</td>\n";
126     }
127     elseif ($nowfield['datatype']== 'table') {
128         $ass_tableinfo=new tableinfo ($db,$nowfield['ass_table_name'],false);
129         $rasslk=$db->Execute("SELECT columnname FROM {$ass_tableinfo->desname} WHERE id={$nowfield['ass_column']}");
130         $ass_Allfields=getvalues($db,$ass_tableinfo,$rasslk->fields[0]);
131         // scary hacks, their ugliness shows that we need to reorganize some stuff
132         $ass_Allfields[0]['name']=$nowfield['name'];
133         $ass_tableinfo->fields="{$nowfield['name']}";
134         searchfield($db,$ass_tableinfo,$ass_Allfields[0],$HTTP_POST_VARS,$jscript);
135      }
136      elseif ($nowfield["datatype"]=="image")
137         echo "<td style='width: 10%'>&nbsp;</td>";
138  }
139 
140  ///////////////////////////////////////////////////////////
141  ////
142  // !Displays information in table in edit mode
143  function display_table_change($db,$tableinfo,$Fieldscomma,$pr_query,$num_p_r,$pr_curr_page,$page_array,$r=false) {
144     global $nr_records,$max_menu_length,$USER,$LAYOUT,$HTTP_SESSION_VARS;
145 
146     $first_record=($pr_curr_page - 1) * $num_p_r;
147     $current_record=$first_record;
148     $last_record=$pr_curr_page * $num_p_r;
149     if (!$r)
150        $r=$db->Execute($pr_query);
151     $r->Move($first_record);
152     if ($HTTP_SESSION_VARS['javascript_enabled']) {
153        echo "<script language='JavaScript'><!--window.name='mainwin';--></script>\n";
154     }
155     // print all entries
156     while (!($r->EOF) && $r && $current_record < $last_record) {
157        // Get required ID and title
158        $id=$r->fields["id"];
159        $title=$r->fields["title"];
160        $Allfields=getvalues($db,$tableinfo,$Fieldscomma,id,$id);
161        $may_write=may_write($db,$tableinfo->id,$id,$USER);
162 
163        // print start of row of selected record
164        if ($current_record % 2) echo "<tr class='row_even' align='center'>\n";
165           else echo "<tr class='row_odd' align='center'>\n";
166        echo "<input type='hidden' name='chgj_".$id."' value=''>\n";
167        $js="onChange='document.g_form.chgj_".$id.".value=\"Change\";document.g_form.submit()'";
168        foreach($Allfields as $nowfield) {
169           if ($nowfield[required]=='Y')
170              $thestar="<sup style='color:red'>&nbsp;*</sup>";
171           else
172              $thestar=false;
173           if ( ($nowfield['modifiable']=='N') || !$may_write) {
174              echo "<input type='hidden' name='$nowfield[name]_$id' value='$nowfield[values]'>\n";
175              echo "<td>$nowfield[text]</td>\n";
176           }
177           elseif ($nowfield['datatype']=='text') {
178        echo "<td><input type='text' name='$nowfield[name]_$id' value='$nowfield[values]' size=15 $js>$thestar</td>\n";
179           }
180           elseif ($nowfield['datatype']=='int' || $nowfield['datatype']=='sequence' || $nowfield['datatype']=='float') {
181        echo "<td><input type='text' name='$nowfield[name]_$id' value='$nowfield[values]' size=8 $js>$thestar</td>\n";
182           }
183           elseif ($nowfield['datatype']=='textlong') {
184        echo "<td><input type='text' name='$nowfield[name]_$id' value='$nowfield[values]' size=15>$thestar</td>\n";
185           }
186           elseif ($nowfield['datatype']=='link') {
187              echo "<td><input type='text' name='$nowfield[name]_$id' value='$nowfield[values]' size=15>$thestar</td>\n";
188           }
189           elseif ($nowfield['datatype']=='pulldown') {
190              // get previous value
191              $rp=$db->Execute("SELECT typeshort,id FROM $nowfield[ass_t] ORDER BY sortkey,typeshort");
192              $text=$rp->GetMenu2("$nowfield[name]_$id",$nowfield[values],true,false,0,$js);
193              echo "\n<td>$text $thestar</td>\n";
194           }
195           elseif ($nowfield['datatype']=='mpulldown') {
196              // get previous values
197              unset ($rp);
198              $rp=$db->Execute("SELECT typeshort,id FROM {$nowfield['ass_t']} ORDER BY sortkey,typeshort");
199              unset ($rbv);
200              unset ($valueArray);
201              $rbv=$db->Execute("SELECT typeid FROM {$nowfield['key_t']} WHERE recordid=$id");
202              while ($rbv && !$rbv->EOF) {
203                 $valueArray[]=$rbv->fields[0];
204                 $rbv->MoveNext();
205              }
206              $text=$rp->GetMenu2($nowfield['name']."_$id",$valueArray,true,true,4,$js);
207              echo "\n<td>$text $thestar</td>\n";
208           }
209           elseif ($nowfield['datatype']=='table') {
210              // only display primary key here
211              if (!$nowfield['ass_local_key']) {
212                 $text=false;
213                 // get previous value
214                 if ($nowfield['ass_column_name'] && $nowfield['ass_table_name']) {
215                    $rcount=$db->Execute("SELECT COUNT(id) FROM {$nowfield['ass_table_name']}");
216                    if ($rcount && ($rcount->fields[0] < $max_menu_length))
217                       $text=GetValuesMenu($db,"{$nowfield['name']}_$id",$nowfield['values'],$nowfield['ass_table_name'],$nowfield['ass_column_name'],false,$js);
218                    else {
219  //print_r ($nowfield);
220  //echo ".<br>\n";
221                       $text="<input type='hidden' name='max_{$nowfield['name']}_$id' value='true'>\n";
222                       $text.="<input type='text' name='{$nowfield['name']}_$id' value='{$nowfield['text']}'>\n<br>";
223                    }
224                 }
225                 echo "<td>$text $thestar</td>\n";
226              }
227              else
228                 echo "<td>$nowfield[text] $thestar</td>\n";
229           }
230           elseif ($nowfield['datatype']=='textlarge') {
231           echo "<td colspan=6><textarea name='$nowfield[name]_$id' rows='5' cols='100%'>$nowfield[values]</textarea>$thestar</td>\n";
232           }
233           else
234              echo "<td>$nowfield[text]</td>\n";
235        }
236 
237        echo "<td align='center'>&nbsp;\n";
238        if ($HTTP_SESSION_VARS['javascript_enabled']) {
239           $jscript=" onclick='MyWindow=window.open (\"general.php?tablename=".$tableinfo->name."&showid=$id&jsnewwindow=true\",\"view\",\"scrollbars,resizable,toolbar,status,menubar,width=700,height=500\");MyWindow.focus()'";
240           echo "<input type=\"button\" name=\"view_" . $id . "\" value=\"View\" $jscript>\n";
241        }
242        else
243           echo "<input type=\"submit\" name=\"view_" . $id . "\" value=\"View\">\n";
244        if ($may_write) {
245           echo "<input type=\"submit\" name=\"chg_" . $id . "\" value=\"Change\">\n";
246           $delstring = "<input type=\"submit\" name=\"del_" . $id . "\" value=\"Remove\" ";
247           $delstring .= "Onclick=\"if(confirm('Are you sure that you want to remove record $title?'))";
248           $delstring .= "{return true;}return false;\">";
249           echo "$delstring\n";
250        }
251        echo "</td>\n";
252        echo "</tr>\n";
253        $r->MoveNext();
254        $current_record++;
255     }
256     // Add Record button
257     if (may_write($db,$tableinfo->id,false,$USER)) {
258        echo "<tr><td colspan=20 align='center'>";
259        echo "<input type=\"submit\" name=\"add\" value=\"Add Record\">";
260        echo "</td></tr>";
261     }
262 
263     echo "</table>\n";
264     next_previous_buttons($page_array);
265     echo "</form>\n";
266  }
267 
268 
269  ///////////////////////////////////////////////////////////
270  ////
271  // !Displays all information within the table
272  function display_table_info($db,$tableinfo,$Fieldscomma,$pr_query,$num_p_r,$pr_curr_page,$page_array,$r=false) {
273     global $nr_records,$USER,$LAYOUT,$HTTP_SESSION_VARS;
274 
275     $first_record=($pr_curr_page - 1) * $num_p_r;
276     $current_record=$first_record;
277     $last_record=$pr_curr_page * $num_p_r;
278     if (!$r)
279        $r=$db->Execute($pr_query);
280     $r->Move($first_record);
281 
282     // we keep a list with fileids in the user settings
283     // these files can be seen without checking the database
284     // to be sure that only the current files can be seen, unset the entry first
285     unset($USER['settings']['fileids']);
286     
287     // print all entries
288     while (!($r->EOF) && $r && ($current_record < $last_record) ) {
289        // Get required ID and title
290        $id=$r->fields['id'];
291        $title=$r->fields['title'];
292        $Allfields=getvalues($db,$tableinfo,$Fieldscomma,id,$id);
293        // print start of row of selected group
294        if ($current_record % 2) echo "<tr class='row_odd' align='center'>\n";
295           else echo "<tr class='row_even' align='center'>\n";
296    
297        foreach($Allfields as $nowfield) {
298           if ($nowfield['link'])
299              echo "<td>{$nowfield['link']}</td>\n";
300           elseif ($nowfield['datatype']=='mpulldown')
301              echo "<td align='left' cellpadding='5%'>{$nowfield['text']}</td>\n";
302           else
303              echo "<td>{$nowfield['text']}</td>\n";
304           // write file ids to a file so that we do not need to check them again when downloading thumbnails
305           if (($nowfield['datatype']=='image' || $nowfield['datatype']=='file') && isset($nowfield['fileids'])) {
306              foreach ($nowfield['fileids'] as $fileid)
307                 $USER['settings']['fileids'][]=$fileid;
308           }
309        }
310 
311        echo "<td align='center'>&nbsp;\n";
312        if ($HTTP_SESSION_VARS['javascript_enabled']) {
313           //$jscript=" onclick='MyWindow=window.open (\"general.php?tablename=".$tableinfo->name."&showid=$id&jsnewwindow=true\",\"view\",\"status,menubar,scrollbar,resizable,width=600,height=400\");MyWindow.focus()'";
314           $jscript=" onclick='MyWindow=window.open (\"general.php?tablename=".$tableinfo->name."&showid=$id&jsnewwindow=true\",\"view\",\"status,menubar,toolbar,scrollbars,resizable,titlebar,width=700,height=500\");MyWindow.focus()'";
315           echo "<input type=\"button\" name=\"view_" . $id . "\" value=\"View\" $jscript>\n";
316        }
317        else
318           echo "<input type=\"submit\" name=\"view_" . $id . "\" value=\"View\">\n";
319        if (may_write($db,$tableinfo->id,$id,$USER)) {
320  // this works, but how do you go back from the modify window to this one???
321           if ($HTTP_SESSION_VARS['javascript_enabled']) {
322              $jscript="onclick='MyWindow=window.open (\"general.php?tablename=".$tableinfo->name."&jsnewwindow=true&modify=true&mod_".$id."=Modify\",\"modify\",\"scrollbars,resizable,status,menubar,toolbar,width=700,height=500\");MyWindow.focus()'";
323              echo "<input type=\"button\" name=\"mod_" . $id . "\" value=\"Modify\" $jscript>\n";
324           }
325           else
326              echo "<input type=\"submit\" name=\"mod_" . $id . "\" value=\"Modify\">\n";
327           $delstring = "<input type=\"submit\" name=\"del_" . $id . "\" value=\"Remove\" ";
328           $jstitle=str_replace("'"," ",$title);
329           $delstring .= "Onclick=\"if(confirm('Are you sure that you want to remove record $jstitle?'))";
330           $delstring .= "{return true;}return false;\">";
331           echo "$delstring\n";
332        }
333        echo "</td>\n";
334        echo "</tr>\n";
335        $r->MoveNext();
336        $current_record++;
337     }
338     // Add Record button
339     if (may_write($db,$tableinfo->id,false,$USER)) {
340        echo "<tr><td colspan=20 align='center'>";
341        echo "<input type=\"submit\" name=\"add\" value=\"Add Record\">";
342        echo "</td></tr>";
343     }
344 
345     echo "</table>\n";
346     next_previous_buttons($page_array);
347     echo "</form>\n";
348  }
349 
350  ///////////////////////////////////////////////////////////
351  ////
352  // !Display a record in a nice format
353  function display_record($db,$Allfields,$id,$tableinfo,$backbutton=true,$previousid=false,$nextid=false)
354  {
355     global $PHP_SELF, $md,$USER;
356 
357     if (!$Allfields[1]['recordid']) {
358        echo "<table border=0 align='center'>\n";
359        echo "<tr>\n<td align='center'><h3>Record not found</h3>\n</td>\n</tr>";
360        echo "<tr>\n<td align='center'>\n<button onclick='self.close();window.opener.focus();' name='Close' value='close'>Close</button></td></tr>\n";
361        echo "</table>\n";
362        exit;
363     }
364     echo "&nbsp;<br>\n";
365     echo "<table border=0 align='center'>\n";
366     $count=0;
367     echo "<tr>\n";
368     foreach ($Allfields as $nowfield) {
369        //Only show the entry when display_record is set
370        if ($nowfield[display_record]=="Y") {
371           // We display the fieldsin two columns
372           if ($count && !($count % 2))
373              echo "</tr>\n<tr>\n";
374           if ($nowfield[datatype]=="textlong") {
375              $textlarge=nl2br(htmlentities($nowfield[values]));
376              echo "<th>$nowfield[label]</th><td colspan=2>$textlarge</td>\n";
377           }
378           elseif ($nowfield['datatype']=='file' || $nowfield['datatype']=='image') {
379              $files=get_files($db,$tableinfo->name,$id,$nowfield['columnid'],0,'big');
380              if ($files) {
381                 echo "<th>$nowfield[label]:</th>\n<td colspan=5>";
382                 for ($i=0;$i<sizeof($files);$i++) {
383                    echo $files[$i]['link']."&nbsp;&nbsp;(<i>".$files[$i]['name']."</i>, ".$files[$i]['type'];
384                    echo " file, ".$files[$i]["size"].")<br>\n";
385                 }
386                 echo "<td>\n";
387              }
388              // to keep odd and even fields right
389              else
390                 $count--;
391           }
392           // most datatypes are handled in getvalues
393           else {
394              echo "<th>$nowfield[label]</th>\n";
395           if ($nowfield['link'])
396                 echo "<td colspan=2>{$nowfield['link']}</td>\n";
397              else
398                 echo "<td colspan=2>$nowfield[text]</td>\n";
399           }
400           $count++;
401        }
402     }
403     echo "</tr>\n";
404     make_link($id,$tableinfo->name);
405     show_reports($db,$tableinfo,$id);
406     if (function_exists ("plugin_display_show")){
407        plugin_display_show ($db,$Allfields,$id);
408        return $Allfields;
409     }
410     echo "<form method='post' action='$PHP_SELF?tablename=".$tableinfo->name."&".SID."'>\n";
411     echo "<input type='hidden' name='md' value='$md'>\n";
412 
413     // for organizational purpose, define buttons here:
414     // next and previous buttons
415     if ($previousid)
416        $previousbutton="<input type=\"button\" name=\"view_".$previousid."\" value=\"Previous\" onClick='MyWindow=window.open(\"general.php?tablename={$tableinfo->name}&showid=$previousid&jsnewwindow=true\",\"view\",\"scrollbars,resizable,toolbar,width=600,height=400\")'>\n";
417     if ($nextid)
418        $nextbutton="<input type=\"button\" name=\"view_".$nextid."\" value=\"Next\" onClick='MyWindow=window.open(\"general.php?tablename={$tableinfo->name}&showid=$nextid&jsnewwindow=true\",\"view\",\"scrollbars,resizable,toolbar,width=600,height=400\")'>\n";
419     // closebutton
420     $closebutton="<input type=\"button\" onclick='self.close();window.opener.focus();' name='Close' value='Close'>\n";
421     if ($backbutton) {
422        $backbutton="<input type='submit' name='submit' value='Back'>\n";
423     }
424     // modify button
425     if (may_write($db,$tableinfo->id,$id,$USER)) {
426        $modifybutton= "<input type=\"submit\" name=\"mod_" . $id . "\" value=\"Modify\">\n";
427     }
428 
429     // and now display the buttons
430     echo "</table>\n";
431     echo "<table border=0 align='center' width='100%'>\n";
432     if ($backbutton) {
433        echo "<tr>\n<td align='left'>";
434        echo " $previousbutton</td><td align='center'>$modifybutton $backbutton</td><td align='right'>$nextbutton </td>\n</tr>\n";
435     }
436     else
437        echo "<tr><td align='left'>$previousbutton &nbsp;</td><td align='center'> $modifybutton $closebutton </td><td align='right'>$nextbutton &nbsp;</td></tr>\n";
438     echo "</table>";
439  }
440 
441  ///////////////////////////////////////////////////////////
442  ////
443  // !make a nice link to the record
444  function make_link($id,$DBNAME) {
445     global $PHP_SELF,$system_settings;
446     echo "<tr><th>Link:</th><td colspan=7><a href='$PHP_SELF?tablename=$DBNAME&showid=$id&".SID;
447     //echo "'>".$system_settings["baseURL"].getenv("SCRIPT_NAME")."?tablename=$DBNAME&showid=$id</a></td></tr>\n";
448     echo "'>".$system_settings["baseURL"].$PHP_SELF."?tablename=$DBNAME&showid=$id</a></td></tr>\n";
449  }
450 
451 
452  ///////////////////////////////////////////////////////////
453  ////
454  // ! Make dropdown menu with available templates
455  // When one is chosen, open the formatted record in a new window
456  function show_reports($db,$tableinfo,$recordid=false) {
457     $r=$db->Execute("SELECT id,label FROM reports WHERE tableid=".$tableinfo->id);
458     if ($r && !$r->EOF) {
459        if ($recordid) {
460           $menu="<tr><th>Report:</th>\n";
461           $menu.="<td><select name='reportlinks' onchange='linkmenu(this)'>\n";
462           $menu.="<option value=''>---Reports---</option>\n";
463           $menu.="<option value='-1'>xml</option>\n";
464           while (!$r->EOF) {
465              $url="target "."report.php?tablename=".$tableinfo->name."&reportid=".$r->fields["id"]."&recordid=$recordid";
466              $menu.="<option value='$url'>".$r->fields["label"]."</option>\n";
467              $r->MoveNext();
468           }
469           $menu.="</select>\n";
470           $menu.="</td></tr>\n";
471        }
472        else { // for tableview reports
473           $menu="<td>Report:</td>\n";
474           $menu.="<td><select name='reportlinks' onchange='linkmenu(this)'>\n";
475           $menu.="<option value=''>---Reports---</option>\n";
476           $url="target "."report.php?tablename=".$tableinfo->name."&reportid=-1&tableview=true";
477           $menu.="<option value='$url'>xml</option>\n";
478           while (!$r->EOF) {
479              $url="target "."report.php?tablename=".$tableinfo->name."&reportid=".$r->fields["id"]."&tableview=true";
480              $menu.="<option value='$url'>".$r->fields["label"]."</option>\n";
481              $r->MoveNext();
482           }
483           $menu.="</select>\n";
484           $menu.="</td>\n";
485        }
486        echo $menu;
487     }
488  }
489 
490  ///////////////////////////////////////////////////////////
491  ////
492  // !display addition and modification form
493  function display_add($db,$tableinfo,$Allfields,$id,$namein,$system_settings) {
494     global $PHP_SELF,$md,$max_menu_length,$USER,$LAYOUT,$HTTP_POST_VARS,$HTTP_SESSION_VARS;
495     
496     $dbstring=$PHP_SELF;$dbstring.="?";$dbstring.="tablename=".$tableinfo->name."&";
497     echo "<form method='post' id='protocolform' enctype='multipart/form-data' name='subform' action='$dbstring";
498          ?><?=SID?>'><?php
499 
500     if (!$magic)
501        $magic=time();
502     echo "<input type='hidden' name='magic' value='$magic'>\n";
503     echo "<input type='hidden' name='md' value='$md'>\n";
504     echo "<table border=0 align='center'>\n";
505     if ($id) {
506        echo "<tr><td colspan=5 align='center'><h3>Modify ".$tableinfo->label." entry <i>$namein</i></h3></td></tr>\n";
507        echo "<input type='hidden' name='id' value='$id'>\n";
508     }
509     else {
510        echo "<tr><td colspan=5 align='center'><h3>New ".$tableinfo->label." entry</h3></td></tr>\n";
511     }
512     echo "<table border=0 align='center'>\n<tr align='center'>\n<td colspan=2></td>\n";
513     
514     foreach ($Allfields as $nowfield) {
515        // give plugin a chance to modify data
516        if (function_exists('plugin_display_add_pre'))
517           plugin_display_add_pre($db,$tableinfo->id,$nowfield);
518          
519        // see if display_record is set
520        if ( (($nowfield['display_record']=="Y") || ($nowfield['display_table']=='Y')) ) {
521           // To persist between multiple invocation, grab POST vars
522           if ($nowfield['modifiable']=='Y' && isset($HTTP_POST_VARS[$nowfield['name']]) && $HTTP_POST_VARS[$nowfield['name']] && isset($HTTP_POST_VARS['submit'])) {
523              $nowfield['values']=$HTTP_POST_VARS[$nowfield['name']];
524              $nowfield['text']=$HTTP_POST_VARS[$nowfield['name']];
525           }
526           if ($nowfield['modifiable']=='N' && $nowfield['datatype']!='sequence') {
527              echo "<input type='hidden' name='$nowfield[name]' value='$nowfield[values]'>\n";
528              if ($nowfield['text'] && $nowfield['text']!="" && $nowfield['text']!=" ") {
529                 echo "<tr><th>$nowfield[label]:</th>";
530                 echo "<td>$nowfield[text]";
531              }
532           }
533           elseif ($nowfield['modifiable']=='Y' && ($nowfield['datatype']=='text' || $nowfield['datatype']=='int' || $nowfield['datatype']=='float' || $nowfield['datatype']=='date')) {
534              echo "<tr><th>$nowfield[label]:";
535              if ($nowfield['required']=='Y') {
536                 echo "<sup style='color:red'>&nbsp;*</sup>";
537              }
538              echo "</th>\n";
539              if ($nowfield['datatype']=='text')
540                 $size=60;
541              else
542                 $size=10;
543        echo "<td><input type='text' name='$nowfield[name]' value='$nowfield[text]' $size>";
544           }
545           elseif ($nowfield['datatype']=='sequence') {
546           if (!$nowfield['text']) {
547           // find the highest sequence and return that plus one
548           $rmax=$db->Execute("SELECT MAX ($nowfield['name']}) AS $nowfield['name']} FROM ".$tableinfo->realname);
549           $newseq=$rmax->fields[0]+1;
550           }
551           else
552           $newseq=$nowfield['text'];
553              echo "<input type='hidden' name='$nowfield[name]' value='$newseq'>\n";
554              echo "<tr><th>$nowfield[label]:";
555              if ($nowfield['required']=='Y') {
556                 echo "<sup style='color:red'>&nbsp;*</sup>";
557           }
558           echo "</th>\n";
559              if ($nowfield['modifiable']=='N') {
560                 echo "<td>$newseq";
561           }
562           else
563        echo "<td><input type='text' name='$nowfield[name]' value='$newseq' 10>";
564           }
565           elseif ($nowfield['datatype']=='textlong') {
566              echo "<tr><th>$nowfield[label]:";
567              if ($nowfield['required']=='Y')
568                 echo "<sup style='color:red'>&nbsp;*</sup>";
569        echo "<td><textarea name='$nowfield[name]' rows='5' cols='100%' value='$nowfield[values]'>$nowfield[values]</textarea>";
570           }
571           elseif ($nowfield['datatype']=='link') {
572              echo "<tr><th>$nowfield[label] (http link):";
573              if ($nowfield['required']=='Y')
574                 echo "<sup style='color:red'>&nbsp;*</sup>";
575              echo "<td><input type='text' name='$nowfield[name]' value='$nowfield[values]' size=60>";
576           }
577           elseif ($nowfield['datatype']=='pulldown') {
578              // get previous value
579              $r=$db->Execute("SELECT typeshort,id FROM {$nowfield['ass_t']} ORDER BY sortkey,typeshort");
580              if ($nowfield['datatype']=='pulldown')
581                 $text=$r->GetMenu2("$nowfield[name]",$nowfield['values'],true,false);
582              else
583                 $text=$r->GetMenu2("$nowfield[name]",$nowfield['values'],true,true);
584              echo "<tr><th>$nowfield[label]:";
585              if ($nowfield['required']=='Y')
586                 echo"<sup style='color:red'>&nbsp;*</sup>";
587              echo "</th>\n<td>";
588              if ($USER['permissions'] & $LAYOUT) {
589                 $jscript=" onclick='MyWindow=window.open (\"general.php?tablename=".$tableinfo->name."&edit_type=$nowfield[ass_t]&jsnewwindow=true&formname=subform&selectname=$nowfield[name]".SID."\",\"type\",\"scrollbars,resizable,toolbar,status,width=700,height=500\");MyWindow.focus()'";
590                 echo "<input type='button' name='edit_button' value='Edit $nowfield[label]' $jscript><br>\n";
591              }
592              echo "$text<br>";
593           }
594           elseif ($nowfield['datatype']=='table') {
595              // only display primary key here
596              if (!$nowfield['ass_local_key']) {
597                 // get previous value
598                 $r=$db->Execute("SELECT COUNT(id) FROM {$nowfield['ass_table_name']}");
599                 if ($r->fields[0] > $max_menu_length) {
600                    $text="<input type='hidden' name='max_{$nowfield['name']}' value='true'>\n";
601                    $text.="<input type='text' name='{$nowfield['name']}' value='{$nowfield['text']}'>";
602                 }
603                 else {
604                    $text=GetValuesMenu($db,$nowfield['name'],$nowfield['values'],$nowfield['ass_table_name'],$nowfield['ass_column_name'],false);
605                 }
606                 echo "<tr><th>$nowfield[label]:";
607                 if ($nowfield[required]=="Y")
608                    echo"<sup style='color:red'>&nbsp;*</sup>";
609                 echo "</th>\n<td>$text<br>";
610              }
611           }
612           if ($nowfield['datatype']=='textlarge') {
613           echo "<tr><th>$nowfield[name]:";
614              if ($nowfield['required']=='Y')
615           echo"<sup style='color:red'>&nbsp;*</sup>";
616           echo "</th><td colspan=6><textarea name='$nowfield[name]' rows='5' cols='100%'>$nowfield[values]</textarea>";
617           }
618           if ($nowfield['datatype']=='file' || $nowfield['datatype']=='image') {
619           $files=get_files($db,$tableinfo->name,$id,$nowfield['columnid'],0,'big');
620           echo '<tr>';
621           echo "<th>$nowfield[label]:</th>\n";
622           echo "</th>\n";
623           echo '<td colspan=4> <table border=0>';
624           for ($i=0;$i<sizeof($files);$i++) {
625           echo "<tr><td colspan=2>".$files[$i]['link'];
626           echo "&nbsp;&nbsp;(<i>".$files[$i]['name']."</i>, ".$files[$i]['type']." file)</td>\n";
627           echo "<td><input type='submit' name='def_".$files[$i]["id"]."' value='Delete' Onclick=\"if(confirm('Are you sure the file ".$files[$i]["name"]." should be removed?')){return true;}return false;\"></td></tr>\n";
628           }
629              if ($files)
630           echo '<tr><th>Replace '.$nowfield['datatype']."(s) with</th>\n";
631              else
632           echo '<tr><th>Upload '.$nowfield['datatype']."</th>\n";
633           echo "<td>&nbsp;</td><td><input type='file' name='".$nowfield[name]."[]' value='$filename'></td>\n";
634           echo "</tr></table><br>\n\n";
635           }
636           elseif ($nowfield['datatype']=='mpulldown') {
637              unset ($valueArray);
638              // get previous value
639              $r=$db->Execute("SELECT typeshort,id FROM {$nowfield['ass_t']} ORDER BY sortkey,typeshort");
640              $rbv=$db->Execute("SELECT typeid FROM {$nowfield['key_t']} WHERE recordid=$id");
641              while ($rbv && !$rbv->EOF) {
642                 $valueArray[]=$rbv->fields[0];
643                 $rbv->MoveNext();
644              }
645              $text=$r->GetMenu2($nowfield['name'].'[]',$valueArray,true,true);
646              echo "<tr><th>$nowfield[label]:";
647              if ($nowfield['required']=='Y')
648                 echo"<sup style='color:red'>&nbsp;*</sup>";
649              echo "</th>\n<td>";
650              if ($USER['permissions'] & $LAYOUT) {
651                 $jscript=" onclick='MyWindow=window.open (\"general.php?tablename=".$tableinfo->name."&edit_type=$nowfield[ass_t]&jsnewwindow=true&formname=subform&selectname=$nowfield[name]".SID."\",\"type\",\"scrollbars,resizable,toolbar,status,width=700,height=500\");MyWindow.focus()'";
652                 echo "<input type='button' name='edit_button' value='Edit $nowfield[label]' $jscript><br>\n";
653              }
654              echo "$text<br>";
655           }
656        }
657        if (function_exists('plugin_display_add'))
658           plugin_display_add($db,$tableinfo->id,$nowfield);
659        echo "</td></tr>\n";
660    
661          
662     }
663     /* Call to a function that runs at the end when adding a new record*/
664     if ((function_exists("plugin_display_add_post")) && (!($id))){
665        plugin_display_add_post($db,$tableinfo->id);
666     }
667           
668     echo "<td colspan=4>";
669     show_access($db,$tableinfo->id,$id,$USER,$system_settings);
670     echo "</td></tr>\n"; echo "<tr>";
671     if ($id) $value="Modify Record";
672     else $value="Add Record";
673 
674     // submit and clear buttons
675     echo "<td colspan=7 align='center'>\n";
676     if ($HTTP_SESSION_VARS['javascript_enabled']) {
677        echo "<input type='hidden' name='subm' value=''>\n";
678        //echo "<input type='button' name='sub' value='$value' onclick='document.subform.subm.value=\"$value\"; document.subform.submit(); window.opener.document.g_form.search.value=\"Search\"; setTimeout(\"window.opener.document.g_form.submit(); window.opener.focus(); self.close()\",300);'>\n";
679        echo "<input type='button' name='sub' value='$value' onclick='document.subform.subm.value=\"$value\"; document.subform.submit(); window.opener.document.g_form.search.value=\"Search\"; window.opener.document.g_form.submit(); window.opener.focus(); '>\n";
680        echo "<input type='button' name='Close' onclick='self.close(); window.opener.focus();' value='Cancel'>\n";
681     }
682     else {
683        echo "<input type='submit' name='submit' value='$value'>\n";
684        echo "&nbsp;&nbsp;";
685        echo "<input type='submit' name='submit' value='Cancel'></td>\n";
686     }
687     echo "</tr>\n</table>\n</form>\n";
688 
689     //end of table
690     $dbstring=$PHP_SELF;$dbstring.="?";$dbstring.="tablename=$tableinfo[name]&";
691     echo "<form method='post' id='protocolform' enctype='multipart/form-data' action='$dbstring";
692  ?><?=SID?>'><?php
693 
694  }
695 
696  ///////////////////////////////////////////////////////////////////////
697  ////
698  // !Get all description table values out for a display
699  // Returns an array with lots of information on every column
700  // If qfield is set, database values for that record will be returned as well
701  function getvalues($db,$tableinfo,$fields,$qfield=false,$field=false) {
702     global $system_settings;
703     if ($qfield) {
704        $r=$db->Execute("SELECT $fields FROM $tableinfo->realname WHERE $qfield=$field");
705        $rid=$db->Execute("SELECT id FROM $tableinfo->realname WHERE $qfield=$field");
706        $id=$rid->fields['id'];
707     }
708     $columns=split(',',$fields);
709     $Allfields=array();
710     foreach ($columns as $column) {
711        if($column!="id") {
712           if ($r)
713              ${$column}['values']= $r->fields[$column];
714           $rb=$db->CacheExecute(2,"SELECT id,label,datatype,display_table,display_record,associated_table,key_table,associated_column,associated_local_key,required,link_first,link_last,modifiable FROM $tableinfo->desname WHERE columnname='$column'");
715           ${$column}['name']=$column;
716           ${$column}['columnid']=$rb->fields['id'];
717           ${$column}['label']=$rb->fields['label'];
718           ${$column}['datatype']=$rb->fields['datatype'];
719           ${$column}['display_table']=$rb->fields['display_table'];
720           ${$column}['display_record']=$rb->fields['display_record'];
721           ${$column}['ass_t']=$rb->fields['associated_table'];
722           ${$column}['key_t']=$rb->fields['key_table'];
723           ${$column}['ass_column']=$rb->fields['associated_column'];
724           ${$column}['ass_local_key']=$rb->fields['associated_local_key'];
725           ${$column}['required']=$rb->fields['required'];
726           ${$column}['modifiable']=$rb->fields['modifiable'];
727           if ($rb->fields['datatype']=='table') {
728              ${$column}['ass_table_desc_name']=get_cell($db,'tableoftables','table_desc_name','id',$rb->fields['associated_table']);
729              ${$column}['ass_table_name']=get_cell($db,'tableoftables','real_tablename','id',$rb->fields['associated_table']);
730              ${$column}['ass_column_name']=get_cell($db,${$column}['ass_table_desc_name'],'columnname','id',$rb->fields['associated_column']);
731           }
732           if ($id) {
733              ${$column}['recordid']=$id;
734              if ($rb->fields['datatype']=='table') {
735                 if ($rb->fields['associated_local_key']) {
736                    ${$column}['ass_local_column_name']=get_cell($db,$tableinfo->desname,'columnname','id',$rb->fields['associated_local_key']);
737                    ${$column}['values']=get_cell($db,$tableinfo->realname,${$column}['ass_local_column_name'],'id',$id);
738                 }
739                 $text=false;
740                 if (${$column}['values']) {
741                    $asstableinfo=new tableinfo($db,${$column}['ass_table_name']);
742                    $tmpvalue=getvalues($db,$asstableinfo,${$column}['ass_column_name'],'id',${$column}['values']);
743                    if (is_array($tmpvalue[0]))
744                       $text=$tmpvalue[0]['text'];
745                    else
746                       $text=$tmpvalue[0];
747                    ${$column}['link']="<a target=_ href=\"general.php?tablename={$asstableinfo->name}&showid=".${$column}['values']."\">$text</a>\n";
748                 }
749                 if (!$text)
750                    $text="&nbsp;";
751                 ${$column}['text']=$text;
752              }
753              elseif ($rb->fields['datatype']=='link') {
754                 if (${$column}['values'])
755                    ${$column}['text']="<a href='".${$column}["values"]."' target='_blank'>link</a>";
756              }
757              elseif ($rb->fields['datatype']=='pulldown') {
758                 ${$column}['text']=get_cell($db,${$column}['ass_t'],'typeshort','id',${$column}['values']);
759              }
760              elseif ($rb->fields['datatype']=='mpulldown') {
761                 unset($rasst);
762                 unset($rasst2);
763                 unset ($typeids);
764                 $rasst=$db->Execute("SELECT typeid FROM {${$column}['key_t']} WHERE recordid='$id'");
765                 while ($rasst && !$rasst->EOF){
766                    $typeids.=$rasst->fields[0].',';
767                    $rasst->MoveNext();
768                 }
769                 if ($typeids){
770                    $typeids=substr($typeids,0,-1);
771                    $rasst2=$db->Execute("SELECT typeshort from {${$column}['ass_t']} where id IN ($typeids) ORDER BY sortkey");
772                    while ($rasst2 && !$rasst2->EOF) {
773                       ${$column}['text'].=$rasst2->fields[0].'<br>';
774                       $rasst2->MoveNext();
775                    }
776                 }
777              }
778              elseif ($rb->fields['datatype']=='textlong') {
779                 if (${$column}['values']=="")
780                    ${$column}['text']='';
781                 else
782                    //${$column}['text']='Click on View';
783                    ${$column}['text']="<input type=\"button\" name=\"view_$id\" value=\"View\" onclick='MyWIndow=window.open(\"general.php?tablename={$tableinfo->name}&showid=$id&jsnewwindow=true\",\"view\",\"scrollbars,resizable,width=600,height=400\")'>\n";
784              }
785              elseif ($rb->fields['datatype']=='file' || $rb->fields['datatype']=='image') {
786                 $tbname=get_cell($db,'tableoftables','tablename','id',$tableinfo->id);
787                 // we can get naming conflicts here. Use a really weird name
788                 $fzsk=get_files($db,$tbname,$id,${$column}['columnid'],3);
789                 if ($fzsk)
790                    for ($i=0;$i<sizeof($fzsk);$i++) {
791                       ${$column}['text'].=$fzsk[$i]['link'];
792                       ${$column}['fileids'][]=$fzsk[$i]['id'];
793                    }
794              }
795              elseif ($rb->fields['datatype']=='user') {
796                 $rname=$db->Execute("SELECT firstname,lastname,email FROM users WHERE id=".${$column}["values"]);
797                 if ($rname && $rname->fields) {
798                    if ($rname->fields['email'])
799                       ${$column}['text']="<a href='mailto:".$rname->fields['email']."'>".$rname->fields['firstname']." ".$rname->fields['lastname']."</a>\n";
800                    else
801                       ${$column}['text']=$rname->fields['firstname']." ".$rname->fields['lastname']."\n";
802                 }
803              }
804              elseif ($rb->fields['datatype']=='date' && ${$column}['values']>0) {
805                 $dateformat=get_cell($db,'dateformats','dateformat','id',$system_settings['dateformat']);
806                 ${$column}['text']=date($dateformat,${$column}['values']);
807              }
808              else
809                 ${$column}['text']=${$column}['values'];
810 
811              if ($rb->fields['link_first'] && ${$column}['values']) {
812                 ${$column}['text']="<a href='".$rb->fields['link_first'].${$column}['text'].$rb->fields['link_last']."'>".${$column}['text']."</a>\n";
813              }
814   
815              if (! isset(${$column}['text']) || strlen(${$column}['text'])<1 )
816                 ${$column}['text']='&nbsp;';
817           }
818        }
819        array_push ($Allfields, ${$column});
820     }
821     if (function_exists("plugin_getvalues"))
822        plugin_getvalues($db,$Allfields,$id,$tableinfo->id);
823     return $Allfields;
824  }
825 
826 
827  //////////////////////////////////////////////////////////////////////
828  //// general functions
829  /****************************FUNCTIONS***************************/
830  ////
831  // !Checks input data to addition
832  // returns false if something can not be fixed
833  function check_g_data ($db,&$field_values,$tableinfo,$modify=false) {
834     global $max_menu_length;
835 
836     // make sure all the required fields are there
837     $rs = $db->Execute("SELECT columnname,datatype FROM {$tableinfo->desname} where required='Y' and (datatype != 'file')");
838     while (!$rs->EOF) {
839        $fieldA=$rs->fields[0];
840        if (!$field_values["$fieldA"]) {
841           echo "<h3 color='red' align='center'>Please enter all fields marked with a <sup style='color:red'>&nbsp;*</sup>.</h3>";
842           return false;
843        }
844        $rs->MoveNext();
845     }
846 
847     // make sure ints and floats are correct, try to set the UNIX date
848     $rs = $db->Execute("SELECT columnname,datatype,label,associated_table,associated_column FROM {$tableinfo->desname} WHERE datatype IN ('int','float','table','date','sequence')");
849     while ($rs && !$rs->EOF) {
850        $fieldA=$rs->fields[0];
851        if (isset($field_values["$fieldA"]) && (strlen($field_values[$fieldA]) >0)) {
852           if ($rs->fields[1]=='int') {
853              if ($field_values["$fieldA"]==' ')
854                 $field_values["$fieldA"]='';
855              else
856                 $field_values["$fieldA"]=(int)$field_values["$fieldA"];
857           }
858           elseif ($rs->fields[1]=='float') {
859              if ($field_values["$fieldA"]==' ')
860                 $field_values["$fieldA"]='';
861              else
862                 $field_values["$fieldA"]=(float)$field_values["$fieldA"];
863           }
864           elseif ($rs->fields[1]=='table') {
865               $field_values["$fieldA"]=(int)$field_values["$fieldA"];
866           }
867           elseif ($rs->fields[1]=='date') {
868              $field_values["$fieldA"]=strtotime($field_values["$fieldA"]);
869              if ($field_values["$fieldA"] < 0)
870                 $field_values["$fieldA"]="";
871           }
872           elseif ($rs->fields[1]=='sequence') {
873              $field_values["$fieldA"]=(int)$field_values["$fieldA"];
874           if ($field_values["$fieldA"]<1)
875           unset($field_values["$fieldA"]);
876              // for new additions, check if this number was given out before:
877           if (!$modify) {
878                 if (get_cell($db,$tableinfo->realname,$rs->fields[0],$rs->fields[0],$field_values["$fieldA"])) {
879                    $rmax=$db->Execute("SELECT max({$rs->fields[0]}) FROM {$tableinfo->realname}");
880                    if ($rmax->fields[0])
881                       $nextmax=$rmax->fields[0]+1;
882                    echo "<h3 color='red' align='center'>The number <i>{$field_values[$fieldA]}</i> has already been used in field <i>{$rs->fields[2]}</i>. ";
883                    if ($nextmax) {
884                       echo "Try <i>$nextmax</i> instead.";
885                       $field_values[$fieldA]=$nextmax;
886                    }
887                    echo "</h3>\n";
888           return false;
889           }
890              }
891           }
892 
893        }
894        $rs->MoveNext();
895     }
896 
897     // Hooray, the first call to a plugin function!!
898     if (function_exists("plugin_check_data")) {
899        if (!plugin_check_data($db,$field_values,$tableinfo->desname,$modify))
900           return false;
901     }
902 
903     return true;
904  }
905 
906 
907  ////
908  // !Prints a form with addition stuff
909  // $fields is a comma-delimited string with column names
910  // $field_values is hash with column names as keys
911  // $id=0 for a new entry, otherwise it is the id
912  function add_g_form ($db,$tableinfo,$field_values,$id,$USER,$PHP_SELF,$system_settings) {
913     if (!may_write($db,$tableinfo->id,$id,$USER))
914        return false;
915     if ($id) {
916          $Allfields=getvalues($db,$tableinfo,$tableinfo->fields,id,$id);
917          $namein=get_cell($db,$tableinfo->desname,"title","id",$id);
918          display_add($db,$tableinfo,$Allfields,$id,$namein,$system_settings);
919     }
920     else {
921          $Allfields=getvalues($db,$tableinfo,$tableinfo->fields);
922          display_add($db,$tableinfo,$Allfields,$id,"",$system_settings);
923     }
924  }
925 
926  ////
927  // !Shows a page with nice information on the record
928  function show_g($db,$tableinfo,$id,$USER,$system_settings,$backbutton=true,$previousid=false,$nextid=false) {
929     if (!may_read($db,$tableinfo,$id,$USER))
930         return false;
931     $Allfields=getvalues($db,$tableinfo,$tableinfo->fields,id,$id);
932     display_record($db,$Allfields,$id,$tableinfo,$backbutton,$previousid,$nextid);
933  }
934          
935  ////
936  // !Tries to convert a MsWord file into html
937  // It calls wvHtml.
938  // When succesfull, the file is added to the database
939  // Returns id of uploaded file
940  function process_file($db,$fileid,$system_settings) {
941     global $HTTP_POST_FILES,$HTTP_POST_VARS;
942     $mimetype=get_cell($db,"files","mime","id",$fileid);
943     if (!strstr($mimetype,"html")) {
944        $word2html=$system_settings["word2html"];
945        $wv_version=$system_settings["wvHtml_version"];
946        $filepath=file_path($db,$fileid);
947        if (!$filepath)
948           return false;
949        if ($wv_version<0.7) {
950           $temp=$system_settings["tmpdir"]."/".uniqid("file");
951           $command= "$word2html $filepath $temp";
952           $result=exec($command);
953        }
954        // version of wvHtml >= 0.7 have to be called differently:
955        //if (@is_readable($temp) || @filesize($temp) < 1) {
956        else {
957           $converted_file=uniqid("file");
958           $command="$word2html --targetdir=".$system_settings["tmpdir"]." \"$filepath\" $converted_file";
959           $result=exec($command);
960           $temp=$system_settings["tmpdir"]."/".$converted_file;
961        }
962        if (@is_readable($temp) && filesize($temp)) {
963           unset ($HTTP_POST_FILES);
964           $r=$db->query ("SELECT filename,mime,title,tablesfk,ftableid,ftablecolumnid FROM files WHERE id=$fileid");
965           if ($r && !$r->EOF) {
966              $filename=$r->fields("filename");
967              // change .doc to .html in a lousy way
968              $filename=str_replace(".doc",".htm",$filename);
969              $mime="text/html";
970              $type=substr(strrchr($mime,"/"),1);
971              $size=filesize($temp);
972              $id=$db->GenID("files_id_seq");
973              $query="INSERT INTO files (id,filename,mime,size,title,tablesfk,ftableid,ftablecolumnid,type) VALUES ($id,'$filename','$mime','$size','".$r->fields("title")."','".$r->fields("tablesfk")."','".$r->fields("ftableid")."','".$r->fields("ftablecolumnid")."','$type')";
974             if ($db->execute($query)) {
975                  $newloc=file_path($db,$id);
976                 `mv $temp '$newloc'`;
977                  return $id;
978              }
979              else
980                 unlink($temp);
981           }
982        }
983        else
984           @unlink($temp);
985     }
986     return false;
987  }
988 
989 
990  ////
991  // !Indexes the content of the given file
992  // The file is converted to a text file (pdfs with ghost script,
993  // word files were already converted to html,html characters are stripped),
994  // all words are lowercased, it is checked whether an entry in the table words
995  // already exists, if not, it is added. A relation to the word is made in
996  // the table associated with the given column
997  function indexfile ($db,$tableinfo,$indextable,$recordid,$fileid,$htmlfileid)
998  {
999     return false;
1000     if (!$indextable)
1001        return false;
1002     // if the html file exists, we'll work with that one
1003     if ($htmlfileid) {
1004        $fp=fopen(file_path($db,$htmlfileid),"r");
1005        if ($fp) {
1006           while (!feof($fp)) {
1007              $filetext.=fgetss($fp,64000);
1008           }
1009           fclose($fp);
1010        }
1011        $filetext=strtolower($filetext);
1012        doindexfile ($db,$filetext,$htmlfileid,$indextable,$recordid,$pagenr);
1013     }
1014  }
1015 
1016  ////
1017  // !Searches (nested) for a match with $value
1018  // returns the associated value by searching recursively
1019  // that can be used in a SQL search
1020  function find_nested_match($db,$tableinfo,$field,$value,$first=true) {
1021     $info=getvalues($db,$tableinfo,$field);
1022 
1023     if ($info[0]['datatype']=='table') {
1024        $ass_tableinfo=new tableinfo($db,$info[0]['ass_table_name']);
1025        $value=find_nested_match($db,$ass_tableinfo,$info[0]['ass_column_name'],$value,false);
1026     }
1027     elseif ($info[0]['datatype']=='pulldown') {
1028        $value=get_cell($db,$info[0]['ass_t'],'id','typeshort',$value);
1029        return get_cell($db,$tableinfo->realname,'id',$field,$value);
1030     }
1031     elseif (!$first) {
1032        return get_cell($db,$tableinfo->realname,'id',$field,$value);
1033     }
1034     return $value;
1035  }
1036  ?>


Generated: Sun Oct 5 21:17:35 2003 SourceForge Logo Generated by PHPXref 0.2