All pastes #351743 Raw Edit

Mine

public text v1 · immutable
·published 2007-02-12 10:35 UTC
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 = "&#65533;&#65533;&#65533;&#65533;";
			$dspt_delete = "&#65533;&#65533;&#65533;&#65533;;
			
			//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&amp;id=" . 
						$line[$keyfield] . "'>$dspt_modify</a></td>";
				$res_string .= "<td><a href='?action=delete&amp;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>
			&#1042; &#1076;&#1072;&#1085;&#1085;&#1086;&#1084; &#1089;&#1087;&#1088;&#1072;&#1074;&#1086;&#1095;&#1085;&#1080;&#1082;&#1077; &#1089;&#1086;&#1076;&#1077;&#1088;&#1078;&#1080;&#1090;&#1089;&#1103; &#1080;&#1085;&#1092;&#1086;&#1088;&#1084;&#1072;&#1094;&#1080;&#1103; &#1087;&#1086; &#1089;&#1077;&#1088;&#1080;&#1103;&#1084; &#1083;&#1086;&#1082;&#1086;&#1084;&#1086;&#1090;&#1080;&#1074;&#1086;&#1074;
		</description>
		<field name="name" type="text">
			<label>&#1057;&#1077;&#1088;&#1080;&#1103; &#1083;&#1086;&#1082;&#1086;&#1084;&#1086;&#1090;&#1080;&#1074;&#1072;</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>&#1057;&#1077;&#1088;&#1080;&#1103;</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>&#1059;&#1095;&#1072;&#1089;&#1090;&#1086;&#1082;</label>
		</field>
		<field name="service_type_id" type="lookup">
			<lookup table="areas" key="id" display="name"/>
			<label>&#1042;&#1080;&#1076; &#1086;&#1073;&#1089;&#1083;&#1091;&#1078;&#1080;&#1074;&#1072;&#1085;&#1080;&#1103;</label>
		</field>
	</table>
	<table name="areas" key="id">
		<field name="name" type="text">
			<label>&#1053;&#1072;&#1080;&#1084;&#1077;&#1085;&#1086;&#1074;&#1072;&#1085;&#1080;&#1077; &#1091;&#1095;&#1072;&#1089;&#1090;&#1082;&#1072;</label>
		</field>
		<field name="supplier_id" type="lookup">
			<lookup table="suppliers" key="id">
				<display name="name"/>
			</lookup>
			<label>&#1055;&#1086;&#1089;&#1090;&#1072;&#1074;&#1097;&#1080;&#1082;</label>
		</field>
	</table>
	<table name="change_types" key="id">
		<field name="name" type="text">
			<label>&#1053;&#1072;&#1080;&#1084;&#1077;&#1085;&#1086;&#1074;&#1072;&#1085;&#1080;&#1077; &#1074;&#1080;&#1076;&#1072; &#1089;&#1084;&#1077;&#1085;&#1099;</label>
		</field>
		<field name="time_begin" type="text">
			<label>&#1042;&#1088;&#1077;&#1084;&#1103; &#1085;&#1072;&#1095;&#1072;&#1083;&#1072;</label>
		</field>
		<field name="time_end" type="text">
			<label>&#1042;&#1088;&#1077;&#1084;&#1103; &#1086;&#1082;&#1086;&#1085;&#1095;&#1072;&#1085;&#1080;&#1103;</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>&#1048;&#1084;&#1103;</label>
		</field>
		<field name="lastname" type="text">
			<label>&#1060;&#1072;&#1084;&#1080;&#1083;&#1080;&#1103;</label>
		</field>
		<field name="middlename" type="text">
			<label>&#1054;&#1090;&#1095;&#1077;&#1089;&#1090;&#1074;&#1086;</label>
		</field>
	</table>
	<table name="supplier_worker_jobs" key="id">
		`
	</table>
</tables>