<?php

/**
 * Subclass for representing a row from the 'tblblogentry' table.
 *
 *
 *
 * @package lib.model
 */
class Tblblogentry extends BaseTblblogentry
{
  protected $entryObj = null;

  public function getObject() {
    if ($this->entryObj == null) $this->entryObj = new entryBase($this);
    return $this->entryObj;
  }

  public function getLinkObject() {
    switch ($this->getEntryType()) {
      case 0:
      case -1:
      case -2:
      case -3:
        return $this->getTblpresentation()->getObject();
      case 1:
        return $this->getTbldocument()->getObject();
      case 2:
        return $this->getObject();
    }
  }

  public function canEditEntry() {
    return (blogStore::$userId == $this->getLinkObject()->getUser() || sfContext::getInstance()->getUser()->isAdministrator());
  }

  public function canDeleteEntry() {
    return (blogStore::$userId == $this->getEntryUser() || blogStore::isAdministator());
  }

  public function canDeleteAll() {
    return $this->canEditEntry();
  }

  public function canPostComment() {
    return blogStore::canComment($this->getEntryCategory());
  }

  public function canMod() {
    return blogStore::canMod($this->getEntryCategory());
  }

  public function getModule() {
    return commonData::getModule($this->getEntryType());
  }

  public function getListUrl() {
    return '/'.$this->getModule().'/list?cat_id='.$this->getEntryCategory().'&entry_id='.$this->getEntryId();
  }

  public function getUpdateBlogCategoryEntryCriteria($create = false) {
    $c = new Criteria();
    if ($create || $this->isColumnModified(TblblogentryPeer::ENTRY_MODIFYDATE)) $c->add(TblblogcategoryentryPeer::CE_MODIFYDATE, $this->getEntryModifydate());
    if ($create || $this->isColumnModified(TblblogentryPeer::ENTRY_HIT)) $c->add(TblblogcategoryentryPeer::CE_HIT, $this->getEntryHit());
    if ($create || $this->isColumnModified(TblblogentryPeer::ENTRY_SCORE)) $c->add(TblblogcategoryentryPeer::CE_SCORE, $this->getEntryScore());
    if ($create || $this->isColumnModified(TblblogentryPeer::ENTRY_STATUS)) $c->add(TblblogcategoryentryPeer::CE_STATUS, $this->getEntryStatus()==1? 0: $this->getEntryStatus());
    if ($create || $this->isColumnModified(TblblogentryPeer::ENTRY_TYPE)) $c->add(TblblogcategoryentryPeer::CE_TYPE, $this->getEntryType());
    if ($create || $this->isColumnModified(TblblogentryPeer::ENTRY_TITLE)) $c->add(TblblogcategoryentryPeer::CE_TITLE, $this->getLinkObject()->getName());
    if ($create || $this->isColumnModified(TblblogentryPeer::ENTRY_LINK)) $c->add(TblblogcategoryentryPeer::CE_LINK, $this->getEntryLink());
    return $c;
  }

  /**
   * If doc_category is changed, modify Tblpresentationcategory table
   */
  public function save(PropelPDO $con = null)
  {
    $createNew = $this->isNew();
    $catModify = $this->isColumnModified(TblblogentryPeer::ENTRY_CATEGORY);
    $cInsert = $this->getUpdateBlogCategoryEntryCriteria($catModify);

    parent::save($con);

    if ($catModify) {  //delete old and insert new to Tblblogcategoryentry
      if (!$createNew) {        
        $c = new Criteria();
        $c->add(TblblogcategoryentryPeer::CE_ENTRY, $this->getEntryId());
        TblblogcategoryentryPeer::doDelete($c);
      }
      $categories = $this->getTblblogcategory()->getCatPath();
      foreach (explode(',', $categories) as $category) {
        $c = clone $cInsert;
        $c->add(TblblogcategoryentryPeer::CE_ENTRY, $this->getEntryId());
        $c->add(TblblogcategoryentryPeer::CE_CATEGORY, $category);
        TblblogcategoryentryPeer::doInsert($c);
      }
    }
    else if (!$createNew && count($cInsert->getMap()) > 0) {  //update Tblblogcategoryentry
      $c = new Criteria();
      $c->add(TblblogcategoryentryPeer::CE_ENTRY, $this->getEntryId());
      BasePeer::doUpdate($c, $cInsert, Propel::getConnection(TblblogcategoryentryPeer::DATABASE_NAME));
    }
  }

  public function delete(PropelPDO $con = null) {
    $entryId = $this->getEntryId();
    parent::delete($con);

    $c = new Criteria();
    $c->add(TblblogcategoryentryPeer::CE_ENTRY, $entryId);
    TblblogcategoryentryPeer::doDelete($c);

    $c = new Criteria();
    $c->add(TblblogentrycommentPeer::CM_ENTRY, $entryId);
    TblblogentrycommentPeer::doDelete($c);

    $c = new Criteria();
    $c->add(TblblogentrylikePeer::EL_ENTRY, $entryId);
    TblblogentrylikePeer::doDelete($c);
  }

  public function getRecentEntrys($limit = 5) {
    $c = new Criteria();
    $c->add(TblblogcategoryentryPeer::CE_CATEGORY, $this->getEntryCategory());
    $c->add(TblblogcategoryentryPeer::CE_STATUS, 0);
    $c->add(TblblogcategoryentryPeer::CE_MODIFYDATE, $this->getEntryModifydate(), Criteria::LESS_THAN);
    $c->addDescendingOrderByColumn(TblblogcategoryentryPeer::CE_MODIFYDATE);
    $c->setLimit($limit);
    return TblblogcategoryentryPeer::doSelect($c);
  }

  public function addEntryInvalid($reason) {
    $tblblogentryinvalid = new Tblblogentryinvalid();
    $tblblogentryinvalid->setEiEntry($this->getEntryId());
    $tblblogentryinvalid->setEiContent($reason);
    $tblblogentryinvalid->setEiFrom(blogStore::$userId);
    $tblblogentryinvalid->setEiTo($this->getEntryUser());
    $tblblogentryinvalid->setEiBlog($this->getEntryBlog());
    $tblblogentryinvalid->setEiDatesent(date('Y-m-d H:i:s'));
    $tblblogentryinvalid->save();
  }

  public function getEntryInvalid() {
    return TblblogentryinvalidPeer::retrieveByPK($this->getEntryId());
  }

  public function addLikeUser($user_id) {
    try {
      $tbllike = new Tblblogentrylike;
      $tbllike->setElEntry($this->getEntryId());
      $tbllike->setElUser($user_id);
      $tbllike->setElDate(date('Y-m-d H:i:s'));
      $tbllike->save();
    } catch (Exception $e) {
      return false;
    }
    $tbluser = TbluserPeer::retrieveByPk($user_id);
    $userStr = $tbluser->getUsId().','.$tbluser->getUsFullName();
    $likeInfo = $this->getEntryLikeinfo();
    $users = $likeInfo? explode('|', $likeInfo): array(); 
    if (!in_array($userStr, $users)) {
      if (count($users) >= 3) array_pop($users);
      array_unshift($users, $userStr);
      $this->setEntryLikeinfo(implode('|', $users));
    }
    $this->setEntryLike($this->getEntryLike() + 1);
    $this->save();
  }

  public function removeLikeUser($user_id) {
    $c = new Criteria();
    $c->add(TblblogentrylikePeer::EL_ENTRY, $this->getEntryId());
    $c->add(TblblogentrylikePeer::EL_USER, $user_id);
    TblblogentrylikePeer::doDelete($c);
    $this->setEntryLike($this->getEntryLike() - 1);
    $this->save();
  }

  public function getLikeUser($user_id) {
    return TblblogentrylikePeer::retrieveByPk($this->getEntryId(), $user_id);
  }

  public function getLikeList($limit = 50) {
    $c = new Criteria();
    $c->add(TblblogentrylikePeer::EL_ENTRY, $this->getEntryId());
    $c->addDescendingOrderByColumn(TblblogentrylikePeer::EL_DATE);
    $c->setLimit($limit);
    return TblblogentrylikePeer::doSelect($c);
  }

  public function deleteByUser($all = false) {
    if ($all) {
      if ($this->getEntryType() == 1) {
        $tbldocument = $this->getTbldocument();
        $tbldocument->setDocStatus(2);
        $tbldocument->save();
      }
      if ($this->getEntryType() <= 0) {
        $tblpresentation = $this->getTblpresentation();
        $tblpresentation->setPrStatus(2);
        $tblpresentation->save();
      }
    } else {
      $this->setEntryStatus(2);
      $this->save();
    }
  }

  public function getTblpresentation(PropelPDO $con = null) {
    $r = parent::getTblpresentation($con);
    if ($r == null) {
      $r = new Tblpresentation();
      $r->setPrName('--- không tìm thấy ---');
      $r->setPrThumbnail(2);
    }
    return $r;
  }

  public function getTbldocument(PropelPDO $con = null) {
    $r = parent::getTbldocument($con);
    if ($r == null) {
      $r = new Tbldocument();
      $r->setDocName('--- không tìm thấy ---');
      $r->setDocThumbnail(2);
    }
    return $r;
  }

  public function getTbluser(PropelPDO $con = null) {
    $tbluser = parent::getTbluser($con);
    return $tbluser? $tbluser: new Tbluser();
  }

  public function getNextUntagId() {
    $c = new Criteria();
    $is_author = $this->getLinkObject()->getUser() == blogStore::$userId;
    if (!$is_author) $c->add(TblblogentryPeer::ENTRY_CATEGORY, $this->getEntryCategory());
    else {
      $c->add(TblblogentryPeer::ENTRY_USER, blogStore::$userId);
      $c->add(TblblogentryPeer::ENTRY_TYPE, $this->getEntryType());
    }
    $c->add(TblblogentryPeer::ENTRY_LESSON, $this->getEntryLesson());
    $c->add(TblblogentryPeer::ENTRY_STATUS, 2, Criteria::LESS_THAN);
    $c->add(TblblogentryPeer::ENTRY_MODIFYDATE, $this->getEntryModifyDate(), Criteria::LESS_THAN);
    $c->addDescendingOrderByColumn(TblblogentryPeer::ENTRY_MODIFYDATE);
    $tblblogentry = TblblogentryPeer::doSelectOne($c);
    return $tblblogentry? $tblblogentry->getEntryId(): 0;
  }

  public function getEntryTitle() {
    $v = parent::getEntryTitle();
    if ($v === null && $this->getEntryType() != 2) {
      $v = $this->getLinkObject()->getName();
      $this->setEntryTitle($v);
      $this->save();
    }
    return $v;
  }

  public function getEntryThumbnail() {
    $v = parent::getEntryThumbnail();
    if ($v === null && $this->getEntryType() != 2) {
      $tblpresentation = $this->getTblpresentation();
      $v = $tblpresentation->updateBlogCategoryEntryPath();
    }
    return $v;
  }
}
