<?php
if(!defined('APP_PATH')||!defined('I_PATH')){exit('Access Denied');}
class ld_sysconfig extends controllerBase
{
	function __construct(){
		parent::__construct();
		$this->gopage=$this->sysParams('page',0,1);
		$this->molds = 'article';
		$this->sqldb=func_get_iglobal(array('db','prefix')).$this->molds;
		$this->moldname=moldsinfo($this->molds,'moldname');
		$this->ClassADD = syClass('ldi_'.$this->molds);
		$this->chtml = syClass("syhtml");
		$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(funsinfo('member','isshow')==1){
		$this->member_group = syDB('member_group')->findAll(null,'weight');
		}
		$this->traits=syDB('traits')->findAll(array('molds'=>$this->molds));
		if(funsinfo('special','isshow')==1){
			$this->specials=syDB('special')->findAll(array('molds'=>$this->molds,'isshow'=>1));
		}
		$this->gocid=$this->sysParams('cid');
		if(($this->sysParams('a',1)=='add'||$this->sysParams('a',1)=='edit')&&$this->sysParams('run')==1){
			$addtime=strtotime($this->sysParams('addtime',1));
			$style=$this->sysParams('style1',1).$this->sysParams('style2',1);
			if($this->sysParams('trait',2)){$trait=','.implode(',',$this->sysParams('trait',2)).',';}else{$trait='';}
			if($this->sysParams('tid')){$tid=$this->sysParams('tid');}
			$this->newrow1 = array(
				'tid' => $tid,
				'sid' => $this->sysParams('sid'),
				'title' => $this->sysParams('title',1),
				'style' => $style,
				'trait' => $trait,
				'gourl' => $this->sysParams('gourl',1),
				'htmlfile' => strtolower($this->sysParams('htmlfile',1)),
				'htmlurl' => '',
				'addtime' => $addtime,
				'hits' => $this->sysParams('hits'),
				'litpic' => $this->sysParams('litpic',1),
				'orders' => $this->sysParams('orders'),
				'mrank' => $this->sysParams('mrank'),
				'mgold' => $this->sysParams('mgold',3),
				'isshow' => $this->sysParams('isshow'),
				'keywords' => $this->sysParams('keywords',1),
				'description' => $this->sysParams('description',1),
			);
			if($this->sysParams('a',1)=='add'){$this->newrow1=array_merge($this->newrow1,array('user' => $_SESSION['auser']['auser']));}
			$this->newrow2 = array(
				'body' => $this->sysParams('body',4),
			);
			$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 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');
		$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->sysParams('all_down_images')==1){
						$upall=syClass('syupload');
						if(!get_magic_quotes_gpc()){$bodynew=$this->newrow2['body'];}else{$bodynew=stripslashes($this->newrow2['body']);}
						preg_match_all('/<img.*?src=[\'|\"](.*?)[\'|\"].*?\/>/si',$bodynew,$pic);
						$pic=array_unique($pic[1]);
						foreach($pic as $v){
							$localUrl=$upall->saveRemoteImg(trim($v));
							if($localUrl){$bodynew=str_ireplace($v,$GLOBALS['WWW'].$localUrl,$bodynew);}
						}
						$this->newrow2=array_merge($this->newrow2,array('body'=>$bodynew));
					}
					if(!syDB($this->molds.'_field')->find(array('aid'=>$this->sysParams('id')))){
						$this->newrow2=array_merge($this->newrow2,array('aid'=>$this->sysParams('id')));
						$edit_field=syDB($this->molds.'_field')->create($this->newrow2);
					}else{
						$edit_field=syDB($this->molds.'_field')->update(array('aid'=>$this->sysParams('id')),$this->newrow2);
					}
					if($edit_field){
						if(func_get_iglobal(array('rewrite','rewrite_open'))!=1&&syExt('site_html')==1&&$this->newrow1['isshow']==1&&$this->newrow1['mrank']==0&&$this->newrow1['mgold']==0){
							$c_html_f=html_rules($this->newrow1['addtime'],$this->sysParams('id'),$this->newrow1['htmlfile']);
							syDB($this->molds)->updateField(array('id'=>$this->sysParams('id')),'htmlurl',$c_html_f);
							$this->chtml->c_molds('article',array('id'=>$this->sysParams('id')),$c_html_f);
							$body=array_filter(explode("[i|page]",$this->newrow2['body']));
							$allb=count($body);
							if($allb>1){
								for ($i = 1; $i <= $allb; $i++) {
									if($i>1){
									$this->chtml->c_molds('article',array('id'=>$this->sysParams('id'),'page'=>$i),str_replace('.',$i.'.',$c_html_f));
									}
								}
							}
						}
						deleteDir(func_get_iglobal(array('sp_cache')));
						message_a($this->moldname."修改成功","?c=ld_".$this->molds);
					}else{message_a("附表数据修改失败，请重新提交");}
				}else{message_b($newVerifier);}
		}
		$this->traits=syDB('traits')->findAll(array('molds'=>$this->molds));
		$this->toptxt=$this->moldname.'修改内容';
		$this->postgo='edit';
		$this->display($this->molds."_edit.html");
	}
	
}	