<?php

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

class Tbldocument extends BaseTbldocument
{
  protected $oldDocCategory = null;
  protected $docObj = null;

  public function getObject() {
    if ($this->docObj == null) $this->docObj = resourceBase::getResourceObject($this, $this->getDocType(), 'docImage');
    return $this->docObj;
  }

  public function setDocCategory($v) {
    if ($this->oldDocCategory == null) $this->oldDocCategory = $this->getDocCategory();
    parent::setDocCategory($v);
  }

  /**
   * If doc_category is changed, modify Tbldocumentcategory table
   */
  public function save(PropelPDO $con = null)
  {
    if (!$this->isNew()) {
      if ($this->isColumnModified(TbldocumentPeer::DOC_CATEGORY)) {
        $this->getObject()->MoveDataFrom($this->oldDocCategory);
      }

      $cEntry = new Criteria();
      if ($this->isColumnModified(TbldocumentPeer::DOC_STATUS)) $cEntry->add(TblblogentryPeer::ENTRY_STATUS, $this->getDocStatus());
      if ($this->isColumnModified(TbldocumentPeer::DOC_DATE)) $cEntry->add(TblblogentryPeer::ENTRY_MODIFYDATE, $this->getDocDate());
      if (count($cEntry->getMap()) > 0) {
        $c = new Criteria();
        $c->add(TblblogentryPeer::ENTRY_TYPE, 1);
        $c->add(TblblogentryPeer::ENTRY_LINK, $this->getDocId());
        BasePeer::doUpdate($c, $cEntry, Propel::getConnection(TblblogentryPeer::DATABASE_NAME));
      }

      $cCatEntry = new Criteria();
      if ($this->isColumnModified(TbldocumentPeer::DOC_NAME)) $cCatEntry->add(TblblogcategoryentryPeer::CE_TITLE, $this->getDocName());
      if ($this->isColumnModified(TbldocumentPeer::DOC_STATUS)) $cCatEntry->add(TblblogcategoryentryPeer::CE_STATUS, $this->getDocStatus());
      if ($this->isColumnModified(TbldocumentPeer::DOC_DATE)) $cCatEntry->add(TblblogcategoryentryPeer::CE_MODIFYDATE, $this->getDocDate());
      if (count($cCatEntry->getMap()) > 0) {
        $c = new Criteria();
        $c->add(TblblogcategoryentryPeer::CE_TYPE, 1);
        $c->add(TblblogcategoryentryPeer::CE_LINK, $this->getDocId());
        BasePeer::doUpdate($c, $cCatEntry, Propel::getConnection(TblblogcategoryentryPeer::DATABASE_NAME));
      }
    }

    parent::save($con);
  }

  public function delete(PropelPDO $con = null)
  {
    // Delete all data files
    unlink($this->getObject()->getFullFileName());
    unlink($this->getObject()->getThumbFileName());
    unlink($this->getObject()->getPreviewFileName());

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

    parent::delete($con);
  }

  /**
   * Update ce_thumbnail to tblblogcategoryentry
   */
  public function updateBlogCategoryEntryPath($thumb = '') {
    if ($thumb === '') {
      $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, 1);
    $c->add(TblblogcategoryentryPeer::CE_LINK, $this->getDocId());
    BasePeer::doUpdate($c, $val, Propel::getConnection(TblblogcategoryentryPeer::DATABASE_NAME));
    return $thumb;
  }

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

  public function getDocFilesize() {
    if (($fileSize = parent::getDocFilesize())===null) {
      $fileName = $this->getObject()->getFullFileName(true);
      $fileSize = intval(@filesize($fileName));
      $this->setDocFilesize($fileSize);
      $this->save();
    }
    return $fileSize;
  }

  public function getDocWidth() {
    $width = parent::getDocWidth();
    if ($width == 0) {
      $this->getObject()->updateDocSize();
      return parent::getDocWidth();
    }
    return $width;
  }
}
