1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
|
<?php
if (preg_match("/\/includes\//", $PHP_SELF)) {
die ("You can't access this file directly!");
}
// This file contains some convenient functions for editing rows
// in a table.
// You need to define the tables (typically this is done in tables.php).
// $tablear - array that defines table (see tables.php)
// $fieldname - name of field
function dbtable_get_field_index ( $tablear, $fieldname ) {
global $error;
for ( $j = 0; $j < count ( $tablear ); $j++ ) {
if ( $tablear[$j]["name"] == $fieldname ) {
//echo "found $fieldname $j<br />";
return $j;
}
}
$error = "dbtable_get_field_index: Invalid fieldname \"$fieldname\".";
return -1;
}
// Create a table for editing a database table entry
// $tablear - array that defines table (see tables.php)
// $valuesar - array of current values
// $action - URL to post form to (or "" for display-only)
// $actionlabel - Value to put on submit form ("Save", etc.)
// $hidden - array of hidden form variables (name1,value1,name2,value2,...)
function dbtable_to_html ( $tablear, $valuesar, $action="", $formname="",
$actionlabel="", $hidden="" ) {
global $CELLBG;
$ret = "<table style=\"border-width:0px;\" cellspacing=\"0\" cellpadding=\"0\">" .
"<tr><td style=\"background-color:#000000;\">" .
"<table style=\"border-width:0px; width:100%;\" cellspacing=\"1\" cellpadding=\"2\">" .
"<tr><td style=\"width:100%; background-color:$CELLBG;\">" .
"<table style=\"border-width:0px; width:100%;\">\n";
if ( ! is_array ( $tablear ) ) {
return "Error: dbtable_to_html parameter 1 is not an array!\n<br />\n";
}
if ( $action != "" ) {
$ret .= "<form action=\"$action\" method=\"post\"";
if ( $formname != "" )
$ret .= " name=\"$formname\"";
$ret .= ">";
if ( is_array ( $hidden ) ) {
for ( $i = 0; $i < count ( $hidden ); $i += 2 ) {
$ret .= "<input type=\"hidden\" name=\"" . $hidden[$i] .
"\" value=\"" . $hidden[$i+1] . "\" />";
}
}
}
for ( $i = 0; $i < count ( $tablear ); $i++ ) {
if ( ! empty ( $tablear[$i]["hide"] ) )
continue;
if ( ! empty ( $action ) && ! empty ( $tablear[$i]["calculated"] ) )
continue;
$ret .= "<tr><td style=\"vertical-align:top;\">";
if ( ! empty ( $tablear[$i]["prompt"] ) ) {
$ret .= "<b";
if ( ! empty ( $tablear[$i]["tooltip"] ) )
$ret .= " class=\"tooltip\" title=\"" . $tablear[$i]["tooltip"] . "\"";
$ret .= ">" . $tablear[$i]["prompt"] . ":</b></td>\n";
} else {
$ret .= " </td>";
}
$ret .= "<td style=\"vertical-align:top;\">";
if ( empty ( $tablear[$i]["noneditable"] ) && ! empty ( $action ) ) {
if ( $tablear[$i]["type"] == "text" ||
$tablear[$i]["type"] == "int" || $tablear[$i]["type"] == "float" ) {
$ret .= "<input type=\"text\" name=\"" . $tablear[$i]["name"] .
"\"";
if ( ! empty ( $tablear[$i]["maxlength"] ) )
$ret .= " maxlength=\"" . $tablear[$i]["maxlength"] . "\"";
if ( ! empty ( $tablear[$i]["length"] ) )
$ret .= " size=\"" . $tablear[$i]["length"] . "\"";
if ( ! empty ( $valuesar[$i] ) )
$ret .= " value=\"" . htmlspecialchars ( $valuesar[$i] ) . "\"";
$ret .= " />";
} else if ( $tablear[$i]["type"] == "boolean" ) {
$ret .= "<input type=\"radio\" value=\"Y\" name=\"" . $tablear[$i]["name"] . "\"";
if ( $valuesar[$i] == "Y" )
$ret .= " checked=\"checked\"";
$ret .= ">" . translate("Yes") . " ";
$ret .= "<input type=\"radio\" value=\"N\" name=\"" . $tablear[$i]["name"] . "\"";
if ( $valuesar[$i] != "Y" )
$ret .= " checked=\"checked\"";
$ret .= ">" . translate("No");
} else if ( $tablear[$i]["type"] == "date" ) {
$ret .= date_selection_html ( $tablear[$i]["name"], $valuesar[$i] );
} else if ( $tablear[$i]["type"] == "dbdate" ) {
// '2002-12-31'
$y = substr ( $valuesar[$i], 0, 4 );
$m = substr ( $valuesar[$i], 5, 2 );
$d = substr ( $valuesar[$i], 8, 2 );
$date = sprintf ( "%04d%02d%02d", $y, $m, $d );
$ret .= date_selection_html ( $tablear[$i]["name"], $date );
} else {
$ret .= "(type " . $tablear[$i]["type"] . " not supported)";
}
} else {
if ( ! empty ( $valuesar[$i] ) ) {
if ( $tablear[$i]["type"] == "text" ||
$tablear[$i]["type"] == "int" || $tablear[$i]["type"] == "float" ) {
$ret .= htmlentities ( $valuesar[$i] );
} else if ( $tablear[$i]["type"] == "boolean" ) {
if ( $valuesar[$i] == "Y" || empty ( $valuesar[$i] ) )
$ret .= translate("Yes");
else
$ret .= translate("No");
} else if ( $tablear[$i]["type"] == "date" ) {
$ret .= date_to_str ( $valuesar[$i] );
} else if ( $tablear[$i]["type"] == "dbdate" ) {
$y = substr ( $valuesar[$i], 0, 4 );
$m = substr ( $valuesar[$i], 5, 2 );
$d = substr ( $valuesar[$i], 8, 2 );
$date = sprintf ( "%04d%02d%02d", $y, $m, $d );
$ret .= date_to_str ( $date );
} else {
$ret .= "(type " . $tablear[$i]["type"] . " not supported)";
}
}
}
$ret .= "</td></tr>\n";
}
if ( ! empty ( $actionlabel ) )
$ret .= "<tr><td colspan=\"2\" style=\"text-align:center;\">\n" .
"<input type=\"submit\" value=\"" . htmlspecialchars ( $actionlabel ) . "\" />" .
"</td></tr></form>\n";
$ret .= "</table>\n</td></tr></table>\n</td></tr></table>\n";
return $ret;
}
// Print rows of a table into an HTML table. The first column will
// include (optionally) href links to a page which can show further
// details.
// $tablear - db table (defined in tables.php)
// $tablename - db table name
// $href - URL (%0 will be replaced with field field 0)
// $fields - list of fields to include in table.
// $keys - array of db key fields (field tagged with "iskey" => 1)
// $order - SQL order text
function dbtable_html_list ( $tablear, $tablename, $href, $fields,
$keys, $order ) {
global $THBG, $THFG, $CELLBG;
if ( ! is_array ( $tablear ) )
return "Error: dbtable_to_html_list parameter 1 is not an array!\n<br />\n";
if ( ! is_array ( $fields ) )
return "Error: dbtable_to_html_list parameter 2 is not an array!\n<br />\n";
if ( ! is_array ( $keys ) )
return "Error: dbtable_to_html_list parameter 3 is not an array!\n<br />\n";
$ret = "<table style=\"border-width:0px;\" cellspacing=\"0\" cellpadding=\"0\">" .
"<tr><td style=\"background-color:#000000;\">" .
"<table style=\"border-width:0px; width:100%;\" cellspacing=\"1\" cellpadding=\"2\">" .
"<tr><td style=\"width:100%; background-color:$CELLBG;\">" .
"<table style=\"border-width:0px; width:100%;\">\n";
// header
$ret .= "<tr>";
for ( $i = 0; $i < count ( $fields ); $i++ ) {
$ind = dbtable_get_field_index ( $tablear, $fields[$i] );
/*
if ( $ind < 0 )
echo "Error: dbtable_html_list invalid fieldname \"$fields[$i]\" $i\n"; exit;
*/
if ( empty ( $tablear[$ind]["hide"] ) )
$ret .= "<th style=\"background-color:$THBG; color:$THFG;\">" .
$tablear[$ind]["prompt"] . "</th>";
}
$ret .= "</tr>\n";
$sql = "SELECT " . $fields[0];
for ( $i = 1; $i < count ( $fields ); $i++ ) {
$sql .= ", " . $fields[$i];
}
$sql .= " FROM " . $tablename . " ";
if ( is_array ( $keys ) && count ( $keys ) > 0 ) {
$sql .= "WHERE ";
$first = 1;
for ( $i = 0; $i < count ( $tablear ); $i++ ) {
if ( ! empty ( $tablear[$i]["iskey"] ) ) {
if ( empty ( $keys[$tablear[$i]["name"]] ) ) {
//echo "Error: key value for " . $tablear[$i]["name"] . " not set.\n";
//exit;
} else {
if ( $first )
$first = 0;
else
$sql .= " AND ";
$sql .= $tablear[$i]["name"] . " = " ;
if ( $tablear[$i]["type"] == "int" ||
$tablear[$i]["type"] == "float" || $tablear[$i]["type"] == "date" )
$sql .= $keys[$tablear[$i]["name"]];
else
$sql .= "'" . $keys[$tablear[$i]["name"]] . "'";
}
}
}
}
if ( ! empty ( $order ) )
$sql .= " ORDER BY " . $order;
//echo "SQL: $sql<br />\n";
$res = dbi_query ( $sql );
if ( $res ) {
while ( $row = dbi_fetch_row ( $res ) ) {
$ret .= "<tr>";
$first_href = 1;
$first = 1;
for ( $i = 0; $i < count ( $fields ); $i++ ) {
// check data type (date)
$ind = dbtable_get_field_index ( $tablear, $fields[$i] );
if ( empty ( $tablear[$ind]["hide"] ) ) {
$ret .= "<td style=\"background-color:$CELLBG; vertical-align:top;\">";
if ( $tablear[$ind]["type"] == "date" )
$val = date_to_str ( $row[$i], "", 1, 1 );
else if ( $tablear[$ind]["type"] == "dbdate" ) {
$y = substr ( $row[$i], 0, 4 );
$m = substr ( $row[$i], 5, 2 );
$d = substr ( $row[$i], 8, 2 );
$date = sprintf ( "%04d%02d%02d", $y, $m, $d );
$val = date_to_str ( $date, "", 1, 1 );
} else
$val = htmlentities ( $row[$i] );
if ( $first_href && ! empty ( $href ) ) {
$first_href = 0;
$url = $href;
for ( $j = count ( $fields ) - 1; $j >= 0; $j-- ) {
$url = str_replace ( "%$j", $row[$j], $url );
}
$ret .= "<a href=\"$url\">" . $val . "</a>";
} else {
$ret .= $val;
}
$ret .= "</td>";
}
}
$ret .= "</tr>\n";
}
} else {
echo translate("Database error") . ": " . dbi_error (); exit;
}
$ret .= "</table>\n</td></tr></table>\n</td></tr></table>\n";
return $ret;
}
// Load a single row of a db table
// $tablear - db table (defined in tables.php)
// $tablename - db table name
// $keys - array of db key fields (field tagged with "iskey" => 1)
function dbtable_load ( $tablear, $tablename, $keys ) {
$ret = false;
$sql = "SELECT ";
if ( ! is_array ( $tablear ) ) {
echo "Error: dbtable_load parameter 1 is not an array!<br />\n";
exit;
}
if ( ! is_array ( $keys ) ) {
echo "Error: dbtable_load parameter 3 is not an array!<br />\n";
exit;
}
$first = 1;
for ( $i = 0; $i < count ( $tablear ); $i++ ) {
if ( $first )
$first = 0;
else
$sql .= ", ";
if ( empty ( $tablear[$i]["name"] ) ) {
echo "Error: dbtable_load $tablename field $i does not define name.\n";
exit;
}
$sql .= $tablear[$i]["name"];
}
$sql .= " FROM " . $tablename . " WHERE ";
$first = 1;
for ( $i = 0; $i < count ( $tablear ); $i++ ) {
if ( ! empty ( $tablear[$i]["iskey"] ) ) {
if ( empty ( $keys[$tablear[$i]["name"]] ) ) {
//echo "Error: key value for " . $tablear[$i]["name"] . " not set.\n";
//exit;
} else {
if ( $first )
$first = 0;
else
$sql .= " AND ";
$sql .= $tablear[$i]["name"] . " = " ;
if ( $tablear[$i]["type"] == "int" ||
$tablear[$i]["type"] == "float" || $tablear[$i]["type"] == "date" )
$sql .= $keys[$tablear[$i]["name"]];
else
$sql .= "'" . $keys[$tablear[$i]["name"]] . "'";
}
}
}
//echo "SQL: $sql<br />\n";
$res = dbi_query ( $sql );
if ( $res ) {
if ( $row = dbi_fetch_row ( $res ) ) {
$ret = array ();
for ( $i = 0; $i < count ( $tablear ); $i++ ) {
$ret[$i] = $row[$i];
}
} else {
$ret = false; // not found
}
dbi_free_result ( $res );
} else {
echo translate("Database error") . ": " . dbi_error (); exit;
}
return $ret;
}
// Delete a single row of a db table
// $tablear - db table (defined in tables.php)
// $tablename - db table name
// $keys - array of db key fields (field tagged with "iskey" => 1)
function dbtable_delete ( $tablear, $tablename, $keys ) {
$ret = false;
if ( ! is_array ( $tablear ) ) {
echo "Error: dbtable_delete parameter 1 is not an array!<br />\n";
exit;
}
if ( ! is_array ( $keys ) ) {
echo "Error: dbtable_delete parameter 3 is not an array!<br />\n";
exit;
}
$sql = "DELETE FROM $tablename WHERE ";
$first = 1;
for ( $i = 0; $i < count ( $tablear ); $i++ ) {
if ( ! empty ( $tablear[$i]["iskey"] ) ) {
if ( empty ( $keys[$tablear[$i]["name"]] ) ) {
//echo "Error: key value for " . $tablear[$i]["name"] . " not set.\n";
//exit;
continue;
} else {
if ( $first )
$first = 0;
else
$sql .= " AND ";
$sql .= $tablear[$i]["name"] . " = " ;
if ( $tablear[$i]["type"] == "int" ||
$tablear[$i]["type"] == "float" || $tablear[$i]["type"] == "date" )
$sql .= $keys[$tablear[$i]["name"]];
else
$sql .= "'" . $keys[$tablear[$i]["name"]] . "'";
}
}
}
//echo "SQL: $sql<br />";
if ( ! dbi_query ( $sql ) ) {
echo translate("Database error") . ": " . dbi_error (); exit;
}
return $ret;
}
// Add a row into a table (SQL insert)
// $tablear - db table (defined in tables.php)
// $tablename - db table name
// $valuesar - array of values
function dbtable_add ( $tablear, $tablename, $valuesar ) {
global $error;
$ret = false;
$sql = "INSERT INTO " . $tablename . " (";
if ( ! is_array ( $tablear ) ) {
echo "Error: dbtable_add parameter 1 is not an array!<br />\n";
exit;
}
if ( ! is_array ( $valuesar ) ) {
echo "Error: dbtable_add parameter 3 is not an array!<br />\n";
exit;
}
$first = 1;
for ( $i = 0; $i < count ( $tablear ); $i++ ) {
if ( $first )
$first = 0;
else
$sql .= ", ";
if ( empty ( $tablear[$i]["name"] ) ) {
echo "Error: dbtable_load $tablename field $i does not define name.\n";
exit;
}
$sql .= $tablear[$i]["name"];
}
$sql .= " ) VALUES (";
$first = 1;
for ( $i = 0; $i < count ( $tablear ); $i++ ) {
if ( $first )
$first = 0;
else
$sql .= ", ";
if ( empty ( $valuesar[$i] ) )
$sql .= "NULL";
else if ( $tablear[$i]["type"] == "date" ||
$tablear[$i]["type"] == "int" )
$sql .= $valuesar[$i];
else
$sql .= "'" . $valuesar[$i] . "'";
}
$sql .= " )";
//echo "SQL: $sql<br />\n";
if ( ! dbi_query ( $sql ) ) {
// Shouldn't happen... complain if it does.
$error = translate("Database error") . ": " . dbi_error ();
return false;
}
return true;
}
// Update a row in a table (SQL update)
// $tablear - db table (defined in tables.php)
// $tablename - db table name
// $valuesar - array of values
function dbtable_update ( $tablear, $tablename, $valuesar ) {
global $error;
$sql = "UPDATE " . $tablename . " SET";
if ( ! is_array ( $tablear ) ) {
echo "Error: dbtable_update parameter 1 is not an array!<br />\n";
exit;
}
if ( ! is_array ( $valuesar ) ) {
echo "Error: dbtable_update parameter 3 is not an array!<br />\n";
exit;
}
$first = 1;
for ( $i = 0; $i < count ( $tablear ); $i++ ) {
if ( ! empty ( $tablear[$i]["iskey"] ) )
continue;
if ( $first )
$first = 0;
else
$sql .= ", ";
if ( empty ( $tablear[$i]["name"] ) ) {
echo "Error: dbtable_update $tablename field $i does not define name.\n";
exit;
}
$sql .= " " . $tablear[$i]["name"] . " = ";
if ( empty ( $valuesar[$i] ) ) {
$sql .= "NULL";
} else if ( $tablear[$i]["type"] == "int" ||
$tablear[$i]["type"] == "date" ) {
$sql .= $valuesar[$i];
} else
$sql .= "'" . $valuesar[$i] . "'";
}
$sql .= " WHERE";
$first = 1;
for ( $i = 0; $i < count ( $tablear ); $i++ ) {
if ( empty ( $tablear[$i]["iskey"] ) )
continue;
if ( $first )
$first = 0;
else
$sql .= " AND";
if ( empty ( $valuesar[$i] ) ) {
echo "Error: you must set field $i (" . $tablear[$i]["name"] .
") by hand. Cannot be empty.";
exit;
}
$sql .= " " . $tablear[$i]["name"] . " = '" . $valuesar[$i] . "'";
}
//echo "SQL: $sql <P>\n";
if ( ! dbi_query ( $sql ) ) {
// Shouldn't happen... complain if it does.
$error = translate("Database error") . ": " . dbi_error ();
return false;
}
return true;
}
// Generate a new ID
function dbtable_genid ( $tablename, $field ) {
$ret = 1;
$sql = "SELECT MAX(" . $field . ") FROM " . $tablename;
$res = dbi_query ( $sql );
if ( $res ) {
if ( $row = dbi_fetch_row ( $res ) ) {
$ret = $row[0] + 1;
}
dbi_free_result ( $res );
}
return $ret;
}
// Convert an array of db values (with index values 0,1,2,... into
// an associative array (with index values of table column names).
// $tablear - db table (defined in tables.php)
// $valuesar - array of values
function dbtable_build_name_index ( $tablear, $valuesar ) {
$ret = array ();
for ( $i = 0; $i < count ( $tablear ); $i++ ) {
$ret[$tablear[$i]["name"]] = $valuesar[$i];
}
return $ret;
}
?>
|