<?php
/**
 * Class represents records from table softsale_file_upload
 * {autogenerated}
 * @property int $file_upload_id 
 * @property int $file_id 
 * @property string $version 
 * @property string $desc 
 * @property string $details
 * @property string $path 
 * @property string $mime 
 * @property int $size 
 * @property bool $hide
 * @property string $date
 * @see Am_Table
 */
class SoftsaleFileUpload extends Am_Record 
{
    protected $_storageItem;
    
    /** @return Am_Storage_File */
    protected function getStorageFile()
    {
        if (!$this->_storageItem)
            $this->_storageItem = $this->getDi()->plugins_storage->getFile($this->path);
        return $this->_storageItem;
    }
    
    function getDisplayFilename()
    {
        return $this->isLocal() ?
            (($upload = $this->getUpload()) ? $upload->name : basename($this->getFullPath())) :
            basename($this->path);
    }

    /** @return Upload|null */
    function getUpload()
    {
        if ($this->_upload && $this->_upload->upload_id == $this->path)
            return $this->_upload;
        return ($this->path > 0) ? $this->_upload = $this->getDi()->uploadTable->load($this->path) : null;
    }

    function getProtectedUrl($exptime)
    {
        return $this->getStorageFile()->getUrl($exptime);
    }
    
    function isLocal()
    {
        return (bool)$this->getStorageFile()->getLocalPath();
    }

    function getFullPath()
    {
        return $this->getStorageFile()->getLocalPath();
    }
    
    function isExists()
    {
        return file_exists($this->getFullPath());
    }
    
    function getMime()
    {
        return $this->mime ? $this->mime : 'application/octet-stream';
    }
    
    function getSize()
    {
        return $this->isLocal() ? filesize($this->getFullPath()) : @$this->size;
    }
    
    function getName()
    {
        $upload = $this->getUpload();
        return $upload ? $upload->getName() : $this->title;
    }
}

class SoftsaleFileUploadTable extends Am_Table 
{
    protected $_key = 'file_upload_id';
    protected $_table = '?_softsale_file_upload';
}
