<?php
/**
 * ZDOO API的附件资源类
 * 版本V1
 *
 * The file entry point of zdoo
 * Version 1
 */
class fileEntry extends entry
{
    /**
     * Download file by id. 
     * 
     * @param  int    $fileID 
     * @access public
     * @return void
     */
    public function get($fileID)
    {
        $control = $this->loadController('file', 'download');
        $control->download($fileID);

        $data = $this->getData();
        return $this->send(200, $data);
    }

    /**
     * Remove file by id. 
     * 
     * @param  int    $fileID 
     * @access public
     * @return void
     */
    public function delete($fileID)
    {
        if(!empty($this->param('uid')))
        {
            $control = $this->loadController('file', 'ajaxDelete');
            $control->ajaxDelete($fileID, $this->param('uid'));
        }
        else
        {
            $control = $this->loadController('file', 'delete');
            $control->delete($fileID);
        }

        $data = $this->getData();
        return $this->send(200, $data);
    }

    /**
     * Process a file.
     *
     * @param  object $file
     * @access public
     * @return object
     */
    public function process($file)
    {
        $creator = empty($file->createdBy) ? null : $this->dao->select('id, account, realname, dept, avatar')->from(TABLE_USER)->where('account')->eq($file->createdBy)->fetch();
        if(!empty($creator)) $creator->dept = empty($creator->dept) ? null : $this->dao->select('id, name')->from(TABLE_CATEGORY)->where('id')->eq($creator->dept)->fetch();

        $fields = 'id:int,title';
        $data = $this->format($file, $fields, true);
        $data->creator = $creator;

        return $data;
    }
}
