<?php

abstract class ControllerBase {
	protected $db ;
    protected $dbsun ;
	protected $orgid = 0 ;
	protected $fldorg = "" ;
	
	abstract function ProcessRequest($params) ;
	protected function sendJsonResponse($status="",$mesg="",$data="",$type="") {
		header('Content-type: application/json');
		$arr = array(FIELD_STATUS => $status, FIELD_MESG => $mesg, FIELD_DATA => $data, FIELD_TYPE => $type);
		echo json_encode($arr) ;
                
                
	}
	protected function sendTextResponse($status="",$mesg="",$data="",$type="") {
		header('Content-type: text/plain');
		echo $status . "|" . $mesg . "|" . $data . "|" . $type ;
	}
	protected function sendImageResponse($file) {
		header('Content-type: image/jpg');
		echo $file;
	}
	protected function sendHtmlResponse($data="") {
		header('Content-type: text/html');
		echo $data ;
	}
	protected function sendPdfResponse($data=null) {
		header('Content-type: application/pdf') ;
		echo $data ;
	}
	protected function getParam($params,$key,$default) {
		if (!isset($params[$key]) || $params[$key] == "") {
			return $default ;
		} else {
			return $params[$key] ;
		}
	}
	protected function getParamDate($params,$key,$default) {
		$result = $default ;
		if (isset($params[$key]) && !empty($params[$key])) {
			$dte = explode("/", $params[$key]);
			if (count($dte) == 3) {
				$d = $dte[0] ;
				$m = $dte[1] ;
				$y = $dte[2] ;
				if (checkdate($m,$d,$y))
					$result = $y . "-" . $m . "-" . $d ;
			}
		}
		return $result ;
	}
	protected function getParamInt($params,$key,$default) {
		$result = $default ;
		if (isset($params[$key]) && $params[$key] != "") {
			if (is_numeric($params[$key]))
				$result = $params[$key] ;
		}
		return $result ;
	}
	protected function getParamNumeric($params,$key,$default) {
		$result = $default ;
		if (isset($params[$key]) && $params[$key] != "") {
			if (is_numeric($params[$key]))
				$result = $params[$key] ;
		}
		return $result ;
	}
	protected function getValueList($table,$code,$desc,$default=null,$filters=null,$orderbys=null) {
		$sql = "select " . $code ."," . $desc . " from " . $table ;
		$params = array() ;
		if (is_array($filters) && count($filters) > 0) {
			$cond = " where " ;
			foreach ($filters as $data) {
				$sql .= $cond . $this->db->fieldParam($data['field']) ;
				$cond = " and " ;
				$params[] = $this->db->valueParam($data['field'],$data['value']) ;
			}
		}
		if($orderbys != null){
			$sql .= " ORDER BY ";
			$first = true;
			foreach($orderbys as $orderby){
				if(!$first) $sql .= ", ";
				$sql .= $orderby['field'] . " " . $orderby['type'];
				if($first) $first = false;
			}
		} else {
			$sql .= " ORDER BY " . $desc . " ASC ";
		}
		
		$vls = array() ;
		if (!is_null($default) && count($default) > 0)
			$vls[] = array ('code'=>$default['code'],'desc'=>$default['desc']) ;
		$rows = $this->db->getTable($sql,$params) ;
		if (!is_null($rows) && count($rows) > 0) {
			foreach ($rows as $row) {
				$vls[] = array ('code'=>$row[$code],'desc'=>$row[$desc]) ;
			}
		}
		return $vls ;
	}
	protected function getDescription($table,$code,$desc,$id) {
		$result = "" ;
		$sql = "select " . $desc . " from " . $table . 
			" where " . $this->db->fieldParam($code) ;
		$params = array() ;
		$params[] = $this->db->valueParam($code,$id) ;
		$rows = $this->db->getRow($sql,$params) ;
		if (!is_null($rows) && count($rows) > 0) {
			$result = $rows[0][$desc] ;
		}
		return $result ;
	}
	protected function createPdfItem($t="",$w=0,$h=0,$a="L",$b="0",$nr="0") {
		return array('width'=>$w,'height'=>$h,'text'=>$t,'border'=>$b,'align'=>$a,'newrow'=>$nr) ;
	}
    protected function getWebPageURL(){
        $pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
        if ($_SERVER["SERVER_PORT"] != "80")
        {
            $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
        } 
        else 
        {
            $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
        }
        if (strpos($pageURL, "?") !== false){
            $pageURL = substr($pageURL, 0, strpos($pageURL, "?"));
        }
        return $pageURL;
	}
	
	protected function getBaseURL(){
        $pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
        if ($_SERVER["SERVER_PORT"] != "80")
        {
            $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"];
        } 
        else 
        {
            $pageURL .= $_SERVER["SERVER_NAME"];
        }
        if (strpos($pageURL, "?") !== false){
            $pageURL = substr($pageURL, 0, strpos($pageURL, "?"));
        }
        return $pageURL;
    }
	
	protected function getPeriodYear(){
		$arr = array() ;
		//generate year
		$dte = new DateTime();
		$year = intval($dte->format('Y'));
		
		for($i=$year-5;$i<=$year+5;$i++){
			$arr[] = array ('code'=>$i,'desc'=>$i );
		}
		return Util::createOptionValue($arr) ;
	}
	
	protected function getPeriodMonth(){
		$arr = array() ;
		$arr[] = array ('code'=>'01','desc'=>'01' );
		$arr[] = array ('code'=>'02','desc'=>'02' );
		$arr[] = array ('code'=>'03','desc'=>'03' );
		$arr[] = array ('code'=>'04','desc'=>'04' );
		$arr[] = array ('code'=>'05','desc'=>'05' );
		$arr[] = array ('code'=>'06','desc'=>'06' );
		$arr[] = array ('code'=>'07','desc'=>'07' );
		$arr[] = array ('code'=>'08','desc'=>'08' );
		$arr[] = array ('code'=>'09','desc'=>'09' );
		$arr[] = array ('code'=>'10','desc'=>'10' );
		$arr[] = array ('code'=>'11','desc'=>'11' );
		$arr[] = array ('code'=>'12','desc'=>'12' );
		return Util::createOptionValue($arr) ;
	}

	protected function getVersion() {
		$result 	= "";
		$file 		= "changelog.txt";
		$path 		= PATH_CODE.$file;

		if(file_exists($path)){
			$version 	= fgets(fopen($path, "r"));

			$result 	= "<a href=\"../code/".$file."\" download=\"changelog_".$version.".txt\">".$version."</a>";
			

		}

		return $result;
	}
} 