<?php
class entryBase extends resourceBase {

  public $entry;

  public function entryBase(Tblblogentry $entry) {
    parent::resourceBase();
    $this->entry = $entry;
  }

  public function getTitleWithLink($baselink = '', $morelink = '', $options = array()) {
    $title = $this->entry->getEntryTitle();
    if ($title == '') return '';
    $link = $baselink.'/entry/show?entry_id='.$this->entry->getEntryId().$morelink;
    return link_to($title, $link, $options);
  }

  public function getThumbWithLink($baselink = '', $morelink = '', $options = array()) {
    $options = array_merge($options, array('class'=>'thumb', 'style'=>'padding-right:5px'));
    $thumbnail = $this->getThumbnail($options, false);
    if ($thumbnail == '') return '';
    $link = $baselink.'/entry/show?entry_id='.$this->entry->getEntryId().$morelink;
    return link_to($thumbnail, $link);
  }

  public function getEntryIntro() {
    $ret = new html2text($this->entry->getEntryContent());
    $ret = $ret->get_text();
    $ret = split(' ', $ret);
    $max_word = (blogStore::getNewsStyle()==2)? 60: 90;
    if (count($ret)>$max_word) {
      $ret = array_splice($ret, 0, $max_word);
    }
    return join(' ', $ret) . '...';
  }

  public function getPreview() {
    return content_tag('div',
      content_tag('div', $this->getThumbWithLink(), array('style'=>'float:left')).
      content_tag('div', $this->getEntryIntro(), array('style'=>'text-align:left;font-size:11px')),
      array('style'=>'margin:10px'));
  }

  public function getLikeLink($etcLink) {
    $likeLink = '';
    $info = explode('|', $this->entry->getEntryLikeinfo());
    for ($i = 0; $i < count($info); $i++) {
      $user = explode(',', $info[$i]);
      if (intval($user[0]) == blogStore::$userId) continue;
      if ($likeLink != '') $likeLink .= ', ';
      $likeLink .= link_to($user[1], '/user/show?id='.$user[0]);
    }
    if ($this->entry->getEntryLike() > 3) {
      if ($likeLink != '') $likeLink .= ', ';
      $likeLink .= $etcLink;
    }
    return $likeLink;
  }

  public function getEditLink($link) {
    $options = array('title'=>__('Edit'));
    if (!$this->entry->canEditEntry()) return image_tag('/images/layout1/edit-0.png', $options);
    return link_to(image_tag('/images/layout1/edit.png'), $link.'?entry_id='.$this->entry->getEntryId(), $options);
  }

  public function getDeleteLink($link) {
    $options = array('title'=>__('Delete'));
    if (!$this->entry->canDeleteEntry()) return image_tag('/images/layout1/delete-0.png', $options);
    return m_link_to(image_tag('/images/layout1/delete.png'), $link.'?entry_id='.$this->entry->getEntryId(), $options);
  }

  public function getMessageLink() {
    $typeName = commonData::getCatTypeName($this->entry->getEntryType());
    $title = str_replace('.', '%2E', urlencode('Trao đổi về '.$typeName.' "'.$this->getName().'"'));
    return '/message/mailbox?type=compose&us_to='.$this->entry->getEntryUser().'&title='.$title;
  }

  /**
   * @param bool $force: require thumbnail and crop from 150x150 image
   */
  public function getThumbnail($options = '', $force = true) {
    if ($this->entry->getEntryType() == 2) {
      $thumbType = $this->entry->getEntryThumb();
      $defIcon = $force? image_tag($this->thumbRoot.'uploads/thumbnails/news-icon.jpg', $options): '';
      if ($thumbType == 2 || ($thumbType == null && $this->createThumbnail() == false)) return $defIcon;
      $tag = image_tag($this->getThumbPath(false).$this->entry->getEntryId().'.jpg', $options);
      if ($force) return content_tag('div', $tag, array('style'=>'height:110px; overflow:hidden;'));
      else return $tag;
    }
    else return image_tag($this->thumbRoot.'uploads/thumbnails/'.$this->entry->getEntryThumbnail(), $options);
  }

  public function getSmallThumbnail($options = array()) {
    if ($this->entry->getEntryType() == 2) {
      $tag = $this->getThumbnail(array_merge($options, array('width'=>40, 'height'=>30)), false);
      if ($tag == '') return image_tag($this->thumbRoot.'uploads/thumbnails/news-icon-small.jpg', $options);
      else return $tag;
    }
    else return $this->entry->getLinkObject()->getSmallThumbnail($options);
  }

  public function getGlobalThumb($type = 'thumb') {
    if ($this->entry->getEntryBlog() != blogStore::$blogId) $link = 'entry/showglobal';
    else $link = $this->entry->getModule().'/show';
    return $this->getHtmlThumb('/'.$link.'?entry_id='.$this->entry->getEntryId(), $type);
  }

  public function getThumbPath($absolute = true) {
    $blogId = $this->entry->getEntryBlog();
    return blogStore::getBlogFolder($absolute, $blogId) . '/entrythumb/';
  }

  public function createThumbnail($forceCreate = false) {
    $this->entry->setEntryThumb(2);
    $this->entry->save();
    $path = $this->getThumbPath();
    $fileName = $path . $this->entry->getEntryId() . '.jpg';
    if ($forceCreate) @unlink($fileName);

    if (file_exists($fileName)) {
      $this->entry->setEntryThumb(1);
      $this->entry->save();
      return true;
    }
    $content = $this->entry->getEntryContent();
    if (preg_match('/<img.*?src="(.*?)".*?>/i', $content, $match)) {
      $src = $match[1];
      if ($src[0] == '/') $src = sfConfig::get('sf_web_dir') . $src;
      if (!file_exists($path)) mkdir($path, 0777, true);
      if (sfConversion::SizeImage(150, 150, $src, $fileName, false, true)) {
        myUtility::track_upload($fileName, 'blogs');
        $this->entry->setEntryThumb(1);
        $this->entry->save();
        return true;
      }
    }
    return false;
  }

  public function getName() {
    return $this->entry->getEntryTitle();
  }

  public function getStatus() {
    return $this->entry->getEntryStatus();
  }

  public function getModifydate() {
    return $this->entry->getEntryModifydate();
  }

  public function getUser() {
    return $this->entry->getEntryUser();
  }

  public function getFileType() {
    return 'news';
  }
}
?>
