/includes/ -> type_inc.php
1 <?php
2
3 // type_inc.php - List, modify, delete and add entries in 'type' tables
4 // type_inc.php - author: Nico Stuurman <nicost@soureforge.net>
5 /***************************************************************************
6 * This script displays a table with protocols in phplabware. *
7 * *
8 * Copyright (c) 2001 by Nico Stuurman *
9 * ------------------------------------------------------------------------ *
10 * This program is free software; you can redistribute it and/or modify it *
11 * under the terms of the GNU General Public License as published by the *
12 * Free Software Foundation; either version 2 of the License, or (at your *
13 * option) any later version. *
14 \**************************************************************************/
15
16 ////
17 // !Generates a javascript array used to dynamically alter pulldown items
18 // after the pulldown list has been changed by the user in another window
19 function update_opener_js ($db,$table) {
20 global $HTTP_GET_VARS;
21
22 $result="<script type='text/javascript'>\n<!--\n";
23 $result.="typeinfo=new Array(\n";
24 $result.=" new Array(\n";
25 //leave first choice blank:
26 $result.=" new Array(\"\",\"\")";
27 $query = "SELECT id,typeshort FROM $table ORDER BY sortkey,typeshort";
28 $r=$db->Execute($query);
29 $rownr=0;
30 // enter all entries into array
31 while (!($r->EOF) && $r) {
32 $result.=",\n new Array(\"".$r->fields[1]."\",\"".$r->fields[0]."\")";
33 $r->MoveNext();
34 }
35 $result.="\n )\n)\n";
36 $form=$HTTP_GET_VARS['formname'];
37 $select=$HTTP_GET_VARS['selectname'];
38 $result.="fillSelectFromArray(opener.document.$form.$select,typeinfo[0])\n";
39 $result.="// End of Javascript -->\n</script>\n";
40 return $result;
41 }
42
43
44 ////
45 // !Generates the page with info on pulldown items
46 // Allows for addition, modifying and deleting pulldown items
47 function show_type ($db,$table,$name, $tablename=false) {
48 global $HTTP_POST_VARS,$PHP_SELF,$HTTP_GET_VARS;
49
50 $dbstring=$PHP_SELF.'?';
51 if ($tablename)
52 $dbstring.="tablename=$tablename&";
53 $dbstring.="edit_type=$table&";
54 // propagate the form and select name as GET variables to be able to manipulate the select list with javascript
55 $dbstring.="formname=".$HTTP_GET_VARS["formname"]."&";
56 $dbstring.="selectname=".$HTTP_GET_VARS["selectname"]."&";
57 if($HTTP_POST_VARS[type_name]) {
58 $name=$HTTP_POST_VARS[type_name];
59 }
60 echo "<form method='post' id='typeform' enctype='multipart/form-data' ";
61 echo "action='$dbstring".SID."'>\n";
62 echo "<input type='hidden' name='edit_type' value='$table'>\n";
63
64 echo "<table align='center'>\n";
65 echo "<center><h3>Edit $name</h3></center>\n";
66 echo "<tr>\n";
67 echo "<th>Name</th>";
68 echo "<th>Shortname</th>";
69 echo "<th>Sort key</th>\n";
70 echo "<th>Action</th>\n";
71 echo "</tr>\n";
72
73 // Column with new record
74 echo "<input type='hidden' name='type_name' value='$name'>\n";
75 echo "<tr><td><input type='text' name='newtype_type' value=''></td>\n";
76 echo "<td><input type='text' name='newtype_typeshort' value=''></td>\n";
77 echo "<td><input type='text' name='newtype_sortkey' value=''></td>\n";
78 echo "<td align='center'><input type='submit' name='addtype_$table' value='Add'></td></tr>\n";
79
80 // retrieve records from database
81 $query = "SELECT id,type,typeshort,sortkey FROM $table ORDER BY sortkey,type";
82
83 $r=$db->Execute($query);
84 $rownr=0;
85 // print all entries
86 while (!($r->EOF) && $r) {
87
88 // get results of each row
89 $id = $r->fields['id'];
90 $type = $r->fields['type'];
91 $typeshort = $r->fields['typeshort'];
92 $sortkey = $r->fields['sortkey'];
93
94 // print start of row of selected group
95 if ($rownr % 2)
96 echo "<tr class='row_odd' align='center'>\n";
97 else
98 echo "<tr class='row_even' align='center'>\n";
99 echo "<input type='hidden' name='type_id[]' value='$id'>\n";
100 echo "<input type='hidden' name='type_name' value='$name'>\n";
101 echo "<td><input type='text' name='type_type[]' value='$type'></td>\n";
102 echo "<td><input type='text' name='type_typeshort[]' value='$typeshort'></td>\n";
103 echo "<td><input type='text' name='type_sortkey[]' value='$sortkey'></td>\n";
104 $modstring = "<input type='submit' name='mdtype"."_$rownr' value='Modify'>";
105 $delstring = "<input type='submit' name='dltype"."_$rownr' value='Remove' ";
106 $delstring .= "Onclick=\"if(confirm('Are you sure the $name \'$type\' ";
107 $delstring .= "should be removed?')){return true;}return false;\">";
108 echo "<td align='center'>$modstring $delstring</td>\n";
109 echo "</tr>\n";
110
111 $r->MoveNext();
112 $rownr+=1;
113 }
114
115 echo "</form>\n";
116 // Back button
117 echo "<tr><td colspan=4 align='center'>\n";
118 $dbstring=$PHP_SELF."?";
119 if ($tablename)
120 $dbstring.="tablename=$tablename&";
121 echo "<form method='post' id='typeform' enctype='multipart/form-data' ";
122 echo "action='$dbstring".SID."'>\n";
123 echo "<input type='submit' name='submit' value='Close' onclick='self.close();window.opener.focus();'>\n";
124 echo "</form>\n";
125 echo "</td></tr>\n";
126
127 echo "</table>\n";
128 echo "</form>\n";
129
130 }
131
132 ////
133 // !Deletes an entry in the type table
134 // Currently, there can be only 1 related table ($table2)
135 // When more are needed,make $table2 into an array
136
137 function del_type ($db,$table,$index,$tableinfo) {
138 global $HTTP_POST_VARS, $HTTP_GET_VARS;
139
140 $id=$HTTP_POST_VARS['type_id'][$index];
141 if ($tableinfo->realname) {
142 $recordref=get_cell($db,$tableinfo->desname,"columnname","associated_table",$table);
143 if ($id) {
144 $r=$db->Execute("UPDATE $tableinfo->realname SET $recordref=NULL WHERE $recordref='$id'");
145 if ($r)
146 $r=$db->Execute("DELETE FROM $table WHERE id=$id");
147 if ($r) {
148 $string="<h3 align='center'>Record removed</h3>\n";
149 echo update_opener_js ($db,$table);
150 }
151 }
152 else
153 $string="<h3 align='center'>Please enter all fields</h3>\n";
154 }
155 else {
156 if ($id) {
157 $table_array=explode('_',$table);
158 $r=$db->Execute("UPDATE $tableinfo->realname SET ".$table_array[1]."='' WHERE ".
159 $table_array[1]."=$id");
160 if ($r) // keep database structure intact
161 $r=$db->Execute("DELETE FROM $table WHERE id=$id");
162 if ($r) {
163 $string="<h3 align='center'>Record removed</h3>\n";
164 echo update_opener_js ($db,$table);
165 }
166 }
167 else
168 $string="<h3 align='center'>Please enter all fields</h3>\n";
169 }
170
171 echo "$string";
172 return false;
173 }
174
175 ////
176 // !Modifies an entry in the type table
177 //
178 function mod_type ($db,$table,$index) {
179 global $HTTP_POST_VARS;
180
181 $id=$HTTP_POST_VARS['type_id'][$index];
182 $type=$HTTP_POST_VARS['type_type'][$index];
183 $typeshort=$HTTP_POST_VARS['type_typeshort'][$index];
184 $sortkey=(int) $HTTP_POST_VARS['type_sortkey'][$index];
185 if ($type && $typeshort && is_int($sortkey)) {
186 $r=$db->Execute("UPDATE $table SET type='$type',typeshort='$typeshort',sortkey=$sortkey WHERE id=$id");
187 if ($r) {
188 echo "<h3 align='center'>Succesfully changed Record</h3>\n";
189 echo update_opener_js ($db,$table);
190 return ($id);
191 }
192 else echo "<h3 align='center'>Please enter all fields</h3>\n";
193 }
194
195 return false;
196 }
197
198 ////
199 // !Adds a new entry in the type table
200 //
201 function add_type ($db,$table) {
202 global $HTTP_POST_VARS;
203
204 $id=$db->GenId($table.'_id_seq');
205 $type=$HTTP_POST_VARS['newtype_type'];
206 $typeshort=$HTTP_POST_VARS['newtype_typeshort'];
207 $sortkey=(int) $HTTP_POST_VARS['newtype_sortkey'];
208 if ($type && $typeshort && is_int($sortkey)) {
209 $r=$db->query("INSERT INTO $table (id,type,typeshort,sortkey)
210 VALUES ($id,'$type','$typeshort',$sortkey)");
211 if ($r) {
212 echo update_opener_js ($db,$table);
213 return ($id);
214 }
215 }
216 else
217 echo "<h3 align='center'>Please enter all fields</h3>\n";
218 return false;
219 }
220 ?>