<?php
namespace cUtil;

use Cmanage;
use DB_where;

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

  function __construct(){
    //parent::__construct(TB_BIZ_MOFACT);
    //$this->DB->tableExists(TB_BIZ_MOFACT);
    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;

//    $sql = "USP_B_MSG_S10";
//    $params = Array(
//      "TRAN_TEXT" => $str?:"",
//    );
//
//    $rst = $this->DB->exec($sql, $params);
//    if( $this->DB->num_rows($rst) ){
//      $txt = $this->DB->fetch_array($rst);
//      $tran = json_decode($txt['TRAN']);
//      return $tran[$this->LANG];
//    }else{
//      $sql = "USP_B_MSG_I10";
//      $params = Array(
//        "TRAN_TEXT" => $str?:"",
//        "TRAN_DATA" => Array("KR"=>$str),
//        "EN" => "",
//        "BD" => "",
//        "BD" => "",
//        "JP" => "",
//        "VN" => "",
//        "PH" => "",
//        "TH" => "",
//        "ID" => "",
//        "IN" => "",
//        "CN" => "",
//        "MN" => "",
//        "NP" => "",
//        "PK" => "",
//        "UZ" => "",
//      );
//      $rst = $this->DB->exec($sql, $params);
//      return $str;
//    }
  }

  function getTran( $str, $isId="" ){
//    $sql = "USP_B_MSG_S10";
//    $params = Array(
//      "TRAN_TEXT" => $str?:"",
//    );
//
//    $rst = $this->DB->exec($sql, $params);

//    if( $this->DB->num_rows($rst) ){
//      $txt = $this->DB->fetch_array($rst);
//      $tran = json_decode($txt['TRAN_DATA']);
//      return $tran;
//    }
//    return false;

    $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 ){
//    $sql = "USP_B_MSG_I10";
//    $params = Array(
//      "TRAN_TEXT" => $str?:"",
//      "TRAN_DATA" => json_encode(Array("KR"=>$str)),
//    );
//    $rst = $this->DB->exec($sql, $params);
//    return $rst;
    $sqldata = Array(
      "TRAN_TEXT" => $str?:"",
      "TRAN_DATA" => json_encode(Array("KR"=>$str)),
      "MAKE_TM"   => $this->DB->sqleval("getdate()"),
    );
    $this->DB->insert($this->TB, $sqldata);
  }

  function setWord( $str, $stran, $isId="" ){
    $tran = $this->getTran( $str, $isId );
    $tran = array_filter(array_merge($tran, $stran));
//    $sql = "USP_B_MSG_U10";
//    $params = Array(
//      "TRAN_TEXT" => $str?:"",
//      "TRAN_DATA" => json_encode($tran),
//    );
//    $rst = $this->DB->exec($sql, $params);
//    return $rst;

    $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),
      "EDIT_TM"   => $this->DB->sqleval("getdate()"),
    );

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

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

  function getLang ( $slang="", $use="" ){
    $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( $use ){
      $where->add("cl.USE_FLG = ?", "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, cl.USE_FLG 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'],
        "USE_FLG" => "Y",
        "MAKE_TM" => $this->DB->sqleval("getdate()"),
        "EDIT_TM" => $this->DB->sqleval("getdate()"),
      );
      $this->DB->insert(TB_COMMENT_LANG, $qryrow);
    }
  }
}