<?php

/**
 * Subclass for representing a row from the 'tblpresentation' table.
 *
 *
 * @package lib.model
 */

class Tblpresentation extends BaseTblpresentation
{
  protected $oldPrCategory = null;
  protected $presentObj = null;

  public function getObject() {
    if ($this->presentObj == null) $this->presentObj = resourceBase::getResourceObject($this, $this->getPrType(), 'presentPPT');
    return $this->presentObj;
  }

  public function setPrCategory($v) {
    if ($this->oldPrCategory === null) $this->oldPrCategory = $this->getPrCategory();
    parent::setPrCategory($v);
  }

  public function save(PropelPDO $con = null)
  {
    if (!$this->isNew()) {
      if ($this->isColumnModified(TblpresentationPeer::PR_CATEGORY) && $this->oldPrCategory !== null) {
        foreach (array('resources', 'previews', 'thumbnails') as $dir) {
          $basePath = sfConfig::get('sf_upload_dir') . '/' . $dir . '/';
          $oldPath = $basePath . $this->oldPrCategory . '/' . $this->getPrId();
          $newPath = $basePath . $this->getPrCategory() . '/' . $this->getPrId();
          myUtility::moveDirectory($oldPath, $newPath);
        }
      }

      $cEntry = new Criteria();
      if ($this->isColumnModified(TblpresentationPeer::PR_LESSON)) $cEntry->add(TblblogentryPeer::ENTRY_LESSON, $this->getPrLesson());
      if ($this->isColumnModified(TblpresentationPeer::PR_STATUS)) $cEntry->add(TblblogentryPeer::ENTRY_STATUS, $this->getPrStatus());
      if ($this->isColumnModified(TblpresentationPeer::PR_DATE)) $cEntry->add(TblblogentryPeer::ENTRY_MODIFYDATE, $this->getPrDate());
      if (count($cEntry->getMap()) > 0) {
        $c = new Criteria();
        $c->add(TblblogentryPeer::ENTRY_TYPE, 0, Criteria::LESS_EQUAL);
        $c->add(TblblogentryPeer::ENTRY_LINK, $this->getPrId());
        BasePeer::doUpdate($c, $cEntry, Propel::getConnection(TblblogentryPeer::DATABASE_NAME));
      }

      $cCatEntry = new Criteria();
      if ($this->isColumnModified(TblpresentationPeer::PR_NAME)) $cCatEntry->add(TblblogcategoryentryPeer::CE_TITLE, $this->getPrName());
      if ($this->isColumnModified(TblpresentationPeer::PR_STATUS)) $cCatEntry->add(TblblogcategoryentryPeer::CE_STATUS, $this->getPrStatus());
      if ($this->isColumnModified(TblpresentationPeer::PR_DATE)) $cCatEntry->add(TblblogcategoryentryPeer::CE_MODIFYDATE, $this->getPrDate());
      if (count($cCatEntry->getMap()) > 0) {
        $c = new Criteria();
        $c->add(TblblogcategoryentryPeer::CE_TYPE, 0, Criteria::LESS_EQUAL);
        $c->add(TblblogcategoryentryPeer::CE_LINK, $this->getPrId());
        BasePeer::doUpdate($c, $cCatEntry, Propel::getConnection(TblblogcategoryentryPeer::DATABASE_NAME));
      }
    }
    parent::save($con);
  }

  public function delete(PropelPDO $con = null) {
    // Delete all data files
    foreach (array('resources', 'previews', 'thumbnails') as $dir) {
      $basePath = sfConfig::get('sf_upload_dir') . '/' . $dir . '/';
      $dataPath = $basePath . $this->getPrCategory() . '/' . $this->getPrId();
      myUtility::cleanDirectory($dataPath);
    }

    // Delete all entry
    $c = new Criteria();
    $c->add(TblblogentryPeer::ENTRY_TYPE, 0, Criteria::LESS_EQUAL);
    $c->add(TblblogentryPeer::ENTRY_LINK, $this->getPrId());
    $tblblogentrys = TblblogentryPeer::doSelect($c);
    foreach ($tblblogentrys as $tblblogentry) $tblblogentry->delete();

    parent::delete($con);
  }

  /**
   * Update ce_thumbnail to tblblogcategoryentry
   */
  public function updateBlogCategoryEntryPath($thumb = '') {
    if ($this->getPrId() == null) return;
    if ($thumb === '') {
      sfLoader::loadHelpers(array('Tag', 'Asset'));
      $thumb = $this->getObject()->getThumbnail();
      if (preg_match('/thumbnails\/(.+?)"/', $thumb, $preg)) $thumb = $preg[1];
    }
    $val = new Criteria();
    $val->add(TblblogcategoryentryPeer::CE_THUMBNAIL, $thumb);

    $c = new Criteria();
    $c->add(TblblogcategoryentryPeer::CE_TYPE, 0, Criteria::LESS_EQUAL);
    $c->add(TblblogcategoryentryPeer::CE_LINK, $this->getPrId());
    BasePeer::doUpdate($c, $val, Propel::getConnection(TblblogcategoryentryPeer::DATABASE_NAME));

    $val = new Criteria();
    $val->add(TblblogentryPeer::ENTRY_THUMBNAIL, $thumb);

    $c = new Criteria();
    $c->add(TblblogentryPeer::ENTRY_TYPE, 0, Criteria::LESS_EQUAL);
    $c->add(TblblogentryPeer::ENTRY_LINK, $this->getPrId());
    BasePeer::doUpdate($c, $val, Propel::getConnection(TblblogentryPeer::DATABASE_NAME));
    return $thumb;
  }

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

  public function getPrPrice(PropelPDO $con = null) {
    return blogStore::$blog->getBlCommercial()? parent::getPrPrice($con): 0;
  }

  public function getUserLicense($userid) {
    return $this->getPrPrice() == 0 || $this->getPrUser() == $userid || TblprolicensePeer::retrieveByPk(1, $userid, 3, $this->getPrId());
  }

  public function createUserLicense($userid) {
    $tbllic = new Tblprolicense();
    $tbllic->setLiType(1);
    $tbllic->setLiCustomer($userid);
    $tbllic->setLiProduct(3);
    $tbllic->setLiVersion($this->getPrId());
    $tbllic->setLiLevel(1);
    $tbllic->setLiCreatedate(date('Y-m-d H:i:s'));
    $tbllic->save();
  }

  public function setPrBasedir($v) {
    // will be changed in future
    $this->setPrKeyword($v);
  }

  public function getPrBasedir() {
    $r = $this->getPrKeyword();
    return ($r !== null)? $r: '';
  }

  public function getLessonName() {
    $lesson = $this->getPrLesson();
    if ($lesson && $lesson > 0) return commonData::$lessons->getTitle($this->getPrCategory(), $lesson);
    else return $this->getPrName();
  }
}
