<?php
namespace cUtil;

use Cmanage;
use DB_where;

if( !defined("TB_comment") ){
  define("TB_comment","");
}

if( !defined("TB_comment_lang") ){
  define("TB_comment_lang","");
}

Class Mlang extends Cmanage {
  public $LANG;
  public $TB;

  function __construct(){
    parent::__construct(TB_comment);
    $this->TB = TB_comment;
    $this->DB->tableExists($this->TB);
    $this->DB->tableExists(TB_comment_lang);
    $this->LANG = "KR";
  }

  function loadWord( $str, $lang="" ){
    $tran = $this->getWord( $str, $lang );
    if( !$tran ){
      $this->putWord( $str );
    }else{
      return $tran;
    }
    return $str;
  }

  function getTran( $str, $isId="" ){
    $where = new DB_where("and");
    if( $isId ){
      $where->add("TRAN_ID = ?", $str);
    }else{
      $where->add("TRAN_TEXT = ?", $str);
    }
    $wqry = $where->get_where();
    $whereqry = $wqry['qry'];
    $whererow = $wqry['val'];

    $sql = "select * from {$this->TB} where {$whereqry}";
    $this->DB->query_record = false; //query log 기록 금지
    $txt = $this->DB->queryFirstRow($sql, $whererow);
    $this->DB->query_record = true; //query log 기록 재개

    if( $txt ){
      $tran = (Array)json_decode($txt['TRAN_DATA']);
      return $tran;
    }
    return false;
  }

  function getWordList($fields, $where=""){
    if( !$where ){
      $where = new DB_where("and");
    }
    $orderby="TRAN_TEXT asc";
    $limit=array();

    $rows = $this->getDataList($fields, $where, $orderby, $limit);

    return $rows;
  }

  public function getWordPage($sqldata, $where=""){
    if( !$where ){
      $where = new DB_where("and");
    }

    if( $sqldata['stxt'] ){
      $subwhere = $where->sub_where("or");
      if( $sqldata['sfld'] ){
        $subwhere->add("{$sqldata['sfld']} like ?", "%{$sqldata['stxt']}%");
      }else{
        $subwhere->add("TRAN_TEXT like ?", "%{$sqldata['stxt']}%");
      }
    }

    $orderby = "TRAN_TEXT desc";
    if( $sqldata['ofld'] && $sqldata['oasc'] ){
      $orderby = "{$sqldata['ofld']} {$sqldata['oasc']}";
    }

    $query = array(
      "mode"     => $sqldata['mode'],
      "viewnum"  => $sqldata['viewnum'],
      "sdate"    => $sqldata['sdate']?:"",
      "fdate"    => $sqldata['fdate']?:"",
      "page"     => $sqldata['page'],
      "sfld"     => $sqldata['sfld'],
      "stxt"     => $sqldata['stxt'],
      "ofld"     => $sqldata['ofld'],
      "oasc"     => $sqldata['oasc'],
    );

    $lists = $this->getDataPage("*", $query, $where ,$orderby);
    return $lists;
  }

  function getWord( $str, $lang ){
    $str = $this->getTran($str);
    $lang = $lang ?: $this->LANG;
    if( $str ){
      return $str[$lang];
    }
    return false;
  }

  function getWordId( $id, $lang="" ){
    $str = $this->getTran($id, 1);
    $lang = $lang ?: $this->LANG;
    if( $str ){
      return $str[$lang];
    }
    return false;
  }

  function putWord( $str ){
    $sqldata = Array(
      "TRAN_TEXT" => $str?:"",
      "TRAN_DATA" => json_encode(Array("KR"=>$str)),
    );
    $this->DB->insert($this->TB, $sqldata);
  }

  function setWord( $str, $stran, $isId="" ){
    $tran = $this->getTran( $str, $isId );
    $tran = array_filter(array_merge($tran, $stran));

    $where = new DB_where("and");
    if( $isId ){
      $where->add("TRAN_ID = ?", $str);
    }else{
      $where->add("TRAN_TEXT = ?", $str);
    }
    $wqry = $where->get_where();
    $whereqry = $wqry['qry'];
    $whererow = $wqry['val'];

    $sqldata = Array(
      "TRAN_DATA" => json_encode($tran),
    );

    return $this->DB->update($this->TB, $sqldata, $whereqry, $whererow);
  }

  public static function getText( $str, $var=array() ){
    $langObj = new Mlang();
    if( $langObj->TB ){
      $str = $langObj->loadWord( $str );
    }
    for( $a=0 ; $a<@count($var) ; $a++ ){
      $str = str_replace("{@".($a+1)."}", $var[$a], $str);
    }
    return $str;
  }

  function getLang ( $slang="" ){
    $where = new DB_where("and");
    $where->add("bc.Bran_Uid = ?", "nation_code");
    $where->add("bc.Code_Uid != ?", "nation_code");
    $where->add("bc.Code_Uid != ?", "KR");
    $where->add("bc.Active = ?", "Y");
    if( $slang ){
      $swhere = $where->sub_where("or");
      foreach( (array)$slang as $lang ){
        $swhere->add("bc.Code_Uid = ?", $lang);
      }
    }
    $wqry = $where->get_where();
    $whereqry = $wqry['qry'];
    $whererow = $wqry['val'];

    $sql = "select bc.Code_Uid, bc.Code_Val from ".TB_basecode." as bc left join ".TB_comment_lang." as cl on bc.Code_Uid = cl.LANG_ID where {$whereqry} order by bc.Code_Val";
    $row = $this->DB->query($sql, $whererow);
    return $row;
  }

  function setLang($params){
    $sql = "delete from ".TB_comment_lang;
    $this->DB->qry($sql);

    $rows = $this->getLang($params);

    foreach( $rows as $lang ){
      $qryrow = Array(
        "LANG_ID" => $lang['Code_Uid'],
        "LANG_NM" => $lang['Code_Val'],
      );
      $this->DB->insert(TB_comment_lang, $qryrow);
    }
  }
}