westdc-zf1/application/admin/controllers/UserController.php

631 lines
16 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
class Admin_UserController extends Zend_Controller_Action
{
function preDispatch()
{
$this->db=Zend_Registry::get('db');
$this->view->config = Zend_Registry::get('config');
$this->messenger=$this->_helper->getHelper('FlashMessenger');
$this->view->messages = $this->messenger->getMessages();
}
function postDispatch()
{
$this->view->messages = $this->messenger->getMessages();
}
function indexAction()
{
$sql="select count(id) as total from users";
$uq=$this->db->query($sql);
$row=$uq->fetch();
$sqlt="select count(id) as total from users where usertype='administrator'";
$uqt=$this->db->query($sqlt);
$adminrow=$uqt->fetch();
$this->view->su=$row;
$this->view->suadmin=$adminrow;
}//indexAction
function listAction()
{
$select=$this->db->select();
$select->from('users')
->where('usertype = ?', 'member')
->order('users.id desc');
$paginator = Zend_Paginator::factory($select);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$paginator->setItemCountPerPage(30);
$paginator->setView($this->view);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
$this->view->paginator=$paginator;
}//listAction
function searchAction()
{
$search = $this->_getParam('search');
$realname = $this->view->realname = $this->_getParam('realname');
$unit = $this->view->unit = $this->_getParam('unit');
$project = $this->view->project = $this->_getParam('project');
$select=$this->db->select();
if(!empty($search) && ( !empty($realname) || !empty($unit) || !empty($project) ))
{
$this->messenger->addMessage('搜索结果');
$select->from('users');
if(!empty($realname))
{
$select->where('realname like ? ', '%'.$realname.'%');
$select->orWhere('username like ? ', '%'.$realname.'%');
}
if(!empty($unit))
$select->where('unit like ? ', '%'.$unit.'%');
if(!empty($project))
$select->where('project like ? ', '%'.$project.'%');
$select->order('users.id desc');
$paginator = Zend_Paginator::factory($select);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$paginator->setItemCountPerPage(30);
$paginator->setView($this->view);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml');
$this->view->paginator=$paginator;
$this->_helper->viewRenderer('list');
}
else {
$this->_redirect("/admin/user/list");
}
}//searchAction
function deleteAction()
{
$delete=(int)$this->_getParam('id');
$deletename = $this->_getParam('uname');
if (isset($delete))
{
$sql="delete from users where id=?";
try {
$this->db->query($sql,array($delete));
$this->messenger->addMessage('您已经成功的删除了用户:'.$deletename);
} catch (Exception $e) {
$this->messenger->addMessage($e->getMessage());
}
$this->_redirect("/admin/user/list");
}
}
function adminlistAction()
{
$select=$this->db->select();
$select->from('users')
->where('usertype = ?', 'administrator')
->order('users.id desc');
$paginator = Zend_Paginator::factory($select);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$paginator->setItemCountPerPage(30);
$paginator->setView($this->view);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
$this->view->paginator=$paginator;
}
function showAction()
{
$id=(int)$this->_getParam('id');
if (isset($id))
{
try {
$sql="select * from users where id=?";
$result=$this->db->query($sql,$id);
$rows = $result->fetch();
$this->view->infos=$rows;
} catch (Exception $e) {
$this->messenger->addMessage($e->getMessage());
}
}
else
{
$this->_redirect("/admin/user/list");
}
}//showAction()
function upAction()
{
$id=(int)$this->_getParam('id');
if (isset($id))
{
try {
$sql="select u.*,m.id as mid from users u left join mdexperts m on m.id=u.id where u.id='$id'";
$result = $this->db->query($sql);
$ex = $result->fetch();
if(empty($ex['mid'])){
$submit=$this->_getParam('submit');
$speciality = $this->_getParam('speciality');
if($submit)
{
$sql="insert into mdexperts (id,speciality) values ('$id','$speciality')";
if($this->db->exec($sql)>0)
{
$this->messenger->addMessage('已经成功将用户提升为评审专家');
$this->_redirect("/admin/user/list");
}
}
else
{
$this->view->infos = $ex;
}
}else
{
$this->messenger->addMessage("该用户已经是评审专家");
$this->_redirect("/admin/user/list");
}
} catch (Exception $e) {
$this->messenger->addMessage($e->getMessage());
$this->_redirect("/admin/user/list");
}
}
else
{
$this->_redirect("/admin/user/list");
}
}//upAction() 把用户提升为评审专家
function editAction()
{
$id=(int)$this->_getParam('id');
$usertype=$this->_getParam('usertype');
$newpwd=$this->_getParam('newpwd');
$cfnewpwd=$this->_getParam('cfnewpwd');
$sql="";
$updates=array();
if (isset($id))
{
if(!empty($newpwd)&&!empty($cfnewpwd))
{
if($newpwd==$cfnewpwd)
{
$password=md5($newpwd);
$updates[]="password='$password'";
}
else
{
$this->messenger->addMessage('两次密码不相同');
$this->_redirect("/admin/user/show/id/$id");
}
}
if(isset($usertype))
{
$updates[]="usertype='$usertype'";
}
$update=join(',',$updates);
$sql="update users set $update where id='$id'";
try {
$this->db->query($sql);
$this->messenger->addMessage('编辑成功!');
} catch (Exception $e) {
$this->messenger->addMessage($e->getMessage());
}
$this->_redirect("/admin/user/show/id/$id");
}
else {
$this->_redirect("/admin/user/list");
}
}
function fetchpwdAction()
{
$id=(int)$this->_getParam('id');
$email=$this->_getParam('email');
if (!empty($email))
{
try {
$sql="select * from users where email=?";
$uq=$this->db->query($sql,$email);
if ($urow=$uq->fetch())
{
//email the url to user
$username=$urow['username'];
$sql="update users set activation=? where email=?";
$uid=uniqid();
$this->db->query($sql,array($uid,$email));
$mail=new WestdcMailer($this->view->config->smtp);
$body="尊敬的西部数据中心用户:
有人提出了针对此用户名的密码重置请求。
用户名:";
$body.=$username;
$body.="
若想重置您的密码请打开下面的链接,否则请忽略此邮件,一切如常。
";
$body.="http://westdc.westgis.ac.cn/account/fetchpwd/".$username."/".$uid;
$mail->setBodyText($body);
$mail->setFrom($this->view->config->service->email,'西部数据中心服务组');
$mail->addTo($email);
$mail->setSubject('密码已重置');
$mail->send();
$this->messenger->addMessage('密码重置成功!');
}
} catch (Exception $e) {
$this->messenger->addMessage($e->getMessage().$email);
}
$this->_redirect("/admin/user/show/id/$id");
}
else {
$this->_redirect("/admin/user/list");
}
} //overview
/*
* groupAction() 用户组管理
*
*/
function groupAction(){
$ac = $this->_getParam('ac');
$groupsTable = "groups";
$userGroupTable = "usergroup";
$nameField = $paramName = "name";
if(empty($ac) || $ac == "index")
{
$select=$this->db->select();
$select->from($groupsTable)
->order('groups.id desc');
$paginator = Zend_Paginator::factory($select);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$paginator->setItemCountPerPage(30);
$paginator->setView($this->view);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
$this->view->paginator=$paginator;
}//首页
if($ac == "add")
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$data[$nameField] = $this->_getParam($paramName);
if(empty($data[$nameField]))
{
$this->jsonexit(array("error"=>'请输入组名'));
return true;
}
if($this->db->insert($groupsTable,$data))
{
$this->jsonexit(array("status"=>1));
return true;
}else{
$this->jsonexit(array("error"=>"出现错误,请重试"));
return true;
}
return true;
}//增加用户组
if($ac == "edit")
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$id = $this->_getParam('id');
$data[$nameField] = $this->_getParam($paramName);
if(empty($id))
{
$this->jsonexit(array("error"=>'参数错误'));
return true;
}
if(empty($data[$nameField]))
{
$this->jsonexit(array("error"=>'请输入组名'));
return true;
}
if($this->db->update($groupsTable,$data,"id=$id"))
{
$this->jsonexit(array("status"=>1,"name"=>$data[$nameField]));
return true;
}else{
$this->jsonexit(array("error"=>"出现错误,请重试"));
return true;
}
return true;
}//编辑
if($ac == "del")
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$id = $this->_getParam('id');
if(empty($id))
{
$this->jsonexit(array("error"=>'参数错误'));
return true;
}
if($this->db->delete($groupsTable,"id=$id"))
{
$this->jsonexit(array("status"=>1));
return true;
}else{
$this->jsonexit(array("error"=>"出现错误,请重试"));
return true;
}
return true;
}//删除
if($ac == "show")
{
$this->_helper->viewRenderer('group-users');
$gid = (int)$this->_getParam('id');
if(empty($gid))
{
echo "参数错误!";
return true;
}
$this->view->groupid = $gid;
$sql = "SELECT ug.uid,ug.gid,u.id,u.username,u.realname,u.email FROM $userGroupTable ug
LEFT JOIN users u ON ug.uid=u.id
WHERE ug.gid=$gid
ORDER BY ug.ts_created DESC";
$sth = $this->db->query($sql);
$rows = $sth->fetchAll();
$paginator = Zend_Paginator::factory($rows);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$paginator->setItemCountPerPage(20);
$paginator->setView($this->view);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml');
$this->view->paginator=$paginator;
}//查看用户
if($ac == "adduser")
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$data['uid'] = $this->_getParam('uid');
$data['gid'] = $this->_getParam('gid');
if(empty($data['uid']) || empty($data['gid']))
{
$this->jsonexit(array("error"=>'参数错误'));
return true;
}
$sql = "SELECT * FROM users WHERE id={$data['uid']}";
$sth = $this->db->query($sql);
$rows = $sth->fetchAll();
if(count($rows)<1)
{
$this->jsonexit(array("error"=>'用户不存在'));
return true;
}
$sql = "SELECT * FROM $userGroupTable WHERE uid={$data['uid']} AND gid='{$data['gid']}'";
$sth = $this->db->query($sql);
$rows = $sth->fetchAll();
if(count($rows)>0)
{
$this->jsonexit(array("error"=>'该用户已经存在于要加入的组'));
return true;
}
if($this->db->insert($userGroupTable,$data))
{
$this->jsonexit(array("status"=>1));
return true;
}else{
$this->jsonexit(array("error"=>"出现错误,请重试"));
return true;
}
return true;
}//往组中添加用户
if($ac == "deluser")
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$uid = $this->_getParam('uid');
$gid = $this->_getParam('gid');
if(empty($uid) || empty($gid))
{
$this->jsonexit(array("error"=>'参数错误'));
return true;
}
if($this->db->delete($userGroupTable,"uid=$uid AND gid=$gid"))
{
$this->jsonexit(array("status"=>1));
return true;
}else{
$this->jsonexit(array("error"=>"出现错误,请重试"));
return true;
}
return true;
}//从组中删除用户
}// groupAction()
/*
*
* 给时间长没有登录的用户发送邀请邮件
*
*/
public function sendmailAction(){
$ac = $this->_getParam('ac');
if(empty($ac) || $ac=='index')
{
$time = date("Y-m-d H:i:s",time()-3*365*24*3600);
$sql = "SELECT * FROM users
WHERE ts_last_login<'$time'
ORDER BY ts_last_login DESC";
$sth = $this->db->query($sql);
$rows = $sth->fetchAll();
$paginator = Zend_Paginator::factory($rows);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$paginator->setItemCountPerPage(20);
$paginator->setView($this->view);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml');
$this->view->paginator=$paginator;
$this->view->count_users = count($rows);
}
if($ac == "send")
{
$this->_helper->viewRenderer('sendmail-send');
//当前进行到的数量
$now = $this->_getParam('now');
//需要将起始的时间传递过来防止sql中出现多余的用户
$time = $this->_getParam('time');
if(empty($time))
{
$time = date("Y-m-d H:i:s",time()-3*365*24*3600);
}
$this->view->gotime = $time;
//当前处理的页数
$page = $this->_getParam('page');
if(empty($page))
{
$page = 0;
}
$step = 20;
$sql = "SELECT count(id) as c FROM users
WHERE ts_last_login<'$time'";
$sth = $this->db->query($sql);
$row = $sth->fetch();
$total = $row['c'];
if(empty($now))
{
$now = 0;
}
//此次读取的起点
$start = $now + $step;
if($start>$total)
{
$this->view->stop = "YES";
return true;
}
$sql = "SELECT * FROM users
WHERE ts_last_login<'$time'
ORDER BY ts_last_login DESC
LIMIT $step
OFFSET $start";
$sth = $this->db->query($sql);
$rows = $sth->fetchAll();
$result = array();
foreach($rows as $k=>$v)
{
include_once("EmailText.php");
$mail=new WestdcMailer($this->view->config->smtp);
$mail->setFrom($this->view->config->service->email,'西部数据中心服务组');
$mailtp=new EmailText($this->db,"author-new",array(
'user' => $v['username'],
'uid' => $v['id'],
'realname'=> $v['realname'],
'lastlogin'=> date("Y-m-d",$v['email']),
));
$mail->setBodyText($mailtp->getBody());
$mail->setSubject($mailtp->getSubject());
@$mail->addTo($v['email']);
$result[$v['id']] = array(
'username'=>$v['username'],
'email'=>$v['email']
);
@$mail->send();
}
$this->view->result = $result;
$this->view->now = $start;
$this->view->total = $total;
$percent = round( ( $start / $total ) * 100 ,1);
$this->view->percent = $percent;
$page ++;
$this->view->page = $page;
}
}//sendemailAction()
public function jsonexit($data){
$this->getResponse()->setHeader('Content-Type', 'application/json')->appendBody(json_encode($data,JSON_NUMERIC_CHECK));
return true;
}
}