MaillistAction.class.php 8.02 KB
<?php
Class MaillistAction extends CommonAction{
	private $tab = 'Webuser';
	private $tab2 = 'Department';
	private $tabview = 'MaillistView';
	public function _initialize() {
		parent::_initialize();
		$this->assign('page_title','通讯录');
		$this->checkLog();	//判断用户是否已经登录
		$this->assign('authlist',$this->getPower());	//用户权限
	}

	public function index(){
		if(I('title','','trim')){
			$where['name']=array('like','%'.I('title','','trim').'%');	
			$search['title']=I('title','','trim');
		}
		if(I('depart','','trim')){
			$where['depart_name']=array('like','%'.I('depart','','trim').'%');	
			$search['depart']=I('depart','','trim');
		}
		$this->assign('search',$search);
		$where['isshow'] = array('eq',1);
		$order = 'id desc';
		$model = M($this->tab);
		$field = array('id','name','depart_name','pid','mobile','tel','email','addtime');
		$data = $this->_getLists($model,$where,$order,$field,100,'Pages3',array('header'=>'<li style="border:none;">共%totalRows%条记录','pages'=>'%nowPage%/%totalPages%页 </li>','prev'=>'<上一页','next'=>'下一页>','first'=>'首页','last'=>'尾页','theme'=>'%header% %pages% %prePage% %linkPage% %nextPage% %end%'));
		$this->assign('list',$data['list']?$data['list']:array());	//列表
		$this->assign('page',$data['page']);	//分页
		$this->assign('p',$data['p']);	//当前页数
		$this->assign('page_title','通讯录');
		$this->assign('empty','<tr><td colspan="7" style="padding-left:15px;font-size:14px;">暂无相关信息!</td></tr>');
		$this->display();
	}

	public function  edit()
	{
		$Admin=D($this->tab);
		$id=I('id',0,'intval');
		if(!$id){
			$this->error('参数有误!');
		}
		if($this->isPost()){
			if(!$Admin->create()){
				$this->error($Admin->getError());
			}else{
				$lastid=$Admin->save();
				if($lastid>0){
					$this->success('通讯录信息修改成功',U('index'));
				}else{
					$this->error('通讯录信息修改失败');
				}
			}
		}else{
			$data=$Admin->where('id='.$id)->find();
			$this->assign('depart',$this->getDepartment());
			$this->assign('data',$data);
			$this->display(); 
		}
	}
/**
 * 添加通讯录
 */
	public function add(){
		$Admin=D($this->tab);
		if($this->isPost()){
			//添加用户
			if(!$Admin->create()){
				$this->error($Admin->getError());
			}else{
				$lastid=$Admin->add();
				if($lastid>0){
					$this->success('通讯录信息添加成功',U('index'));
				}else{
					$this->error('通讯录信息添加失败');
				}
			}
		}else{
			$this->assign('depart',$this->getDepartment());
			$this->display(); 
		}
	}
/**
 * 公用删除
 * @param  int $id	[description]
 * @param  object $model [description]
 * @return bool		[description]
 */
	private function pubDelete($id,$model){
		if(is_array($id)){
			$map['id'] = array('in',$id);
		}else{
			$map['id']=array('eq',$id);			
		}
		$aa=$model->where($map)->delete();
		if($aa){
			return true;
		}else{
			return false;
		}
	}
/**
 * 删除通讯录
 */
	public function delete()
	{
		$id=I('id',0,'intval');
		if(!$id){
			$this->error('参数有误!');
		}
		$m=M($this->tab);
		$back=$this->pubDelete($id,$m);
		if($back){
			$this->success('通讯录信息删除成功',U('index',array('p'=>I('p',1,'intval'))));
		}else{
			$this->error('通讯录删除失败');
		}
	}
/**
 * 导出excel
 */
	public function download(){
		set_time_limit(0);
		$fields = array('id','name','tel','email','username');
		$filename = '通讯录-'.date('_YmdHis');
		$filenamebac=$filename;
		$filename = iconv('utf-8', 'gb2312', $filename);//文件名称
		
		$dir=str_replace('\\','/',realpath(__ROOT__));
		if(!file_exists($dir.'/Uploads/excel')){
			@mkdir($dir.'/Uploads/excel','0777');
		}
		$pp_file_path='/Uploads/excel/'.$filename.'.xls';
		$pp_file_pathbac='/Uploads/excel/'.$filenamebac.'.xls';
		$pp_file = $dir.$pp_file_path;

		$order['id'] = 'desc';
		$where['isshow']=array('eq',1);
		$list=M($this->tab)->where($where)->field($fields)->order($order)->select();
		$f=fopen($pp_file, "w");
		fwrite($f,'<table width="100%" border="1" cellpadding="0" cellspacing="1">');
		fwrite($f,'');
		fwrite($f,'<tr><td align="center">'.iconv("UTF-8", "GBK", "序号").'</td><td align="center">'.iconv("UTF-8", "GBK", "登录帐号").'</td><td align="center">'.iconv("UTF-8", "GBK", "姓名").'</td><td align="center">'.iconv("UTF-8", "GBK", "手机").'</td><td align="center">Email</td></tr>');
		if($list){
			foreach($list as $k => $v){
				fwrite($f,'<tr>');
				fwrite($f,'<td align="center">'.($k+1).'</td><td align="center">'.iconv("UTF-8", "GBK", $v['username']).'</td><td align="center">'.iconv("UTF-8", "GBK", $v['name']).'</td><td align="center">'.iconv("UTF-8", "GBK", $v['tel']).'</td><td align="center">'.iconv("UTF-8", "GBK", $v['email']).'</td>');
				fwrite($f,'</tr>');
			}
		}
		fwrite($f,'</table>');
		fclose($f);
		echo '<script>location.href="'.$pp_file_pathbac.'";setTimeout("window.close();",6000);</script>';
	}




/**
 * 返回部门信息 id name
 * @return array
 */
	private function getDepartment(){
		$field='id,name';
		$order='firstname';
		$info=M('Department')->field($field)->order($order)->select();
		return $info;
	}
/**
 * 部门管理
 */
	public function department(){
		if(I('title','','trim')){
			$where['name']=array('like','%'.I('title','','trim').'%');	
			$search['title']=I('title','','trim');
		}
		$this->assign('search',$search);

		$order='firstname';
		$model=M($this->tab2);
		$field=array('id','name','firstname','addtime');
		$data = $this->_getLists($model,$where,$order,$field,12,'Pages3',array('header'=>'<li style="border:none;">共%totalRows%条记录','pages'=>'%nowPage%/%totalPages%页 </li>','prev'=>'<上一页','next'=>'下一页>','first'=>'首页','last'=>'尾页','theme'=>'%header% %pages% %prePage% %linkPage% %nextPage% %end%'));
		$this->assign('list',$data['list']?$data['list']:array());	//列表
		$this->assign('page',$data['page']);	//分页
		$this->assign('p',$data['p']);	//当前页数
		$this->assign('page_title','部门管理');
		$this->assign('empty','<tr><td colspan="4" style="padding-left:15px;font-size:14px;">暂无相关信息!</td></tr>');
		$this->display();
	}
/**
 * 添加通讯录
 */
	public function add_depart(){
		$Admin=D($this->tab2);
		if($this->isPost()){
			//添加部门
			if(!$Admin->create()){
				$this->error($Admin->getError());
			}else{
				$lastid=$Admin->add();
				if($lastid>0){
					$this->success('部门添加成功',U('department',array('p',I('p',1,'intval'))));
				}else{
					$this->error('部门添加失败');
				}
			}
		}else{
			$this->assign('p',I('p',1,'intval'));
			$this->assign('page_title','部门信息');
			$this->display(); 
		}
	}
/**
 * 修改部门信息
 * @return [type] [description]
 */
	public function  edit_depart()
	{
		$Admin=D($this->tab2);
		$id=I('id',0,'intval');
		if(!$id){
			$this->error('参数有误!');
		}
		if($this->isPost()){
			//修改
			if(!$Admin->create()){
				$this->error($Admin->getError());
			}else{
				$lastid=$Admin->save();
				if($lastid>0){
					$this->success('部门信息修改成功',U('department',array('p'=>I('p',1,'intval'))));
				}else{
					$this->error('部门信息修改失败');
				}
			}
		}else{
			$where['id'] = array('eq',$id);
			$data=$Admin->where($where)->find();
			$this->assign('data',$data);
			$this->assign('page_title','部门信息');
			$this->assign('p',I('p',1,'intval'));
			$this->display(); 
		}
	}
/**
 * 删除部门信息
 */
	public function del_depart(){
		$id=I('id',0,'intval');
		if(!$id){
			$this->error('参数有误!');
		}
		$m=M($this->tab2);
		$back=$this->pubDelete($id,$m);	//公用删除
		if($back){
			$this->success('部门信息删除成功',U('department',array('p'=>I('p',1,'intval'))));
		}else{
			$this->error('部门删除失败');
		}
	}

/**
 * ajax 获取首字母
 */
	public function ajaxname(){
		$name=pinyin1(I('name','trim'));
		echo $name;
	}
}