<?php

/**
 * Subclass for representing a row from the 'tblblogentrycomment' table.
 *
 *
 *
 * @package lib.model
 */
class Tblblogentrycomment extends BaseTblblogentrycomment
{
 /**
   * check permission delete comment
   */
  public function canDelete() {
    $user = sfContext::getInstance()->getUser();
    if (blogStore::isAdministator()) return true;
    if ($this->getCmUser() == $user->getUserId()) return true;
    return false;
  }

  public function getCommentIntro($limit = 10) {
    $ret = new html2text($this->getCmContent());
    $ret = $ret->get_text();
    $ret = split(' ', $ret);
    if (count($ret)>$limit) {
      $ret = array_splice($ret, 0, $limit);
    }
    return join(' ', $ret) . '...';
  }

  public function getRefTbluser() {
    if ($userInfo = $this->getCmUserInfo()) {
      $info = explode('|@|', $userInfo);
      $tbluser = new Tbluser();
      $tbluser->setUsId($this->getCmUser());
      $tbluser->setUsAvatar($info[0]);
      $tbluser->setUsFullname($info[1]);
    } else {
      if ($tbluser = $this->getTbluser()) {
        $avatar = $tbluser->getObject()->getAvatar();
        preg_match('@uploads\/thumbnails\/avatar\/([^"]*)\"@', $avatar, $match);
        $userInfo = $match[1].'|@|'.$tbluser->getUsFullname();
      } else {
        $userInfo = '|@|Không tên';
      }
      $this->setCmUserInfo($userInfo);
      $this->save();
    }
    return $tbluser;
  }
}
