westdc-zf1/application/models/Users.php

106 lines
1.9 KiB
PHP

<?php
/**
* Users 用户相关操作
*/
class Users
{
private $db;//传入PDO对象
//使用到的公共变量
public $tbl_user = "users";
public $tbl_group = "groups";
public $tbl_userToGroup = "usergroup";
function __construct($db)
{
$this->db = $db;
}
//获取用户的组ID
public function getGroup($uid=0){
if(!empty($uid) && is_numeric($uid))
{
$sql = "SELECT gid FROM ".$this->tbl_userToGroup." WHERE uid=$uid";
$rs = $this->db->query($sql);
$row = $rs->fetch();
return $row['gid'];
}else{
$select = $this->db->select();
return $select ->from($this->tbl_group)
->order('groups.id desc');
}
}
//获取组名
public function getGroupName($gid){
if(!is_numeric($gid))
{
return false;
}
$sql = "SELECT * FROM ".$this->tbl_group." WHERE id=$gid";
$rs = $this->db->query($sql);
$row = $rs->fetch();
return $row['name'];
}
//创建用户组
public function CreateGroup($name){
$groupTable = $this->tbl_group;
if(empty($name))
{
return false;
}
$data = array(
"name" => $name
);
return $this->db->insert($groupTable,$data);
}
//把用户移动到组
public function AddTo($uid,$gid){
if(!is_numeric($uid) || !is_numeric($gid))
{
return false;
}
$sql = "SELECT * FROM ".$this->tbl_userToGroup." WHERE uid=? AND gid=?";
$sth = $this->db->prepare($sql);
$sth->execute(array($uid,$gid));
$row = $sth->fetch();
if(!empty($row['ts_created']))
{
$data = array(
"uid"=>$uid,
"gid"=>$gid
);
$whereSql = " uid=$uid AND gid=$gid ";
return $this->db->update($this->tbl_userToGroup,$data,$whereSql);
}else{
$data = array(
"uid"=>$uid,
"gid"=>$gid
);
return $this->db->insert($this->tbl_userToGroup,$data);
}
}
}