<?php
class Attach extends Cmanage {
  function __construct( $table ){
    parent::__construct($table);

    $this->DB->query_record = false; //query log 기록 금지

    $this->DB->tableExists($this->TB);
  }

  public function getAttachList($fields, $gubun, $nid="", $orderby=""){
    $where = new DB_where("and");
    if( $gubun ){
      $where->add("gubun = ?", $gubun);
    }
    if( $nid ){
      $where->add("nid = ?", $nid);
    }

    $orderby=$orderby?:"file_id desc";
    $limit=array();

    $rows = $this->getDataList($fields, $where, $orderby, $limit);
    for( $a=0 ; $a<count($rows) ; $a++ ){
      $rows[$a]['file_url'] = _SITE_INC_URL_."/getFile.php?gubun={$rows[$a]['gubun']}&nid={$rows[$a]['nid']}&sid={$rows[$a]['sid']}";
    }
    return $rows;
  }

  public function getAttach( $gubun, $nid, $sid ){
    $whererow = Array(
      "gubun" => $gubun,
      "nid" => $nid,
      "sid" => $sid,
    );
    $row = $this->getData("*", $whererow);
    $row['file_url'] = _SITE_INC_URL_."/getFile.php?gubun={$row['gubun']}&nid={$row['nid']}&sid={$row['sid']}";
    return $row;
  }
  public function getAttachId( $file_id ) {
    $whererow = Array(
      "file_id" => $file_id,
    );
    $row = $this->getData("*", $whererow);
    $row['file_url'] = _SITE_INC_URL_."/getFile.php?gubun={$row['gubun']}&nid={$row['nid']}&sid={$row['sid']}";
    return $row;
  }

  public function putAttach( $upfile, $gubun, $nid, $sid ) {
    $target_dir = _UP_PATH_."/{$gubun}";
    if( !file_exists($target_dir) ){
      mkdir($target_dir);
    }
    $rst = move_uploaded_file($upfile['tmp_name'], "{$target_dir}/{$nid}_{$sid}.dat");
    if( !$rst ) return false;

    if( strpos($upfile['type'], "image") !== false ){
      $iinfo = getimagesize("{$target_dir}/{$nid}_{$sid}.dat");
      $upfile['width'] = $iinfo[0];
      $upfile['height'] = $iinfo[1];
    }

    $sqldata = Array(
      "gubun"       => $gubun?:"",
      "nid"         => $nid?:"",
      "sid"         => $sid?:"",
      "file_name"   => $upfile['name']?:"",
      "file_size"   => $upfile['size']?:0,
      "file_type"   => $upfile['type']?:"",
      "file_width"  => $upfile['width']?:"",
      "file_height" => $upfile['height']?:"",
      "reg_date"    => $this->DB->sqleval("getdate()"),
    );

    return $this->putData( $sqldata );
  }

  public function setAttach( $upfile, $gubun, $nid, $sid ) {
    $target_dir = _UP_PATH_."/{$gubun}";
    if( !file_exists($target_dir) ){
      mkdir($target_dir);
    }
    $rst = move_uploaded_file($upfile['tmp_name'], "{$target_dir}/{$nid}_{$sid}.dat");
    if( !$rst ) return false;

    if( strpos($upfile['type'], "image") !== false ){
      $iinfo = getimagesize("{$target_dir}/{$nid}_{$sid}.dat");
      $upfile['width'] = $iinfo[0];
      $upfile['height'] = $iinfo[1];
    }

    $sqldata = Array(
      "gubun"       => $gubun?:"",
      "nid"         => $nid?:"",
      "sid"         => $sid?:"",
      "file_name"   => $upfile['name']?:"",
      "file_size"   => $upfile['size']?:0,
      "file_type"   => $upfile['type']?:"",
      "file_width"  => $upfile['width']?:"",
      "file_height" => $upfile['height']?:"",
      "reg_date"    => $this->DB->sqleval("getdate()"),
    );

    $whererow = Array(
      "gubun" => $gubun?:"",
      "nid" => $nid?:"",
      "sid" => $sid?:"",
    );

    return $this->setData( $sqldata, $whererow );
  }
  public function setAttachId( $upfile, $file_id ) {
    $ainfo = $this->getAttachId( $file_id );
    if( !$ainfo['file_id'] ) return false;

    return $this->setAttach( $upfile, $ainfo['gubun'], $ainfo['nid'], $ainfo['sid'] );
  }

  public function delAttach( $gubun, $nid, $sid ){
    $urow = Array();
    $urow['gubun'] = $gubun;
    $urow['nid'] = $nid;
    $urow['sid'] = $sid;
    $where = "";

    $target_dir = _UP_PATH_."/{$gubun}";
    //$rst = @unlink("{$target_dir}/{$nid}_{$sid}.dat");
    $rst = $this->fileRemove("{$target_dir}/{$nid}_{$sid}.dat*");
    if( !$rst ) return false;

    return $this->delData($urow, $where);
  }
  public function delAttachId( $file_id ){
    $ainfo = $this->getAttachId( $file_id );
    return $this->delAttach( $ainfo['gubun'], $ainfo['nid'], $ainfo['sid'] );
  }

  static function fByte($bytes, $precision=2) {
    $unit = ["B", "KB", "MB", "GB"];
    $exp = floor(log($bytes, 1024)) ?: 0;
    return round($bytes / (pow(1024, $exp)), $precision).$unit[$exp];
  }

  static function fileRemove($filepath, $unremove=""){
    $glob = glob($filepath);
    foreach( $glob as $filename ) {
      if( !$unremove || $filename != $unremove ){
        @unlink($filename);
      }
    }
    return true;
  }
}