From bf7d3f2cc931f887ce7fa0cbb1d52f3d2d3e3cf0 Mon Sep 17 00:00:00 2001
From: Tim Ansell <mithro@mithis.com>
Date: Sun, 14 Jun 2009 01:07:10 +1000
Subject: [PATCH] First commit of the Register data entry thingy.

---
 www/docs/register/question-toForm.php |   42 +++++
 www/docs/register/question-toXML.php  |   22 +++
 www/docs/register/questions.php       |   60 ++++++++
 www/docs/register/register-toForm.php |   50 ++++++
 www/docs/register/register-toXML.php  |   15 ++
 www/docs/register/register.php        |  267 +++++++++++++++++++++++++++++++++
 6 files changed, 456 insertions(+), 0 deletions(-)
 create mode 100644 www/docs/register/question-toForm.php
 create mode 100644 www/docs/register/question-toXML.php
 create mode 100644 www/docs/register/questions.php
 create mode 100644 www/docs/register/register-toForm.php
 create mode 100644 www/docs/register/register-toXML.php
 create mode 100644 www/docs/register/register.php

diff --git a/www/docs/register/question-toForm.php b/www/docs/register/question-toForm.php
new file mode 100644
index 0000000..5a2bd11
--- /dev/null
+++ b/www/docs/register/question-toForm.php
@@ -0,0 +1,42 @@
+<br>
+<br>
+<table>
+	<tr>
+		<td><h2><?php echo $this->name ?></h2></td>
+		<td><?php echo $this->description ?></h2></td>
+	</tr>
+<?php foreach($this->persons as $person => $person_desc) { ?>
+	<tr>
+		<td colspan="2">
+			<table>
+				<tr>
+					<td><h3><?php echo $person_desc; ?></h3></td>
+<?php	foreach($this->fields as $field_name => $field_desc) { ?>
+					<td><h4><?php echo $field_desc ?></h4></td>
+<?php	} ?>
+				</tr><tr>
+
+<?php
+	$question_responses = $responses[$this->name][$person];
+	for($i = 0; $i <= count($question_responses); $i++) { 
+		$row = @$question_responses[$i];
+?>
+				</tr><tr>
+					<td><?php echo $i+1 ?>.</td>
+<?php		foreach($this->fields as $field_name => $field_desc) { ?>
+					<td><input type="text" name="questions[<?php echo $this->name; ?>][<?php echo $person; ?>][<?php echo $i ?>][<?php echo $field_name;?>]"
+<?php 				if (end($this->fields) == $field_desc and $i == count($question_responses)) { ?>
+						onKeyPress="return addRowOnEnter(event, this.parentNode.parentNode)"
+<?php 				} else { ?>
+						onKeyPress="return noSubmit(event)"
+<?php				} ?>
+						value="<?php echo htmlentities($row[$field_name]); ?>">
+					</td>
+<?php			} ?>
+<?php 		} ?>
+				</tr>
+			</table>
+		</td>
+	</tr>
+<?php } ?>
+</table>
diff --git a/www/docs/register/question-toXML.php b/www/docs/register/question-toXML.php
new file mode 100644
index 0000000..e310bd0
--- /dev/null
+++ b/www/docs/register/question-toXML.php
@@ -0,0 +1,22 @@
+		<question name="<?php echo $this->name ?>">
+<?php 
+foreach($this->persons as $person => $person_desc) {
+	$question_responses = $responses[$this->name][$person];
+	if (count($question_responses) == 0)
+		continue;
+?>
+			<<?php echo $person; ?>>
+<?php	for($i = 0; $i < count($question_responses); $i++) { 
+		$row = @$question_responses[$i]; ?>
+				<row>
+<?php		foreach($this->fields as $field_name => $field_desc) { 
+			if (strlen(trim($row[$field_name])) == 0)
+				continue;
+?>
+					<<?php echo $field_name ?>><?php echo htmlentities($row[$field_name]) ?></<?php echo $field_name ?>>
+<?php 		} ?>
+				</row>
+<?php 	} ?>
+			</<?php echo $person; ?>>
+<?php } ?>
+		</question>
diff --git a/www/docs/register/questions.php b/www/docs/register/questions.php
new file mode 100644
index 0000000..cda52fd
--- /dev/null
+++ b/www/docs/register/questions.php
@@ -0,0 +1,60 @@
+<?php
+
+$questions[] = new Question("1",
+		"Shareholdings in public and private companies (including holding companies indicating the name of the company or companies)",
+		array('nameofcompany' => "Name of company - (including bolding and subsidiary companies if applicable)"));
+$questions[] = new Question("2-i",
+		"Family and business trusts and nominee companies:<br /> (i) in which a benificial interest is held, indicating the name of the trust, the nature of its operations and benificial interest.",
+		array('name' => "Name of trust/nominee company",
+		      'nature' => "Nature of its operation",
+		      'beneficial' => "Benefical interest"));
+$questions[] = new Question("2-ii",
+		"Family and business trusts and nominee companies:<br /> (ii) in which the Member, the Member's spouse, or a child who is wholly or mainly dependent on the Member for support, is a trustee (but not including a trustee of an estate where no beneficial interest is held by the Member, the Member's spouse or dependent children), indicating the name of the trust, the nature of its operation and the beneficiary of the trust.",
+		array('name' => "Name of trust/nominee company",
+	              'nature' => "Nature of its operation",
+		      'beneficial' => "Benefical interest"));
+$questions[] = new Question("3",
+		"Real estate, including the location (suburb or area only) and the purpose for which it is owned",
+		array("location" => "Location",
+		      "purpose" => "Purpose for which owned"));
+$questions[] = new Question("4",
+		"Registered directorships of companies",
+		array("name" => "Name of company",
+		      "purpose" => "Purpose for which owned"));
+$questions[] = new Question("5",
+		"Partnerships indicating the nature of the interests and the activities of the partnership",
+		array("name" => "Name",
+		      "nature" => "Nature of interests",
+		      "activities" => "Activities of partnership"));
+$questions[] = new Question("6",
+		"Liabilities indicating the nature of the liability and the creditor concerned.",
+		array("nature" => "Nature of liability",
+			"creditor" => "Creditor"));
+$questions[] = new Question("7",
+		"The nature of any bonds, debentures and like investments.",
+		array("type" => "Type of investment",
+			"body" => "Body in which investment is held"));
+$questions[] = new Question("8",
+		"Saving or investment accounts, indicating their natuer and the name of the bank or other institution concerned",
+		array("account" => "Name of account",
+			"institution" => "Name of bank/institution"));
+$questions[] = new Question("9",
+		"The nature of any other assets (excluding household and personal effects) each valued at over $7500",
+		array("name" => "Name of any other assets"));
+$questions[] = new Question("10",
+		"The nature of any other substantial sources of income",
+		array("nature" => "Nature of income"));
+$questions[] = new Question("11",
+		"Gifts valued at more than $750 received from official sources or at more than $300 where received from other than official sources provided that a gift received by a membr, the member's spouse or dependent children from family members or personal friends in a purely personal capacity need not be registered unless the member judges that an appearence of conflict of interest may be seen to exist",
+		array("details" => "Details"));
+$questions[] = new Question("12",
+		"Any sponsored travel or hospitality received where the value of the sponsored travel or hospitality exceeds $300",
+		array("details" => "Details of travel/hospitality"));
+$questions[] = new Question("13",
+		"Membership of any organisation where a conflict of interest with a member's public duties could foreseeably arise or be seen to arise",
+		array("details" => "Details of travel/hospitality"));
+$questions[] = new Question("14",
+		"Any other interests where a conflict of interst with a member's public duties could forseeably arise or be seen to arise.",
+		array("nature" => "Nature of interest"));
+
+
diff --git a/www/docs/register/register-toForm.php b/www/docs/register/register-toForm.php
new file mode 100644
index 0000000..2609421
--- /dev/null
+++ b/www/docs/register/register-toForm.php
@@ -0,0 +1,50 @@
+<form method="post" action="?register=<?php echo $_GET['register'] ?>">
+
+<h1>House of representatives Statement of Registrable Interests</h1>
+
+<input type="submit" name="save" value="save">  
+
+<p>
+	<input type="radio" name="type" <?php if($this->type == "continue") echo "checked";?> value="continue">At dissolution (continuing members)<br />
+  	<input type="radio" name="type" <?php if($this->type == "new") echo "checked";?> value="new">At date of election (newly elected members)
+</p>
+
+<p>Notes ...</p>
+
+<table>
+	<tr>
+		<td>Surname:</td>
+		<td><input type="text" name="surname" value="<?php echo $this->surname ?>"
+			onKeyPress="return noSubmit(event)"></td>
+	</tr><tr>
+		<td>Other names:</td>
+		<td><input type="text" name="othernames" value="<?php echo $this->othernames ?>"
+			onKeyPress="return noSubmit(event)"></td>
+	</tr><tr>
+		<td>Electoral division:</td>
+		<td><input type="text" name="division" value="<?php echo $this->division ?>"
+			onKeyPress="return noSubmit(event)"></td>
+	</tr><tr>
+		<td>State:</td>
+		<td>
+			<select name="state">
+				<option<?php if($this->state == "NSW") echo " selected";?>>NSW</option>
+				<option<?php if($this->state == "QLD") echo " selected";?>>QLD</option>
+				<option<?php if($this->state == "VIC") echo " selected";?>>VIC</option>
+				<option<?php if($this->state == "WA") echo " selected";?>>WA</option>
+				<option<?php if($this->state == "SA") echo " selected";?>>SA</option>
+				<option<?php if($this->state == "TAS") echo " selected";?>>TAS</option>
+				<option<?php if($this->state == "NT") echo " selected";?>>NT</option>
+				<option<?php if($this->state == "ACT") echo " selected";?>>ACT</option>
+			</select>
+		</td>
+	</tr>
+</table>
+
+<?php
+foreach($questions as $question) {
+	$question->toForm($this->questions);
+}
+?>
+</form>
+
diff --git a/www/docs/register/register-toXML.php b/www/docs/register/register-toXML.php
new file mode 100644
index 0000000..836dae6
--- /dev/null
+++ b/www/docs/register/register-toXML.php
@@ -0,0 +1,15 @@
+<xml>
+	<register>
+		<type><?php echo $this->type; ?></type>
+		<surname><?php echo $this->surname; ?></surname>
+		<othernames><?php echo $this->othernames; ?></othernames>
+		<divison><?php echo $this->divison; ?></divison>
+		<state><?php echo $this->state; ?></state>
+<?php
+foreach($questions as $question) {
+	if (count($this->questions[$question->name]) > 0)
+		$question->toXML($this->questions);
+}
+?>
+	</register>
+</xml>
diff --git a/www/docs/register/register.php b/www/docs/register/register.php
new file mode 100644
index 0000000..dfb9f48
--- /dev/null
+++ b/www/docs/register/register.php
@@ -0,0 +1,267 @@
+<?php
+
+class Question {
+	var $persons = array("self" => "Self", "spouse" => "Spouse", "children" => "Dependent Children");
+	var $name;
+	var $description;
+	var $fields;
+	
+	function Question($name, $description, $fields) {
+		$this->name = $name;
+		$this->description = $description;
+		$this->fields = $fields;
+	}
+
+	//
+	// Convert the question values into an XML.
+	//
+	function toXML($responses) {
+		include "question-toXML.php";
+	}
+
+	//
+	// Convert the question values into a HTML form.
+	//
+	function toForm($responses) {
+		include "question-toForm.php";
+	}
+
+	//
+	// Remove any empty values from the responses.
+	//
+	function cleanup(&$responses) {
+		foreach($this->persons as $person => $person_desc) {
+			$question_responses = $responses[$this->name][$person];
+			$question_responses_new = array();
+
+			while(count($question_responses) > 0) {
+				$row = array_shift($question_responses);
+
+				foreach($this->fields as $field_name => $field_desc) {
+					if (strlen(trim($row[$field_name])) == 0)
+						unset($row[$field_name]);
+				}
+				
+				if (count($row) > 0)
+					$question_responses_new[] = $row;
+			}
+
+			if (count($question_responses_new) > 0)
+				$responses[$this->name][$person] = $question_responses_new;
+			else
+				unset($responses[$this->name][$person]);
+		}
+
+		if (count($responses[$this->name]) == 0)
+			unset($responses[$this->name]);
+	} 
+
+
+	//
+	// Remove any empty values from the responses.
+	//
+	function from_xml(&$xml, &$output) {
+		$name = $this->name;
+
+		$xml_question = $xml->xpath("//question[@name='$name']");
+		if (count($xml_question) != 1)
+			return;
+
+		$output[$name] = array();
+		foreach($this->persons as $person => $person_desc) {
+			if (!isset($xml_question[0]->$person))
+				continue;
+
+			$output[$name][$person] = array();
+			foreach($xml_question[0]->$person->row as $row) {
+				$row_output = array();
+				foreach($this->fields as $field_name => $field_desc)
+					$row_output[$field_name] = (String)$row->$field_name;
+
+				$output[$name][$person][] = $row_output;
+			}
+		}
+	} 
+
+}
+
+/* The questions are imported from another file. */
+$questions = array();
+include "questions.php";
+
+$states = array("NSW", "QLD", "VIC", "WA", "SA", "TAS", "NT", "ACT");
+
+class Register {
+	var $type;
+	var $surname;
+	var $othernames;
+	var $divison;
+	var $state;
+
+	static function from_post($topopulate) {
+		global $states, $questions;
+		$r = new Register();
+
+		if ($topopulate['type'] == "new" or $topopulate['type'] == "continue") 
+			$r->type = $topopulate['type'];
+
+		$r->surname = $topopulate['surname'];
+		$r->othernames = $topopulate['othernames'];
+		$r->divison = $topopulate['divison'];
+
+		if (in_array($topopulate['state'], $states))
+			$r->state = $topopulate['state'];
+
+		foreach($questions as $question)
+			$question->cleanup($topopulate['questions']);
+
+		$r->questions = $topopulate['questions'];
+		return $r;
+	}
+
+	static function from_xml(&$xml) {
+		global $states, $questions;
+		$r = new Register();
+
+		if ($xml->type == "new" or $xml->type == "continue") 
+			$r->type = (String)$xml->type;
+		
+		$r->surname = (String)$xml->surname;
+		$r->othernames = (String)$xml->othernames;
+		$r->divison = (String)$xml->divison;
+
+		if (in_array($xml->state, $states))
+			$r->state = (String)$xml->state;
+	
+		$r->questions = array();
+		foreach($questions as $question) {
+			$question->from_xml($xml, $r->questions);
+		}
+
+		return $r;
+	}
+
+	function toForm() {
+		global $questions;
+		include "register-toForm.php";
+	}
+
+	function toXML() {
+		global $questions;
+		ob_start();
+		include "register-toXML.php";
+		$result = ob_get_contents();
+		ob_end_clean();
+		return $result;
+	}
+}
+
+?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ 	<title>Register of Interests Transcriber</title>
+	<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
+	<style>
+table {
+	width: 100%;
+}
+
+input {
+	width: 98%;
+}
+
+td {
+	border: 1px solid blue;
+	text-align: center;
+}
+
+textarea {
+	width: 100%;
+	height: 500px;
+}
+
+	</style>
+	<script type="text/javascript">
+
+function rows(row) {
+	return row.getElementsByTagName('tr');
+}
+
+function cells(row) {
+	return row.getElementsByTagName('td');
+}
+
+function inputs(row) {
+	return row.getElementsByTagName('input');
+}
+
+function addRowOnEnter(event, row) {
+	if (event && event.keyCode == 13) {
+		var table = row.parentNode;
+
+		var tablerows = rows(table);
+		var lastnode = tablerows[tablerows.length-1];
+
+		var newnode = lastnode.cloneNode(true);
+	
+		// Get the index of the old value	
+		var i = parseInt(cells(lastnode)[0].textContent)+1;
+
+		cells(newnode)[0].innerHTML = i+".";
+
+		var input = inputs(lastnode);
+		input[input.length-1].onkeyup = null;
+
+		lastnode.parentNode.appendChild(newnode);
+
+		var newinputs = inputs(rows(table)[tablerows.length-1]);
+		newinputs[0].focus();
+		for(var j = 0; j < newinputs.length; j++) {
+			newinputs[j].value = "";
+			newinputs[j].name = newinputs[j].name.replace('['+(i-2)+']','['+(i-1)+']');
+			newinputs[j].innerHtml = "";
+		}
+		return false;
+	}
+	return true;
+}
+
+function noSubmit(event) {
+	return (event && event.keyCode != 13);
+}
+
+	</script>
+</head>
+<body>
+
+<?php
+	// FIXME: Security bug - should check filename matches a given pattern
+	$register = $_GET['register'].".xml";
+	if (!file_exists($register)) {
+		die("Don't know about that person!");
+	}
+
+	if (isset($_POST['save'])) {
+		try {
+			$registerobj = Register::from_post($_POST);
+			
+			$f = fopen($register, "w"); 
+			fwrite($f, $registerobj->toXML());
+			fclose($f);
+		} catch (Exception $e) {
+			$registerobj = new Register();
+		}
+	} else {
+		try {
+			$registerobj = Register::from_xml(simplexml_load_file($register)->register);
+		} catch (Exception $e) {
+			$registerobj = new Register();
+		}
+	}
+	var_dump($registerobj);
+
+	$registerobj->toForm(); ?>
+
+</body>
+</html>
-- 
1.5.4.3


