<?php
namespace Admin\Controller;
use Think\Controller;

class CategoryController extends BasicController
{
    /*
     * 网站信息
     */
    public function setting()
    {
        $model = M('config');
        if (IS_POST) {
            $data = I('post.');

            foreach ($data as $k => $v) {
                $model->where(array('name' => $k))->setField('value', $v);
            }
            echo 'ok';
            die;
        }
        $info = array();
        $info['logo'] = $model->where(array('name' => 'logo'))->find();
        $info['copyright'] = $model->where(array('name' => 'copyright'))->find();
        $info['color'] = $model->where(array('name' => 'color'))->find();
        $this->assign('title', '网站信息');
        $this->assign('info', $info);
        $this->display();
    }

    /*
     * 上传图片
     */
    public function uploadImage()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Upload/'; // 设置附件上传根目录
        $upload->savePath = 'image/'; // 设置附件上传（子）目录
        $dir = '/Upload/image';
        if (!file_exists($dir))//文件夹不存在，先生成文件夹
        {
            mkdir($dir);
        }
        // 上传文件
        $info = $upload->upload();
        $path = "/Upload/" . $info['thumb']['savepath'] . $info['thumb']['savename'];
        if ($info) {
            echo $path;

        } else {
            echo "-1";//上传失败
        }
    }

    /*
     * 排序
     */
    public function sort()
    {
        $data = I('POST.');
        if (!empty($data)) {
            $db = $data['db'];
            $model = M("$db");
            foreach ($data as $k => $v) {
                $model->where(array('id' => $k))->save(array('sort' => $v));
            }
        }
        echo 'ok';
    }


}