From f0d1d0cf2879896dca32c6c4461724f62561c129 Mon Sep 17 00:00:00 2001 From: ntimane Date: Fri, 22 Feb 2019 12:56:01 +0200 Subject: [PATCH] Changes made to assessment for todo list --- Task_Data.txt | 1 + index.php | 82 +++++++++++++++++++++++++++++-------- task.class.php | 105 ++++++++++++++++++++++++++++++++++++++++++------ update_task.php | 20 ++++++++- 4 files changed, 178 insertions(+), 30 deletions(-) diff --git a/Task_Data.txt b/Task_Data.txt index e69de29..3bc631d 100644 --- a/Task_Data.txt +++ b/Task_Data.txt @@ -0,0 +1 @@ +{"4":{"TaskId":false,"TaskName":"Eish son","TaskDescription":"goo"},"5":{"TaskId":false,"TaskName":"Hmmm","TaskDescription":"new"},"6":{"TaskId":false,"TaskName":"Stratu","TaskDescription":"solve"},"7":{"TaskId":false,"TaskName":"sss","TaskDescription":"dddd"},"8":{"TaskId":false,"TaskName":"Buy Eggs","TaskDescription":"eggs are from chickens"}} \ No newline at end of file diff --git a/index.php b/index.php index ea44966..606d058 100644 --- a/index.php +++ b/index.php @@ -40,7 +40,7 @@ @@ -60,18 +60,6 @@
- -

Task Name

-

Task Description

-
- -

Task Name

-

Task Description

-
- -

Task Name

-

Task Description

-
@@ -83,27 +71,87 @@ \ No newline at end of file diff --git a/task.class.php b/task.class.php index e0fd225..d31f30c 100644 --- a/task.class.php +++ b/task.class.php @@ -6,33 +6,114 @@ class Task { public $TaskId; public $TaskName; public $TaskDescription; + protected $TaskDataSource; + public function __construct($Id = null) { - if ($Id) { - // This is an existing task - $this->LoadFromId($Id); - } else { - // This is a new task + $this->TaskDataSource = file_get_contents('Task_Data.txt'); + if (strlen($this->TaskDataSource) > 0) + $this->TaskDataSource = json_decode($this->TaskDataSource, true); // Should decode to an array of Task objects + else + $this->TaskDataSource = array(); // If it does not, then the data source is assumed to be empty and we create an empty array + + 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 + //unique id code taken from http://php.net/manual/en/function.uniqid.php (44 )by hackan at gmail dot com + if (!empty($this->TaskDataSource)) { + // Assignment: Code to get new unique ID + // uniqid gives 13 chars, but you could adjust it to your needs. + $length = 0; + if (function_exists("random_bytes")) { + $bytes = random_bytes(ceil($length / 2)); + } elseif (function_exists("openssl_random_pseudo_bytes")) { + $bytes = openssl_random_pseudo_bytes(ceil($length / 2)); + } else { + throw new Exception("no cryptographically secure random function available"); + } + return substr(bin2hex($bytes), 0, $length); + } + return -1; // Placeholder return for now } + protected function LoadFromId($Id = null) { - if ($Id) { - // Assignment: Code to load details here... - } else - return null; + $Id = (int) $Id; + if ($Id && !empty($this->TaskDataSource)) { + foreach($this->TaskDataSource as $dataSource) { + if ($Id == $dataSource['TaskId']) { + $this->TaskId = $dataSource['TaskId']; + $this->TaskName = $dataSource['TaskName']; + $this->TaskDescription = $dataSource['TaskDescription']; + + return true; + } + } + } + + return null; } public function Save() { //Assignment: Code to save task here + $display_message = ['message' => 'Nothing was found', 'success' => false]; + if (isset($_POST)) { + + $this->TaskId = filter_input(INPUT_POST, 'task_id', FILTER_SANITIZE_NUMBER_INT); + $this->TaskName = filter_input(INPUT_POST, 'task_name', FILTER_SANITIZE_STRING); + $this->TaskDescription = filter_input(INPUT_POST, 'task_description', FILTER_SANITIZE_STRING); + $updated = false; + + if ($this->TaskId > 0 && !empty($this->TaskDataSource)) { + foreach ($this->TaskDataSource as $key=>$dataSource) { + if ($this->TaskId == $dataSource['TaskId']) { + $this->TaskDataSource[$key] = ['TaskId'=>$this->TaskId, 'TaskName' =>$this->TaskName, 'TaskDescription' => $this->TaskDescription]; + $updated = true; + $display_message = ['message' => 'Task has been successfully updated', 'success' => true]; + break; + } + } + } + + if (!$updated) { + $this->TaskId = $this->getUniqueId(); + $this->TaskDataSource[] = ['TaskId'=>$this->TaskId, 'TaskName' =>$this->TaskName, 'TaskDescription' => $this->TaskDescription]; + $display_message = ['message' => 'Task has been successfully added', 'success' => true]; + } + + file_put_contents('Task_Data.txt', json_encode($this->TaskDataSource)); + } + echo json_encode($display_message); + exit(); } + public function Delete() { //Assignment: Code to delete task here + $display_message = ['message' => 'Entered task not found', 'success' => false, 'task Id'=>$this->TaskId]; + if (!is_null($this->TaskId) && $this->TaskId > 0) { + foreach ($this->TaskDataSource as $key=>$dataSource) { + if ($this->TaskId == $dataSource['TaskId']) { + unset($this->TaskDataSource[$key]); + file_put_contents('Task_Data.txt', json_encode($this->TaskDataSource)); + $display_message = ['message' => 'Tasks successfully deleted', 'success' => true]; + } + } + } + + echo json_encode($display_message); + exit; + } } ?> \ No newline at end of file diff --git a/update_task.php b/update_task.php index 4c69960..16ed86c 100644 --- a/update_task.php +++ b/update_task.php @@ -2,6 +2,24 @@ /** * 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'); +require_once('task.class.php'); // Assignment: Implement this script + +if (isset($_POST) ) { + $action = filter_input(INPUT_POST, 'action', FILTER_SANITIZE_STRING); + $taskId = filter_input(INPUT_POST, 'task_id', FILTER_SANITIZE_NUMBER_INT); + + $taskClass = new Task($taskId); + + if ($action == 'save') { + $taskClass->Save(); + } + + if ($action == 'delete') { + $taskClass->Delete(); + } +} + +echo json_encode(['message' => 'You either tried accessing this directly or submitted an invalid action. Please trying saving the form again', 'success'=>false]); +exit; ?> \ No newline at end of file