<?php

/**
 * Subclass for representing a row from the 'tblblogcategory' table.
 *
 *
 *
 * @package lib.model
 */
class Tblblogcategory extends BaseTblblogcategory
{
  public function saveWithUpdateCatPath(PropelPDO $con = null, Tblblogcategory $parent = null) {
    if ($this->getCatParent() != -1) {
      if ($parent == null) $parent = TblblogcategoryPeer::retrieveByPK($this->getCatParent());
      $this->setCatPath($parent->getCatPath() .','. $this->getCatId());
    } else {
      $this->setCatPath($this->getCatId());
    }
    parent::save($con);

    // Recursive update CatPath for sub categories
    $c = new Criteria();
    $c->add(TblblogcategoryPeer::CAT_BLOG, $this->getCatBlog());
    $c->add(TblblogcategoryPeer::CAT_PARENT, $this->getCatId());
    $children = TblblogcategoryPeer::doSelect($c);
    foreach ($children as $child) $child->saveWithUpdateCatPath($con, $this);
  }

  public function save(PropelPDO $con = null) {
    $create = $this->isNew();
    parent::save($con);
    if ($create && !$this->getCatPath()) $this->saveWithUpdateCatPath($con);
  }

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

    $c = new Criteria();
    $c->add(TblblogcategoryentryPeer::CE_CATEGORY, $catId);
    TblblogcategoryentryPeer::doDelete($c);
  }

  public function getDependCategories() {
    // Get root blogs
    $c = new Criteria();
    $c->add(TblblogPeer::BL_TYPE, -1);
    $tblblogs = TblblogPeer::doSelect($c);
    $parentBlogIds = array();
    foreach ($tblblogs as $tblblog) $parentBlogIds[] = $tblblog->getBlId();

    // Get parent blogs
    $c = new Criteria();
    $c->add(TblblogdependsPeer::DP_BLOG, $this->getCatBlog());
    $tblblogdepends = TblblogdependsPeer::doSelect($c);
    foreach ($tblblogdepends as $tblblogdepend) $parentBlogIds[] = $tblblogdepend->getDpParent();

    $tblblogcategorys = array();
    foreach (array_unique($parentBlogIds) as $blogId) {
      $c = new Criteria();
      $c->add(TblblogcategoryPeer::CAT_BLOG, $blogId);
      $c->add(TblblogcategoryPeer::CAT_TYPE, $this->getCatType());
      $c->add(TblblogcategoryPeer::CAT_BASE, $this->getCatBase());
      $c->add(TblblogcategoryPeer::CAT_PUBLIC, -1, Criteria::NOT_EQUAL);
      $category = TblblogcategoryPeer::doSelectOne($c);
      if ($category) $tblblogcategorys[] = $category;
    }
    return $tblblogcategorys;
  }

  public function addBlogEntry($source_id, $source_status, $source_lesson, $source_user) {
    // To avoid 1 blog has many entry that link to the same resource
    $c = new Criteria();
    $c->add(TblblogentryPeer::ENTRY_LINK, $source_id);
    $c->add(TblblogentryPeer::ENTRY_TYPE, $this->getCatType());
    $c->add(TblblogentryPeer::ENTRY_BLOG, $this->getCatBlog());
    $c->add(TblblogentryPeer::ENTRY_STATUS, 2, Criteria::LESS_THAN);
    if (TblblogentryPeer::doCount($c) > 0) return null;

    $tblblogentry = new Tblblogentry();
    $tblblogentry->setEntryModifydate(date('Y-m-d H:i:s'));
    $tblblogentry->setEntryCreatedate(date('Y-m-d H:i:s'));
    $tblblogentry->setEntryUser($source_user);
    $tblblogentry->setEntryLink($source_id);
    $tblblogentry->setEntryStatus($source_status);
    $tblblogentry->setEntryLesson($source_lesson);
    $tblblogentry->setEntryCategory($this->getCatId());
    $tblblogentry->setEntryBlog($this->getCatBlog());
    $tblblogentry->setEntryType($this->getCatType());
    $tblblogentry->save();

    return $tblblogentry;
  }
}