Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions application/core/Memory_Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

/**
* Generic data access model, with data stored in memory only.
*
*
* Single "key" only, at this point.
*
*
* Provide additional base models for different persistence choices
* by extending this, and over-riding the load() and store() methods.
*
Expand Down Expand Up @@ -49,7 +49,7 @@ function __construct($origin = null, $keyfield = 'id', $entity = null)

// start with an empty collection
$this->_data = array(); // an array of objects
$this->fields = array(); // an array of strings
$this->_fields = array(); // an array of strings
// and populate the collection
//$this->load(); // UNCOMMENT THIS LINE IF PERSISTENT
}
Expand Down Expand Up @@ -209,7 +209,7 @@ function trailing($count = 10)

/**
* Return filtered records as an array of records.
*
*
* @param type $what Field name to select by
* @param type $which Value to select
* @return type
Expand Down
54 changes: 5 additions & 49 deletions application/core/XML_Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* XML-persisted collection.
*
*
* @author Ian Park
* ------------------------------------------------------------------------
*/
Expand Down Expand Up @@ -40,59 +40,28 @@ function __construct($origin = null, $keyfield = 'id', $entity = null)
$this->load();
}

/**
* Load the collection state appropriately, depending on persistence choice.
* OVER-RIDE THIS METHOD in persistence choice implementations
*/
protected function load()
{

/*
if (($tasks = simplexml_load_file($this->_origin)) !== FALSE)
{
foreach ($tasks as $task) {
$record = new stdClass();
$record->id = (int) $task->id;
$record->task = (string) $task->task;
$record->priority = (int) $task->priority;
$record->size = (int) $task->size;
$record->group = (int) $task->group;
$record->deadline = (string) $task->deadline;
$record->status = (int) $task->status;
$record->flag = (int) $task->flag;

$this->_data[$record->id] = $record;
}
}

// rebuild the keys table
$this->reindex();

*/
if (file_exists(realpath($this->_origin))) {

$this->xml = simplexml_load_file(realpath($this->_origin));
if ($this->xml === false) {
// error so redirect or handle error
header('location: /404.php');
exit;
}

$xmlarray =$this->xml;

//if it is empty;
if(empty($xmlarray)) {
return;
}

//get all xmlonjects into $xmlcontent
$rootkey = key($xmlarray);
$xmlcontent = (object)$xmlarray->$rootkey;

$keyfieldh = array();
$first = true;

//if it is empty;
if(empty($xmlcontent)) {
return;
}
Expand All @@ -102,25 +71,22 @@ protected function load()
foreach ($xmlcontent as $oj) {
if($first){
foreach ($oj as $key => $value) {
$keyfieldh[] = $key;
//var_dump((string)$value);
$keyfieldh[] = $key;
}
$this->_fields = $keyfieldh;
}
$first = false;
$first = false;

//var_dump($oj->children());
$one = new stdClass();

//get objects one by one
foreach ($oj as $key => $value) {
$one->$key = (string)$value;
}
$this->_data[$dataindex++] =$one;
}
$this->_data[$dataindex++] =$one;
}


//var_dump($this->_data);
} else {
exit('Failed to open the xml file.');
}
Expand All @@ -136,21 +102,11 @@ protected function load()
*/
protected function store()
{
/*
// rebuild the keys table
$this->reindex();
//---------------------
*/
if (($handle = fopen($this->_origin, "w")) !== FALSE)
{
/*
fputcsv($handle, $this->_fields);
foreach ($this->_data as $key => $record)
fputcsv($handle, array_values((array) $record));
fclose($handle);
}
// --------------------
*/
$xmlDoc = new DOMDocument( "1.0");
$xmlDoc->preserveWhiteSpace = false;
$xmlDoc->formatOutput = true;
Expand Down
34 changes: 18 additions & 16 deletions application/models/Tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,26 @@ public function __construct()
parent::__construct(APPPATH . '../data/tasks.xml', 'id');
}

public function load()
protected function load()
{
if (($tasks = simplexml_load_file($this->_origin)))
{
foreach ($tasks as $task) {
$record = new stdClass();
$record->id = (int) $task->id;
$record->task = (string) $task->name;
$record->priority = (int) $task->priority;
$record->size = (int) $task->size;
$record->group = (int) $task->group;
$record->deadline = (string) $task->deadline;
$record->status = (int) $task->status;
$record->flag = (int) $task->flag;
$this->_data[$record->id] = $record;
}
}
if (($tasks = simplexml_load_file($this->_origin)) !== FALSE)
{
foreach ($tasks as $task) {
$record = new stdClass();
$record->id = (int) $task->id;
$record->task = (string) $task->task;
$record->priority = (int) $task->priority;
$record->size = (int) $task->size;
$record->group = (int) $task->group;
$record->deadline = (string) $task->deadline;
$record->status = (int) $task->status;
$record->flag = (int) $task->flag;
$this->_data[$record->id] = $record;
}
}
// rebuild the keys table
$this->reindex();
parent::load();
}

public function lessCompletedThanIncomplete() {
Expand Down
114 changes: 52 additions & 62 deletions data/tasks.xml
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,63 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0"?>
<tasks>
<task>
<id>1</id>
<name>COMP1234 assignment</name>
<priority>3</priority>
<size>2</size>
<group>2</group>
<deadline>20170219</deadline>
<status>2</status>
<flag></flag>
</task>
<task>
<id>2</id>
<name>Mow the lawn</name>
<priority>2</priority>
<size>2</size>
<group>1</group>
<deadline></deadline>
<status>2</status>
<flag></flag>
</task>
<task>
<id>3</id>
<name>Wash the car</name>
<priority>2</priority>
<size>2</size>
<group>1</group>
<deadline></deadline>
<status>1</status>
<flag></flag>
</task>
<task>
<id>4</id>
<name>Paint the fence</name>
<priority>1</priority>
<size>2</size>
<group>1</group>
<deadline></deadline>
<status>1</status>
<flag></flag>
</task>
<task>
<id>6</id>
<name>Intramural hockey game</name>
<priority>1</priority>
<size>2</size>
<group>4</group>
<deadline></deadline>
<status>1</status>
<flag></flag>
</task>
<task>
<id>7</id>
<name>Canucks hockey game</name>
<priority>3</priority>
<size>3</size>
<group>4</group>
<deadline>20170305</deadline>
<status>2</status>
<flag></flag>
</task>
</tasks>
<task>
<id>1</id>
<task>COMP1234 assignment</task>
<priority>3</priority>
<size>2</size>
<group>2</group>
<deadline>20170219</deadline>
<status>2</status>
<flag/>
</task>
<task>
<id>2</id>
<task>Mow the lawn</task>
<priority>2</priority>
<size>2</size>
<group>1</group>
<deadline/>
<status>2</status>
<flag/>
</task>
<task>
<id>3</id>
<task>Wash the car</task>
<priority>2</priority>
<size>2</size>
<group>1</group>
<deadline/>
<status>1</status>
<flag/>
</task>
<task>
<id>4</id>
<task>Paint the fence</task>
<priority>1</priority>
<size>2</size>
<group>1</group>
<deadline/>
<status>1</status>
<flag/>
</task>
<task>
<id>6</id>
<task>Intramural hockey game</task>
<priority>1</priority>
<size>2</size>
<group>4</group>
<deadline/>
<status>1</status>
<flag/>
</task>
</tasks>