<?php
namespace Upload\Src;

use Dever;

class Manage
{
    public function search_tag()
    {
        $name = Dever::input('term');
        $param['name'] = $name;

        $data = Dever::db('upload/tag')->search($param);

        if ($data) {
            return $data;
        } else {
            return array
            (
                0 => array('id' => -1, 'value' => '没有找到您搜索的数据', 'label' => '没有找到您搜索的数据'),
            );
        }
    }

    public function search_tag_by_ids($id)
    {
        $data = Dever::db('upload/tag')->search($id);

        return $data;
    }

    public function add_tag($id, $name, $data)
    {
        $tag = Dever::param('tag', $data);

        if ($tag) {
            $temp = explode(',', $tag);
            foreach ($temp as $k => $v) {
                $info = Dever::db('upload/tag')->one(array('name' => $v));
                if (!$info) {
                    Dever::db('upload/tag')->insert(array('name' => $v));
                }
            }
        }
    }

    public function file($id)
    {
        $info = Dever::db('upload/file')->one($id);

        $table = array();

        $html = '';
        $location = '';
        $config = Dever::db('upload/upload')->one($info['upload']);
        if ($config['yun'] && $config['save_type'] > 2) {
            $yun = Dever::db('upload/yun')->one($config['yun']);
            $link = $yun['host'] . $info['file'];
            $location = $yun['name'] . ':' . $info['file'];
            if ($config['save_type'] == 3) {
                $location = '<br/>' . '本地服务器:' . $info['file'];
            }
        } else {
            $link = Dever::upload('{uploadRes}' . $info['file']);
            $location = '本地服务器:' . $info['file'];

            if ($config['save_type'] == 2) {
                $yun = Dever::db('upload/yun')->one($config['yun']);
                $link = $yun['host'] . $info['file'];
                $location = '<br />' . $yun['name'] . ':' . $info['file'];
            }
        }
        
        if (strstr($info['file'], '.png') || strstr($info['file'], '.jpg') || strstr($info['file'], '.gif')) {
            $html = '<img src="'.$link.'" width="100" />';
        }

        $table['存储位置'] = $location;
        $table['文件名'] = $info['name'];
        $html .= Dever::table($table);
        return $html;
    }
}