<?php
class Keycode
{
	static function getKeycodeList($listName = "", $id="", $width = 250, $displayName){		
		global $db,$conn;
		$keycode_result 	= 	$db->query("SELECT id, name FROM keycode GROUP BY name ORDER BY name");		
		$dataArray = array();
		if($db->numRows($keycode_result)) {		
		      while($keycode = $db->next_record($keycode_result)){								
				$dataArray[$keycode['name']] = $keycode['name'];		      	
		      }		     
		}		
		print utility::createList($dataArray,$listName,$id,$width,$displayName,false);
  	}
	
	static function getKeycodeListValue($listName = "", $id="", $width, $displayName, $key, $value, $whereName, $returnType, $className="",$multiple=false,$required=""){

		global $db,$conn;
		$keycode_result 	= 	$db->query("SELECT id, $key,$value FROM keycode WHERE `name`='$whereName' AND status !=2 ORDER BY code ");		
		$dataArray = array();
		if($db->numRows($keycode_result)) {		
		      while($keycode = $db->next_record($keycode_result)){								
				$dataArray[$keycode[$key]] = $keycode[$value];		      	
		      }		     
		}		
		if($returnType){
			return $dataArray;
		}else{
			return utility::createList($dataArray,$listName,$id,$width,$displayName,$multiple,$className,"100%",$required);
		}
		
		//print utility::createList($dataArray,$listName,$id,$width,$displayName,false);
  	}
	// get keycode by id 
	static function getkeycodeById($id=0){
		return utility::getTableFieldById("keycode","code","id","$id");
  	}
	static function getkeycodeIdByValue($keycode_value,$name){
		return utility::getTableFieldByTwoCondition("keycode","id","code",$keycode_value,"name",$name);
  	}
	static function getMaxKeycodeOrderByName($name){
		global $db,$conn;
		$keycode_result 	= 	$db->query("SELECT MAX(ord) as ord FROM keycode WHERE `name`='$name' ");	
		$keycode = $db->next_record($keycode_result);
		return $keycode["ord"];
	}
	static function otherDataInsertIfNotExist($name,$field_value){
		global $db,$conn;		
		if($field_value){
			if(utility::getTableFieldById("keycode","code","id","$field_value")){
				return $field_value;
			}else{		
				$max_order = "SELECT MAX(ord)+1 as ord FROM keycode WHERE name = '$name' ";
				$ord = 1;
				$result_max_order = mysqli_query($conn,$max_order);
				if(mysqli_num_rows($result_max_order)){
					$row_max_order = mysqli_fetch_array($result_max_order);
					if($row_max_order['ord'])
					$ord =  $row_max_order['ord'];
				}
				$updated_by = $_SESSION['user_id'];
				$sql= "INSERT INTO keycode (name,code,ord,updated_by) VALUES('$name','$field_value','$ord','$updated_by')";
				mysqli_query($conn,$sql);
				return $insert_id = mysqli_insert_id($conn);
			}
		}

	}
}
?>