<?php
require_once (PATH_TABLES . "admin/LocalLangTermDefinedTable.php") ;
require_once (PATH_TABLES . "admin/LocalLangTermTable.php") ;

class LocalLangTermDefinedClass  {
	private $db;
	private $tbl;
	private $fldid1;
	private $fldid2;
	private $fldid3;
	private $termdesc;
	private $termdefine;
	
	function __construct($db) {
		$this->db = $db ;
		$this->tbl = LocalLangTermDefinedTable::C_TABLE ;
		$this->fldid1 = LocalLangTermDefinedTable::C_TERM_ID ;
		$this->fldid2 = LocalLangTermDefinedTable::C_TRANSLATION_LANG ;
		$this->fldid3 = LocalLangTermDefinedTable::C_APP_TYPE ;
		$this->termdesc = LocalLangTermDefinedTable::C_DESC ;
		$this->termdefine = LocalLangTermDefinedTable::C_TRANSLATION ;
	}
	
	function __destruct() {
	}
    
    function getDefinedTerms($langid, $appid, $termtypeid, $termid = -1){
        $sql = "SELECT T.*, " . 
                " CASE WHEN RTRIM(LTRIM(COALESCE(D." . LocalLangTermDefinedTable::C_TRANSLATION . ", T." . LocalLangTermTable::C_DESC . "))) = '' THEN T." . LocalLangTermTable::C_DESC . " " . 
                " ELSE RTRIM(LTRIM(COALESCE(D." . LocalLangTermDefinedTable::C_TRANSLATION . ", T." . LocalLangTermTable::C_DESC . "))) END AS " . LocalLangTermDefinedTable::C_TRANSLATION . " " . 
                " FROM " . LocalLangTermTable::C_TABLE . " T " . 
                " LEFT OUTER JOIN " . LocalLangTermDefinedTable::C_TABLE . "  D " . 
                " ON (T." . LocalLangTermTable::C_APP_ID . " = D." . LocalLangTermDefinedTable::C_APP_TYPE . " " . 
                " and T." . LocalLangTermTable::C_TERM_TYPE_ID . " = D." . LocalLangTermDefinedTable::C_TERM_TYPE . " " . 
                " and T." . LocalLangTermTable::C_TERM_ID . " = D." . LocalLangTermDefinedTable::C_TERM_ID . " " . 
                " AND D." . LocalLangTermDefinedTable::C_TRANSLATION_LANG . " = " . $langid . ") " .
               " WHERE T." . LocalLangTermTable::C_APP_ID . " = " . $appid . 
                " AND T." . LocalLangTermTable::C_TERM_TYPE_ID . " = " . $termtypeid . 
                ($termid == -1 ? "" : " AND T." . LocalLangTermTable::C_TERM_ID . " = " . $termid) . 
                " ORDER BY T." . LocalLangTermTable::C_TERM_ID;
        
        $rows = $this->db->getTable($sql);
        
        return $rows;
    }
	
	function isFound($termId,$langId,$appType) {
		$filter = $this->db->fieldParam($this->fldid1) .
		" and " . $this->db->fieldParam($this->fldid2) .
		" and " . $this->db->fieldParam($this->fldid3);
		
		$params = array() ;
		$params[] = $this->db->valueParam($this->fldid1,$termId) ;
		$params[] = $this->db->valueParam($this->fldid2,$langId) ;
		$params[] = $this->db->valueParam($this->fldid3,$appType) ;

		if ($this->db->getRowsCount($this->tbl,$filter,$params) > 0 )
			return true ;
		else
			return false ;
	}
	
	function addRecord($datas) {
		$sql = "insert into " . $this->tbl ; ;
		$fld = "" ;
		$val = "" ;
		$fs = "" ;
		$params = array() ;
		try {
			if (is_array($datas) && count($datas) > 0) {
				foreach ($datas as $data) {
					$fld .= $fs . $data['field'] ;
					$val .= $fs . $this->db->formatValueParam($data['field']) ;
					$params[] = $this->db->valueParam($data['field'],$data['value']) ;
					$fs = ", " ;
				}
				$sql .= " (" . $fld . ") values (" . $val . ")";
				$this->db->insertRow($sql,$params) ;
				return true ;
			} else {
				return false ;
			}
		} catch (Exception $e) {
			return $e->getMessage(); ;
		}
	}
	
	
	function updateRecord($termId,$langId,$datas,$appType) {
		$sql = "update " . $this->tbl . " set " ;
		$fs = "" ;
		$params = array() ;
		if (is_array($datas) && count($datas) > 0) {
			foreach ($datas as $data) {
				if($data['field'] == $this->fldid1 || $data['field'] == $this->fldid2 || $data['field'] == $this->fldid3)
					continue;
				
				$sql .= $fs . $this->db->fieldParam($data['field']) ;
				$fs = ", " ;
				$params[] = $this->db->valueParam($data['field'],$data['value']) ;
			}
			$sql .= " where " . $this->db->fieldParam($this->fldid1) .
					" and " . $this->db->fieldParam($this->fldid2)  .
					" and " . $this->db->fieldParam($this->fldid3) ;
            
			$params[] = $this->db->valueParam($this->fldid1,$termId) ;
			$params[] = $this->db->valueParam($this->fldid2,$langId) ;
			$params[] = $this->db->valueParam($this->fldid3,$appType) ;
			
			
			return $this->db->updateRow($sql,$params) ;
		} else  {
			return false ;
		}
	}
	
	
	
	function deleteRecord($termId,$langId,$appType) {
		$sql = "delete from " . $this->tbl .
		" where " . $this->db->fieldParam($this->fldid1) .
		" and " . $this->db->fieldParam($this->fldid2)  .
		" and " . $this->db->fieldParam($this->fldid3) ;
	
		$params = array() ;
		$params[] = $this->db->valueParam($this->fldid1,$termId) ;
		$params[] = $this->db->valueParam($this->fldid2,$langId) ;
		$params[] = $this->db->valueParam($this->fldid3,$appType) ;
	
		return $this->db->deleteRows($sql,$params) ;
	}
}
?>