<?php
class Cmanage extends BaseCode {
  public $TB;

  function __construct( $table ){
    parent::__construct();
    $this->TB = $table;
  }

  public function getDataList( $fields="", $where="", $orderby="", $limit=array() ){ //데이터 리스트
    if( !$fields ) $fields = "*";
    if( !$where ){
      $where = new DB_where("and");
    }
    $wqry = $where->get_where();
    $whereqry = $wqry['qry'];
    $whererow = $wqry['val'];

    //$sql = "select {$fields} from {$this->TB} where {$whereqry} ".(($orderby)?"order by {$orderby}":""). $this->DB->_limit($limit[0],$limit[1]);
    $sql = $this->DB->addLimit("select {$fields} from {$this->TB} where {$whereqry}",$orderby,$limit[0],$limit[1]);
    $rst = $this->DB->qry($sql, $whererow);
    $row = array();
    while( $tmp = $this->DB->fetch_array($rst) ){
      $row[] = $tmp;
    }
    return $row;
  }

  public function getDataPage($fields="", $query=array(), $where="" ,$orderby=""){ //데이터 리스트 (페이징)
    $lists = $this->getDataPageCommon($fields, $query, $where, $orderby);

    $lists->url      = $_SERVER['PHP_SELF'];
    $lists->linktype = "";
    $lists->getCommonList($this->TB);

    return $lists;
  }

  public function getDataPageScript($fields="", $query=array(), $where="" ,$orderby="", $url=""){ //데이터 리스트 (페이징)
    $lists = $this->getDataPageCommon($fields, $query, $where, $orderby);

    $lists->linktype = "script";
    $lists->url      = $url?:$_SERVER['PHP_SELF'];
    $lists->getCommonList($this->TB);

    return $lists;
  }

  public function getDataPageCommon($fields="", $sqldata=array(), $where="" ,$orderby=""){ //데이터 리스트 (페이징)
    if( !$fields ) $fields = "*";
    if( !$where ){
      $where = new DB_where("and");
    }

    $wqry = $where->get_where();
    $whereqry = $wqry['qry'];
    $whererow = $wqry['val'];

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

    $page = $sqldata['page']?:1;
    unset($query['page']);

    $lists = new clist();
    $lists->DB->query_print = $this->DB->query_print;
    $lists->DB->query_record = $this->DB->query_record;
    $lists->fld      = $fields;
    $lists->where    = $whereqry;
    $lists->whererow = $whererow;
    $lists->orderby  = $orderby;
    $lists->url      = $_SERVER['PHP_SELF'];
    $lists->query    = $query;
    $lists->page     = $page;
    $lists->viewnum  = $query['viewnum']?:$GLOBALS['_varsBaseCode']['line_num'];
    $lists->rownum   = $query['rownum']?:$GLOBALS['_varsBaseCode']['page_num'];
    return $lists;
  }

  public function getData( $fields="", $urow="", $where="", $orderby="" ) {
    if( !$fields ) $fields = "*";
    if( gettype($urow) == "object" && get_class($urow) == "DB_where" ){
      $where = $urow;
    }
    if( !$where ){
      $where = new DB_where("and");
    }
    if( gettype($urow) == "array" ){
      foreach((array)$urow as $key => $val){
        if( $key ){
          $where->add("{$key} = ?",$val);
        }
      }
    }

    $wqry = $where->get_where();
    $whereqry = $wqry['qry'];
    $whererow = $wqry['val'];

    $sql = "select {$fields} from ".$this->TB." where {$whereqry}". ($orderby?" order by {$orderby}":"");
    return $this->DB->queryFirstRow($sql, $whererow);
  }

  public function putData( $sqldata ) { //데이터 등록
    if( $this->DB->tableExists( $this->TB ) ){
      return $this->DB->insert( $this->TB, $sqldata);
    }
    return 0;
  }

  public function setData( $sqldata, $urow="", $where="" ) { //데이터 수정
    if( gettype($urow) == "object" && get_class($urow) == "DB_where" ){
      $where = $urow;
    }
    if( !$where ){
      $where = new DB_where("and");
    }
    if( gettype($urow) == "array" ){
      foreach((array)$urow as $key => $val){
        if( $key ){
          $where->add("{$key} = ?",$val);
        }
      }
    }

    $wqry = $where->get_where();
    $whereqry = $wqry['qry'];
    $whererow = $wqry['val'];

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

  public function setDataParts( $sqldata, $field="", $urow="", $where=""){ //데이터 일괄 수정
    $fld = "";
    $arr = array();
    $wre = new DB_where("and");

    if( gettype($field) == "object" && get_class($field) == "DB_where" ){
      $wre = $field;
    }else if( gettype($urow) == "object" && get_class($urow) == "DB_where" ){
      $wre = $urow;
    }else if( $where ){
      $wre = $where;
    }

    if( gettype($field) == "array" ){
      $arr = $field;
    }else if( gettype($urow) == "array" ){
      $arr = $urow;
    }

    if( gettype($field) == "string" ){
      $fld = $field;
    }

    if( @count($arr) > 0 ){
      $subwhere = $wre->sub_where("or");
      foreach((array)$arr as $key => $val){
        $subwhere->add( ($fld?:$key)." = ?",$val);
      }
    }

    $wqry = $wre->get_where();
    $whereqry = $wqry['qry'];
    $whererow = $wqry['val'];

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

  public function delData( $urow="", $where="" ) { //데이터 삭제
    if( gettype($urow) == "object" && get_class($urow) == "DB_where" ){
      $where = $urow;
    }
    if( !$where ){
      $where = new DB_where("and");
    }
    if( gettype($urow) == "array" ){
      foreach((array)$urow as $key => $val){
        if( $key ){
          $where->add("{$key} = ?",$val);
        }
      }
    }

    $wqry = $where->get_where();
    $whereqry = $wqry['qry'];
    $whererow = $wqry['val'];

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