审查视图

app/portal/controller/SchoolController.php 5.6 KB
5  
anyv authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\WeChatBaseController;
use think\Db;
class SchoolController extends WeChatBaseController{

    /**
     * 显示学校管理页
     */
    public function school(){
2  
anyv authored
20 21 22 23 24 25
        $uid = cmf_get_current_user_id();
        $school = Db::name('school') -> where("uid",$uid) -> select() -> toArray();
        foreach ($school as $key => $val){
            $school[$key]['class'] = Db::name('grade_class') -> where('school_id',$val['id']) -> select() -> toArray();
        }
        $this -> assign('school',$school);
5  
anyv authored
26 27 28 29
        return $this -> fetch();

    }
5  
anyv authored
30 31 32 33 34
    /**
     * 添加学校页
     */
    public function add_school(){
anyv authored
35 36 37 38 39 40 41
        if($this -> request -> isPost()){
            $class = explode(',',$_POST['str']);
            foreach ($class as $key => $val){
                if($val != '' && $val != 'undefined'){
                    $class_data[] = $val;
                }
            }
5  
anyv authored
42 43 44 45 46 47
            //获取所有学校
            $school_data = Db::name('school') -> select() -> toArray();
            foreach ($school_data as $key => $val){
                $school_data[$key]['grade'] = Db::name('grade_class') -> where("school_id",$val['id']) -> select() -> toArray();
            }
            //循环遍历比对
anyv authored
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
            foreach ($school_data as $key => $val){
                //如果地区 学校 类型相同就进行下一步比对
                if($_POST['region']==$val['region'] && $_POST['school']==$val['school'] && $_POST['type']==$val['type']){
                    foreach ($val['grade'] as $key1 => $val1){
                        //循环传过来的年级
                        foreach ($class_data as $key2 => $val2){
                            $val2 = explode('-',$val2);
                            if($val2[0] == $val1['grade']){
                                //将班级拆分循环装进数组
                                $class_s = explode('-',$val1['class']);
                                for($i=$class_s[0];$i<=$class_s[1];$i++){
                                    $class_db[] = $i;
                                }
                                //将传过来的班级循环
                                for($j=$val2[1];$j<=$val2[2];$j++){
                                    $class_my[] = $j;
                                }
                                $result = array_intersect($class_db,$class_my);
                                $result = count($result);
                                if($result > 0){
                                    return 5;
                                }
                            }
                        }
                    }
                }
            }
anyv authored
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
            $school['uid'] = cmf_get_current_user_id();
            $school['create_time'] = time();
            $school['region'] = $_POST['region'];
            $school['school'] = $_POST['school'];
            $school['type'] = $_POST['type'];
            $school_id = Db::name('school') -> insertGetId($school);
            foreach ($class_data as $key => $val){
                $grade_class = explode('-',$val);
                $grade['school_id'] = $school_id;
                $grade['grade'] = $grade_class[0];
                $grade['class'] = $grade_class[1].'-'.$grade_class[2];
                Db::name('grade_class') -> insert($grade);
            }
            if($school_id){
               return true;
            }else{
                return false;
            }

        }else{
anyv authored
95
anyv authored
96 97
            return $this -> fetch();
        }
5  
anyv authored
98 99

    }
5  
anyv authored
100
2  
anyv authored
101 102 103 104 105
    /**
     * 编辑学校
     */
    public function school_edit(){
anyv authored
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        if($this -> request -> isPost()){
            $school['id'] = $_POST['id'];
            $school['school'] = $_POST['school'];
            $school['type'] = $_POST['type'];
            $school['region'] = $_POST['region'];
            $updae = Db::name('school') -> update($school);
            Db::name('grade_class') -> where('school_id',$_POST['id']) -> delete();
            $class = explode(',',$_POST['str']);
            foreach ($class as $key => $val){
                if($val != '' && $val != 'undefined'){
                    $class_data[] = $val;
                }
            }
            foreach ($class_data as $key => $val){
                $grade_class = explode('-',$val);
                $grade['school_id'] = $_POST['id'];
                $grade['grade'] = $grade_class[0];
                $grade['class'] = $grade_class[1].'-'.$grade_class[2];
                Db::name('grade_class') -> insert($grade);
            }
5  
anyv authored
126
            return true;
anyv authored
127 128 129 130 131 132 133
        }else{
            $id = $this -> request -> param();
            $data = Db::name('school') -> where('id',$id['id']) -> find();
            $data_class = Db::name('grade_class') -> where('school_id',$id['id']) -> select();
            $this -> assign('data',$data);
            return $this -> fetch();
        }
5  
anyv authored
134
2  
anyv authored
135
    }
5  
anyv authored
136
2  
anyv authored
137 138 139 140 141
    /**
     * 删除学校
     */
    public function school_del(){
anyv authored
142 143 144 145 146 147 148
        $data = Db::name('school') -> delete($_POST['id']);
        Db::name('grade_class') -> where('school_id',$_POST['id']) -> delete();
        if($data){
            return true;
        }else{
            return false;
        }
2  
anyv authored
149 150

    }
5  
anyv authored
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170



















}