<?php

/**
 * 开发者相关
 *
 * @author Yang
 */
class DevelopAction extends PublicAction {

    //初始化检测
    public function _initialize() {
        parent::_initialize();
        if (!isset($_SESSION['user_id']) || empty($_SESSION['user_id'])) {
            //页面跳转
            echo "<script language='javascript' type='text/javascript'>window.location.href='" . __APP__ . "/Login/index?url=" . rawurlencode(__APP__ . '/Develop/') . "'</script>";
        }

    }

    public function index() {
        $Dev = M('developer');
		$Dev_db = $Dev->field('`id`')->where("uid='" . $_SESSION['user_id'] . "'")->find();
		if(!$Dev_db){
			$url = rawurlencode('info');
        	$this->assign('jumpUrl', $url);
        	$this->success('请先登记开发者信息！');
		}
		$map['upload_uid'] = $_SESSION['user_id'];
        $this->my_software_list($map, 'time');
		$softdb = M('software');


        //总发布
        $softcount = $softdb->where('upload_uid = '.$_SESSION['user_id'])->count("id");
        //审核通过
        $checkcount = $softdb->where('upload_uid = '.$_SESSION['user_id'].' AND status = 1')->count("`id`");
        //审核中
        $awaitcount = $softdb->where('upload_uid = '.$_SESSION['user_id'].' AND status = 0')->count("id");
        //下架
        $refusecount = $softdb->where('upload_uid = '.$_SESSION['user_id'].' AND status = -1')->count("id");

        //总发布
        $softcount = $checkcount+$awaitcount+$refusecount;

        $this->assign('softcount', $softcount);
	    $this->assign('checkcount', $checkcount);
	    $this->assign('awaitcount', $awaitcount);
	    $this->assign('refusecount', $refusecount);
	    $this->assign('title', '开发者');
        $this->display();
    }




	public function manage() {
        $Dev = M('developer');
		$Dev_db = $Dev->field('`id`')->where("uid='" . $_SESSION['user_id'] . "'")->find();
		if(!$Dev_db){
			$url = rawurlencode(__APP__ . 'info');
        	$this->assign('jumpUrl', $url);
        	$this->success('请先登记开发者信息！');
		}
        $map['upload_uid'] = $_SESSION['user_id'];
        $this->my_software_list($map, 'time');
		$this->news_soft_list();
		$softdb = M('software');
        $this->assign('softcount', $softcount);
	    $this->assign('checkcount', $checkcount);
	    $this->assign('awaitcount', $awaitcount);
	    $this->assign('refusecount', $refusecount);
	    $this->assign('title', '开发者');
        $this->display();
    }

	//下架软件
    public function forbidden_soft() {
        $map['id'] = $_GET['id'];
        $map['status'] = -1;

        $soft = M('software');

        if (!$soft->save($map)) {
            $this->ajaxReturn('', '', 0);
        } else {
            F('List/menu_list', NULL);
            // 清空缓存
            F('Roll/list', NULL);   //清空滚动显示缓存
            $this->ajaxReturn('', '', 1);
        }
    }

	//审核软件
    public function open_soft() {
        $map['id'] = $_GET['id'];
        $map['status'] = 0;

        $soft = M('software');

        if (!$soft->save($map)) {
            $this->ajaxReturn('', '', 0);
        } else {
            F('List/menu_list', NULL);
            F('Roll/list', NULL);   //清空滚动显示缓存
            $this->ajaxReturn('', '', 1);
        }
    }

    //添加软件
    public function app() {
        $developer = M('developer');
		$info_db = $developer->field('`type`, `name`, `url`, `email`, `phone`')->where("uid='" . $_SESSION['user_id'] . "'")->find();
		if(!$info_db){
			$url = rawurlencode(__APP__ . 'info');
            $this->assign('jumpUrl', $url);
            $this->success('请先登记开发者信息！');
		}
		$this->get_passageway(); //获取镜像列表
        $this->get_menu(); //获取类别
		$this->assign('info', $info_db);
        $this->assign('title', '添加软件');
        $this->display();
    }



	//上传软件ICON(仅支持PNG)
    public function upload_icon() {
        import("ORG.Net.UploadFile");
        $data = date('Ymd');
        $upload = new UploadFile();
        $upload->maxSize = 50000; //大小限制50K
        $upload->allowExts = array('png'); //仅支持PNG
        $upload->savePath = './Public/Upload/soft_icon/' . $data . '/';
        $upload->saveRule = 'time';

        if (!$upload->upload()) {
            $this->ajaxReturn('', $upload->getErrorMsg(), 0);
        } else {
            $info = $upload->getUploadFileInfo();
            $this->ajaxReturn('', $data . '/' . $info[0]['savename'], 1);
        }
    }

    //上传软件内容
    public function upload_soft_url() {
        $data = date('Ymd');
        $size = 31457280; //大小限制30M
        $ext = array();
        $path = './Public/Upload/soft/' . $data . '/';
        $name = "";
        $maxwidth = "";
        $maxheight = "";

        $info = $this->upload($size, $ext, $path, $name, $maxwidth, $maxheight, $rule = true);

        if ($info[0] == 0) { //上传失败返回错误信息
            $this->ajaxReturn('', $info[1], 0);
        } else {    //上传成功时返回文件信息
            $info[0]['savename'] = $data . '/' . $info[0]['savename'];
            $this->ajaxReturn('', $info[0], 1);
        }
    }

    //上传软件截图
    public function soft_picture() {
        $data = date('Ymd');
        $size = 600000; //大小限制150K
        $ext = array('jpg', 'gif', 'png', 'jpeg');
        $path = './Public/Upload/soft_img/' . $data . '/';
        $name = "img_";
        $maxwidth = 200;
        $maxheight = 200;

        $info = $this->upload($size, $ext, $path, $name, $maxwidth, $maxheight, $rule = true);

        if ($info[0] == 0) {
            $this->ajaxReturn('', $info[1], 0);
        } else {
            import("@.ORG.Image");
            //缩略图添加水印, Image::water('原文件名','水印图片地址')
            //Image::water($info[0]['savepath'] . $name . $info[0]['savename'], './Public/Hi/chrome.png');
            $this->ajaxReturn('', $data . '/' . $name . $info[0]['savename'], 1);
        }
    }


	//增加软件数据库保存
    public function upload_soft_save() {
        $soft = M('software');
		$soft_db = $soft->field('`id`')->where("name='" . $_POST['soft_name'] . "'")->find();
		if($soft_db){
			$this->ajaxReturn('', '发布失败，该软件已存在！', 0);
		}
		Vendor('qr.phpqrcode'); //引入二维码类库
        $map['name'] = $_POST['soft_name'];
        $map['keyword'] = $_POST['soft_keyword'];
        $map['mobile_name'] = $_POST['soft_mobile_name'];
        $map['charge'] = $_POST['soft_chrage'];
        $map['version'] = $_POST['version'];
        $map['language'] = $_POST['soft_language'];
        $map['explain'] = $_POST['soft_explain'];
        $map['icon'] = $_POST['soft_icon_name'];
        $map['url'] = $_POST['soft_url_value'];
        $map['md5_file'] = $_POST['soft_url_hash'];
        $map['time'] = time();
        $map['status'] = 0;
        $map['recommend'] = 0;
		$map['upload_uid'] = $_SESSION['user_id'];

        $arg['menu_id'] = $_POST['menu_list'];
        $arg['menu_child_id'] = $_POST['menu_child_list'];


        $map['size'] = byte_format($_POST['soft_url_size']); //字节格式化


        $this->ajaxReturn('', $_POST['soft_picture'], 0);
        $map['picture'] = '';   //获取截图信息，多个截图以,隔开
        $dh = '';
        if (isset($_POST['soft_picture'])) {
            $count = count($_POST['soft_picture']);
            for ($i = 0; $i < $count; $i++) {
                $map['picture'] .= $dh . $_POST['soft_picture'][$i];
                $dh = ',';
            }
        }
		//$this->ajaxReturn('', $map['picture'], 0);


        $map['passageway'] = '';
        $dh = '';
        if (isset($_POST['check'])) {
            $count = count($_POST['check']);
            for ($i = 0; $i < $count; $i++) {
                $map['passageway'] .= $dh . $_POST['check'][$i];
                $dh = ',';
            }
        }

        $soft_belong = M('soft_belong');
        $soft_version = M('soft_version');

        $time = date('Ymd');
        $map['qr_url'] = '/Public/Upload/qr/' . $time . '/';
        $id = $soft->add($map);

        if (!$id) {
            $this->ajaxReturn('', '', 0);
        } else {
            //增加软件版本列表
            $cond['soft_id'] = $id;
            $cond['soft_version'] = $map['version'];
            $cond['update_time'] = time();
            $soft_version->add($cond);
            F('List/menu_list', NULL); //清空列表缓存
            //绘制二维码保存
            if (!is_dir('./Public/Upload/qr/')) {
                mkdir('./Public/Upload/qr/');
            }
            if (!is_dir('./Public/Upload/qr/' . $time)) {
                mkdir('./Public/Upload/qr/' . $time);
            }
            QRcode::png($id, '.' . $map['qr_url'] . $id . '.png', $level = QR_ECLEVEL_L, $size = 3, $margin = 2);
            if (isset($_POST['assign_check']) && $_POST['assign_check'] == 'on') { //没有分配列表
                $this->ajaxReturn('', '', 1);
            } else {
                $arg['soft_id'] = $id;
                if (!$soft_belong->add($arg)) {
                    $this->ajaxReturn('', '上传软件成功，但分配失败。稍后请手动分配', 0);
                } else {
                    $this->ajaxReturn('', '', 1);
                }
            }
        }
    }

	//更新软件
    public function update_soft() {
        $map['id'] = $_GET['id'];

        $soft = M('software');

        $soft_msg = $soft->field('id, name, version, update_explain, icon, url')->where($map)->find();

        $this->assign('soft', $soft_msg);
        $this->display();
    }

    //软件更新临时保存
    public function update_soft_save() {
		$rsdb['id'] = $_GET['id'];
		$soft = M('software');
        $soft_msg = $soft->field()->where($rsdb)->find();
		$map['soft_id'] = $_GET['id'];
        $map['version'] = $_POST['version'];
		$map['status'] = 0;
        $map['url'] = $_POST['soft_url_value'];
        $map['update_explain'] = $_POST['soft_explain'];
        $map['md5_file'] = $_POST['soft_url_hash'];
        $map['time'] = time();
        $map['size'] = byte_format($_POST['soft_url_size']); //字节格式化
		$map['name'] = $soft_msg['name'];
		$map['mobile_name'] = $soft_msg['mobile_name'];
		$map['charge'] = $soft_msg['charge'];
		$map['language'] = $soft_msg['language'];
		$map['explain'] = $soft_msg['explain'];
		$map['keyword'] = $soft_msg['keyword'];
		$map['icon'] = $soft_msg['icon'];
		$map['picture'] = $soft_msg['picture'];
		$map['passageway'] = $soft_msg['passageway'];
		$map['recommend'] = $soft_msg['recommend'];
		$map['recommend_order'] = $soft_msg['recommend_order'];
		$map['jumpurl'] = $soft_msg['jumpurl'];
		$map['roll'] = $soft_msg['roll'];
		$map['roll_img'] = $soft_msg['roll_img'];
		$map['roll_order'] = $soft_msg['roll_order'];
		$map['apk_show'] = $soft_msg['apk_show'];
		$map['upload_uid'] = $soft_msg['upload_uid'];
		$map['qr_url'] = $soft_msg['qr_url'];

        $soft_temp = M('software_temp');
        $soft_version = M('soft_version');

        if (!$soft_temp->add($map)) {
			$this->ajaxReturn('', '', 0);
        } else {
            //增加软件版本信息
            $arg['soft_id'] = $_GET['id'];
            $arg['soft_version'] = $map['version'];
            $arg['update_time'] = time();
            $soft_version->add($arg);
            $this->ajaxReturn('', '', 1);
        }
    }

	//删除软件
    public function news_del_soft() {
        $map['id'] = $_GET['id'];
        $soft = M('software_temp');


        if (!$soft->where('id='.$map['id'])->delete()) {
            $this->ajaxReturn('', '', 0);
        } else {
            $this->ajaxReturn('', '', 1);
        }
    }


	//开发者信息
	public function info() {
        $developer = M('developer');
		$info_db = $developer->field('`type`, `name`, `url`, `email`, `phone`')->where("uid='" . $_SESSION['user_id'] . "'")->find();
		if($info_db['type'] == '1'){
			$selec1 = 'selected';
			$disabled = 'disabled="true"';
		}else if($info_db['type'] == '2'){
			$selec2 = 'selected';
			$disabled = 'disabled="true"';
		}else{
			$selec = 'selected';
		}
		$this->assign('title', '开发者信息');
        $this->assign('info', $info_db);
		$this->assign('selec', $selec);
		$this->assign('selec1', $selec1);
		$this->assign('selec2', $selec2);
		$this->assign('disabled', $disabled);
        $this->display();
    }



    //保存开发者信息
	public function info_edit() {
        $developer = M('developer');
		$rsdb = $developer->field('`id`,`type`')->where("uid='" . $_SESSION['user_id'] . "'")->find();
		$data['uid'] = $_SESSION['user_id'];
        $data['name'] = $_POST['name'];
		$data['url'] = $_POST['url'];
		$data['email'] = $_POST['email'];
		$data['phone'] = $_POST['phone'];
		$data['time'] = time();
		if($rsdb['id'] != ''){
			$data['type'] = $rsdb['type'];
			$data['id'] = $rsdb['id'];
			if (!$developer->save($data)) {
				$this->ajaxReturn('', '数据写入错误,请检查内容是否有改动！', 0);
			}else{
				$this->ajaxReturn('', '操作成功!', 1);
			}
		}else{
			$data['type'] = $_POST['type'];
			$ip = get_client_ip();
			$data['ip'] = $ip;
			if (!$developer->add($data)) {
				$this->ajaxReturn('', '数据写入错误！', 0);
			}else{
				$this->ajaxReturn('', '操作成功!', 1);
			}
		}

    }


}

?>
