<?php

class upload_control
{
    private $store;
    private $store_grade;

    public function __construct()
    {

    }
        
    public function set($key,$value) {
        $this->$key = $value;
    }

    public function set_store($storeid)
    {
        $this->store = $storeid;
    }

    /**
     * 读取
     */
    public function get($key){
        return $this->$key;
    }

    const POST_NAME = 'goods_image';

    private function fill_files($filename)
    {
        if(file_exists(fetch_config::tmp_file)) {
              unlink(fetch_config::tmp_file);
        }
        copy($filename,fetch_config::tmp_file);
        if(empty($_FILES['goods_image'])) {
            $_FILES['goods_image'] = array();
        }

        $post_info = &$_FILES['goods_image'];
        $path_info = pathinfo($filename);
        $post_info['path'] = $filename;
        $post_info['name'] = $path_info['basename'];
        $post_info['type'] = 'image/'.$path_info['extension'];
        $post_info['error'] = 0;
        $post_info['size'] = filesize($filename);
        $post_info['tmp_name'] = fetch_config::tmp_file;
    }

    public function upload_img($file)
    {
        // 判断图片数量是否超限
        $model_album = Model('album');

        // 上传图片
        $upload = new upload_file();
        $upload->set('default_dir', ATTACH_GOODS . DS . $this->store . DS . $upload->getSysSetPath());
        $upload->set('max_size', C('image_max_filesize'));

        $upload->set('thumb_width', GOODS_IMAGES_WIDTH);
        $upload->set('thumb_height', GOODS_IMAGES_HEIGHT);
        $upload->set('thumb_ext', GOODS_IMAGES_EXT);
        $upload->set('fprefix', $this->store);
        $upload->set('allow_type', array('gif', 'jpg', 'jpeg', 'png'));

        $this->fill_files($file);

        $result = $upload->upfile(upload_control::POST_NAME);
        if (!$result) {
            if (strtoupper(CHARSET) == 'GBK') {
                $upload->error = Language::getUTF8($upload->error);
            }
            $output = array();
            $output['error'] = $upload->error;
            $output = json_encode($output);
            exit($output);
        }

        $img_path = $upload->getSysSetPath() . $upload->file_name;

        // 取得图像大小
        list($width, $height, $type, $attr) = getimagesize(BASE_UPLOAD_PATH . '/' . ATTACH_GOODS . '/' . $this->store . DS . $img_path);

        // 存入相册
        $image = explode('.', $_FILES[upload_control::POST_NAME]["name"]);
        $insert_array = array();
        $insert_array['apic_name'] = $image['0'];
        $insert_array['apic_tag'] = '';
        $insert_array['aclass_id'] = $this->store;//$class_info['aclass_id'];
        $insert_array['apic_cover'] = $img_path;
        $insert_array['apic_size'] = intval($_FILES[upload_control::POST_NAME]['size']);
        $insert_array['apic_spec'] = $width . 'x' . $height;
        $insert_array['upload_time'] = time();
        $insert_array['store_id'] = $this->store;
        $model_album->addPic($insert_array);

        return $upload->file_name;
    }
}
