建立控制器

§   abstract

每一个应用必须有一个abstract类,用于系统公共数据处理。

文件:./apps/book/controller/abstract.php

abstract class book_controller_abstract extends controller
{
            protected $app, $json, $template, $view, $config, $setting, $system, $_userid, $_username, $_groupid, $_roleid;
 
    function __construct(& $app)
    {
        parent::__construct();
 
                        $this->app = $app;
                        $this->_userid = & $app->userid;
                        $this->_username = & $app->username;
                        $this->_groupid = & $app->groupid;
                        $this->_roleid = & $app->roleid;
 
                        $this->config = config::get('config');
                        $this->setting = setting::get($app->app);
                        $this->system = setting::get('system');
 
                        $this->json = & factory::json();
 
                        $array = array('_userid'=>$this->_userid, '_username'=>$this->_username, '_groupid'=>$this->_groupid, '_roleid'=>$this->_roleid);
 
                        if ($app->client === 'admin')
                        {
                                   $this->view = & factory::view($app->app);
                            $this->view->assign('CONFIG', & $this->config);
                                   $this->view->assign('SETTING', & $this->setting);
                                   $this->view->assign('SYSTEM', & $this->system);
                                   $this->view->assign($array);
                        }
 
                        $this->template = & factory::template($app->app);
                        $this->template->assign('CONFIG', & $this->config);
                        $this->template->assign('SETTING', & $this->setting);
                        $this->template->assign('SYSTEM', & $this->system);
                        $this->template->assign($array);
    }
 
    public function execute()
    {
            if ($this->action_exists($this->app->action))
            {
                        $response = call_user_func_array(array($this, $this->app->action), $this->app->args);
            }
            else
            {
                        $this->_action_not_defined($this->app->action);
            }
        return $response;
    }
 
    protected function _action_not_defined($action)
    {
            $this->showmessage("<font color='red'>$action</font>
动作不存在");
    }
}

§   前台控制器

前台控制器主要的功能有,分页显示留言页面,发布留言。

文件:./apps/book/controller/index.php

class controller_index extends book_controller_abstract
{
            private $book;
            private $pagesize = 15;
 
            function __construct(&$app)
            {
                        parent::__construct($app);
                        $this->book = loader::model('book');
            }
            function index()
            {
                        $this->template->display('book/index.html');
            }
 
            function page()
            {
                        $page = max((isset($_GET['page']) ? intval($_GET['page']) : 1), 1);
                        $size = isset($_GET['pagesize'])? intval($_GET['pagesize']): $this->pagesize;
                        $where = 'state > 0';
                        $fields = '*';
                        $order = 'addtime DESC';
                        $data = $this->book->ls($where, $fields, $order, $page, $size);
                        $total = $this->book->count($where);
                        $result = json_encode($data);
                        echo $result;
            }
 
            function add()
            {
                        if($this->setting['iscode'])
                        {
                                   import('helper.seccode');
                                   $seccode = new seccode();
                                   if(!$seccode->valid())
                                   {
                                               $result = array('state' => false, 'error' => '
验证码不正确');
                                               exit(json_encode($result));
                                   }
                        }
                        if ($this->setting['ischeck'])
                        {
                                   $_POST['state'] = 0;
                        }
                        else
                        {
                                   $_POST['state'] = 1;
                        }
                        if($id = $this->book->add($_POST))
                        {
                                   $data = $this->book->get($id);
                                   $result = array('state'=>true,'data'=>$data);
                        }
                        else {
                                   $result = array('state'=>false,'error'=>$this->book->error());
                        }
                        echo json_encode($result);
            }
}

§   后台控制器

后台控制器主要的功能有,分页显示留言列表,审核留言,查看详细留言,回复留言,删除留言。

文件:./apps/book/controller/admin/index.php

class controller_admin_index extends book_controller_abstract
{
            private $book;
            private $pagesize = 15;
 
            function __construct(&$app)
            {
                        parent::__construct($app);
                        $this->book = loader::model('admin/book');
            }
 
            function index()
            {
                        $this->view->display('index');
            }
 
            function page()
            {
                        $page = max((isset($_GET['page']) ? intval($_GET['page']) : 1), 1);
                        $size = isset($_GET['pagesize'])? intval($_GET['pagesize']): $this->pagesize;
                        $state = isset($_GET['state']) ? trim($_GET['state']) : 'all';
                        $where = null;
                        if($state != 'all')
                        {
                                   $where = 'state = ' .intval($state);
                        }
                        $fields = '*';
                        $order = isset($_GET['orderby']) ? str_replace('|',' ',$_GET['orderby']) : 'addtime DESC';
                        $data = $this->book->ls($where, $fields, $order, $page, $size);
                        $total = $this->book->count($where);
                        $result = array('total'=>$total, 'data'=>$data);
                        $result = json_encode($result);
                        echo $result;
            }
 
            function view(){
                        $id = intval($_GET['id']);
                        if(!$id){
                                   $this->showmessage('
指定的ID错误');
                        }
                        $data = $this->book->get($id);
                        if(!$data){
                                   $this->showmessage('
指定的ID错误');
                        }
                        $this->view->assign($data);
                        $this->view->display('view');
            }
 
            function reply()
            {
                        $id = intval($_POST['id']);
                        if(!$id){
                                   $result = array('state'=>false,'error'=>'
指定的ID错误');
                                   exit(json_encode($result));
                        }
                        $reply = $_POST['reply'];
                        $replytime = time();
                        $data = array('reply'=>$reply,'replytime'=>$replytime,'state'=>2);
                        if($this->book->reply($id, $data))
                        {
                                   $result = array('state'=>true,'info'=>'
回复成功');
                                   echo json_encode($result);
                        }
                        else
                        {
                                   $result = array('state'=>false,'error'=>$this->book->error());
                                   echo json_encode($result);
                        }
            }
 
            function check()
            {
                        $num = 0;
                        $id = isset($_GET['id']) ? $_GET['id'] : 0;
                        if(!$id)
                        {
                                   $id = isset($_POST['id']) ? $_POST['id'] : 0;
                        }
                        $num = $this->book->check($id);
                        $result = array('state'=>true,'info'=>'
成功审核' .$num .'');
                        echo json_encode($result);
            }
 
            function delete()
            {
                        $num = 0;
                        $id = isset($_GET['id']) ? $_GET['id'] : 0;
                        if(!$id)
                        {
                                   $id = isset($_POST['id']) ? $_POST['id'] : 0;
                        }
                        $num = $this->book->delete($id);
                        $result = array('state'=>true,'info'=>'
成功删除' .$num .'');
                        echo json_encode($result);
            }
 
}



Copyright ©2009 - 2014 CmsTop.Com.All rights reserved.
思拓合众