<?php
/**
 * JYmusic音乐管理系统
 *
 * @version     2.0
 * @author      战神~~巴蒂 <jyuucn@163.com>
 * @license     http://jyuu.cn/license
 * @copyright   2014 - 2017 JYmusic
 */
namespace addons\Upload;

use think\Url;
use app\services\Addons;

/**
 * 上传插件
 * @author by JYmusic <jyuucn@163.com>
 */
class Upload extends Addons
{
    public $info = [
        'name'        => 'Upload',
        'title'       => '上传插件',
        'description' => 'JYmusic2.0 上传插件',
        'status'      => 0,
        'author'      => 'JYmusic',
        'version'     => '0.1',
    ];

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        $this->existHook('file', 'Upload');
        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        return true;
    }

    /**
     * 实现的testHook钩子方法
     * @return \think\Response
     */
    public function file($param)
    {
        if ($param['wiget'] == 'Uplaod') {
            $config = $this->getConfig();
            
            if (isset($param['is_admin']) && (true === $param['is_admin'])) {
                if ($config['admin_drive'] == 'local') {
                    $param['uploadServer']  = Url::build('/apply/upload/local');
                } elseif($config['admin_drive'] == 'ftp') {
                    $param['uploadServer']  = Url::build('/apply/upload/ftp');
                } else {
                    $param['uploadServer']  = '';
                }
                $param['activeDrive'] = $config['admin_drive'];
            } else {
                if ($config['home_drive'] == 'local') {
                    $param['uploadServer']  = addons_url('upload/local');
                } elseif($config['home_drive'] == 'ftp') {
                    $param['uploadServer']  = addons_url('upload/ftp');
                }
                $param['activeDrive'] = $config['home_drive'];
            }

            if ($param['activeDrive'] == 'ftp') {
                $param['bindServer'] = intval($config['ftp_bind_server']);
            } else {
                $param['bindServer'] = 0;
            }

            $param['isBatch'] = isset($param['isBatch']) ?: false;  
            $this->assign('addons_config', $config);
            $this->assign('addons_param', $param);
            return $this->fetch('widget');
        } elseif ($param['wiget'] == 'Avatar') {
            if (isset($param['is_admin']) && true === $param['is_admin']) {
                Url::root('/index.php');
                $param['uploadServer']  = Url::build('/apply/upload/avatar');
            } else {
                $param['uploadServer']  = addons_url('upload/avatar');
            }
            $this->assign('addons_param', $param);
            return $this->fetch('avatar');
        }
    }
}
