<?php
ini_set('display_errors', 1);
ini_set('error_reporting', 1);

class OrganisationsController extends Zend_Controller_Action {
	private $identity;
	private $AccessRights;
private $UnionDetails;
	public function init() {
		try{
		$Auth = new PG_Controller_Plugin_Authontication;
		 $User = $Auth->Auth();
		$this -> identity = $User;
		
		$this -> AccessRights = json_decode($this -> identity -> Role, true);
		$this -> view -> assign("AccessRoles", $this -> AccessRights);
		$this -> view -> assign("UserDetails", $this -> identity);
		$this -> view -> assign("getControllerName", $this -> getRequest() -> getControllerName());
		$this -> view -> assign("getActionName", $this -> getRequest() -> getActionName());
		$this -> AccessDB = new Model_DbTable_AccessControl;
		
		//$UnionsDB = new Model_DbTable_Unions;
		//$this->UnionDetails = $UnionsDB->getRow($this->identity->UnionID);	
		
		}catch(exception $expression){
			 echo "<pre>";
			print_r($expression);
		}
	
	}
	
	public function indexAction()
    {
       $this->AccessControll();
        $this->getInstitutionType();
    }
	
	public function addAction()
    {
	}
	
	 public function addtestAction()
    {
    	try{
    	$Model_Organisations = new Model_DbTable_Organisations;
    		//get Organisation Levels
    		
			
			//get organisation Types
			$Types	= $Model_Organisations->getTypes();
			$this->view->assign("Types",$Types);
			
			//get school Types
			$SchoolTypes	= $Model_Organisations->getSchoolTypes();
			$this->view->assign("SchoolTypes",$SchoolTypes);
			
			//get association Types
			$AssociteTypes	= $Model_Organisations->getAssociationTypes();
			$this->view->assign("AssociateTypes",$AssociteTypes);
			
			//get organisations
			$Orgenanisations	= $Model_Organisations->getParents("Country = 197  AND un_type >'".$this->identity->UnionType."' AND un_type < 4");
			
			$this->view->assign("Parents",$Orgenanisations);
			
			//GET COUNTRIES
			$CountryDB = new Model_DbTable_Country;
        	$Countries = $CountryDB->getAll();
       		$this->view->assign("CountryList",$Countries);
		}catch(exception $expression){
			 echo "<pre>";
			print_r($expression);
		}
    	
    }
	
	// FUNCTIONS
	private function AccessControll() {
		$array = array("profile", "edit");
		$Action = $this -> getRequest() -> getActionName();
		if ($Action == "getmember") {
			$Action = "profile";
		}
		if (in_array($this -> getRequest() -> getActionName(), $array)) {
			$request = $this -> getRequest();
			$id = $request -> getParam("id");
			if ($this -> identity -> UserID != $id) {

				if (!isset($this -> AccessRights[$this -> getRequest() -> getControllerName()]["Actions"][$Action])) {
					$this -> _redirect("/");
				}
			}
		} else {
			if (!isset($this -> AccessRights[$this -> getRequest() -> getControllerName()]["Actions"][$Action])) {
				$this -> _redirect("/");
			}

		}

	}
	private function getInstitutionType() {

		$Instdb = new Model_DbTable_IntuitutionTypes;
		$UnionsDB = new Model_DbTable_Unions;
		//$union = $UnionsDB->GetUnion($this->identity->UnionID);

		$this -> view -> assign("Institution", $Instdb -> listInstByUserLogin($this -> identity));
		$this -> view -> assign("Unions", $UnionsDB -> listUnionsByUserLogin($this -> identity));

	}

}