<?php

class IndexController extends Zend_Controller_Action {
	private $identity;
	private $userDetailsDB;

	public function init() {
		$auth = Zend_Auth::getInstance();
		
		if (!$auth -> hasIdentity()) {

			$this -> _redirect('/login/');

		} else {

			$user = $auth -> getIdentity();

			$this -> identity = $user;
			//	echo $this->identity->Status;
			//print_r($this->identity);

			// if($this->identity->UnionType >2 && $this->identity->Active ==0 ){
			if ($this -> identity -> UnionType > 2) {

				if ($this -> identity -> Administrator == 1) {
				} else {
					//$author = $db->getRow($this->identity->author);
					if ($this -> identity -> UnionType < 3) {
						$this -> _redirect('/union/first');
					} else {
						$this -> _redirect('/union/');
					}
				}
			}else
			if ($this -> identity -> Status == 0 && $this -> identity -> UnionType < 3) {

				$this -> _redirect('/first');

			}
			setcookie('FootPrintApp', 'FootPrintApp', time() + 86400, '/');
		}

		$this -> view -> assign("UserDetails", $this -> identity);
		$this -> AccessRights = json_decode($this -> identity -> Role, true);

		// GET CURRENT REQUEST METHOD AND ACTIONS
		$this -> view -> assign("getControllerName", $this -> getRequest() -> getControllerName());
		$this -> view -> assign("getActionName", $this -> getRequest() -> getActionName());
		//$this->userDetailsDB = new Model_UsersDetails;

	}
public function indexAction() {
}
	public function indexoldAction() {
		//Ethnicity Count
		$EthnicityCountDB = new Model_DbTable_EthnicityCount;
		$Players = $EthnicityCountDB -> getCount(2);
		$this -> view -> assign("Players", $Players);
		$Coaches = $EthnicityCountDB -> getCount(3);
		$this -> view -> assign("Coaches", $Coaches);
		$Refereers = $EthnicityCountDB -> getCount(8);
		$this -> view -> assign("Refereers", $Refereers);
		$Medical = $EthnicityCountDB -> getCount(5);
		$this -> view -> assign("Medical", $Medical);
		
		//get Transfer Request
		$TransferDB = new Model_DbTable_TransferRequest;
		$Transfers = $TransferDB -> getRequestsCompleted();
		$this -> view -> assign("Transfers", $Transfers);
		
		//new accounts
		$playersDB = new Model_DbTable_Profile;
		$Registrations = $playersDB -> getNewActivationsAccounts();
		$this -> view -> assign("Registrations", $Registrations);

		//get fixtures
		$FixturesDB = new Model_DbTable_Fixture();
		$Fixtures = $FixturesDB -> getFixturesThisWeek();
		$this -> view -> assign("Fixtures", $Fixtures);

		//get match officals positions
		$positionsDB = new Model_DbTable_MatchOfficialPositions;
		$MatchOfficailsposition = $positionsDB -> fetchAll();
		$MatchOfficailsPos = array();
		foreach ($MatchOfficailsposition as $pos) {
			$MatchOfficailsPos[$pos["PID"]] = $pos;
		}
		//get match officals Profiles
		
		$RefereesList = array();

		if (count($Fixtures) > 0) {
			foreach ($Fixtures as $Fixtures_list){

				foreach ($Fixtures_list["FIX"] as $Fixture) {
					$MatchOfficials = json_decode($Fixture["MatchOfficials"], true);
					$Officials = array();
					$OfficialsList = array();
					if (count($MatchOfficials) > 0) {
						foreach ($MatchOfficials as $Official) {
							$Officials[] = $Official["ID"];
							$OfficialsList[$Official["ID"]] = $Official;
						}

					}

					if (count($Officials) > 0) {
						$Profiles = $playersDB -> getFullNamesByIDs($Officials);
						foreach ($Profiles as $Profile) {
							$RefereesList[] = array("FID" => $Fixture["FID"], "TID" => $Fixture["TID"], "TournName" => $Fixture["TournName"], "Fixture" => $Fixture["Home"] . " V " . $Fixture["Away"], "FullName" => $Profile["FullName"], "Pos" => $MatchOfficailsPos[$OfficialsList[$Profile["UserID"]]["Pos"]]["Position"], "UserID" => $Profile["UserID"]);

						}

					}

				}
			}
		}
		$this -> view -> assign("RefereersThisWeek", $RefereesList);
		
		
	}

	function get_user_browser() {
		$u_agent = $_SERVER['HTTP_USER_AGENT'];
		$ub = '';
		if (preg_match('/MSIE/i', $u_agent)) {
			$ub = "ie";
		} elseif (preg_match('/Firefox/i', $u_agent)) {
			$ub = "firefox";
		} elseif (preg_match('/Safari/i', $u_agent)) {
			$ub = "safari";
		} elseif (preg_match('/Chrome/i', $u_agent)) {
			$ub = "chrome";
		} elseif (preg_match('/Flock/i', $u_agent)) {
			$ub = "flock";
		} elseif (preg_match('/Opera/i', $u_agent)) {
			$ub = "opera";
		}

		return $ub;
	}

}
