[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/cms/src/model/ -> lmbCmsFileObject.class.php (source)

   1  <?php
   2  /*

   3   * Limb PHP Framework

   4   *

   5   * @link http://limb-project.com

   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)

   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html

   8   */
   9  lmb_require('limb/active_record/src/lmbActiveRecord.class.php');
  10  lmb_require('limb/dbal/src/criteria/lmbSQLFieldCriteria.class.php');
  11  
  12  /**

  13   * class lmbCmsFileObject.

  14   *

  15   * @package cms

  16   * @version $Id: lmbCmsFileObject.class.php 6010 2007-06-21 09:46:14Z pachanga $

  17   */
  18  class lmbCmsFileObject extends lmbActiveRecord
  19  {
  20    protected $_has_one = array('node' => array('field' => 'node_id',
  21                                                'class' => 'lmbCmsNode',
  22                                                'can_be_null' => true));
  23  
  24    protected $_db_table_name = 'file_object';
  25  
  26    function _onBeforeSave()
  27    {
  28      $this->_ensureUid();
  29  
  30      parent :: _onBeforeSave();
  31    }
  32  
  33    function loadFile($file_path)
  34    {
  35      $this->_ensureUid();
  36  
  37      $this->_store($file_path);
  38      $this->setSize(filesize($file_path));
  39    }
  40  
  41    function _ensureUid()
  42    {
  43      if(!$this->getUid())
  44        $this->generateAndSetUid();
  45    }
  46  
  47    static function findByUid($class_name, $uid, $conn = null)
  48    {
  49      return lmbActiveRecord :: findFirst($class_name,
  50                                          array('criteria' => new lmbSQLFieldCriteria('UID', $uid)),
  51                                          $conn);
  52    }
  53  
  54    static function findForParentNode($parent, $conn = null)
  55    {
  56      $sql = 'SELECT file_object.* '.
  57             ' FROM file_object LEFT JOIN node ON node.id = file_object.node_id '.
  58             ' WHERE node.parent_id = '. $parent->id;
  59  
  60      $stmt = $conn->newStatement($sql);
  61      return lmbActiveRecord :: decorateRecordSet($stmt->getRecordSet(), 'FileObject', $conn);
  62    }
  63  
  64    function generateUid()
  65    {
  66      return md5(mt_rand());
  67    }
  68  
  69    function generateAndSetUid()
  70    {
  71      $uid = $this->generateUid();
  72      $this->setUid($uid);
  73      return $uid;
  74    }
  75  
  76    function getFilePath()
  77    {
  78      return FILE_REPOSITORY_DIR . '/'. $this->getUid() . '.media';
  79    }
  80  
  81    function _store($disk_file_path)
  82    {
  83      if(!file_exists($disk_file_path))
  84        throw new lmbFileNotFoundException('file not found', $disk_file_path);
  85  
  86      lmbFs :: mkdir(FILE_REPOSITORY_DIR);
  87  
  88      $media_file = $this->getFilePath();
  89  
  90      if (!copy($disk_file_path, $media_file))
  91      {
  92        throw new lmbFsException('copy failed',
  93          array(
  94            'dst' => $media_file,
  95            'src' => $disk_file_path
  96            )
  97        );
  98      }
  99    }
 100  
 101    function destroy()
 102    {
 103      $file_path = $this->getFilePath();
 104      if(file_exists($file_path))
 105        unlink($file_path);
 106  
 107      parent :: destroy();
 108    }
 109  
 110    function __clone()
 111    {
 112      parent :: __clone();
 113  
 114      $file_path = $this->getFilePath();
 115  
 116      if(file_exists($file_path))
 117      {
 118        $this->setUid('');
 119        $this->_store($file_path);
 120      }
 121    }
 122  
 123    function getShowUrl()
 124    {
 125      return '/file_object/show/' . $this->getUid();
 126    }
 127  }
 128  ?>


Generated: Fri Aug 29 04:49:26 2008 Cross-referenced by PHPXref 0.7