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

		return $this->link;
	}
	function AdminLogin($username,$password)
	{
		global $prefix;
		$query = $this->link->prepare("SELECT * FROM `".$prefix."admins` WHERE `ausername`=? AND `apass`=?");
		$values = array($username,$password);
		$query->execute($values);
		$counts = $query->rowCount();

		return $counts;
	}

	function Add($ausername, $apass, $aactive, $aexpiration_date, $afname, $alname, $agender, $atel, $aemail, $apic, $acomments, $allow_add_admin, $allow_delete_admin, $allow_edit_admin, $allow_list_admins, $allow_add_issue, $allow_edit_issue, $allow_delete_issue, $allow_list_issues, $allow_manage_issue_types, $allow_add_project, $allow_edit_project, $allow_delete_project, $allow_list_projects, $allow_add_task, $allow_edit_task, $allow_delete_task, $allow_list_tasks, $tskids)
	{
		global $prefix;
		$query = $this->link->prepare("INSERT INTO `".$prefix."admins` (`ausername`, `apass`, `aactive`, `aexpiration_date`, `afname`, `alname`, `agender`, `atel`, `aemail`, `apic`, `acomments`, `allow_add_admin`, `allow_delete_admin`, `allow_edit_admin`, `allow_list_admins`, `allow_add_issue`, `allow_edit_issue`, `allow_delete_issue`, `allow_list_issues`, `allow_manage_issue_types`, `allow_add_project`, `allow_edit_project`, `allow_delete_project`, `allow_list_projects`, `allow_add_task`, `allow_edit_task`, `allow_delete_task`, `allow_list_tasks`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ");
		$values = array($ausername, $apass, $aactive, $aexpiration_date, $afname, $alname, $agender, $atel, $aemail, $apic, $acomments, $allow_add_admin, $allow_delete_admin, $allow_edit_admin, $allow_list_admins, $allow_add_issue, $allow_edit_issue, $allow_delete_issue, $allow_list_issues, $allow_manage_issue_types, $allow_add_project, $allow_edit_project, $allow_delete_project, $allow_list_projects, $allow_add_task, $allow_edit_task, $allow_delete_task, $allow_list_tasks);
		$query->execute($values);
		// print_r ($query->errorInfo());
		$counts= $query->rowCount();

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

			}
		}

		return $counts;
	}



	function Update($ausername, $apass, $aactive, $aexpiration_date, $afname, $alname, $agender, $atel, $aemail, $apic, $acomments, $allow_add_admin, $allow_delete_admin, $allow_edit_admin, $allow_list_admins, $allow_add_issue, $allow_edit_issue, $allow_delete_issue, $allow_list_issues, $allow_manage_issue_types, $allow_add_project, $allow_edit_project, $allow_delete_project, $allow_list_projects, $allow_add_task, $allow_edit_task, $allow_delete_task, $allow_list_tasks, $aid)
	{
		global $prefix;
		$query = $this->link->prepare("UPDATE `".$prefix."admins` SET `ausername`=?, `apass`=?, `aactive`=?, `aexpiration_date`=?, `afname`=?, `alname`=?, `agender`=?, `atel`=?, `aemail`=?, `apic`=?, `acomments`=?, `allow_add_admin`=?, `allow_delete_admin`=?, `allow_edit_admin`=?, `allow_list_admins`=?, `allow_add_issue`=?, `allow_edit_issue`=?, `allow_delete_issue`=?, `allow_list_issues`=?, `allow_manage_issue_types`=?, `allow_add_project`=?, `allow_edit_project`=?, `allow_delete_project`=?, `allow_list_projects`=?, `allow_add_task`=?, `allow_edit_task`=?, `allow_delete_task`=?, `allow_list_tasks`=? WHERE `aid`=? ");
		$values = array($ausername, $apass, $aactive, $aexpiration_date, $afname, $alname, $agender, $atel, $aemail, $apic, $acomments, $allow_add_admin, $allow_delete_admin, $allow_edit_admin, $allow_list_admins, $allow_add_issue, $allow_edit_issue, $allow_delete_issue, $allow_list_issues, $allow_manage_issue_types, $allow_add_project, $allow_edit_project, $allow_delete_project, $allow_list_projects, $allow_add_task, $allow_edit_task, $allow_delete_task, $allow_list_tasks, $aid);
		$query->execute($values);
		// print_r ($query->errorInfo());
		return $query->rowCount();
	}

	function SetDeactive($aid)
	{
		global $prefix;
		$query = $this->link->prepare("UPDATE `".$prefix."admins` SET `aactive`=0 WHERE `aid`=? ");
		$values = array($aid);
		$query->execute($values);
		// print_r ($query->errorInfo());
		return $query->rowCount();
	}

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

	function GetList()
	{
		global $prefix;
		$query = $this->link->query("SELECT * FROM `".$prefix."admins` ");
		// $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."admins` WHERE $field=?");
		$values = array($value);
		$query->execute($values);
		return $query->fetchAll();
	}

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

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

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

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

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