<?php
/**
 * Made By Frody: ffrody@gmail.com (H.S.Choi)
 *
 * 본인 이외에 복제/사용 금지
 */

class Clist extends BaseCode {
  public $DB; //DB
  public $rows; //데이터 배열
  public $pages; //페이지 html
  public $url; //페이지 URL
  public $query; //uri query row (링크:URL 만들기용 URI Query필드)
  public $fld; //SQL 검색필드
  public $where; //SQL where
  public $whererow; //SQL where 배열
  public $viewnum; //라인수
  public $rownum; //하단 페이지수
  public $linktype; //클릭 링크 타입
  public $allcnt; //전체 수

  function __construct(){
    parent::__construct();
    $this->rows = array();
    $this->pages = array();
    $this->url = $_SERVER['PHP_SELF'];
    $this->fld = "*";
    $this->where = "1=1";
    $this->query = array();
    $this->page = 1;
    $this->orderby = "1";
    $this->viewnum = $GLOBALS['_varsBaseCode']['line_num']?:10;
    $this->rownum = $GLOBALS['_varsBaseCode']['page_num']?:10;
    $this->linktype = "";
  }

  public function getCommonList($tb="",$prt=false){//테이블, 필드, where절, order by절, 메인url, urlquery, 현재페이지, 목록수, 페이지수
    if( $tb ){
      $sql = "select count(*) from {$tb} where {$this->where}";
      //if( $prt ) echo $sql;
      $this->allcnt = $this->DB->queryFirstField($sql,$this->whererow,array(),$prt);
    }
    $pobj = new Pagenation();
    $pgobj = $pobj->get_pagenation($this->allcnt, $this->viewnum, $this->rownum, $this->page);
    $this->pagenation = $pgobj;

    $this->line_cnt = $pgobj->total_rows - $this->viewnum*($pgobj->current - 1);
    $this->pages = $this->getPages($pgobj);
    if( $tb ){
      //$sql = "select {$this->fld} from {$tb} where {$this->where} order by {$this->orderby} ". $this->DB->_limit($pgobj->limit[0],$pgobj->limit[1]);
      $sql = $this->DB->addLimit("select {$this->fld} from {$tb} where {$this->where}",$this->orderby,$pgobj->limit[0],$pgobj->limit[1]);
      $this->rows = $this->DB->query( $sql, $this->whererow );
      if( !$this->rows ){
        $this->rows = Array();
      }
    }
  }

  public function getCount($tb,$prt=false){//테이블, 필드, where절, order by절, 메인url, urlquery, 현재페이지, 목록수, 페이지수
    $sql = "select count(*) from {$tb} where {$this->where}";
    //if( $prt ) echo $sql;
    $allcnt = $this->DB->queryFirstField($sql, $this->whererow,array(),$prt);
    return $allcnt;
  }

  private function getHref( $url ){
    if( $this->linktype == "script" ){
      return 'href="###" onclick="common_page_move(\''.$url.'\')"';
    }else{
      return 'href='.$url;
    }
  }

  private function getPages($pgobj){
    $pagehtml = array();
    $query = $this->getQuery($this->query);
    if( $pgobj->pages[0] && $pgobj->pages[0] != 1 ){
      $pagehtml[] = "<a class='pn_link btn btn-light' ".$this->getHref("{$this->url}?{$query}&page=1")."><span class='f_page'>[<</span></a>";
      $pagehtml[] = "<a class='pn_link btn btn-light' ".$this->getHref("{$this->url}?{$query}&page={$pgobj->bprev}")."><span class='f_page'><</span></a>";
    }
    for( $a=0 ; $a<count($pgobj->pages) ; $a++ ){
      if( $pgobj->current == $pgobj->pages[$a] ){
        $pagehtml[] = "<a class='pn_link npage btn btn-light active' ".$this->getHref("{$this->url}?{$query}&page={$pgobj->pages[$a]}")."><span class='f_page'>{$pgobj->pages[$a]}</span></a>";
      }else{
        $pagehtml[] = "<a class='pn_link btn btn-light' ".$this->getHref("{$this->url}?{$query}&page={$pgobj->pages[$a]}")."><span class='f_page'>{$pgobj->pages[$a]}</span></a>";
      }
    }
    if( $pgobj->pages[count($pgobj->pages)-1] != $pgobj->last ){
      $pagehtml[] = "<a class='pn_link btn btn-light' ".$this->getHref("{$this->url}?{$query}&page={$pgobj->bnext}")."><span class='f_page'>></span></a>";
      $pagehtml[] = "<a class='pn_link btn btn-light' ".$this->getHref("{$this->url}?{$query}&page={$pgobj->last}")."><span class='f_page '>>]</span></a>";
    }
    return $pagehtml;
  }

  private function getQuery($qrow, $field="",$value=""){
    if( $field ){
      $qrow[$field] = $value;
    }
    $query = array();
    if( count($qrow) > 0 ){
      foreach( $qrow as $key => $val ){
        if( hasval($val) ){
          $query[] = "{$key}={$val}";
        }
      }
    }
    return implode("&", $query);
  }

  public function getUrl( $chgrow ){ //기존 query 에 $chgrow 부분을 변경/적용하여 url 가져오기
    $qrow = $this->query;
    if( count($chgrow) > 0 ){
      foreach( $chgrow as $key => $val ){
        $qrow[$key] = $val;
      }
    }
    return $this->url."?".$this->getQuery($qrow);
  }

  public function getOrdUrl($field,$oasc=""){ //기존 query에 order용 변경/적용할 부분을 간소화
    $oasc = $oasc?:($this->query['oasc']=="asc"?"desc":"asc");
    return $this->getUrl(array("ofld"=>$field,"oasc"=>$oasc,"page"=>1));
  }

  public function getEcptUrl($fields){ //기존 query 에 $fields 부분을 제외하고 url 가져오기
    $qrow = $this->query;
    if( count($qrow) > 0 ){
      foreach( $qrow as $key => $val ){
        if( is_array($fields) ){
          if( in_array($key, $fields) ) unset($qrow[$key]);
        }else{
          if( $key == $fields ) unset($qrow[$key]);
        }
      }
    }

    return $this->url."?".$this->getQuery($qrow);
  }

  public function getSearchUrl(){ //기존 query에 정렬부분만 남기고 초기화 하여 url 가져오기
    $qrow = $this->query;
    if( count($qrow) > 0 ){
      foreach( $qrow as $key => $val ){
        if( $key != "ofld" && $key != "oasc" ) unset($qrow[$key]);
      }
    }
    return $this->url."?".$this->getQuery($qrow);
  }

  public function getOrdQueryRow($field,$oasc=""){ //기존 query에 order용 변경/적용할 부분을 간소화하여 배열로 리턴
    $oasc = $oasc?:($this->query['oasc']=="asc"?"desc":"asc");
    return array("ofld"=>$field,"oasc"=>$oasc,"page"=>1);
  }

  public function getEcptQueryRow($fields){ //기존 query 에 $fields 부분을 제외하고 배열로 리턴
    $qrow = $this->query;
    if( count($qrow) > 0 ){
      foreach( $qrow as $key => $val ){
        if( is_array($fields) ){
          if( in_array($key, $fields) ) unset($qrow[$key]);
        }else{
          if( $key == $fields ) unset($qrow[$key]);
        }
      }
    }

    return $qrow;
  }

  public function getSearchQueryRow(){ //기존 query에 정렬부분만 남기고 초기화 하여 배열로 리턴
    $qrow = $this->query;
    if( count($qrow) > 0 ){
      foreach( $qrow as $key => $val ){
        if( $key != "ofld" && $key != "oasc" ) unset($qrow[$key]);
      }
    }
    return $qrow;
  }
}
