westdc-zf1/application/module/Users/Local.php

187 lines
3.7 KiB
PHP
Raw Permalink Normal View History

2014-05-27 07:04:25 +00:00
<?php
namespace Users;
use \Helpers\View as view;
use \Helpers\dbh as dbh;
use \Users\Account;
use \Helpers\Table;
/*
\Users\Local 进行抽象,满足后台调用的需求,屏蔽一些错误等等
*/
class Local extends \Zend_Controller_Plugin_Abstract
{
private $db;
protected $events = NULL; //事件
public $table;
public $account;
function __construct($db = NULL,$mail = NULL)
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = \Zend_Registry::get('config');
2014-05-27 07:04:25 +00:00
$this->table = new \Helpers\Table();
}
//添加本地帐号
public function addLocalUser($data = NULL)
{
if(empty($data) || !is_array($data))
{
$data = $this->getLocalParam();
}
if(!isset($data['userid']) || !is_numeric($data['userid']))
{
return "参数错误";
}
if(empty($data['usertype']))
{
return "请选择用户类型";
}
$user = $this->fetchUser($data['userid']);
$local_user = $this->checkLocalUserExists($data['userid']);
if(isset($local_user['uid']) && $local_user['uid'] > 0)
{
$localUserExists = TRUE;
}else{
$localUserExists = FALSE;
}
if(!isset($user['id']) || $user['id'] < 1 )
{
if($localUserExists)
{
$this->deleteUser($data['userid']);
}
return "该用户不存在";
}
$cache_data = array(
'uid'=>$data['userid'],
'usertype'=>$data['usertype']
);
if($localUserExists)
{
$status = $this->addUser($cache_data,$local_user['uid']);
}else{
$status = $this->addUser($cache_data);
}
if($status === true)
{
return true;
}else{
return $status;
}
}
# 获取添加本地用户的参数
public function getLocalParam(\Zend_Controller_Request_Abstract $request = NULL)
{
$request = new \Zend_Controller_Request_Http();
$data = array(
'userid'=> (int)$request->getParam('userid'),
'usertype'=> trim($request->getParam('usertype'))
);
return $data;
}
# 在总数据库查询所要添加到本地的用户ID信息
public function fetchUser($id =0)
{
$sql = "SELECT * FROM {$this->table->users} WHERE id=$id LIMIT 1";
$rs = $this->db->query($sql);
$row = $rs->fetch();
return $row ;
}
//检查用户是否在本地库
public function checkLocalUserExists($id)
{
$sql = "SELECT * FROM {$this->table->users_local} WHERE uid=$id LIMIT 1";
$rs = $this->db->query($sql);
$row = $rs->fetch();
return $row;
}
# 如果传入用户ID为空写入新用户否则修改用户
public function addUser($data,$id = 0)
{
$dbh = new dbh();
$id = (int)$id;
if(empty($id))
{
$status = $dbh->insert($this->table->users_local,$data);
if($status)
{
return true;
}else{
return "用户信息写入失败";
}
}else{
$status = $dbh->update($this->table->users_local,$data);
if($status)
{
return true;
}else{
return "用户信息更新失败";
}
}
return;
}
# 查询用户列表
public function listUser($key)
{
#所有本地用户列表显示
if(empty($key))
{
$dbh = new dbh();
$sql = "SELECT * FROM {$this->table->users_local}";
$rs = $this->db->query($sql);
$row = $rs->fetch();
return $row ;
}
#按条件查询本地用户
}
# 删除用户
public function deleteUser($id)
{
if(!empty($id) | is_numeric($id))
{
$sql = "DELETE FROM {$this->table->users_local} WHERE uid=$id";
@$this->db->exec($sql);
return "该用户已删除";
}else{
return '删除用户操作失败';
}
}
}