<?php 
include_once 'class.database.php';
class ManageTasks{
	public $link;
	function __construct()
	{
		global $prefix;
		$db_connection = new dbConnection();
		$this->link = $db_connection->connect();

		return $this->link;
	}
	function Add($aid , $prjid , $tskcode , $tsktitle , $tskdesc , $tskdone , $tskdone_date , $tskdone_aid , $tskcomments , $iids)
	{
		global $prefix;
		$query = $this->link->prepare("INSERT INTO `".$prefix."tasks` (`aid` , `prjid` , `tskcode` , `tsktitle` , `tskdesc` , `tskdone` , `tskdone_date` , `tskdone_aid`, `tskcomments`, `tskdate`) VALUES (?,?,?,?,?,?,?,?,?,NOW()) ");
		$values = array($aid , $prjid , $tskcode , $tsktitle , $tskdesc , $tskdone , $tskdone_date , $tskdone_aid , $tskcomments);
		$query->execute($values);
		// print_r ($query->errorInfo());
		$counts= $query->rowCount();

		if ($counts==1) {
			$tskid = $this->link->lastInsertId();
			foreach ($iids as $iid) {
				$query=$this->link->prepare("INSERT INTO `".$prefix."tasks_issues` (`iid`,`tskid`) VALUES (?,?)");
				$values = array($iid,$tskid);
				$query->execute($values);
				// print_r ($query->errorInfo());

			}

		}
		
		return $counts;
	}

	function Update($prjid , $tskcode , $tsktitle , $tskdesc , $tskdone , $tskdone_date , $tskdone_aid , $tskcomments , $tskid)
	{
		global $prefix;
		$query = $this->link->prepare("UPDATE `".$prefix."tasks` SET `prjid`=?, `tskcode`=?, `tsktitle`=?, `tskdesc`=?, `tskdone`=?, `tskdone_date`=?, `tskdone_aid`=?, `tskcomments`=? WHERE `tskid`=? ");
		$values = array($prjid , $tskcode , $tsktitle , $tskdesc , $tskdone , $tskdone_date , $tskdone_aid , $tskcomments , $tskid);
		$query->execute($values);
		// print_r ($query->errorInfo());
		return $query->rowCount();
	}

	//When Press TaskDone in list Tasks Table
	function UpdateDone($tskdone , $tskdone_date , $tskid)
	{
		global $prefix;
		$query = $this->link->prepare("UPDATE `".$prefix."tasks` SET `tskdone`=?, `tskdone_date`=? WHERE `tskid`=? ");
		$values = array($tskdone , $tskdone_date , $tskid);
		$query->execute($values);
		// print_r ($query->errorInfo());
		return $query->rowCount();
	}

	function ListTasks($where, $order='ORDER BY tskid DESC' ,$limit=NULL)
	{
		global $prefix;
		$query = $this->link->query("SELECT * FROM `".$prefix."tasks` INNER JOIN `".$prefix."admins` ON ".$prefix."tasks.aid=".$prefix."admins.aid INNER JOIN `".$prefix."projects` ON ".$prefix."tasks.prjid=".$prefix."projects.prjid $where $order $limit");
		// $values = array($where);
		// $query->execute($values);
		// print_r ($query->errorInfo());
		return $query->fetchAll();
	}

		function Getinfo($field,$value)
	{
		global $prefix;
		$query = $this->link->prepare("SELECT * FROM `".$prefix."tasks` INNER JOIN `".$prefix."admins` ON ".$prefix."tasks.aid=".$prefix."admins.aid INNER JOIN `".$prefix."projects` ON ".$prefix."tasks.prjid=".$prefix."projects.prjid  WHERE $field=?");
		$values = array($value);
		$query->execute($values);
				// print_r ($query->errorInfo());
		return $query->fetchAll();
	}

	
	function GetList()
	{
		global $prefix;
		$query = $this->link->query("SELECT * FROM `".$prefix."tasks` ");
		// $values = array($where);
		// $query->execute($values);
		// print_r ($query->errorInfo());
		return $query->fetchAll();
	}

	function GetTaskIssues($tskid)
	{
		global $prefix;
		$query = $this->link->prepare("SELECT iid FROM `".$prefix."tasks_issues` WHERE tskid=?");
		$values = array($tskid);
		$query->execute($values);
		return $query->fetchAll(PDO::FETCH_COLUMN);
	}



	function DelTaskIssues($tskid,$iid)
	{
		global $prefix;
		$query = $this->link->prepare("DELETE FROM `".$prefix."tasks_issues` WHERE tskid=? AND iid=?");
		$values = array($tskid,$iid);
		$query->execute($values);
		return $query->rowCount();
	}

	function AddTaskIssues($tskid,$iid)
	{
		global $prefix;
		$query = $this->link->prepare("INSERT INTO `".$prefix."tasks_issues` (`tskid`,`iid`) VALUES (?,?) ");
		$values = array($tskid,$iid);
		$query->execute($values);
		// print_r ($query->errorInfo());
		return $query->rowCount();
	}

	function RowCount($where=NULL)
		{
			global $prefix;
			$query = $this->link->query("SELECT * FROM `".$prefix."tasks` $where");
			// $values = array($where);
			// $query->execute($values);
			// print_r ($query->errorInfo());
			return $query->rowCount();
		}

	function Delete($id)
	{
		global $prefix;
		$query = $this->link->prepare("DELETE FROM `".$prefix."tasks` WHERE tskid=?");
		$values = array($id);
		$query->execute($values);
		// print_r ($query->errorInfo());
		return $query->rowCount();
	}
}
 ?>