<?php
class docBase extends resourceBase {

  public $doc;
  public function docBase(Tbldocument $doc) {
    parent::resourceBase();
    $this->doc = $doc;
  }

  public function getPath($absolute = true, $data_dir = 'resources', $folder = '') {
    if ($folder == '') $folder = $this->doc->getDocCategory();
    return sfConfig::get($absolute? 'sf_upload_dir': 'sf_upload_dir_name')."/$data_dir/$folder/";
  }

  public function getFullFileName($absolute = true) {
    return $this->getPath($absolute).$this->doc->getDocFilename();
  }

  public function getThumbFileName($absolute = true) {
    return $this->getPath($absolute, $this->thumbdir).'thumbnails/'.$this->doc->getDocFilename().'.jpg';
  }

  public function getThumbnail($options = null) {
    if ($this->doc->getDocThumbnail() <= 1) $thumbFile = $this->getThumbFileName(false);
    else $thumbFile = 'uploads/thumbnails/media_icon.jpg';
    return image_tag($this->thumbRoot.$thumbFile, $options);
  }

  public function getPreviewFileName($absolute = true) {
    return $this->getFullFileName($absolute);
  }

  public function getPreviewUrl() {
    return $this->viewRoot.$this->getPreviewFileName(false);
  }

  public function getGlobalThumb($type = 'thumb') {
    return $this->getHtmlThumb('/entry/showglobal?doc_id='.$this->doc->getDocId(), $type);
  }

  public function getInnerSize($width, $height) {
    $dw = $this->doc->getDocWidth();
    $dh = $this->doc->getDocHeight();
    if ($dw > 0 && $dh > 0) {
      $sx = $width / $dw;
      $sy = $height / $dh;
      $s = min($sx, $sy);
      $width = round($s * $dw);
      $height = round($s * $dh);
    }
    return array('width' => $width, 'height' => $height);
  }

  public function onUpload($action)
  {
    $fileName = $action->fileName;
    $ext = myUtility::getFileExt($fileName);
    $catItem = blogStore::$catTree->getCatItem($action->cat_id);
    $allowExts = $catItem->getAllowExts();
    if (!in_array($ext, $allowExts))
      throw new Exception('Chỉ đưa lên được các loại file: '.strtoupper(join(', ', $allowExts)));

    $path = $this->getPath();
    $ext2 = in_array($ext, array('avi', 'mpg', 'mpeg', 'asf', 'wmv', 'mov', '3gp', 'dat', 'flv', 'mp4'))? 'flv': $ext;
    $fileNoExt = myUtility::getFileName($fileName);
    myUtility::GetNoDuplicateFileName($path, $fileNoExt, $ext2);
    $fileName = $fileNoExt.'.'.$ext;

    $this->doc->setDocFileName($fileNoExt.'.'.$ext2);
    $this->doc->setDocFileSize($action->fileSize);
    $this->doc->setDocType($ext);
    if (!$action->created) {
      $this->doc->setDocWidth(0);
      $this->doc->setDocHeight(0);
    }

    $tempFile = $action->getRequest()->CopyTempFile($action->uploaded, $this->doc->getDocDate().'_'.$fileName);
    $this->doc->getObject()->onPrivateUpload($tempFile);
    $this->doc->updateBlogCategoryEntryPath(null);

    myUtility::log("Start copy document $doc_id", "uploads.log");
    copy($tempFile, $path.$fileName);
    myUtility::track_upload($path.$fileName, 'resources');
    unlink($tempFile);
    $this->doc->setDocStatus(0);
  }

  function getManualThumbnail() {
    switch (myUtility::getFileExt($this->doc->getDocFileName())) {
      case 'swf':
        if (!file_exists($this->getFullFileName(true))) return;
        return flash_tag($this->viewRoot.'plugins/flash/thumbCreateManual.swf', 300, 250, array(
        'childMov'=>$this->viewRoot.$this->getFullFileName(false),
        'sizex'=>$this->doc->getDocWidth(),
        'sizey'=>$this->doc->getDocHeight(),
        'docId'=>$this->doc->getDocId(),
        'createMov'=>$this->viewRoot.'main/document/swfthumb'));
        break;
      case 'flv':
        return flash_tag($this->viewRoot.'plugins/flash/thumbFlvCreateManual.swf', 300, 250, array(
        'flvName'=>$this->viewRoot.$this->getFullFileName(false),
        'docId'=>$this->doc->getDocId(),
        'createMov'=>$this->viewRoot.'main/document/flvthumb'));
        break;
      default:
        return $this->getPreview();
    }
  }

  function MoveDataFrom($folder)
  {
    $fileName = $this->doc->getDocFileName();
    $oldPath = $this->getPath(true, 'resources', $folder);
    $oldThumbPath = $this->getPath(true, $this->thumbdir, $folder);
    myUtility::log("Move $fileName from $folder to ".$this->doc->getDocCategory(), 'uploads.log');

    $oldFile = $oldPath.$fileName;
    $oldThumbFile = $oldThumbPath."thumbnails/$fileName.jpg";
    $oldThumbFile2 = $oldThumbPath."thumbnails2/$fileName.jpg";

    $newPath = $this->getPath();
    $ext = myUtility::getFileExt($fileName);
    $originNoExt = $fileNoExt = myUtility::getFileName($fileName);
    myUtility::GetNoDuplicateFileName($newPath, $fileNoExt, $ext);
    $fileName = $fileNoExt.'.'.$ext;
    $this->doc->setDocFileName($fileName);

    $newThumDir = $this->getPath(true, $this->thumbdir).'thumbnails/';
    $newThumDir2 = $this->getPath(true, $this->thumbdir).'thumbnails2/';

    umask(0000);
    if (!file_exists($newThumDir)) mkdir($newThumDir, 0777, true);
    if (!file_exists($newThumDir2)) mkdir($newThumDir2, 0777, true);
    if (file_exists($oldThumbFile)) rename($oldThumbFile, $newThumDir.$fileName.'.jpg');
    if (file_exists($oldThumbFile2)) rename($oldThumbFile2, $newThumDir2.$fileName.'.jpg');
    if (file_exists($oldFile)) rename($oldFile, $newPath.$fileName);

    $type = $this->doc->getDocType();
    if ($type == 'mpe') $type = 'mpeg';
    if (in_array($type, array('avi','mpg','mpeg','asf','wmv','mov','3gp','dat'))) {
      $fileName = $originNoExt.'.'.$type;
      $oldFile = $oldPath.$fileName;
      $newFile = $newPath.$fileName;
      if (file_exists($oldFile)) rename($oldFile, $newFile);
    }
  }

  public function updateDocSize() {
    $fname = $this->getFullFileName(true);
    $ext = myUtility::getFileExt($fname);
    if (in_array($ext, array('gif', 'jpg', 'png', 'bmp', 'tif', 'swf')) && file_exists($fname)) {
      $size = sfConversion::getImageSize($fname);
      if ($size[0] == 0) return;
      $this->doc->setDocWidth($size[0]);
      $this->doc->setDocHeight($size[1]);
      $this->doc->save();
    }
  }

  public function getName() {
    return $this->doc->getDocName();
  }

  public function getStatus() {
    return $this->doc->getDocStatus();
  }

  public function getModifydate() {
    return $this->doc->getDocDate();
  }

  public function getUser() {
    return $this->doc->getDocUser();
  }

  public function getFileType() {
    return $this->doc->getDocType();
  }
}
?>
