<?php

class QrcodeAction extends CommonAction{
    function _initialize() {
        import('@.ORG.Util.Cookie');
        //检查认证识别号
        if (!$_SESSION [C('USER_AUTH_KEY')]) {
            //跳转到认证网关
            redirect(PHP_FILE . C('USER_AUTH_GATEWAY'));
        }
    }
    //过滤查询字段
    function _filter(&$map){
        if(isset($_GET['catid'])){
            $map['catid']=  array('eq',$_GET['catid']);
        }
        if(isset($_POST['catid'])){
            $map['catid']=  array('eq',$_POST['catid']);
        }
        
        $map['title'] = array('like',"%".$_POST['name']."%");
    }
    public function _before_index() {
        if(isset($_GET['catid'])){
            $this->catid=$_GET['catid'];
        }
        if(isset($_POST['catid'])){
            $this->catid=$_POST['catid'];
        }

    }
    public function _before_add() {
        if(isset($_GET['id'])){
            $this->catid=$_GET['id'];
        }
        
    }
    public function insert() {
        
        $this->_uploadimg();
        
        //生成二维码
        import("@.ORG.qrcode_img");
        
        if(isset($_POST["title"])){
            $data=$_POST["title"];
        }
        if(isset($_POST["fcolor"])){
            $f=$_POST["fcolor"];
        }else{
            $f='#000000';
        }
        if(isset($_POST["bcolor"])){
            $b=$_POST["bcolor"];
        }else{
            $b='#FFFFFF';
        }
        if(isset($_POST["ptcolor"])){
            $pt=$_POST["ptcolor"];
        }else{
            $pt='#000000';
        }
        if(isset($_POST["inptcolor"])){
            $inpt=$_POST["inptcolor"];
        }else{
            $inpt='#000000';
        }
        //样式状态 液态 直角 圆圈
        if(isset($_POST["style"])){
            $s=intval($_POST["style"]);
        }else{
            $s=1;
        }
        //纠错等级
        if(isset($_POST["level"])){
            $level=$_POST["level"];
        }else{
            $level="L";
        }
        if(isset($_POST['logopath'])&&!empty($_POST['logopath'])){
            $filelogo= '../Uploads/'.$_POST['logopath'];
        }  else {
            $filelogo='';
        }
        
        if(isset($_POST['bgpath'])&&!empty($_POST['bgpath'])){
            $filebg= '../Uploads/'.$_POST['bgpath'];
        }  else {
            $filebg='';
        }

        //设置附件上传目录
        $y = date('Y',time());
        $m = date('m',time());
        $d = date('d',time());
        
        $dir='../Uploads/qrcode';
        if (!is_dir($dir)) {
            mkdir($dir, 0777);
        }
        $dir.='/'.$y;
        if (!is_dir($dir)) {
            mkdir($dir, 0777);
        }
        $dir.='/'.$m;
        if (!is_dir($dir)) {
            mkdir($dir, 0777);
        }
        $dir.='/'.$d;
        if (!is_dir($dir)) {
            mkdir($dir, 0777);
        }
        $dir.='/';
        $filename=uniqid().'.png';
        $filepath=$dir.$filename;
        
        $_POST['qrcodepath']='/qrcode/'.$y.'/'.$m.'/'.$d.'/'.$filename;

        $z=new Qrcode_image;

        //$z->set_qrcode_version($_GET['id']);           # set qrcode version 1
        $z->set_qrcode_error_correct($level);   # set ecc level H
        //$z->set_module_size(4);              # set module size 3pixel
        //$z->set_quietzone(5);                # set quietzone width 5 modules

        $z->qrcode_image_out($data,$filepath,380,$filelogo,$filebg,$pt,$inpt,$f,$b,'#000000',$s);
        
        $name = $this->getActionName();
        $model = D($name);
        if (false === $model->create()) {
            $this->error($model->getError());
        }
        //保存当前数据对象
        $list = $model->add();
        if ($list !== false) { //保存成功
            $this->assign('jumpUrl', Cookie::get('_currentUrl_'));
            $this->success('新增成功!');
        } else {
            //失败提示
            $this->error('新增失败!');
        }
    }
    
    public function update() {
        
        $this->_uploadimg();
        
        //生成二维码
        import("@.ORG.qrcode_img");
        
        if(isset($_POST["title"])){
            $data=$_POST["title"];
        }
        if(isset($_POST["fcolor"])){
            $f=$_POST["fcolor"];
        }else{
            $f='#000000';
        }
        if(isset($_POST["bcolor"])){
            $b=$_POST["bcolor"];
        }else{
            $b='#FFFFFF';
        }
        if(isset($_POST["ptcolor"])){
            $pt=$_POST["ptcolor"];
        }else{
            $pt='#000000';
        }
        if(isset($_POST["inptcolor"])){
            $inpt=$_POST["inptcolor"];
        }else{
            $inpt='#000000';
        }
        //样式状态 液态 直角 圆圈
        if(isset($_POST["style"])){
            $s=intval($_POST["style"]);
        }else{
            $s=1;
        }
        //纠错等级
        if(isset($_POST["level"])){
            $level=$_POST["level"];
        }else{
            $level="L";
        }
        if(isset($_POST['logopath'])&&!empty($_POST['logopath'])){
            $filelogo= '../Uploads/'.$_POST['logopath'];
        }  else {
            $filelogo='';
        }
        
        if(isset($_POST['bgpath'])&&!empty($_POST['bgpath'])){
            $filebg= '../Uploads/'.$_POST['bgpath'];
        }  else {
            $filebg='';
        }

        //设置附件上传目录
        $y = date('Y',time());
        $m = date('m',time());
        $d = date('d',time());
        
        $dir='../Uploads/qrcode';
        if (!is_dir($dir)) {
            mkdir($dir, 0777);
        }
        $dir.='/'.$y;
        if (!is_dir($dir)) {
            mkdir($dir, 0777);
        }
        $dir.='/'.$m;
        if (!is_dir($dir)) {
            mkdir($dir, 0777);
        }
        $dir.='/'.$d;
        if (!is_dir($dir)) {
            mkdir($dir, 0777);
        }
        $dir.='/';
        $filename=uniqid().'.png';
        $filepath=$dir.$filename;
        
        $_POST['qrcodepath']='/qrcode/'.$y.'/'.$m.'/'.$d.'/'.$filename;

        $z=new Qrcode_image;

        //$z->set_qrcode_version($_GET['id']);           # set qrcode version 1
        $z->set_qrcode_error_correct($level);   # set ecc level H
        //$z->set_module_size(4);              # set module size 3pixel
        //$z->set_quietzone(5);                # set quietzone width 5 modules

        $z->qrcode_image_out($data,$filepath,380,$filelogo,$filebg,$pt,$inpt,$f,$b,'#000000',$s);
        
        
        $name = $this->getActionName();
        $model = D($name);
        if (false === $model->create()) {
            $this->error($model->getError());
        }
        // 更新数据
        $list = $model->save();
        if (false !== $list) {
            //成功提示
            $this->assign('jumpUrl', Cookie::get('_currentUrl_'));
            $this->success('编辑成功!');
        } else {
            //错误提示
            $this->error('编辑失败!');
        }
    }
    
}

?>
