zoukankan      html  css  js  c++  java
  • 项目准备(model)

    user model manage

    <?php
        /**
         * UserModelManager is a class used to manage UserModel class
         * @author liupengcheng
         * @date 2014-3-3
         */
        class UserModelManager{
            private $dao;
            private $_si;
            private $db_name;
            private $table_name;
            private $table_apply;
            
            public function __construct(){
                    $this->dao = new DAO();
                    $this->_si = new SqlIntepreter();
                    $this->db_name=Loader::loadConfigs('db_usermodel','db1');
                    $this->table_name=Loader::loadConfigs('db_usermodel','table1');
                    $this->table_apply=Loader::loadConfigs('db_usermodel','table2');
            }
            
            public function userExist($user_name, $password){
                $select =array();
                array_push($select,'user_name');
                array_push($select,'password');
            
                try{
                    $this->dao->connect();  //连接数据库
                    $this->dao->selectDB($this->db_name); //选择数据库
                }catch(Exception $e){
                    throw $e;
                }
                $where = array('user_name'=>$user_name,'password'=>$password);
                //重置状态
                $this->_si->renew();
                $sqlstat = $this->_si->select($select)->from($this->table_name)->where($where)->getSql();   //构造sql语句
                $result=$this->dao->execute($sqlstat);
                $n = mysql_num_rows($result);
                mysql_close();
                if ($n>0) {
                    return true;
                }
                return false;
                
            }
            /**
             * 
             * @param $user_name
             * @param $password
             * @param string $limit must be integer
             * @throws Exception
             * @return resource
             */
            public function getUsers($user_name,$password,$limit=null){
                $select=array();
                array_push($select,'user_name');
                array_push($select, 'password');
                array_push($select, 'role');
                array_push($select,'email');
                array_push($select,'comment');
                try{
                    $this->dao->connect();
                    $this->dao->selectDB($this->db_name);
                }catch(Exception $e){
                    throw $e;
                }
                $where = array('user_name'=>$user_name,'password'=>$password);
                //重置状态
                $this->_si->renew();
                if(!isset($limit)){
                    $sqlstat = $this->_si->select($select)->from($this->table_name)->where($where)->getSql();
                }else{
                    $sqlstat = $this->_si->select($select)->from($this->table_name)->where($where)->limit($limit)->getSql();
                }
                $result = $this->dao->execute($sqlstat);
                $users = mysql_fetch_array($result);
                mysql_close();
                return new UserModel($users);
            }
            
            public function addUser($user_name,$password,$role,$email,$comment){
                $insert=array('user_name','password','role','email','comment');
                $values = array('user_name'=>$user_name,'password'=>$password,'role'=>$role,'email'=>$email,'comment'=>$comment);
                //重置状态
                
                $this->_si->renew();
                $sqlstat = $this->_si->insert($this->table_name, $insert)->values($values)->getSql();
                
                try{
                    $this->dao->connect();
                    $this->dao->selectDB($this->db_name);
                }catch(Exception $e){
                    throw $e;
                }
                $result=$this->dao->execute($sqlstat);
                if(mysql_affected_rows()>0){
                    mysql_close();
                    return true;
                }
                return false;
            }
            
            public function delUser($id){
                $where=array('id'=>$id);
                //重置状态
                $this->_si->renew();
                $sqlstat = $this->_si->delete($this->table_name)->where($where);
                try{
                    $this->dao->connect();
                    $this->dao->selectDB($this->db_name);
                }catch(Exception $e){
                    throw $e;
                }
                $result = $this->dao->execute($sqlstat);
                if(mysql_affected_rows()>0){
                    mysql_close();
                    return true;
                }
                else
                    return false;
            }
            
            public function userNameAvailable($user_name){
                try{
                    $this->dao->connect();
                    $this->dao->selectDB($this->db_name);
                }catch(Exception $e){
                    throw $e;
                }
                
                
                $user_table = true;
                $apply_table = true;
                $where = array('user_name'=>$user_name);
                
                //重置状态
                $this->_si->renew();
                $sqlstat = $this->_si->select()->from($this->table_name)->where($where)->getSql();
                $result = $this->dao->execute($sqlstat);
                
                if(mysql_num_rows($result)>0){
                    $user_table = false;
                }
                
                $this->_si->renew();
                $sqlstat = $this->_si->select()->from($this->table_apply)->where($where)->getSql();
                $result = $this->dao->execute($sqlstat);
                    
                if(mysql_num_rows($result)>0){
                    $apply_table = false;
                }
                
                mysql_close();
                
                if($user_table && $apply_table){
                    return true;
                }else{
                    return false;
                }
                
            }
            
            public function updateBasicInfo($role='', $email='', $comment=''){
                session_start();
                $sqlstat=null;
                if(isset($_SESSION['user_name'])&&!empty($_SESSION['user_name'])){
                    $where=array('user_name'=>$_SESSION['user_name']);
                    $params=array();
                    $role = empty($role)? $_SESSION['role']:$role;
                    $email = empty($email) ? $_SESSION['email']:$email;
                    $comment = empty($comment)? $_SESSION['comment']:$comment;
                    $params=array('role'=>$role,'email'=>$email,'comment'=>$comment);
                    //重置状态
                    $this->_si->renew();
                    $sqlstat=$this->_si->update($this->table_name)->set($params)->where($where)->getSql();
                }else{
                    throw new Exception("Error: session expired");    
                }
                try{
                    $this->dao->connect();
                    $this->dao->selectDB($this->db_name);
                }catch(Exception $e){
                    throw $e;
                }
                $result=$this->dao->execute($sqlstat);
                if(mysql_affected_rows()>0){
                    mysql_close();
                    return true;
                }else 
                    return false;
            }
            
            public function passUpdate($pass, $newPass){
                session_start();
                $sqlstat=null;
                if(isset($_SESSION['user_name'])&&!empty($_SESSION['user_name'])){
                    $where=array('user_name'=>$_SESSION['user_name'],'password'=>$pass);
                    $set=array('password'=>$newPass);
                    //重置状态
                    $this->_si->renew();
                    $sqlstat=$this->_si->update($this->table_name)->set($set)->where($where);
                }else{
                    throw new Exception('Error: session expired');
                }
                try{
                    $this->dao->connect();
                    $this->dao->selectDB($this->db_name);
                }catch(Exception $e){
                    throw $e;
                }
                $result = $this->dao->execute($sqlstat->getSql());
                if(mysql_affected_rows()>0){
                    mysql_close();
                    return true;
                }else 
                    return false;
            }
            
            
            public function getapplycount(){   //获得apply表的个数-hss
                try{
                    $this->dao->connect();
                    $this->dao->selectDB($this->db_name);
                }catch(Exception $e){
                    throw $e;
                }
                    
                $this->_si->renew();
                    
                $sqlstat = $this->_si->select()->from($this->table_apply)->getSql();   //构造sql语句
                //return $sqlstat;
                $result=$this->dao->execute($sqlstat);   //数据库执行的问题
                    
                $n = mysql_num_rows($result);
                mysql_close();
                    
                return $n;
            }
            
            public function getApplyUserInfo(){
                try{
                    $this->dao->connect();
                    $this->dao->selectDB($this->db_name);
                }catch(Exception $e){
                    throw $e;
                }
    
    //             //重置状态
                $this->_si->renew();
                if(!isset($limit)){
                    $sqlstat = $this->_si->select()->from($this->table_apply)->getSql();
                }else{
                    $sqlstat = $this->_si->select()->from($this->table_apply)->limit($limit)->getSql();
                }
                
                $result = $this->dao->execute($sqlstat);
                
                $users=array();
                
                $user=array();    
                while($row = mysql_fetch_array($result)){
                    $user['name']=$row["user_name"];
                    $user['role']=$row["role"];
                    if(isset($row["email"])){
                        $user['email']=$row["email"];
                    }else{
                        $user['email']="";
                    }
                    if(isset($row["comment"])){
                        $user['comment']=$row["comment"];
                    }else{
                        $user['comment']="";
                    }
                    array_push($users, $user);
                }
                
                mysql_close();
                return json_encode($users);
            }
            
            //把申请用户从apply_info中转到user_info中,删除apply_info中的这些用户
            public function addApplyUser($names){
                try{
                    $this->dao->connect();
                    $this->dao->selectDB($this->db_name);
                }catch(Exception $e){
                    throw $e;
                }
                
                $add_user_info = array();
                $opera_status = true;
                //从apply_info中读取申请用户,并删除这些申请用户
                foreach ($names as $name){
                    $this->_si->renew();
                    $where = array('user_name'=>$name);
                    
                    //读用户
                    $sqlstat = $this->_si->select()->from($this->table_apply)->where($where)->getSql();
                    $read_result = $this->dao->execute($sqlstat);
                    
                    $user= mysql_fetch_array($read_result);
                    $read_state = false;
                    if(mysql_affected_rows()>0){
                        $read_state = true;
                    }
                    array_push($add_user_info, $user);
                    
                    //删除用户
                    $this->_si->renew();
                    $sqlstat = $this->_si->delete($this->table_apply)->where($where)->getSql();
                    $delete_result = $this->dao->execute($sqlstat);
                    $delete_state = false;
                    if(mysql_affected_rows()>0){
                        $delete_state = true;
                    }
                    
                    if(!($read_state && $delete_state)){
                        $opera_status = false;
                        break;
                    }
                }
                
                //添加apply_info中读取的用户到users_info中
                foreach ($add_user_info as $user){
                    $insert=array('user_name','password','role','email','comment');
                    $values = array('user_name'=>$user['user_name'],
                                        'password'=>$user['password'],
                                        'role'=>$user['role'],
                                        'email'=>$user['email'],
                                        'comment'=>$user['comment']);
                    
                    
                    $this->_si->renew();
                    $sqlstat = $this->_si->insert($this->table_name, $insert)->values($values)->getSql();
                    $result=$this->dao->execute($sqlstat);
                    if(!(mysql_affected_rows()>0)){
                        $opera_status = false;
                        break;
                    }
                }
                mysql_close();
                //重置状态
                return $opera_status;                
            }
            
            //把申请用户从apply_info中删除
            public function  deleteApplyUser($names){
                try{
                    $this->dao->connect();
                    $this->dao->selectDB($this->db_name);
                }catch(Exception $e){
                    throw $e;
                }
                    
                $add_user_info = array();
                $delete_state = true;
                //从apply_info中读取申请用户,并删除这些申请用户
                foreach ($names as $name){
                    $this->_si->renew();
                    $where = array('user_name'=>$name);
                
                    //删除用户
                    $this->_si->renew();
                    $sqlstat = $this->_si->delete($this->table_apply)->where($where)->getSql();
                    $delete_result = $this->dao->execute($sqlstat);
                 
                    if(!(mysql_affected_rows()>0)){
                        $delete_state = false;
                        break;
                    }
    
                }
                mysql_close();
                //重置状态
                return $delete_state;
            }
        }
  • 相关阅读:
    Java 字节码解释说明
    JVM垃圾回收:G1回收器
    JVM 参数
    HotSpot 虚拟机对象探秘
    JDK 内置图形界面工具
    Java 内存模型
    在网络设备上调试 Android 程序
    .NET MVC异步调用中的Session问题
    在MVC的ApiController中实现统一校验
    使用 AndroidX86 在虚拟机中作为调试设备
  • 原文地址:https://www.cnblogs.com/hemi/p/4019993.html
Copyright © 2011-2022 走看看