TrainController.class.php 3.1 KB
<?php
/**
 * Created by PhpStorm.
 * User: 29925
 * Date: 2018/4/2
 * Time: 20:13
 */

namespace Portal\Controller;

use Common\Controller\HomebaseController;

class TrainController extends HomebaseController {

    protected $train_model;
    protected $train_apply_model;

    function _initialize() {
        parent::_initialize();
        $this->train_model = D("Common/Train");
        $this->train_apply_model = D("Common/TrainApply");
    }

    // 工作坊列表
    public function work(){
        $this->_lists(array('sort'=>1,'is_del'=>0));
        $this->display();
    }

    // 课程培训列表
    public function lesson(){
        $this->_lists(array('sort'=>2,'is_del'=>0));
        $this->display();
    }

    // 文化之旅列表
    public function cultural(){
        $this->_lists(array('sort'=>3,'is_del'=>0));
        $this->display();
    }

    /**
     * 体验培训列表处理方法,根据不同条件显示不同的列表
     * @param array $where 查询条件
     */
    private function _lists($where=array()){
        $start_time = I('request.start_time');
        if(!empty($start_time)){
            $where['ctime']=array(
                array('EGT',$start_time)
            );
        }

        $end_time = I('request.end_time');
        if(!empty($end_time)){
            if(empty($where['ctime'])){
                $where['ctime']=array();
            }
            array_push($where['ctime'], array('ELT',$end_time));
        }

        $keyword = I('request.keyword');
        if(!empty($keyword)){
            $where['name']=array('like',"%$keyword%");
        }

        $this->train_model
            ->where($where);

        $count = $this->train_model->count();

        $page = $this->page($count, 20);

        $posts = $this->train_model
            ->where($where)
            ->limit($page->firstRow , $page->listRows)
            ->order("ctime DESC")
            ->select();

        $this->assign("page", $page->show('Admin'));
        $this->assign("formget",array_merge($_GET,$_POST));
        $this->assign("posts",$posts);
    }

    /**
     * 体验培训提交
     * @param post 提交的申请数据
     * @param user_id 用户ID
     */
    public function submit() {
        if(IS_AJAX) {
            $post =I('post.');
            $user_id = $post['user_id'] = 1;
//            $user_id = $post['user_id'] = sp_get_current_userid();
//            if(!$user_id) {
//                $this->ajaxReturn(array('status'=>false,'msg'=>'用户未登录'));
//            }
            $post['ctime'] = $post['utime'] = time();
            if(!$this->train_apply_model->create($post)) {
                $this->ajaxReturn(array('status'=>false,'msg'=>$this->train_apply_model->getError(),'data'=>$post));
            }
            $train_id = $this->train_apply_model->add($post);
            if(!$train_id) {
                $this->ajaxReturn(array('status'=>false,'msg'=>'提交失败'));
            }
            $this->ajaxReturn(array('status'=>true,'msg'=>'提交成功','data'=>$train_id));
        } else {
            $this->error('非法操作');
        }
    }
}