<?php
require_once(__DIR__."/../includes/sysinit.php");

class Team
{
	// Properties of the class:
	var $hddnTeamId, $edtTeamName, $selVehicleId, $cbTeamActive, $userList, $lstDOW;
	function __construct(){}
	function __destruct(){}

	function addTeam()
	{
		if (empty($this->hddnTeamId))
		{
			if ($this->checkTeamExists())
			SableSystem::ajaxResponse(false, "", "This Team has already been added.", 
				5, "edtTeamName", false, "edtTeamName");
			$this->loadTeam();
		}
		else
		{
			$this->updateTeam();
		}
	}

	function loadTeam()
	{
		global $sableDB;
		$insertQuery = "INSERT INTO team (
						team_name, vehicle_id, team_active, team_timestamp, team_user, team_ip, last_update
						) VALUES (
						'".addslashes($this->edtTeamName)."',
						'".$this->selVehicleId."',
						".$this->cbTeamActive.",
						'".date('Y-m-d H:i:s')."',
						'".$_SESSION['user']['user_code']."',
						'".SableGeneralFunctions::getRealIpAddr()."',
						'".date('Y-m-d H:i:s')."')";
		$res=$sableDB->updateQuery($insertQuery);
		//Check if the Team details were successfully added:
		if ($res)
		{
			$this->setTeamId();
			$this->storeActiveDays();
			if ($this->storeUsers())
			{
				SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'New Team added: '.$this->edtTeamName, $this->hddnTeamId);
				SableSystem::ajaxResponse(true, '/team/', 'The Team was successfully added.', 0, "", 0, "");
			} else SableSystem::ajaxResponse(false, "", "Addition of Team Users failed. Please try again or contact support.", 
											  5, "selUserCode", false, "selUserCode");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Team failed. Please try again or contact support.", 
				5, "edtTeamName", false, "edtTeamName");
	}

	function updateTeam()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnTeamId);
		$updateQuery = "UPDATE team set 
						team_name = '".addslashes($this->edtTeamName)."', 
						vehicle_id = '".$this->selVehicleId."', 
						team_active = ".$this->cbTeamActive.", 
						team_timestamp = '".date('Y-m-d H:i:s')."', 
						team_user = '".$_SESSION['user']['user_code']."', 
						team_ip = '".SableGeneralFunctions::getRealIpAddr()."', 
						last_update = '".date('Y-m-d H:i:s')."' 
						where team_id = '".$code."'";
		$res=$sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			if ($this->storeUsers())
			{
				$this->storeActiveDays();
				SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'Team details modified: '.$this->edtTeamName, $code);
				SableSystem::ajaxResponse(true, '/team/', 'The Team was successfully updated.', 0, "", 0, "");
			} 
			else SableSystem::ajaxResponse(false, "", "Modification of Team Users failed. Please try again or contact support.", 
											  5, "selUserCode", false, "selUserCode");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the Team failed. Please try again or contact support.", 
									  5, "edtTeamName", false, "edtTeamName");
	}

	private function getTeamId()
	{
		global $sableDB;
		$sql = "select team_id from team where team_name = '".$this->edtTeamName."'";
		return $sableDB->getValue($sql);
	}

	private function checkTeamExists()
	{
		$cid = $this->getTeamId();
		if (!empty($cid)) return true; else return false;
	}

	private function setTeamId()
	{
		$this->hddnTeamId = $this->getTeamId();
	}

	function viewTeam()
	{
		$url = '/team/view.php?'.SableSystem::sableDecode($this->hddnTeamId);
		SableSystem::ajaxPageRedirect($url);
	}

	function editTeam()
	{
		$url = '/team/edit.php?'.SableSystem::sableDecode($this->hddnTeamId);
		SableSystem::ajaxPageRedirect($url);
	}

	function deleteTeam()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnTeamId);
		$cnt = $sableDB->getValue("select count(*) from team where team_id = ".$code);
		if ($cnt <= 0)
			SableSystem::ajaxResponse(false, "", "Delete failed - invalid Team selected.", 
				5, "edtTeamName", false, "edtTeamName");
		else
		{
			$sql = "delete from team where team_id = '".$code."'";
			$res = $sableDB->updateQuery($sql);
			if ($res)
			{
				SableDBFunctions::loadAuditDetail($_SESSION["user"]["user_code"], 'Team record deleted.', $code);
				SableSystem::ajaxPageRedirect('/team/','The Team record was successfully deleted.');
			}
			else
			SableSystem::ajaxResponse(false, "", "
			System error: The Team failed to delete. Please try again later or contact support.", 
				5, "edtTeamName", false, "edtTeamName");
		}
	}
	
	private function storeUsers()
	{
		return SableDBFunctions::storeArray('team__user', 'team_id', $this->hddnTeamId, 'user_code', $this->userList);
	}
	
	private function storeActiveDays()
	{
		return SableDBFunctions::storeArray('team__dow', 'team_id', $this->hddnTeamId, 'dow_id', $this->lstDOW);
	}

} //classTeam
?>
