<?php
class Admin{
	// get admin allowed modules 
	static function getPermissions($id){
  		global $db,$conn;	
		$group_id	=	utility::getTableFieldById("admin","group_id","id",$id);			
		$result 	= 	$db->query("SELECT m.id,m.module,ag.module_id FROM  admin_group ag LEFT JOIN module m ON (ag.module_id = m.id AND ag.group_id=$group_id)");
		$permissionsArray = array('index','index1');
		while($row = $db->next_record($result)){
			if($row['module']){
				array_push($permissionsArray,strtolower($row['module']));
			}
		}
		return $permissionsArray;
	}
	
	static function hasPermission($permission){
		$permissionsArray = self::getPermissions($_SESSION['admin_user_id']);				
		if($_SESSION['is_super_admin'] or in_array(strtolower($permission),$permissionsArray)){
			return true;
		}
		return false;
	}
	static function userPermissions(){
		return array(1=>"index",
					 2=>"enquiry_management",
					 3=>"appointment",
					 4=>"timesheet_management",
					 5=>"notes",
					 6=>"mileage");
	}
	static function userHasPermission($permission){
		$permissionsArray = self::userPermissions();				
		if(in_array(strtolower($permission),$permissionsArray)){
			return true;
		}
		return false;
	}
	static function getAdminUserById($id){
		global $db,$conn;	  
		$result = $db->query("SELECT * FROM admin WHERE id=$id");	  
		if ($db->num_rows($result) > 0){
		  return $db->next_record($result);
		}
		return false;
	}
	
	static function getGroupList($listName = "", $id="", $width = 250, $return_type=0, $className="ed_pro_input" ){
  		global $db,$conn;		
		$result = $db->query("SELECT * FROM groups ORDER BY group_name");
		$dataArray = array();
		if ($db->num_rows($result) > 0){
			while($row = $db->next_record($result)){
				$dataArray[$row['id']] = $row['group_name'];
			}			
		}		
		if($return_type){
			return $dataArray;
		}else{
			print utility::createList($dataArray,$listName,$id,$width,"Select Department",false,$className);
		}
		
	}
	static function getGroupById($id) {
		global $db, $conn;        
		$result = $db->query("SELECT * FROM groups WHERE id=$id");
    
		if ($db->num_rows($result) > 0) {
			$row = $db->next_record($result);
			return stripslashes($row['group_name']);
		}
		return false;
    }
	/* static function getAdminList($listName = "", $id="", $width = "100%", $multiple,$className,$required="",$admin_ids=""){
  		global $db,$conn;		
		$condition = "";
		if(isset($admin_ids) && $admin_ids !=""){
			$condition = " AND id IN ($admin_ids)";
		}
		$result = $db->query("SELECT * FROM admin WHERE id != 1 $condition ORDER BY name");
		$dataArray = array();
		if ($db->num_rows($result) > 0){
			while($row = $db->next_record($result)){
				$dataArray[$row['id']] = stripslashes($row['name']);
			}			
		}
		print utility::createList($dataArray,$listName,$id,$width,"Select Consultant",$multiple,$className,100,$required);
	} */
	static function getAdminList($listName = "", $id="", $width = "100%", $multiple,$className,$required="",$admin_ids="",$return_type="print",$from_session=""){
  		global $db,$conn;		
		
		if($from_session){
			print utility::createList($_SESSION['lead_consultant_session_array'],$listName,$id,$width,"Select Consultant",$multiple,$className);
			return false;
		}
		
		$condition = "";
		if(isset($admin_ids) && $admin_ids !=""){
			$condition = " AND id IN ($admin_ids)";
		}
		$result = $db->query("SELECT * FROM admin WHERE id != '1' $condition ORDER BY name");
		$dataArray = array();
		if ($db->num_rows($result) > 0){
			while($row = $db->next_record($result)){
				$dataArray[$row['id']] = stripslashes($row['name']);
			}			
		}
		if($return_type == "print"){
			print utility::createList($dataArray,$listName,$id,$width,"Select Consultant",$multiple,$className,100,$required);
		}else{
			
			if($return_type == "1"){
				return $dataArray;
			}else{				
				return utility::createList($dataArray,$listName,$id,$width,"Select Consultant",$multiple,$className,100,$required);
				
			}
		}
	}
	static function getAdminById($admin_id){
		global $db,$conn;		
		$result = $db->query("SELECT * FROM admin WHERE id = $admin_id");
		if ($db->num_rows($result) > 0){
			$row = $db->next_record($result);
			return $row['uname'];
		}else{
			return "";
		}
	}
	static function getAdminsById($admin_id){
		global $db,$conn;	
		if($admin_id == ""){
			return "";
		}else{
			$result = $db->query("SELECT * FROM admin WHERE id IN($admin_id) ");
			if ($db->num_rows($result) > 0){
				$admin_name = "";
				while($row = $db->next_record($result)){
					$admin_name .= $row['name'].",";
				}
				$admin_name = substr($admin_name,0,-1);
				return $admin_name;
			}
		}
	}
	static function getAdminsInitialById($admin_id){
		global $db,$conn;	
		if($admin_id == ""){
			return "";
		}else{
			$result = $db->query("SELECT * FROM admin WHERE id IN($admin_id) ");
			$no_of_admin = count(explode(",",$admin_id));
			if ($db->num_rows($result) > 0){
				$admin_name = "";
				$i = 1;
				while($row = $db->next_record($result)){
					if($i == 3){
						break;
					}
					$name_array = explode(" ",$row['name']);
					$admin_name .= $name_array[0].",";
					$i++;
				}
				$admin_name = substr($admin_name,0,-1);
				if($no_of_admin > 2){
					$admin_name = $admin_name."...";
				}
				return $admin_name;
			}
		}
	}
}
?>