<?php

/**
 * Subclass for representing a row from the 'tblblog' table.
 *
 *
 *
 * @package lib.model
 */
class Tblblog extends BaseTblblog
{
  private $blogObj = null;

  public function getObject() {
    if ($this->blogObj == null) $this->blogObj = blogBase::getBlogObject($this);
    return $this->blogObj;
  }

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

    //delete category
    $c = new Criteria();
    $c->add(TblblogcategoryPeer::CAT_BLOG, $blogId);
    TblblogcategoryPeer::doDelete($c);

    //delete entry
    $c = new Criteria();
    $c->add(TblblogentryPeer::ENTRY_BLOG, $blogId);
    TblblogentryPeer::doDelete($c);

    //delete tblbloguser
    $c = new Criteria();
    $c->add(TblbloguserPeer::US_BLOG, $blogId);
    TblbloguserPeer::doDelete($c);

    //delete tblblogpoll
    $c = new Criteria();
    $c->add(TblblogpollPeer::POLL_BLOG, $blogId);
    TblblogpollPeer::doDelete($c);

    //delete all relation in tblblogdepends
    $c = new Criteria();
    $criterion = $c->getNewCriterion(TblblogdependsPeer::DP_PARENT, $blogId);
    $criterion->addOr($c->getNewCriterion(TblblogdependsPeer::DP_BLOG, $blogId));
    $c->add($criterion);
    TblblogdependsPeer::doDelete($c);

    //delete all resource files on server
    myUtility::cleanDirectory(blogStore::getBlogFolder(true, $blogId));
  }

  /**
   * Return root url of blog
   *
   * @return unknown
   */
  public function getRootUrl($postfix = '/', $prefix = 'http://') {
    $url = $this->getBlHost();
    if (!$url) $url = sfConfig::get('app_base_host').'/'.$this->getBlShortname();
    return $prefix. $url. $postfix;
  }

  public function save(PropelPDO $con = null) {
    if ($this->isColumnModified(TblblogPeer::BL_USER) || $this->isColumnModified(TblblogPeer::BL_HOST)) {
      // clear home page in user record
      $user_id = $this->getBlUser();
      if ($user_id) {
        $tbluser = TbluserPeer::retrieveByPK($user_id);
        if ($tbluser) {
          $tbluser->setUsHomepage(null);
          $tbluser->save();
        }
      }
    }

    parent::save($con);
  }

  public function getBlCommercial() {
    $etime = $this->getBlCommexpire();
    return $etime && strtotime($etime) > time();
  }
}
