<?php
/**
 * Class represents records from table files
 * "path" field may contain numeric id - from the uploads table
 * {autogenerated}
 * @property int $file_id PK
 * @property string $title
 * @property string|null $desc
 * @property string $path
 * @property string|null $filename
 * @property string|null $mime
 * @property int|null $size
 * @property string|null $display_type
 * @property datetime|null $dattm
 * @property int|null $hide
 * @property string $download_limit
 * @property string|null $no_access_url
 * {/autogenerated}
 */
class File extends ResourceAbstractFile
{
    public function getUrl()
    {
        return $this->getDi()->url(
            "content/f/id/" . (defined('AM_OBFUSCATE_FILE_IDS') ? $this->getDi()->security->obfuscate($this->file_id) : $this->file_id),
            false
        );
    }

    public function renderLink()
    {
        [$type, $subtype] = explode('/', $this->mime);
        $prefix = "am-resource-" . $this->getTable()->getAccessType();
        return preg_replace('/(class="[-_a-zA-Z]*?)(")/', "$1 $prefix-$type $prefix-$type-$subtype$2", parent::renderLink());
    }
}

/** @extends ResourceAbstractTable<File> */
class FileTable extends ResourceAbstractTable
{
    protected $_key = 'file_id';
    protected $_table = '?_file';
    protected $_recordClass = 'File';

    public function getAccessType()
    {
        return ResourceAccess::FILE;
    }

    public function getAccessTitle()
    {
        return ___('Files');
    }

    public function getPageId()
    {
        return 'files';
    }
}