<?php

// +-----------------------------------------------------------------------------------

// | Software: [ThinkPHP framework]

// | Version: V4.0

// | Site: http://www.czreg.com

// +-----------------------------------------------------------------------------------

// | Author: 飘叶 <cds425@163.com>

// | Copyright (c) 2012-2014, http://www.czreg.com All Rights Reserved.

// | time:2014.11.08

// | explain:下载版块管理类

// +-----------------------------------------------------------------------------------

namespace Admin\Controller;

use Think\Controller;

class SoftController extends AdminController {

    public function _initialize () {

        parent::_initialize();

        $this->leftmain=5;

    }

    //下载管理

    public function index(){

        $this->topmain=1;

    	$soft=M('soft');

    	$prefix=C('DB_PREFIX');

        if(I('get.action')=="del"){

            $delsoft=$soft->where(array('id'=>I('get.id')))->delete();

            if($delsoft!==false){

                $this->success('删除成功',U('index',array('p'=>I('get.p'))));

            }else{

                $this->error('删除失败');

            }

            exit;

        }

        $p=empty($_GET['p'])?'1':$_GET['p'];

        $this->list = $soft->field('title,readnum,downnum,name,'.$prefix.'soft.id')->join('__SOFT_CLS__ ON __SOFT_CLS__.id = __SOFT__.type','LEFT')->order($prefix.'soft.id desc')->page($p.',10')->select();

        $count=$soft->field('id')->count();

        $Page = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数

        $Page->rollPage=5;//页码每页显示数量

        $Page->lastSuffix = false;//尾页是否显示总页数

        $Page->setConfig('header','条记录');

        $Page->setConfig('prev','上一页');

        $Page->setConfig('next','下一页');

        $Page->setConfig('first','首页');

        $Page->setConfig('last','尾页');

        $Page->setConfig('theme','<div class="digg">%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% </div>');

        $show       = $Page->show();// 分页显示输出

        $this->assign('page',$show);// 赋值分页输出

        $this->p=$p;

        $this->display();

    }



    //下载增加

   public function add(){

        $this->topmain=4;

        if(IS_POST){

            $data            = I('post.');

            $data['timeline']= time();

            $data['content'] = I('post.content','',false);
            //如果get_magic_quotes_gpc()是打开的
            if(get_magic_quotes_gpc()){
                $data['content']=stripslashes($data['content']);
            }

            $savesoft=M('soft')->add($data);

             if($savesoft!==false){

                $this->success('新增成功',U('index'));

            }else{

                $this->error('新增失败');

            }

        }else{

            $this->list = M('soft_cls')->select();

           $this->show();

        }

    }



    //下载修改

   public function edit(){

        $this->topmain=2;

        $id=I('get.id');

        $p=I('get.p');

        if(IS_POST){

            $data            = I('post.');

            $data['content'] = I('post.content','',false);
            //如果get_magic_quotes_gpc()是打开的
            if(get_magic_quotes_gpc()){
                $data['content']=stripslashes($data['content']);
            }

            $savesoft=M('soft')->where('id='.$id)->save($data);

             if($savesoft!==false){

                $this->success('修改成功',U('index',array('p'=>$p)));

            }else{

                $this->error('修改失败');

            }

        }else{

            $this->soft = M('soft')->where('id='.$id)->find();

            $this->list = M('soft_cls')->select();

            $this->p= $p;

           $this->show();

        }

    }

  

           //类型管理

    public function cls(){

        $this->topmain=3;

        $softclsMonde=M('soft_cls');

        if(IS_POST){

            $data['name']=I('post.name');

            if(I('get.id')!=''){

                $savetitle="更新";

                $savecls=$softclsMonde->where(array('id'=>I('get.id')))->save($data);

            }else{

                $savetitle="新增";

                $savecls=$softclsMonde->add($data);

            }

            if($savecls!==false){

                if(I('get.id')!=''){

                    echo "<script language=javascript>

                        parent.showlog();

                        parent.$('#name".I('get.id')."').css('color','red');

                        parent.$('#name".I('get.id')."').text('".$data['name']."');

                        var index = parent.layer.getFrameIndex(window.name);

                        parent.layer.close(index);

                        </script>"; 

                    exit;

                }else{

                    $this->success($savetitle.'成功',U('cls'));

                }

            }else{

                $this->error($savetitle.'失败');

            }

        }else{

            //判断是否删除操作

            if(I('get.action')=="del"){

                $deladmin=$softclsMonde->where(array('id'=>I('get.id')))->delete();

                if($deladmin!==false){

                    $this->success('删除成功',U('cls'));

                }else{

                    $this->error('删除失败');

                }

            }else{

                if(I('get.do')=='edit'){

                    $this->admin=$softclsMonde->where('id='.I('get.id'))->find();

                    $this->display('clsedit');

                    exit;

                }

                $this->dodo=I('get.do');

                $list=$softclsMonde->select();

                $this->list=$list;

                $this->show();

            }

        }

    }

}