Location: PHPKode > projects > JaxBlog > JaxBlog/app/common/xml.php
<?php

function makeHumanReadable($field) {
	$field = str_replace("_", " ", $field);
	$words = explode(" ", $field);
	$phrase = '';
	for ($i = 0; $i < count($words); $i++) {
		if (strlen($words[$i]) > 1) {
			$phrase .= strtoupper(substr($words[$i], 0, 1)) . substr($words[$i], 1);
		} else {
			$phrase .= strtoupper($words[$i]);
		}
		$phrase .= " ";
	}
	return $phrase;
}

function processXMLRequestForJaxTable($sql, $args) {
	
	$request = isset($_GET['request']) ? $_GET['request'] : '';

	if ($request == 'xml') {
		
		header('Content-Type: text/xml');
		$xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\n";
		
		$connection = databaseGetConnection();
		$result = $connection->Execute($sql, $args);
		if ($result == null) {
			echo "<root></root>\n";
			exit;
		}
		
		$xml .= "<root>\n";
		$xml .= "	<list_titles>\n";
		
		// Loop through rows
		$cols = $result->FieldCount();
		for ($i = 0; $i < $cols; $i++) {
			$xml .= "		<record>" . makeHumanReadable($result->FetchField($i)->name) . "</record>\n";
		}
		
		$xml .= "	</list_titles>\n";
		$xml .= "	<list_data>\n";
		
		$result->MoveFirst();
		$rows = $result->GetAll();
		
		if (count($rows) == 0) {
			echo "<root></root>\n";
			exit;
		}
		
		foreach($rows as $row) {
			$xml .= "		<record>\n";
			for ($i = 0; $i < $cols; $i++) {
				$xml .= "			<a" . $i . ">" . $row[$i] . "</a" . $i . ">\n";
			}
			$xml .= "		</record>\n";
		}
		
		$xml .= "	</list_data>\n";
		$xml .= "</root>\n";
		echo $xml;
		exit;
	}
}

function processJaxFormXML($request, $stage, $sql, $args) {
	
	$request = '';
	if (isset($_GET['request'])) $request = $_GET['request'];
	
	if ($request == 'xml') {
	
		header('Content-Type: text/xml');
		$xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\n";
		$xml .= "<root>\n";
		
		if ($stage == 'add') {
			$xml .= "	<controls_input>\n";
			$xml .= "		<record><key>request</key><value>add</value></record>\n";
			$xml .= "		<record><key>stage</key><value>$stage</value></record>\n";
			$xml .= "	</controls_input>\n";
			
		} else if ($stage == 'edit') {
//			$connection = databaseGetConnection();
//			$recordset = $connection->Execute($sql, $args);
			
			$xml .= "	<controls_input>\n";
			$xml .= "		<record><key>request</key><value>edit</value></record>\n";
			$xml .= "		<record><key>stage</key><value>$stage</value></record>\n";
			
			$pairs = array();
			$rows = databaseGetRows($sql, $args);
			foreach ($rows as $row) {
				// get the list of keys for the recordset.
				$counter = 0;
				$even = false; // every other key is a field name.
				foreach (array_keys($row) as $field) {
					if ($even) {
						$pairs[$counter++] = "		<record><key>$field</key>";
					}
					$even = !$even;
				}
				
				$counter = 0;
				$even = false; // fields are duplicated, only use every other one.
				foreach ($row as $field) {
					if ($even) {
						$pairs[$counter++] .= "<value><![CDATA[$field]]></value></record>\n";
					}
					$even = !$even;
				}
			}
			
			// Move the key value pairs to the xml string.
			foreach ($pairs as $pair) {
				$xml .= $pair;
			}
			
			$xml .= "	</controls_input>\n";
		};
		
		$xml .= "</root>\n";
		echo $xml;
		exit;
	}
	
}

function processJaxFormRequest(
	$request, $stage,
	$sql_query, $args_query, 
	$sql_insert, $args_insert, $seq_insert, 
	$sql_update, $args_update,
	$sql_delete, $args_delete,
	$redirect_on_error) {
	
	$exitearly = true;
	$errors = '';

	processJaxFormXML($request, $stage, $sql_query, $args_query);

	if ($request == 'add') {
		if ($stage == 'add') {
			// User just submitted.
			databaseExecuteReturnId($sql_insert, $args_insert, $seq_insert);
			
		} else {
			// Send user to the 'add' form.
			$_SESSION['id'] = isset($_GET['id']) ? $_GET['id'] : null;
			$stage = 'add';
			$exitearly = false;
		}
		
	} else if ($request == 'edit') {
		if ($stage == 'edit') {
			// User just submitted.
			databaseExecute($sql_update, $args_update);
			
		} else {
			// Send user to the 'edit' form.
			$_SESSION['id'] = $_GET['id'];
			$stage = 'edit';
			$exitearly = false;
		}
		
	} else if ($request == 'delete') {
		databaseExecute($sql_delete, $args_delete);
	}
	
	if ($exitearly) {
		// TODO: find a neat way to out the error messages to the user.
		if ($errors != '') { 
			echo $errors;
			exit;
		}
	    header($redirect_on_error);
		exit;
	}
	
	return $stage;

}

?>
Return current item: JaxBlog