fix remapping behavior. Remapping is only necessary if we are rendering on the workbe...
[AROS-Contrib.git] / bgui / bugs / bugs_table_wrapper.php
blob6069c69d5898e1bd9b778e42b00413b17bf84c12
1 <?
2 /*
3 * bugs_table_wrapper.php
5 * @(#) $Header$
7 */
9 #+-------------------------------------+
10 #| Table Wrapper for SQL Engines |
11 #| By Zeev Suraski |
12 #+-------------------------------------+
14 function table_wrapper()
16 global $fields,$tables,$bugs_debug;
18 if (!isset($fields) || count($fields)==0) {
19 $bugs_debug("No fields specified");
20 return 0;
22 if (!isset($tables) || count($tables)==0) {
23 $bugs_debug("No tables specified");
24 return 0;
27 global $SERVER_NAME;
28 global $PHP_SELF;
29 $url = "http://$SERVER_NAME$PHP_SELF?foo=bar";
31 for (reset(&$fields),$field_list=current(&$fields); next(&$fields); $field_list.=",".current(&$fields));
33 for (reset(&$tables),$table_list=current(&$tables); next(&$tables); $table_list.=",".current(&$tables));
35 # Generate query...
37 global $select_options;
38 $query = "select $select_options $field_list from $table_list";
40 global $where_clause;
41 if (@$where_clause) {
42 $query .= " where $where_clause";
43 $url .= "&where_clause=$where_clause";
46 global $group_by_clause;
47 if (isset($group_by_clause)) {
48 $query .= " group by $group_by_clause";
49 $url .= "&group_by_clause=$group_by_clause";
52 global $order_by_clause;
53 global $last_order_by;
54 if (isset($order_by_clause)) {
55 $query .= " order by $order_by_clause";
56 if (isset($last_order_by) && $last_order_by==$order_by_clause) {
57 $query .= " desc";
58 } else {
59 $last_order_by="last_order_by=$order_by_clause";
63 global $having_clause;
64 if (isset($having_clause)) {
65 $query .= " having $having_clause";
66 $url .= "&having_clause=$having_clause";
69 if (!isset($total_num_rows)) {
70 # echo "$query<br>";
71 if(($result = BugsDatabaseQuery($query))<=0)
73 $bugs_debug("SQL query failed: $query");
74 return(0);
76 $total_num_rows = BugsDatabaseNumberOfRows($result);
77 BugsDatabaseFreeResult($result);
79 $url .= "&total_num_rows=$total_num_rows";
81 /* limit support not yet completed
82 if (isset($GLOBALS["limit_start"])) {
83 $limit_start = $GLOBALS["limit_start"];
84 if (isset($GLOBALS["limit_length"])) {
85 $limit_length=$GLOBALS["limit_length"];
86 } else {
87 $limit_length="";
89 if ($limit_start!=-1) {
90 $query .= " limit $limit_start";
91 if ($limit_length) {
92 $query .= ",$limit_length";
95 } else {
97 $limit_length = $limit_start = "";
103 # escape URL
104 $url = ereg_replace("[ ]","+",$url);
106 global $debug;
107 if (isset($debug)) {
108 echo "URL: $url<br>\n";
109 echo "Query: $query<br>\n";
112 if(($result = BugsDatabaseQuery($query))<=0)
114 $bugs_debug("SQL query failed: $query");
115 return(0);
118 $num_rows = BugsDatabaseNumberOfRows($result);
119 if ($num_rows==0) {
120 return 0;
123 $field_count = BugsDatabaseNumberOfFields($result);
125 if($num_rows==1)
126 echo LocaleText("1-entry");
127 else
128 printf(LocaleText("n-entries"),$num_rows);
129 if ($total_num_rows>$num_rows) {
130 printf(LocaleText("out-of-total",$total_num_rows));
132 echo ":<br>\n";
134 echo "<center><table border=1 width=\"95%\"><tr>\n";
136 # print table header
137 global $conversion_table,$dont_display,$centering,$default_alignment,$coloring,$header_coloring,$dont_link;
138 global $default_color,$default_header_color,$row_coloring_function;
139 global $pass_on;
140 $i=0;
141 while($field=BugsDatabaseFetchNextField($result)) {
142 $fieldnames[$i] = $field->name;
143 $tablenames[$i] = $field->table;
144 if (strlen($tablenames[$i])>0 && substr($tablenames[$i],0,3)!="SQL") {
145 $fullnames[$i] = $tablenames[$i].".".$fieldnames[$i];
146 } else {
147 $fullnames[$i] = $fieldnames[$i];
149 if (isset($conversion_table[$fullnames[$i]])) {
150 $display_fieldname = $conversion_table[$fullnames[$i]];
151 } else if (isset($conversion_table[$fieldnames[$i]])) {
152 $display_fieldname = $conversion_table[$fieldnames[$i]];
153 } else {
154 $display_fieldname=$fieldnames[$i];
156 if (isset($header_coloring[$fullnames[$i]])) {
157 $add_attributes = "bgcolor=\"#".$header_coloring[$fullnames[$i]]."\"";
158 } else if (isset($header_coloring[$fieldnames[$i]])) {
159 $add_attributes = "bgcolor=\"#".$header_coloring[$fieldnames[$i]]."\"";
160 } else if (isset($coloring[$fullnames[$i]])) {
161 $add_attributes = "bgcolor=\"#".$coloring[$fullnames[$i]]."\"";
162 } else if (isset($coloring[$fieldnames[$i]])) {
163 $add_attributes = "bgcolor=\"#".$coloring[$fieldnames[$i]]."\"";
164 } else if (isset($default_header_color)) {
165 $add_attributes = "bgcolor=\"#".$default_header_color."\"";
166 } else if (isset($default_color)) {
167 $add_attributes = "bgcolor=\"#".$default_color."\"";
168 } else {
169 $add_attributes="";
171 if (!isset($dont_display[$fullnames[$i]]) && !isset($dont_display[$fieldnames[$i]])) {
172 if (isset($dont_link[$fullnames[$i]]) || isset($dont_link[$fieldnames[$i]])) {
173 $link=0;
174 } else {
175 $link=1;
177 echo "<th $add_attributes>";
178 if ($link) {
179 echo "<a href=\"$url$pass_on&order_by_clause=$fullnames[$i]&$last_order_by\">";
181 $df = ereg_replace("<", "&lt;", $display_fieldname);
182 $df = ereg_replace(">", "&gt;", $df);
183 echo $df;
184 if ($link) {
185 echo "</a>";
187 echo "</th>";
189 $i++;
191 echo "</tr>\n";
193 # print table data
194 global $external_processing_function;
195 while ($row=BugsDatabaseFetchArray($result)) {
196 if (isset($row_coloring_function)) {
197 print "<tr bgcolor=\"".$row_coloring_function($row)."\">";
198 } else {
199 echo "<tr>\n";
201 for ($i=0; $i<$field_count; $i++) {
202 if (strlen($row[$i])==0) {
203 $row[$i]= " &nbsp; ";
205 if (!isset($dont_display[$fullnames[$i]]) && !isset($dont_display[$fieldnames[$i]])) {
206 if (isset($centering[$fullnames[$i]])) {
207 $align = $centering[$fullnames[$i]];
208 } else if (isset($centering[$fieldnames[$i]])) {
209 $align = $centering[$fieldnames[$i]];
210 } else if (isset($default_alignment)) {
211 $align = $default_alignment;
212 } else {
213 $align = "left";
215 if (isset($coloring[$fullnames[$i]])) {
216 $add_attributes = "bgcolor=\"#".$coloring[$fullnames[$i]]."\"";
217 } else if (isset($coloring[$fieldnames[$i]])) {
218 $add_attributes = "bgcolor=\"#".$coloring[$fieldnames[$i]]."\"";
219 } else if (isset($default_color)) {
220 $add_attributes = "bgcolor=\"#".$default_color."\"";
221 } else {
222 $add_attributes="";
224 echo "<td align=\"$align\" $add_attributes>";
225 if (isset($external_processing_function)) {
226 $external_processing_function($fieldnames[$i],$tablenames[$i],$row[$i],&$row);
227 } else {
228 $df = ereg_replace("<", "&lt;", $row[$i]);
229 $df = ereg_replace(">", "&gt;", $df);
230 echo "$df\n";
232 echo "</td>";
236 echo "</table></center>\n";
238 if ($num_rows!=$total_num_rows) {
239 if ($limit_length && $limit_start!=-1) {
240 echo "<hr width=\"70%\"><center><table border><tr>\n";
241 if ($limit_start>0) {
242 $new_limit_start = $limit_start-$limit_length;
243 if ($limit_start<0) {
244 $limit_start=0;
246 echo "<td>\n";
247 echo "<a href=\"$url$pass_on&order_by_clause=$order_by_clause&limit_start=$new_limit_start&limit_length=$limit_length\">",sprintf(LocaleText("Previous-n-records"),$limit_length),"</a>";
248 echo "</td>\n";
250 echo "<td><a href=\"$url$pass_on&order_by_clause=$order_by_clause&limit_start=-1\">",LocaleText("All-Records"),"</a></td>";
251 if ($num_rows+$limit_start < $total_num_rows) {
252 $new_limit_start = $limit_start+$limit_length;
253 $remaining_records = $total_num_rows-$new_limit_start;
254 echo "<td>";
255 echo "<a href=\"$url&order_by_clause=$pass_on&$order_by_clause&limit_start=$new_limit_start&limit_length=$limit_length\">";
256 if ($remaining_records>$limit_length) {
257 printf(LocaleText("Next-n-records"),$limit_length);
258 } else {
259 if($remaining_records==1)
260 echo LocaleText("Last-record");
261 else
262 printf(LocaleText("Last-n-records"),$remaining_records);
264 echo "</a></td>\n";
266 echo "</tr></table></center>\n";
268 BugsDatabaseFreeResult($result);
270 return $num_rows;