rendered paste body<?php
function DBLookupComboBox($name, $lookuptable, $keyfield,
$displayfield, $selected_id = NULL)
{
$query = "SELECT $keyfield, $displayfield " .
" FROM $lookuptable ORDER BY $displayfield";
$result = pg_query($query);
if (!$result) {return 0;}
$ret_string = '<select name="' . $name . '" id="' . $name . '">';
while($line = pg_fetch_array($result,null,PGSQL_ASSOC)) {
$ret_string .= '<option';
// Making the item selected by default
if (!is_null($selected_id) && $line[$keyfield] == $selected_id) {
$ret_string .= ' selected';
}
$ret_string .= ' label="' . $line[$displayfield] .
'" name="' . $line[$keyfield] . '">' .
$line["$displayfield"] . '</option>';
}
$ret_string .= "</select>";
return $ret_string;
}
function DBTextBox($name, $value=NULL)
{
$ret_string = '<input type="text" name="' . $name . '" id="' . $name . '"';
if (!is_null($value)) {
$ret_string = '"' . $value . '"';
}
$ret_string .= '/>';
}
class TableXML {
private $xml;
function __construct($config_file) {
if (!file_exists($config_file)) {
throw new Exception('Error loading from file');
}
$xml = simplexml_load_file($config_file);
if (!isset($xml)) {
throw new Exception('Error importing xml file ' . $config_file);
}
}
function DisplayForm($configfile, $tablename)
{
$btn_submit = "OK";
$ret_string = '<form action="' . $action_script . '" method="post">';
// Now producing text and combobox fields
foreach($table_settings[0]->field as $field) {
// Making label
$ret_string .= '<label for="' . $field["name"] . '">' .
iconv("UTF-8", "KOI8-R", $field->label) .
'</label>';
if ($field["type"] == "text") {
$ret_string .= DBTextBox($field["name"]);
} elseif ($field["type"] == "lookup") {
$ret_string .= DBLookupComboBox($field["name"],
$field->lookup["table"],
$field->lookup["key"],
$field->lookup["display"]);
}
$ret_string .= "<br/>";
}
$ret_string .= '<input type="submit" value="'. $btn_submit . '"/>';
$ret_string .= "</form>";
return $ret_string;
}
function DisplayTable($configfile, $tablename)
{
$dspt_modify = "����";
$dspt_delete = "����;
//Adding text fields
$keyfield = $tablename . "." . $table_settings[0]["key"];
$fields[] = $keyfield;
$tables[] = $tablename;
foreach ($table_settings[0]->field as $field) {
if ($field["type"] == "text") {
$fieldname = $tablename . "." . $field["name"];
$labels[$fieldname] = $field->label;
$fields[] = $fieldname;
}
}
//Adding lookup fields
$lookupfields = $xml->xpath("/tables/table[@name='$tablename']".
"/field[@type=\"lookup\"]");
foreach($lookupfields as $lookupfield) {
$lookuptable = (string)$lookupfield->lookup[0]["table"];
$lookupkeyfield = (string)$lookupfield->lookup[0]["key"];
foreach ($lookupfield->lookup[0]->display as $df) {
$displayfield = (string)$df["name"];
$fields[] = $lookuptable . "." . $displayfield;
$joins[$lookuptable] =
$tablename . "." . $lookupfield["name"] . " = " .
$lookuptable . "." . $lookupkeyfield;
$labels[$displayfield] = $lookupfield->label;
}
}
//now building the query string
foreach($fields as $key => $field) {
$fields[$key] = "$field AS \"$field\"";
}
$query = "SELECT " . implode(',',$fields);
$query .= " FROM $tablename ";
// adding joins if they are present
if (isset($joins)) {
foreach($joins as $jointable => $joinexpression) {
$query .= " INNER JOIN $jointable ON $joinexpression";
}
}
$result = pg_query($query);
if (!$result) {return 0;}
//Generating table
$res_string = "<table>";
//Head
$res_string .= "<thead><tr>";
foreach($labels as $label) {
$res_string .= "<td>" . iconv("UTF-8","KOI8-R", $label) . "</td>";
}
$res_string .= "<td>$dspt_modify</td><td>$dspt_delete</td>";
$res_string .= "</tr></thead>";
//Body
$res_string .= "<tbody>";
while($line = pg_fetch_array($result,null,PGSQL_ASSOC)) {
$res_string .= "<tr>";
foreach($labels as $key => $value) {
$res_string .= "<td>$line[$key]</td>";
}
$res_string .= "<td><a href='?action=modify&id=" .
$line[$keyfield] . "'>$dspt_modify</a></td>";
$res_string .= "<td><a href='?action=delete&id=" .
$line[$keyfield] . "'>$dspt_delete</a></td>";
$res_string .= "</tr>";
}
$res_string .= "</tbody>";
$res_string .= "</table>";
return $res_string;
}
function ModifyRecordForm($configfile,$tablename,$id)
{
$btn_submit = "OK";
$ret_string = '<form action="' . $action_script . '" method="post">';
// Obtaining information on the specified record to fill the fields
$query = "SELECT * FROM " . $table_settings["name"] .
" WHERE " . $table_settings["key"] . " = $id";
$result = pg_query($query);
$line = pg_fetch_array($result,null,PGSQL_ASSOC);
// Now producing text and combobox fields
foreach($table_settings[0]->field as $field) {
// Making label
$ret_string .= '<label for="' . $field["name"] . '">' .
iconv("UTF-8", "KOI8-R", $field->label) .
'</label>';
if ($field["type"] == "text") {
$ret_string .= DBTextBox($field["name"],
$line[$field["name"]]);
} elseif ($field["type"] == "lookup") {
$ret_string .= DBLookupComboBox($field["name"],
$field->lookup["table"],
$field->lookup["key"],
$field->lookup["display"],
$id);
}
$ret_string .= "<br/>";
}
$ret_string .= '<input type="submit" value="'. $btn_submit . '"/>';
$ret_string .= "</form>";
return $ret_string;
}
function UpdateTable ($tablename, $key, $values)
{
// updating table
}
}
?>
-------------
and the xml file
-------------
<?xml version="1.0" encoding="koi8-r"?>
<tables>
<table name="locomotive_series" key="id">
<description>
В данном справочнике содержится информация по сериям локомотивов
</description>
<field name="name" type="text">
<label>Серия локомотива</label>
</field>
</table>
<table name="locomotives" key="id">
<field name="series_id" type="lookup">
<lookup table="locomotive_series" key="id">
<display name="name"/>
<lookup>
<label>Серия</label>
</field>
</table>
<table name="area_services" key="id">
<field name="area_id" type="lookup">
<lookup table="areas" key="id">
<display name="name"/>
</lookup>
<label>Участок</label>
</field>
<field name="service_type_id" type="lookup">
<lookup table="areas" key="id" display="name"/>
<label>Вид обслуживания</label>
</field>
</table>
<table name="areas" key="id">
<field name="name" type="text">
<label>Наименование участка</label>
</field>
<field name="supplier_id" type="lookup">
<lookup table="suppliers" key="id">
<display name="name"/>
</lookup>
<label>Поставщик</label>
</field>
</table>
<table name="change_types" key="id">
<field name="name" type="text">
<label>Наименование вида смены</label>
</field>
<field name="time_begin" type="text">
<label>Время начала</label>
</field>
<field name="time_end" type="text">
<label>Время окончания</label>
</field>
</table>
<table name="supplier_workers" key="id">
<field name="supplier_id" type="lookup">
<lookup table="suppliers" key="id">
<display name="shortname">
</lookup>
</field>
<field name="firstname" type="text">
<label>Имя</label>
</field>
<field name="lastname" type="text">
<label>Фамилия</label>
</field>
<field name="middlename" type="text">
<label>Отчество</label>
</field>
</table>
<table name="supplier_worker_jobs" key="id">
`
</table>
</tables>