<?php
/**
 *	Generated by miceCMS
 *	http://www.miceCMS.com/
 */
class filecategory_updateAction extends security_login {
	function action(){
		ModuleLimit(__CLASS__);
		global $conn;
		$filecategory_id=$_POST['filecategory_id'];
		if($filecategory_id){
			$filecategory=new model_Filecategory();
			$filecategory->setSubsection('WHERE filecategory_id='.$filecategory_id);
			$results=$filecategory->doSelectOne();
		}

		$title=MiceFilterHtml($_POST['title']);
		$parentid="";
		foreach($_POST['FilecategoryIdS'] as $ciditem){
			$parentid=$ciditem;
		}

		$type=$_POST['type'];
		$orderid=$_POST['orderid'];
		$summary=MiceFilterQuote($_POST['summary']);

		if($title==""){
			$jsstring='Dialog.alert("分类名称不能为空",function(){history.back(-1);});';
			MessageBox($jsstring);
			exit;
		}
		if($parentid==""){
			$jsstring='Dialog.alert("上级分类不能为空",function(){history.back(-1);});';
			MessageBox($jsstring);
			exit;
		}

		if($results)
		{
			$ins='内容已存在';
			$cid=$results->getFilecategoryId();
			$filecategory->setTitle($title);
			$filecategory->setParentid($parentid);
			$filecategory->setOrderid($orderid);
			$filecategory->setSummary($summary);
			$filecategory->setType($type);
			$results=$filecategory->doUpdate();
			if($results)
			{
				$exeresult='更新成功';					
				$newParentid=',';
				if($filecategory->getParentid()){
					$filecategory3=new model_Filecategory();
					$filecategory3->setSubsection('WHERE filecategory_id='.$filecategory->getParentid());
					$results3=$filecategory3->doSelectOne();
					if($results3->getPath())	$newParentid=$results3->getPath();
				}
				$newParentid.=$filecategory_id.',';
				$depth=count(explode(",",$newParentid))-2;
				$filecategory->setPath($newParentid);
				$filecategory->setDepth($depth);
				$filecategory->doUpdate();

				//新上级分类的childcount+1,原上级分类的childcount-1
				if($parentid!=$filecategory->getParentid()){
					$filecategory3=new model_Filecategory();
					$filecategory3->setSubsection('WHERE filecategory_id='.$filecategory->getParentid());
					$results3=$filecategory3->doSelectOne();
					$filecategory3->setChildcount($results3->getChildcount()-1);
					$filecategory3->doUpdate();

					$filecategory3=new model_Filecategory();
					$filecategory3->setSubsection('WHERE filecategory_id='.$parentid);
					$results3=$filecategory3->doSelectOne();
					$filecategory3->setChildcount($results3->getChildcount()+1);
					$filecategory3->doUpdate();
				}
			}else{
				$exeresult='更新失败';
			}

		}else{
			$ins='内容已不存在';
			$filecategory=new model_Filecategory();
			$filecategory->setTitle($title);
			$filecategory->setParentid($parentid);
			$filecategory->setOrderid($orderid);
			$filecategory->setSummary($summary);
			$filecategory->setType($type);
			$results=$filecategory->doInsert();
			if($results)
			{
				$cid=$conn->Insert_ID();
				$exeresult='插入成功';
				if($conn->Insert_ID()){
					$newParentid=',';
					$filecategory2=new model_Filecategory();
					$filecategory2->setSubsection('WHERE filecategory_id='.$conn->Insert_ID());
					$results2=$filecategory2->doSelectOne();

					if($parentid){
						$filecategory3=new model_Filecategory();
						$filecategory3->setSubsection('WHERE filecategory_id='.$parentid);
						$results3=$filecategory3->doSelectOne();
						if($results3->getPath())	$newParentid=$results3->getPath();
					}
					$newParentid.=$results2->getFilecategoryId().',';
					$depth=count(explode(",",$newParentid))-2;
					$filecategory2->setPath($newParentid);
					$filecategory2->setDepth($depth);
					$filecategory2->doUpdate();
				}

				//上级分类的childcount+1
				if($parentid){
					$filecategory3=new model_Filecategory();
					$filecategory3->setSubsection('WHERE filecategory_id='.$parentid);
					$results3=$filecategory3->doSelectOne();
					$filecategory3->setChildcount($results3->getChildcount()+1);
					$filecategory3->doUpdate();
				}
			}else{
				$exeresult='插入失败';
			}
		}

		if($_POST['goon']==1){
			$jsstring='Dialog.alert(\''.$exeresult.'\',function(){window.location.href="'.MICE_URL.'admin.php/filecategory/edit";window.event.returnValue = false;},300,100,"继续添加分类");';
			MessageBox($jsstring);
		}else{
			$jsstring='Dialog.alert(\''.$exeresult.'\',function(){window.location.href="'.MICE_URL.'admin.php/filecategory";window.event.returnValue = false;},300,100,"跳转到分类列表");';
			MessageBox($jsstring);
		}
		exit;
	}
}
?>