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

class ld_message extends controllerBase
{
	function __construct(){
		parent::__construct();
		$this->gopage=$this->sysParams('page',0,1);
		$this->molds = 'message';
		$this->sqldb= func_get_iglobal(array('db','prefix')).$this->molds;
		$this->moldname=moldsinfo($this->molds,'moldname');
		$this->ClassADD = syClass('ldi_'.$this->molds);
		$this->ClassT=syClass('ldi_classtype');
		$this->ClassF=syClass('ldi_fields');
		$this->auser=syClass('syauser');
		$classtype=syDB('classtype')->findAll(array('molds'=>$this->molds),null,'tid,classname,pid,molds');
		$this->types=syClass('syclasstype');
		$this->typesdb=$this->types->type_txt();		
		if($this->sysParams('a',1)=='edit'&&$this->sysParams('run')==1){
			$retime=strtotime($this->sysParams('retime',1));
			$this->newrow1 = array(
				'tid' => $this->sysParams('tid'),
				'user' => $this->sysParams('user',1),
				'title' => $this->sysParams('title',1),
				'retime' => $retime,
				'orders' => $this->sysParams('orders'),
				'isshow' => $this->sysParams('isshow'),
				'body' => $this->sysParams('body',1),
				'feedtime' => time(),
				'feedback' => $this->sysParams('feedback',1),
				'feed_admin' => $_SESSION['auser']['auser'],
			);
			
			$this->newrow2 = array();
			$v=$this->ClassF->findAll(" molds='".$this->molds."' and types like '%|".$this->sysParams('tid')."|%' ");
			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);
			}
		}
	}
	function index(){
		$this->toptxt=$this->moldname.'列表';
		if($this->sysParams('isshow')){
			if($this->sysParams('isshow')==1){$this->toptxt='已审'.$this->toptxt;}else{$this->toptxt='待审'.$this->toptxt;}
			$conditions.="and isshow=".$this->sysParams('isshow')." ";
		}
		if($_SESSION['auser']['level']==1){
			if($this->sysParams("tid")){$conditions.= "and tid in(".$this->types->leafid($this->sysParams('tid')).") ";}
		}else{
			if($this->sysParams("tid") && $this->auser->checkclass($this->sysParams("tid"))){$conditions.= "and tid in(".$this->types->leafid($this->sysParams('tid')).") and tid in(".trim($_SESSION['auser']['pclasstype'],',').") ";}
			else{if($this->auser->checkclass()==false)message_a("您没有栏目权限");$conditions.= "and tid in(".trim($_SESSION['auser']['pclasstype'],',').") ";}
		}
		if($this->sysParams("title",1)){$conditions.= "and title like '%".$this->sysParams('title',1)."%' ";}
		if($conditions!=''){$conditions=' where '.substr($conditions,3);}
		$sql='select * from '.$this->sqldb.$conditions.' order by orders desc,addtime desc,id desc';
		$this->listarray = $this->ClassADD->syPager($this->gopage,15)->findSql($sql); 
		$this->pages = pagetxt($this->ClassADD->syPager()->getPager());
		$this->display($this->molds.".html");
	}
	function edit(){
		$this->carray=$this->ClassADD->findSql('select * from '.$this->sqldb.' a left join '.$this->sqldb.'_field b on (a.id=b.aid) where id='.$this->sysParams('id').' limit 0,1');		
		//$_SESSION['auser']['auser']
		$this->carray=$this->carray[0];
		if(!$this->auser->checkclass($this->carray['tid'])){message_a("无权操作本栏目内容");}
		if ($this->sysParams('run')==1){
			$newVerifier=$this->ClassADD->syVerifier($this->newrow1);
				if(false == $newVerifier){
					
					if($this->ClassADD->update(array('id'=>$this->sysParams('id')),$this->newrow1)==FALSE)
					{message_a("主表数据修改失败，请重新提交");}
					if(!$this->newrow2){message_a("修改成功","?c=ld_".$this->molds);}
					if(syDB($this->molds.'_field')->update(array('aid'=>$this->sysParams('id')),$this->newrow2)){
						message_a($this->moldname."修改成功","?c=ld_".$this->molds);
					}else{message_a("附表数据修改失败，请重新提交");}
				}else{message_b($newVerifier);}
		}
		$this->toptxt=$this->moldname.'修改内容';
		$this->postgo='edit';
		$this->display($this->molds."_edit.html");
	}
	function del(){
		$this->toptxt=$this->moldname.'删除内容';
		$this->del=$this->ClassADD->find(array('id'=>$this->sysParams('id')));
		if(!$this->auser->checkclass($this->del['tid'])){message_a("无权操作本栏目内容");}
		$id=$this->del['id'];
		if ($this->sysParams('run')==1){
			if(syDB($this->molds)->delete(array('id'=>$id))&&syDB($this->molds.'_field')->delete(array('aid'=>$id)))
			{message_a("删除成功","?c=ld_".$this->molds);}else{message_a("删除失败,请重新提交");}
		}
		$this->msgtitle='确定要删除 <strong>['.$this->del['title'].']</strong> 吗？';
		$this->msg='';
		$this->msggo='<a href="?c=ld_'.$this->molds.'&a=del&run=1&id='.$id.'">确定删除</a><a href="?c=ld_'.$this->molds.'">取消操作</a>';
		$this->display("msg.html");
	}
	function alledit(){
		if($this->sysParams('ids',2)===NULL || $this->sysParams('go')===NULL){message_a("请选择操作对象");}
		if($this->sysParams('run')==1){
			$ids=explode(',',$this->sysParams('ids',1));
			switch ($this->sysParams('go')) {
				case 0:
					foreach($ids as $tp){
						$a=$this->ClassADD->find(array('id'=>$tp), null, " id,tid ");
						if($this->auser->checkclass($a['tid']))$this->ClassADD->update(array('id'=>$tp),array('isshow'=>1));
					}
					$this->jump("?c=ld_".$this->molds);
					break;
				case 1:
					foreach($ids as $tp){
						$a=$this->ClassADD->find(array('id'=>$tp), null, " id,tid ");
						if($this->auser->checkclass($a['tid']))$this->ClassADD->update(array('id'=>$tp),array('isshow'=>0));
					}
					$this->jump("?c=ld_".$this->molds);
					break;
				case 2:
					foreach($ids as $tp){
						$a=$this->ClassADD->find(array('id'=>$tp), null, " id,tid ");
						if($this->auser->checkclass($a['tid'])){
							syDB($this->molds)->delete(array('id'=>$tp));
							syDB($this->molds.'_field')->delete(array('aid'=>$tp));
						}
					}
					$this->jump("?c=ld_".$this->molds);
					break;
			}
		}
		if($this->sysParams('go')==0)$txt='审核';
		if($this->sysParams('go')==1)$txt='取消审核';
		if($this->sysParams('go')==2)$txt='删除';
		$this->toptxt='批量'.$txt.$this->moldname.'内容';
		$ids=implode(',',$this->sysParams('ids',2));
		$this->msgtitle='确定要'.$txt.'这些'.$this->moldname.'内容吗？';
		$this->msg='本操作将批量'.$txt.'ID为['.$ids.']的内容<br>';
		$this->msggo='<a href="?c=ld_'.$this->molds.'&a=alledit&run=1&ids='.$ids.'&go='.$this->sysParams('go').'&tid='.$this->sysParams('tid').'">确定'.$toptxt.'</a><a href="?c=ld_'.$this->molds.'">取消操作</a>';
		$this->display("msg.html");
	}
}	