From 51f3ec2c0c843a7afc093889b059bd4d20444311 Mon Sep 17 00:00:00 2001
From: deadlocker8 <deadlocker@gmx.de>
Date: Wed, 2 Nov 2016 18:22:31 +0100
Subject: [PATCH] implemented adminarea --> add/update/delete task

---
 js/main.js                       |  87 ++++++++++++++++++
 php/admin/admin-edit-task.php    | 146 +++++++++++++++++++++++++++++++
 php/admin/admin-tasks.php        | 119 +++++++++++++++++++++++++
 php/admin/helper/delete-task.php |  30 +++++++
 php/admin/helper/edit-task.php   |  77 ++++++++++++++++
 php/language.json                |   1 +
 php/mysql.php                    |  12 ++-
 7 files changed, 470 insertions(+), 2 deletions(-)
 create mode 100644 php/admin/admin-edit-task.php
 create mode 100644 php/admin/admin-tasks.php
 create mode 100644 php/admin/helper/delete-task.php
 create mode 100644 php/admin/helper/edit-task.php

diff --git a/js/main.js b/js/main.js
index 8cc5beb..26f4d58 100644
--- a/js/main.js
+++ b/js/main.js
@@ -43,6 +43,11 @@ $(document).ready(function()
 		editMilestone(this.dataset.id, this.dataset.roadmapid);
 	});
 
+	$('.button-save-task').click(function()
+	{
+		editTask(this.dataset.id, this.dataset.milestoneid);
+	});
+
 	$('.button-delete-roadmap').click(function()
 	{
 		var r = confirm("Do you really want to delete this roadmap?");
@@ -61,6 +66,15 @@ $(document).ready(function()
 		}
 	});
 
+	$('.button-delete-task').click(function()
+	{
+		var r = confirm("Do you really want to delete this task?");
+		if(r == true)
+		{
+			deleteTask(this.dataset.id, this.dataset.milestoneid);
+		}
+	});
+
 	$('#checkbox-done').click(function()
 	{
 		var checked = document.getElementById("checkbox-done").checked;
@@ -319,4 +333,77 @@ function deleteMilestone(milestone_ID, roadmap_ID)
 				alert('An error occurred while deleting the milestone with the ID ' + milestone_ID);
 			}
 		});
+}
+
+function editTask(task_ID, milestone_ID)
+{
+	var edit = document.getElementById('edit').innerHTML;
+	var title = $('#title').val();
+	var description = $('#description').val();;
+	var done = document.getElementById("checkbox-done").checked;
+
+	if(isNull(title))
+	{
+		alert("Title shouldn't be empty!");
+		return;
+	}
+
+	if(done)
+	{
+		done = 1;
+	}
+	else
+	{
+		done = 0;
+	}
+
+	$.post('../admin/helper/edit-task.php',
+		{
+			"title": title,
+			"description": description,
+			"done": done,
+			"edit": edit,
+			"ID": task_ID,
+			"milestone-ID": milestone_ID
+
+		}, function(data, error)
+		{
+			data = data.toString().trim();
+			switch(data)
+			{
+				case "error":
+					alert('An error occurred');
+					break;
+				case "error-edit":
+					alert('An error occurred while editing the task with the ID ' + task_ID);
+					break;
+				case "error-insert":
+					alert('An error occurred while inserting the new task');
+					break;
+				default:
+					window.location.href = "../admin/admin-tasks.php?id=" + milestone_ID;
+					break;
+			}
+		});
+}
+
+function deleteTask(task_ID, milestone_ID)
+{
+	$.post('../admin/helper/delete-task.php',
+		{
+			"task_ID": task_ID,
+
+		}, function(data, error)
+		{
+			data = data.toString().trim();
+
+			if(data != "error")
+			{
+				window.location.href = "../admin/admin-tasks.php?id=" + milestone_ID;
+			}
+			else
+			{
+				alert('An error occurred while deleting the task with the ID ' + task_ID);
+			}
+		});
 }
\ No newline at end of file
diff --git a/php/admin/admin-edit-task.php b/php/admin/admin-edit-task.php
new file mode 100644
index 0000000..1f84210
--- /dev/null
+++ b/php/admin/admin-edit-task.php
@@ -0,0 +1,146 @@
+<!DOCTYPE html>
+
+<?php
+include_once('../getLanguageJSON.php');
+include_once('../mysql.php');
+
+
+if(!isset($_GET['milestoneID']))
+{
+	header('Location: ../error.php?message=error_param_missing');
+	exit;
+}
+
+$milestoneID = $_GET['milestoneID'];
+if(!is_numeric($milestoneID) || $milestoneID < 1)
+{
+	header('Location: ../error.php?message=error_param_invalid');
+	exit;
+}
+
+
+if(!isset($_GET['edit']))
+{
+	$_GET['edit'] = "false";
+
+	$ID = 0;
+
+	$db = new DB();
+	$db->createTables();
+}
+else
+{
+	if(!isset($_GET['id']))
+	{
+		header('Location: ../error.php?message=error_param_missing');
+		exit;
+	}
+
+	$ID = $_GET['id'];
+	if(!is_numeric($ID) || $ID < 1)
+	{
+		header('Location: ../error.php?message=error_param_invalid');
+		exit;
+	}
+
+	$db = new DB();
+	$db->createTables();
+
+	$task = $db->getTask($ID);
+	if($task == false)
+	{
+		header('Location: ../error.php?message=error_task_not_existing');
+		exit;
+	}
+}
+?>
+<html xmlns="http://www.w3.org/1999/html">
+	<head>
+		<meta charset="UTF-8"/>
+		<?php
+		if($_GET['edit'] == "false")
+		{
+			echo '<title>New Task</title>';
+		}
+		else
+		{
+			echo '<title>Edit Task</title>';
+		}
+		?>
+		<!--Import Google Icon Font-->
+		<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
+		<!--Import materialize.css-->
+		<link type="text/css" rel="stylesheet" href="../../materialize/css/materialize.min.css" media="screen,projection"/>
+		<link type="text/css" rel="stylesheet" href="../../css/style.css"/>
+
+		<!--Import jQuery before materialize.js-->
+		<script type="text/javascript" src="../../js/jquery-2.2.4.min.js"></script>
+		<script type="text/javascript" src="../../materialize/js/materialize.min.js"></script>
+		<script type="text/javascript" src="../../js/main.js"></script>
+		<script type="text/javascript" src="../../js/ResizeSensor.js"></script>
+		<script type="text/javascript" src="../../js/ElementQueries.js"></script>
+
+		<!--Let browser know website is optimized for mobile-->
+		<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
+	</head>
+
+	<body class="grey lighten-3">
+		<div class="hide" id="edit"><?php echo $_GET['edit'];?></div>
+		<div id="main">
+			<div class="container">
+				<?php
+				if($_GET['edit'] == "false")
+				{
+					echo '<h2 class="center-align" id="headline">New Task</h2>';
+				}
+				else
+				{
+					echo '<h2 class="center-align" id="headline">Edit Task</h2>';
+				}
+				?>
+
+				<div class="row center-align">
+					<div class="col s6 m8 offset-m2 l6 offset-l3">
+						<div class="input-field col s12">
+							<input id="title" name="title" type="text" value="<?php if(isset($task)){echo $task['Title'];}?>">
+							<label for="title">Title</label>
+						</div>
+					</div>
+				</div>
+				<div class="row center-align">
+					<div class="col s6 m8 offset-m2 l6 offset-l3">
+						<div class="input-field col s12">
+							<input id="description" name="description" type="text" value="<?php if(isset($task)){echo $task['Description'];}?>">
+							<label for="description">Description</label>
+						</div>
+					</div>
+				</div>
+				<div class="row center-align">
+					<div class="col s6 m8 offset-m2 l6 offset-l3">
+						<div class="col s12 left-align">
+							<input type="checkbox" id="checkbox-done"
+							<?php
+								if(isset($task))
+								{
+									if($task['Status'] == "1")
+									{
+										echo "checked";
+									}
+								}
+							?>
+							/>
+							<label for="checkbox-done">Done</label>
+						</div>
+					</div>
+				</div>
+
+				<div class="row center-align margin-top">
+					<div class="col s12 m8 offset-m2 l6 offset-l3">
+						<a class="waves-effect waves-light btn blue darken-3" href="admin-tasks.php?id=<?php echo $milestoneID;?>"><i class="material-icons left">arrow_back</i>Back</a>
+						<a class="waves-effect waves-light btn blue darken-3 margin-left button-save-task" data-id="<?php echo $ID;?>" data-milestoneid="<?php echo $milestoneID;?>"><i class="material-icons left">save</i>Save</a>
+					</div>
+				</div>
+			</div>
+		</div>
+	</body>
+</html>
\ No newline at end of file
diff --git a/php/admin/admin-tasks.php b/php/admin/admin-tasks.php
new file mode 100644
index 0000000..bd6fb64
--- /dev/null
+++ b/php/admin/admin-tasks.php
@@ -0,0 +1,119 @@
+<!DOCTYPE html>
+
+<?php
+include_once('../getLanguageJSON.php');
+include_once('../mysql.php');
+
+if(!isset($_GET['id']))
+{
+	header('Location: ../error.php?message=error_param_missing');
+	exit;
+}
+
+$ID = $_GET['id'];
+if(!is_numeric($ID) || $ID < 1)
+{
+	header('Location: ../error.php?message=error_param_invalid');
+	exit;
+}
+
+$db = new DB();
+$db->createTables();
+
+$milestone = $db->getMilestone($ID);
+if($milestone == false)
+{
+	header('Location: ../error.php?message=error_milestone_not_existing');
+	exit;
+}
+
+?>
+<html xmlns="http://www.w3.org/1999/html">
+<head>
+	<meta charset="UTF-8"/>
+	<title>Tasks - Adminarea</title>
+	<!--Import Google Icon Font-->
+	<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
+	<!--Import materialize.css-->
+	<link type="text/css" rel="stylesheet" href="../../materialize/css/materialize.min.css" media="screen,projection"/>
+	<link type="text/css" rel="stylesheet" href="../../css/style.css"/>
+
+	<!--Import jQuery before materialize.js-->
+	<script type="text/javascript" src="../../js/jquery-2.2.4.min.js"></script>
+	<script type="text/javascript" src="../../materialize/js/materialize.min.js"></script>
+	<script type="text/javascript" src="../../js/main.js"></script>
+	<script type="text/javascript" src="../../js/ResizeSensor.js"></script>
+	<script type="text/javascript" src="../../js/ElementQueries.js"></script>
+
+	<!--Let browser know website is optimized for mobile-->
+	<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
+</head>
+
+<body class="grey lighten-3">
+<a class="waves-effect waves-light btn blue darken-3" href="admin-milestones.php?id=<?php echo $milestone['RoadmapID'];?>"><i class="material-icons left">arrow_back</i>Back</a>
+<div id="main">
+	<div class="container">
+		<h2 class="center-align" id="headline"><?php echo $milestone['Title'];?></h2>
+		<h4 class="center-align" id="headline">Tasks</h4>
+
+		<div class="row">
+			<div class="col s12 m8 offset-m2 l6 offset-l3 center-align">
+				<a class="waves-effect waves-light btn blue darken-3" href="admin-edit-task.php?milestoneID=<?php echo $ID;?>"><i
+						class="material-icons left">add</i>New</a>
+			</div>
+		</div>
+		<div class="row">
+			<div class="col s12 m10 offset-m1 l8 offset-l2">
+				<table class="bordered">
+					<thead>
+					<tr>
+						<th data-field="id">ID</th>
+						<th data-field="project-name">Title</th>
+						<th data-field="project-name">Status</th>
+					</tr>
+					</thead>
+
+					<tbody>
+					<?php
+					$tasks = $db->getTasks($ID);
+
+					if($tasks == false)
+					{
+						echo '<td colspan="6" class="center-align">No Tasks</td>';
+						exit;
+					}
+					else
+					{
+						for($i = 0; $i < sizeof($tasks); $i++)
+						{
+							$status = $tasks[$i]['Status'];
+							echo '<tr>' .
+								'<td>' . $tasks[$i]['ID'] . '</td>' .
+								'<td>' . $tasks[$i]['Title'] . '</td>';
+
+							if($status == "0")
+							{
+								echo '<td><i class="material-icons red-text">build</i></td>';
+							}
+							else
+							{
+								echo '<td><i class="material-icons green-text">check</i></td>';
+							}
+
+							echo '<td class="right-align">' .
+								'<a class="btn-flat no-padding tooltipped" href="admin-edit-task.php?id=' . $tasks[$i]['ID'] . '&milestoneID='. $ID .'&edit=true" data-position="bottom" data-delay="50" data-tooltip="Edit"><i class="material-icons left">edit</i></a>' .
+								'<a class="btn-flat button-delete-task no-padding tooltipped" data-id="' . $tasks[$i]['ID'] . '" data-milestoneid="' . $ID . '" data-position="bottom" data-delay="50" data-tooltip="Delete"><i class="material-icons left">delete</i></a>' .
+								'<a class="btn-flat no-padding tooltipped" href="admin-subtasks.php?id=' . $tasks[$i]['ID'] . '" data-position="bottom" data-delay="50" data-tooltip="Edit Subtasks"><i class="material-icons left">assignment</i></a>' .
+								'</td>' .
+								'</tr>';
+						}
+					}
+					?>
+					</tbody>
+				</table>
+			</div>
+		</div>
+	</div>
+</div>
+</body>
+</html>
\ No newline at end of file
diff --git a/php/admin/helper/delete-task.php b/php/admin/helper/delete-task.php
new file mode 100644
index 0000000..59013ec
--- /dev/null
+++ b/php/admin/helper/delete-task.php
@@ -0,0 +1,30 @@
+<?php
+include_once('../../mysql.php');
+
+if($_SERVER['REQUEST_METHOD'] == 'POST')
+{
+	if(!isset($_POST['task_ID']))
+	{
+		echo "error";
+		exit;
+	}
+
+	$db = new DB();
+	$db->createTables();
+
+	if($db->deleteTask($_POST['task_ID']) == false)
+	{
+		echo "error";
+		exit;
+	}
+	else
+	{
+		echo "success";
+		exit;
+	}
+}
+else
+{
+	echo "error";
+	exit;
+}
\ No newline at end of file
diff --git a/php/admin/helper/edit-task.php b/php/admin/helper/edit-task.php
new file mode 100644
index 0000000..0a4fc50
--- /dev/null
+++ b/php/admin/helper/edit-task.php
@@ -0,0 +1,77 @@
+<?php
+include_once('../../mysql.php');
+
+if($_SERVER['REQUEST_METHOD'] == 'POST')
+{
+	if(!isset($_POST['edit']))
+	{
+		echo "error";
+		exit;
+	}
+
+	if(!isset($_POST['ID']))
+	{
+		echo "error-edit";
+		exit;
+	}
+
+	if(!isset($_POST['milestone-ID']))
+	{
+		echo "error-edit";
+		exit;
+	}
+
+
+	if(!isset($_POST['title']))
+	{
+		echo "error-edit";
+		exit;
+	}
+
+	if(!isset($_POST['description']))
+	{
+		echo "error-edit";
+		exit;
+	}
+
+	if(!isset($_POST['done']))
+	{
+		echo "error-edit";
+		exit;
+	}
+
+	$db = new DB();
+	$db->createTables();
+
+	if($_POST['edit'] == "true")
+	{		
+		if($db->updateTask($_POST['ID'], $_POST['milestone-ID'], $_POST['title'], $_POST['description'], $_POST['done']) == false)
+		{
+			echo "error-edit";
+			exit;
+		}
+		else
+		{
+			echo "success";
+			exit;
+		}
+	}
+	else
+	{
+		if($db->insertTask($_POST['milestone-ID'], $_POST['title'], $_POST['description'], $_POST['done']) == false)
+		{
+			echo "error-insert";
+			exit;
+		}
+		else
+		{
+			echo "success";
+			exit;
+		}
+	}
+}
+else
+{
+	echo "error";
+	exit;
+}
\ No newline at end of file
diff --git a/php/language.json b/php/language.json
index f67de0d..ec3517f 100644
--- a/php/language.json
+++ b/php/language.json
@@ -7,6 +7,7 @@
   "error_param_invalid": "ERROR: parameter is not numeric or less than 1",
   "error_roadmap_not_existing": "ERROR: no roadmap with this ID existing",
   "error_milestone_not_existing": "ERROR: no milestone with this ID existing",
+  "error_task_not_existing": "ERROR: no task with this ID existing",
   "error_no_milestones": "ERROR: no milestones for this roadmap",
   "error_database_connection" : "An error occurred while getting data from the server."
 }
\ No newline at end of file
diff --git a/php/mysql.php b/php/mysql.php
index 566fe0b..abfc283 100644
--- a/php/mysql.php
+++ b/php/mysql.php
@@ -87,9 +87,8 @@ class DB
 		return $statement->execute();
 	}
 
-	function insertTask($milestoneID, $title, $description)
+	function insertTask($milestoneID, $title, $description, $status)
 	{
-		$status = "0";
 		$statement = self::$db->prepare("INSERT INTO tasks VALUES('', :milestoneID, :title, :description, :status);");
 		$statement->bindParam("milestoneID", $milestoneID);
 		$statement->bindParam("title", $title);
@@ -249,6 +248,15 @@ class DB
 		return $statement->fetchAll();
 	}
 
+	function getTask($taskID)
+	{
+		$statement = self::$db->prepare("SELECT * FROM tasks WHERE tasks.ID=:taskID;");
+		$statement->bindParam("taskID", $taskID);
+		$statement->execute();
+
+		return $statement->fetch();
+	}
+
 	function getNumberOfOpenTasks($milestoneID)
 	{
 		$statement = self::$db->prepare("SELECT COUNT(*) AS 'count' FROM tasks WHERE tasks.MilestoneID=:milestoneID AND status = '0';");
-- 
GitLab