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

		$title=MiceFilterHtml($_POST['title']);
		$parentid=$_POST['parentid'];
		$type=$_POST['type'];
		$orderid=$_POST['orderid'];
		$summary=MiceFilterQuote($_POST['summary']);
		$img=MiceFilterQuote($_POST['img']);
		$isvisible=$_POST['isvisible'];

		if($title==""){
			$jsstring='Dialog.alert("分类名称不能为空",function(){history.back(-1);});';
			MessageBox($jsstring);
			exit;
		}
		if(strlen($img)<1){
			$img=MiceFilterQuote(GetImgUrl(MiceStripslashes($_POST['summary'])));
		}

		if($results)
		{
			$ins='内容已存在';
			$cid=$results->getCategoryId();
			$category->setTitle($title);
			$category->setParentid($parentid);
			$category->setOrderid($orderid);
			$category->setSummary($summary);
			$category->setImg($img);
			$category->setIsvisible($isvisible);
			$category->setType($type);
			$results=$category->doUpdate();
			if($results)
			{
				$exeresult='更新成功';					
				$newParentid=',';
				if($category->getParentid()){
					$category3=new model_Category();
					$category3->setSubsection('WHERE category_id='.$category->getParentid());
					$results3=$category3->doSelectOne();
					if($results3->getPath())	$newParentid=$results3->getPath();
				}
				$newParentid.=$category_id.',';
				$depth=count(explode(",",$newParentid))-2;
				$category->setPath($newParentid);
				$category->setDepth($depth);
				$category->doUpdate();

				$article=new model_Article();
				$article->setSubsection('WHERE category_id=\''.$newParentid.'|\'');
				$article->setType($type);
				$article->doUpdate();
			}else{
				$exeresult='更新失败';
			}

		}else{
			$ins='内容已不存在';
			$category=new model_Category();
			$category->setTitle($title);
			$category->setParentid($parentid);
			$category->setOrderid($orderid);
			$category->setSummary($summary);
			$category->setImg($img);
			$category->setIsvisible($isvisible);
			$category->setType($type);
			$results=$category->doInsert();
			if($results)
			{
				$cid=$conn->Insert_ID();
				$exeresult='插入成功';
				if($conn->Insert_ID()){
					$newParentid=',';
					$category2=new model_Category();
					$category2->setSubsection('WHERE category_id='.$conn->Insert_ID());
					$results2=$category2->doSelectOne();

					if($parentid){
						$category3=new model_Category();
						$category3->setSubsection('WHERE category_id='.$parentid);
						$results3=$category3->doSelectOne();
						if($results3->getPath())	$newParentid=$results3->getPath();
					}
					$newParentid.=$results2->getCategoryId().',';
					$depth=count(explode(",",$newParentid))-2;
					$category2->setPath($newParentid);
					$category2->setDepth($depth);
					$category2->doUpdate();
				}
			}else{
				$exeresult='插入失败';
			}
		}

		

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