<?php
if(!defined('APP_PATH')||!defined('I_PATH')){exit('Access Denied');}

class ld_member extends controllerBase
{
	function __construct(){
		parent::__construct();
		$this->cu=syClass('ldi_member');
		
		$this->glista = syDB('member_group')->findAll(null,' weight,gid ');
		if(($this->sysParams('a',1)=='add'||$this->sysParams('a',1)=='edit')&&$this->sysParams('run')==1){
			$this->newrow1 = array(
				'user' => $this->sysParams('user',1),
				'pass' => md5(md5($this->sysParams("pass",1)).$this->sysParams("user",1)),
				'email' => $this->sysParams('email',1),
				'gid' => $this->sysParams('gid'),
				'money' => !isset($_POST['money'])?'0.00':$_POST['money'],
				'regtime' => time(),
			);
			$this->newrow2 = array();
			$v=syDB('fields')->findAll(" molds='member' ");
			foreach($v as $f){
				if($f['fieldstype']=='varchar' || $f['fieldstype']=='files' || $f['fieldstype']=='select'){$ns=$this->sysParams($f['fields'],1);}
				if($f['fieldstype']=='int'){$ns=$this->sysParams($f['fields']);}
				if($f['fieldstype']=='decimal'){$ns=$this->sysParams($f['fields'],3);}
				if($f['fieldstype']=='text'){$ns=$this->sysParams($f['fields'],4);}
				if($f['fieldstype']=='time'){$ns=strtotime($this->sysParams($f['fields'],1));}
				if($f['fieldstype']=='fileall'){
					$fieldsall=$this->sysParams($f['fields'].'file',2);
					if($fieldsall){
						$txt=$this->sysParams($f['fields'].'txt',2);$ns='';
						foreach($fieldsall as $k=>$v){
							$ns.='|-|'.$v.'|,|'.$txt[$k];
						}
						$ns=substr($ns,3);
					}
				}
				if($f['fieldstype']=='checkbox'){if($this->sysParams($f['fields'],2)){$ns='|'.implode('|',$this->sysParams($f['fields'],2)).'|';}else{$ns='';}}
				$n=array($f['fields'] => $ns);
				$this->newrow2=array_merge($this->newrow2,$n);
			}
		}
		if(($this->sysParams('a',1)=='gadd'||$this->sysParams('a',1)=='gedit')&&$this->sysParams('run')==1){
			$this->newrow = array(
				'name' => $this->sysParams('name',1),
				'weight' => $this->sysParams('weight'),
				'submit' => $this->sysParams('submit'),
				'audit' => $this->sysParams('audit'),
				'filetype' => strtolower($this->sysParams('filetype',1)),
				'filesize' => $this->sysParams('filesize'),
				'fileallsize' => $this->sysParams('fileallsize'),
			);
		}
	}
	function index(){
		$this->toptxt='会员管理';
		$this->lists = $this->cu->findAll(null,null,'id,user,gid');
		$this->display("member.html");
	}
	function email(){
		if($this->sysParams('run')==1)
		{
			$emails= explode(',', $this->sysParams('to_email',1));
			$body=$this->sysParams('content',1);
			$title=$this->sysParams('title',1);
			if(array_count_values($emails)==0)
			{
				 message_err('请输入有效的email');
			}
			require I_PATH.'/mail.php';
		$out="";
		 // echo $body;
		  $body = htmlspecialchars_decode($body);
		  //die;
			foreach ($emails as $e)
			{	
			   if(!ldmail($e, $title, $body))
			   {
			   		$out.="邮箱".$e."无法发送,请检查邮箱是否正常<br/>";
			   }
			}
			if($out=="")
			{
				message_a('邮件发送成功', '?c=ld_member&a=email');
			}
			else 
			{
				message_a($out, '-1');
			}
		}
		$this->toptxt='邮件群发';
		$this->postgo='email';
		$this->display("member_email.html");
	}
	function add(){
		if ($this->sysParams('run')==1){
			if(!$this->sysParams('pass',1)){message_a("密码不能为空");}
			$newVerifier=$this->cu->syVerifier($this->newrow1);
				if(false == $newVerifier){
					$addnewrow=$this->cu->create($this->newrow1);
					if($addnewrow==FALSE){message_a("会员主表数据写入失败，请重新提交");}
					$arrays = array(
						'aid' => $addnewrow,
					);
					$this->newrow2=array_merge($this->newrow2,$arrays);
					/*
					if(syDB('member_field')->create($this->newrow2)){
						message_a("会员添加成功","?c=ld_member");
					}else{message_a("会员附表数据写入失败，请重新提交");}
					*/
				}else{message_b($newVerifier);}
		}
		$this->toptxt='添加会员';
		$this->postgo='add';
		$this->display("member.html");
	}
	function edit(){
		$this->d=$this->cu->find(array('id'=>$this->sysParams('id')));
		if ($this->sysParams('run')==1){
			$a=$this->newrow1;
			if($this->sysParams('pass',1)==''){unset($a['pass']);}
				if($this->cu->update(array('id'=>$this->sysParams('id')),$a)==FALSE)
				{message_a("会员主表数据修改失败，请重新提交");}
				if(!$this->newrow2){message_a("会员修改成功","?c=ld_member");}
				/*
				if(syDB('member_field')->update(array('aid'=>$this->sysParams('id')),$this->newrow2)){
					message_a("会员修改成功","?c=ld_member");
				}else{message_a("会员附表数据修改失败，请重新提交");}
				*/
		}
		$this->toptxt='修改会员';
		$this->postgo='edit';
		$this->display("member.html");
	}
	function del(){
		$this->toptxt='删除会员';
		$this->delarray=$this->cu->find(array('id'=>$this->sysParams('id')));
		$id=$this->delarray['id'];
		if ($this->sysParams('run')==1){
			/*
			if($this->cu->delete(array('id'=>$id))&&syDB('member_field')->delete(array('aid'=>$id)))
			{message_a("会员删除成功","?c=ld_member");}else{message_a("会员删除失败,请重新提交");}
			*/
		}
		$this->msgtitle='确定要删除会员 <strong>['.$this->delarray['user'].']</strong> 吗？';
		$this->msg='';
		$this->msggo='<a href="?c=ld_member&a=del&run=1&id='.$id.'">确定删除</a><a href="?c=ld_member">取消操作</a>';
		$this->display("msg.html");
	}
	function glist(){
		$this->toptxt='会员分组管理';
		$this->lists = syDB('member_group')->findAll();
		$this->postgo='glist';
		$this->display("member.html");
	}
	function gadd(){
		if ($this->sysParams('run')==1){
			if(!$this->sysParams('name',1)){message_a("分组名称不能为空");}
			if(syDB('member_group')->create($this->newrow)){
				message_a("会员分组添加成功","?c=ld_member&a=glist");
			}else{message_a("会员分组添加失败，请重新提交");}
		}
		$this->toptxt='添加会员分组';
		$this->postgo='gadd';
		$this->display("member.html");
	}
	function gedit(){
		$this->d=syDB('member_group')->find(array('gid'=>$this->sysParams('gid')));
		if ($this->sysParams('run')==1){
			if(!$this->sysParams('name',1)){message_a("分组名称不能为空");}
			if(syDB('member_group')->update(array('gid'=>$this->sysParams('gid')),$this->newrow)){
				message_a("会员分组修改成功","?c=ld_member&a=glist");
			}else{message_a("会员分组修改失败，请重新提交");}
		}
		$this->toptxt='修改会员分组';
		$this->postgo='gedit';
		$this->display("member.html");
	}
	function gdel(){
		$this->toptxt='删除会员分组';
		if($this->sysParams('gid')==1)message_a("系统分组，禁止删除");
		$this->delarray=syDB('member_group')->find(array('gid'=>$this->sysParams('gid')));
		$gid=$this->delarray['gid'];
		if ($this->sysParams('run')==1){
			if(syDB('member_group')->delete(array('gid'=>$gid))){
				syDB('member')->update(array('gid'=>$gid),array('gid'=>1));
				message_a("会员分组删除成功","?c=ld_member&a=glist");
			}else{message_a("会员分组删除失败,请重新提交");}
		}
		$this->msgtitle='确定要删除会员分组 <strong>['.$this->delarray['name'].']</strong> 吗？';
		$this->msg='删除此分组后，此分组下所有会员自动转移自系统初级分组';
		$this->msggo='<a href="?c=ld_member&a=gdel&run=1&gid='.$gid.'">确定删除</a><a href="?c=ld_member&a=glist">取消操作</a>';
		$this->display("msg.html");
	}
}	