<?php
class resourceBase {

  protected $viewRoot;
  protected $viewdir;
  protected $thumbRoot;
  protected $thumbdir;

  public function resourceBase() {
    $this->viewRoot  = sfConfig::get('app_resource_root');
    $this->viewdir   = sfConfig::get('app_resource_dir');
    $this->thumbRoot = sfConfig::get('app_thumbnail_root');
    $this->thumbdir  = sfConfig::get('app_thumbnail_dir');
  }

  /**
   * Get object for each type of presentation, document, media,...
   */
  public static function getResourceObject($resource, $type, $defClass) {
    $app_classes = sfConfig::get('app_resource_class');
    foreach ($app_classes as $class => $typeList) {
      if (in_array($type, $typeList)) return new $class($resource);
    }
    return new $defClass($resource);
  }

  /**
   * return html thumbnail include image, shortname and link
   *
   * @param $path: path of link. For exam "present/show?pr_id="
   * @param $type: 'thumb' or 'shortdesc'
   */
  public function getHtmlThumb($link, $type = 'thumb') {

    if ($this->getStatus() >= 2) {
      if ($type != 'shortdesc') {
        $thumb = $this->thumbRoot.'uploads/thumbnails/deleted.jpg';
        return content_tag('li', image_tag($thumb).content_tag('label', 'Đã bị xóa'));
      } else {
        $thumb = $this->thumbRoot.'uploads/thumbnails/deleted-small.jpg';
        $info = image_tag($thumb).content_tag('div', 'Đã bị xóa');
        return content_tag('div', $info, array('class'=>'shortdesc'));
      }
    }

    switch ($type) {
    case 'thumb':
      return content_tag('li',
        $this->getThumbnail().
        content_tag('label', link_to($this->getShortName(), $link)),
        array('onclick' => preg_match('/^javascript:(.*)$/', $link, $match)? $match[1]: "window.location='".url_for($link)."'",
              'onmouseover' => "this.className='over'",
              'onmouseout' => "this.className=''"));

    case 'shortdesc':
      return content_tag('div',
        link_to($this->getSmallThumbnail(), $link).
        content_tag('div',
        content_tag('h3', link_to(myUtility::mb_truncate_text($this->getName(), 32), $link), array('style'=>'white-space: nowrap')).
        content_tag('p', 'Ngày gửi: ' . $this->getModifydate('d-m-Y'))),
        array('class'=>'shortdesc'));

    case 'title':
      return content_tag('div',
        link_to(image_tag('filetype/16x16/'.$this->getFileType().'.png'), $link).
        content_tag('h3', link_to(myUtility::mb_truncate_text($this->getName(), 32), $link), array('style'=>'white-space: nowrap')),
        array('class'=>'shortdesc'));

    case 'smallview':
      $w = 253; $h = 215;
      return content_tag('div',
        content_tag('div', $this->getPreview($w, $h), array('style'=>'border: 3px solid #6699AA; width:'.$w.'px; height:'.$h.'px; overflow:hidden')).
        content_tag('div', link_to($this->getShortName(15), $link), array('style'=>'padding-top:3px; width:'.$w.'px')),
        array('style'=>'float:left; text-align:center; font-size:0.9em; line-height: 1.1em; height:'.($h+33).'px; padding-right:5px;'));

    case 'bigview':
      return content_tag('div',
        content_tag('div', $this->getPreview(333, 283), array('style'=>'border: 3px solid #6699AA; width:333px; height:280px; overflow:hidden')).
        content_tag('div', link_to($this->getShortName(9, 0), $link), array('style'=>'padding-top:3px; width:333px')),
        array('style'=>'float:left; text-align:center; font-weight:bold; height:306px; overflow:hidden; padding-right:5px'));
    }
  }

  public function getHighlightThumb($options = array()) {
    return content_tag('li', $this->getThumbnail(), array(
      'onmouseover' => "this.className='over'",
      'onmouseout' => "this.className=''"));
  }

  public function getSmallThumbnail($options = array()) {
    return $this->getThumbnail(array_merge($options, array('width'=>40, 'height'=>30)));
  }

  /**
   * return short name of presentation or document name
   *
   * @param $maxlength: Maximum number of word in name
   * @param $lastlength: Number of last words
   */
  public function getShortName($maxlength = 11, $lastlength = 3) {
    $name = $this->getName();
    $name = trim($name, '"');
    $names = split(' ', $name);
    $length = count($names);

    for ($i = 0; $i < $length; $i++) {
      if (mb_strlen($names[$i], 'utf8') > 10) {
        $names[$i] = mb_substr($names[$i], 0, 10, 'utf8');
      }
    }

    if ($length > $maxlength) {
      $names1 = array_slice($names, 0, $maxlength-$lastlength);
      $names2 = array_slice($names, $length-$lastlength, $lastlength);
      $name = join($names1, ' ') . ' ... ' . join($names2, ' ');
    } else {
      $name = join($names, ' ');
    }

    return $name;
  }
}
?>