diff --git a/.idea/Stratusolve-Exercise.iml b/.idea/Stratusolve-Exercise.iml index c956989..b313593 100644 --- a/.idea/Stratusolve-Exercise.iml +++ b/.idea/Stratusolve-Exercise.iml @@ -4,5 +4,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 19f74da..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 77e0469..8bd72f9 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,7 @@ - + @@ -14,7 +14,6 @@ - @@ -23,26 +22,24 @@ - + - - + + - + - - + + - - @@ -51,18 +48,18 @@ - - + + - - + + - - + + @@ -76,34 +73,27 @@ - + + true - - - + DEFINITION_ORDER - - - - - - - - + + - @@ -116,10 +106,10 @@ + + - - @@ -128,22 +118,14 @@ - - - - - - + + - + @@ -168,43 +150,68 @@ + project + + + + + - + + 1463122043916 + + - + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + @@ -215,39 +222,105 @@ - - + + + + + + + + + + + + + + + + + + + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + + + + + + + \ No newline at end of file diff --git a/Task_Data.txt b/Task_Data.txt index e69de29..eb99a7e 100644 --- a/Task_Data.txt +++ b/Task_Data.txt @@ -0,0 +1 @@ +[{"TaskId":1,"TaskName":"Test","TaskDescription":"Test"},{"TaskId":"2","TaskName":"Test2","TaskDescription":"Test2"}] \ No newline at end of file diff --git a/index.php b/index.php index ea44966..ffa9582 100644 --- a/index.php +++ b/index.php @@ -60,18 +60,6 @@
@@ -83,27 +71,50 @@ \ No newline at end of file diff --git a/list_tasks.php b/list_tasks.php new file mode 100644 index 0000000..4f43640 --- /dev/null +++ b/list_tasks.php @@ -0,0 +1,31 @@ + + * Task_Data.txt is expected to be a json encoded string, e.g: [{"TaskId":1,"TaskName":"Test","TaskDescription":"Test"},{"TaskId":"2","TaskName":"Test2","TaskDescription":"Test2"}] + */ +$taskData = file_get_contents('Task_Data.txt'); +$html = ' +

No Tasks Available

+

Click here to create one

+
'; +if (strlen($taskData) < 1) { + die($html); +} +$taskArray = json_decode($taskData); +if (sizeof($taskArray) > 0) { + $html = ''; + foreach ($taskArray as $task) { + $html .= ' +

'.$task->TaskName.'

+

'.$task->TaskDescription.'

+
'; + } +} +die($html); +?> \ No newline at end of file diff --git a/task.class.php b/task.class.php index e0fd225..352bda3 100644 --- a/task.class.php +++ b/task.class.php @@ -6,33 +6,82 @@ class Task { public $TaskId; public $TaskName; public $TaskDescription; + protected $TaskDataSource; + protected $TaskDataSourcePath = 'Task_Data.txt'; public function __construct($Id = null) { - if ($Id) { - // This is an existing task - $this->LoadFromId($Id); - } else { - // This is a new task + $this->TaskDataSource = $this->loadFromFile($this->TaskDataSourcePath); + if (!$this->TaskDataSource) + $this->TaskDataSource = array(); // If it does not, then the data source is assumed to be empty and we create an empty array + if (!$this->LoadFromId($Id)) $this->Create(); - } } protected function Create() { // This function needs to generate a new unique ID for the task // Assignment: Generate unique id for the new task - $this->TaskName = ''; - $this->TaskDescription = ''; + $this->TaskId = $this->getUniqueId(); + $this->TaskName = 'New Task'; + $this->TaskDescription = 'New Description'; + } + protected function getUniqueId() { + // Assignment: Code to get new unique ID + $newID = count($this->TaskDataSource); + if($newID == 0) { + ++$newID; + return $newID; + } else { + sort($this->TaskDataSource); + if ($newID < (int)end($this->TaskDataSource)->TaskId) { + $newID = (int)end($this->TaskDataSource)->TaskId; + ++$newID; + } + return $newID; + } + } + public function loadFromFile($Path) { + $TempTaskDataSource = file_get_contents($Path); + if (strlen($TempTaskDataSource) > 0) + $TempTaskDataSource = json_decode($TempTaskDataSource); // Should decode to an array of Task objects + else + $TempTaskDataSource = array(); // If it does not, then the data source is assumed to be empty and we create an empty array + return $TempTaskDataSource; + } protected function LoadFromId($Id = null) { if ($Id) { - // Assignment: Code to load details here... + $this->TaskDataSource = $this->loadFromFile($this->TaskDataSourcePath); + foreach ($this->TaskDataSource as $task) { + if($Id == $task->TaskId) { + return json_encode($task); + } + } } else return null; } - public function Save() { - //Assignment: Code to save task here + if($this->TaskId == -1) { + $this->TaskId = $this->getUniqueId(); + array_push($this->TaskDataSource, $this); + } else { + foreach ($this->TaskDataSource as $task) { + if($this->TaskId == $task->TaskId) { + $task->TaskName = $this->TaskName; + $task->TaskDescription = $this->TaskDescription; + } + } + } + file_put_contents('Task_Data.txt', json_encode($this->TaskDataSource)); + echo $this->TaskId; } public function Delete() { - //Assignment: Code to delete task here + $this->TaskDataSource = $this->loadFromFile($this->TaskDataSourcePath); + //echo '$this->TaskId=' . $this->TaskId . ' | $this->TaskName=' . $this->TaskName . ' | $this->TaskDescription=' . $this->TaskDescription . '\n
/n'; + for ($pos = 0; $pos < count($this->TaskDataSource); $pos++) { + if ($this->TaskDataSource[$pos]->TaskId == $this->TaskId) { + unset($this->TaskDataSource[$pos]); + } + } + file_put_contents($this->TaskDataSourcePath, json_encode($this->TaskDataSource)); + echo $this->TaskId; } } ?> \ No newline at end of file diff --git a/update_task.php b/update_task.php index 4c69960..e8a4d37 100644 --- a/update_task.php +++ b/update_task.php @@ -2,6 +2,18 @@ /** * This script is to be used to receive a POST with the object information and then either updates, creates or deletes the task object */ -require('Task.class.php'); -// Assignment: Implement this script -?> \ No newline at end of file +require('task.class.php'); + +$action = $_POST['action']; +$task = new Task(); +$taskToUpdate = $_POST['taskToUpdate']; +$task->TaskId = $taskToUpdate['TaskId']; +$task->TaskName = $taskToUpdate['TaskName']; +$task->TaskDescription = $taskToUpdate['TaskDescription']; +if ($action == 'update') { + $task->Save(); +} else if ($action == 'delete') { + $task->Delete(); +} + +?>