From 2790c385808eef4fd442f20a659c4506e4ba9a2f Mon Sep 17 00:00:00 2001 From: Lesenya Date: Fri, 9 Dec 2016 07:23:27 -0800 Subject: [PATCH 1/3] Add files via upload --- index1.php | 109 ++++++++++++++++++++++++++++++++++++++++++++++++ update_task.php | 7 +++- 2 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 index1.php diff --git a/index1.php b/index1.php new file mode 100644 index 0000000..ea44966 --- /dev/null +++ b/index1.php @@ -0,0 +1,109 @@ + + */ +?> + + + + Basic Task Manager + + + + + + + + +
+ +
+ + + + + \ No newline at end of file diff --git a/update_task.php b/update_task.php index 4c69960..bdac9b7 100644 --- a/update_task.php +++ b/update_task.php @@ -2,6 +2,9 @@ /** * 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 +require('task.class.php'); +$task = new Task(); +$task->TaskName = $_POST['name']; +$task->TaskDescription = $_POST['description']; +$task->Save(); ?> \ No newline at end of file From a7ca022d23d406ff684f4e3ac7b6eacf29d5128b Mon Sep 17 00:00:00 2001 From: Lesenya Date: Mon, 12 Dec 2016 19:26:14 +0200 Subject: [PATCH 2/3] Add files via upload --- index.php | 34 +++++++++++++++++------ task.class.php | 73 +++++++++++++++++++++++++++++++++++++++++++++---- update_task.php | 17 ++++++++++-- 3 files changed, 107 insertions(+), 17 deletions(-) diff --git a/index.php b/index.php index ea44966..7ed76c1 100644 --- a/index.php +++ b/index.php @@ -29,18 +29,18 @@
- +
- +
@@ -95,14 +95,32 @@ console.log('Task ID: '+triggerElement.attr("id")); } }); + $(function(){ $('#saveTask').click(function() { - //Assignment: Implement this functionality - alert('You clicked save! Now implement this functionality.'); + var TaskToSave = $(this).attr("id"); + //Perform the ajax post + $.post("update_task.php",{$("#InputTaskDescription")}, + function(data){ + $("#InputTaskDescription").text(data); + }); + $.post("update_task.php",{$("#InputTaskName")}, + function(data){ + $("#InputTaskName").text(data); + }); + alert('Saved'); + }); $('#myModal').modal('hide'); }); + $(function(){ $('#deleteTask').click(function() { - //Assignment: Implement this functionality - alert('You clicked delete! Now implement this functionality.'); + //Get id from the link + //Perform the ajax post + $.post("update_task.php",{$("#InputTaskName")}, + function(data){ + $("#TaskList").text(data); + }); + alert('Task successfully deleted'); + }); $('#myModal').modal('hide'); }); diff --git a/task.class.php b/task.class.php index e0fd225..6b0fcf8 100644 --- a/task.class.php +++ b/task.class.php @@ -6,6 +6,7 @@ class Task { public $TaskId; public $TaskName; public $TaskDescription; + public $fileName = "testTask.txt"; public function __construct($Id = null) { if ($Id) { // This is an existing task @@ -16,23 +17,83 @@ public function __construct($Id = null) { } } protected function Create() { - // This function needs to generate a new unique ID for the task - // Assignment: Generate unique id for the new task + $this->TaskId = uniqid(); $this->TaskName = ''; $this->TaskDescription = ''; } protected function LoadFromId($Id = null) { if ($Id) { // Assignment: Code to load details here... + //Load file data + $fileData = file_get_contents($this->fileName); + //put file data into array + $jsonData = json_decode($fileData,true); + foreach($jsonData as $value) + { + if(!is_array($value)) + { + + } + else + { + foreach($value as $val) + { + if($Id == $value["id"]) + { + $this->TaskName = $value["name"]; + $this->TaskDescription = $value["description"]; + } + } + } + } + } else return null; } - public function Save() { - //Assignment: Code to save task here + public function Save() + { + $data = array('id'=>$this->TaskId,'name'=>$this->TaskName,'description'=>$this->TaskDescription); + $arr_data = array(); + $file_data = file_get_contents($this->fileName); + $arr_data = json_decode($file_data,true); + $arr_data[] = $data; + $jsonMobiles = json_encode($arr_data,JSON_PRETTY_PRINT); + if(file_put_contents($this->fileName,$jsonMobiles)) + { + echo "Success"; + } + else + { + echo "Error"; + } } - public function Delete() { - //Assignment: Code to delete task here + public function Delete() + { + //Load file data + $fileData = file_get_contents($this->fileName); + //put file data into array + $jsonData = json_decode($fileData,true); + foreach($jsonData as $value) + { + if(!is_array($value)) + { + + } + else + { + foreach($value as &$val) + { + if($this->TaskName == $value["name"]) + { + unset($val); + $jsonEncode = json_encode($jsonData,JSON_PRETTY_PRINT); + file_put_contents($this->fileName,$jsonEncode); + } + } + } + } + } } ?> \ No newline at end of file diff --git a/update_task.php b/update_task.php index bdac9b7..cc70ca4 100644 --- a/update_task.php +++ b/update_task.php @@ -4,7 +4,18 @@ */ require('task.class.php'); $task = new Task(); -$task->TaskName = $_POST['name']; -$task->TaskDescription = $_POST['description']; -$task->Save(); +$task->TaskName = $_POST['InputTaskName']; +$task->TaskDescription = $_POST['InputTaskDescription']; +if(isset($_POST['InputTaskDescription'])&&isset($_POST['InputTaskName'])) + { + if(empty($_POST['InputTaskName])||empty($_POST['InputTaskDescription'])) + { + echo "Fields required"; + } + else + { + $task->Save(); + } + } + ?> \ No newline at end of file From 5450bc50d18a1b53c9b94915f4de914108eeb654 Mon Sep 17 00:00:00 2001 From: Lesenya Date: Wed, 28 Dec 2016 00:24:06 +0200 Subject: [PATCH 3/3] Add files via upload Everything is working. I have tested all the functions --- Task_Data.txt | 37 +++++++++++ index.php | 76 +++++++++++----------- list_tasks.php | 31 +++++++++ task.class.php | 169 +++++++++++++++++++++++++++--------------------- update_task.php | 44 ++++++++----- 5 files changed, 228 insertions(+), 129 deletions(-) create mode 100644 list_tasks.php diff --git a/Task_Data.txt b/Task_Data.txt index e69de29..6697904 100644 --- a/Task_Data.txt +++ b/Task_Data.txt @@ -0,0 +1,37 @@ +[ + { + "TaskId": 1, + "TaskName": "Test", + "TaskDescription": "Test" + }, + { + "TaskId": "2", + "TaskName": "Test2", + "TaskDescription": "Test2" + }, + { + "TaskId": 3, + "TaskName": "Lesenya", + "TaskDescription": "Lesenya Man" + }, + { + "TaskId": 4, + "TaskName": "Mookho", + "TaskDescription": "My Wife" + }, + { + "TaskId": 5, + "TaskName": "Likeleli", + "TaskDescription": "My girl" + }, + { + "TaskId": 6, + "TaskName": "Demy", + "TaskDescription": "My life" + }, + { + "TaskId": 7, + "TaskName": "Keneuoe", + "TaskDescription": "My sister" + } +] \ No newline at end of file diff --git a/index.php b/index.php index 7ed76c1..07754c2 100644 --- a/index.php +++ b/index.php @@ -29,18 +29,18 @@
- +
- +
@@ -60,18 +60,6 @@
@@ -83,45 +71,53 @@ \ 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 6b0fcf8..8c8824b 100644 --- a/task.class.php +++ b/task.class.php @@ -3,11 +3,21 @@ * This class handles the modification of a task object */ class Task { - public $TaskId; - public $TaskName; - public $TaskDescription; - public $fileName = "testTask.txt"; + //private fields + private $TaskId; + private $TaskName; + private $TaskDescription; + protected $TaskDataSource; + //Constructor to provide default values public function __construct($Id = null) { + $this->TaskDataSource = file_get_contents('Task_Data.txt'); + if (strlen($this->TaskDataSource) > 0) + $this->TaskDataSource = json_decode($this->TaskDataSource); // 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 ($Id) { // This is an existing task $this->LoadFromId($Id); @@ -17,83 +27,96 @@ public function __construct($Id = null) { } } protected function Create() { - $this->TaskId = uniqid(); - $this->TaskName = ''; - $this->TaskDescription = ''; + // This function needs to generate a new unique ID for the task + $this->setTaskId(); + //$this->TaskName = 'New Task'; + //$this->TaskDescription = 'New Description'; + $this->setTaskName(); + $this->setTaskDescription(); + } + //functions to get task properties(Data Encapsulation) + public function getTaskName(){ + return $this->TaskName; + } + public function getTaskDescription(){ + return $this->TaskDescription; + } + public function getTaskId(){ + return $this->TaskId; + } + //functions to set tast properties(Data encapsulation) + public function setTaskName($name = "New Task"){ + $this->TaskName = $name; + } + public function setTaskId(){ + $this->TaskId = $this->getUniqueId(); + } + public function setTaskDescription($description = "New Description"){ + $this->TaskDescription = $description; + } + + protected function getUniqueId() { + // Assignment: Code to get new unique ID + $arr_data = array(); + $file_data = file_get_contents("Task_Data.txt"); + $arr_data = json_decode($file_data,true); + $arr_id = array(); + //Initialise counter variable + $i = 0; + //Put all ids in an array and assign new Id to the number next to the highest value in the array + foreach($arr_data as $value){ + $arr_id[$i] = $value["TaskId"]; + $i++; + } + sort($arr_id); + return ($arr_id[count($arr_id)-1]+1); } protected function LoadFromId($Id = null) { if ($Id) { // Assignment: Code to load details here... - //Load file data - $fileData = file_get_contents($this->fileName); - //put file data into array - $jsonData = json_decode($fileData,true); - foreach($jsonData as $value) - { - if(!is_array($value)) - { - - } - else - { - foreach($value as $val) - { - if($Id == $value["id"]) - { - $this->TaskName = $value["name"]; - $this->TaskDescription = $value["description"]; - } + //Create an empty array + $arr_data = array(); + //load all file contents + $file_data = file_get_contents("Task_Data.txt"); + //Decode data and put it into associative array + $arr_data = json_decode($file_data,true); + foreach($arr_data as $value){ + if($Id==$value["TaskId"]){ + $this->TaskId = $value["TaskId"]; + $this->setTaskName($value["TaskName"]); + $this->setTaskDescription($value["TaskDescription"]); } - } - } - - } else - return null; + } + } else{ + return null; + } } - public function Save() - { - $data = array('id'=>$this->TaskId,'name'=>$this->TaskName,'description'=>$this->TaskDescription); - $arr_data = array(); - $file_data = file_get_contents($this->fileName); - $arr_data = json_decode($file_data,true); - $arr_data[] = $data; - $jsonMobiles = json_encode($arr_data,JSON_PRETTY_PRINT); - if(file_put_contents($this->fileName,$jsonMobiles)) - { - echo "Success"; - } - else - { - echo "Error"; - } + public function Save() { + //Assignment: Code to save task here + $my_data = array("TaskId"=>$this->getTaskId(),"TaskName"=>$this->getTaskName(),"TaskDescription"=>$this->getTaskDescription()); + //append data to the end of file data + $this->TaskDataSource[] = $my_data; + file_put_contents("Task_Data.txt",json_encode($this->TaskDataSource,JSON_PRETTY_PRINT)); } - public function Delete() - { - //Load file data - $fileData = file_get_contents($this->fileName); - //put file data into array - $jsonData = json_decode($fileData,true); - foreach($jsonData as $value) - { - if(!is_array($value)) - { - - } - else - { - foreach($value as &$val) - { - if($this->TaskName == $value["name"]) - { - unset($val); - $jsonEncode = json_encode($jsonData,JSON_PRETTY_PRINT); - file_put_contents($this->fileName,$jsonEncode); - } - } - } - } - + public function Delete() { + //Assignment: Code to delete task here + //Create an empty array + $arr_data = array(); + //Read all file contents + $file_data = file_get_contents("Task_Data.txt"); + //Decode data and put it into associative array + $arr_data = json_decode($file_data,true); + //Declare and initialize a counter variable + $i = 0; + foreach($arr_data as $key=>$value){ + if(($this->TaskName==$value["TaskName"])&&($this->TaskDescription==$value["TaskDescription"])){ + unset($arr_data[$i]); + } + $i++; + } + $arr_data = array_values($arr_data);//re-index the array after deletion + file_put_contents("Task_Data.txt",json_encode($arr_data,JSON_PRETTY_PRINT)); } } ?> \ No newline at end of file diff --git a/update_task.php b/update_task.php index cc70ca4..88ef454 100644 --- a/update_task.php +++ b/update_task.php @@ -1,21 +1,33 @@ TaskName = $_POST['InputTaskName']; -$task->TaskDescription = $_POST['InputTaskDescription']; -if(isset($_POST['InputTaskDescription'])&&isset($_POST['InputTaskName'])) - { - if(empty($_POST['InputTaskName])||empty($_POST['InputTaskDescription'])) - { - echo "Fields required"; - } - else - { - $task->Save(); - } - } +require('Task.class.php'); +// Assignment: Implement this script +$tas = new Task(); +if(isset($_POST['mysave'])){ + if($_POST['mysave']=="savetask"){ +if(isset($_POST['TaskName'])||isset($_POST['TaskDescription'])){ + $tas->setTaskName($_POST['TaskName']); + $tas->setTaskDescription($_POST['TaskDescription']); + $tas->Save(); + echo $tas->getTaskName(); +} + } +} + +if(isset($_POST['mydelete'])){ + if($_POST['mydelete']=="deletetask"){ +if(isset($_POST['TaskName'])||isset($_POST['TaskDescription'])){ + $tas->setTaskName($_POST['TaskName']); + $tas->setTaskDescription($_POST['TaskDescription']); + $tas->Delete(); + echo $tas->getTaskName(); +} + } +} + ?> \ No newline at end of file