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

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

		if($title==""){
			$jsstring='Dialog.alert("菜单名不能为空",function(){history.back(-1);});';
			MessageBox($jsstring);
			exit;
		}

		if($img=="" AND $parentid>0){
			$jsstring='Dialog.alert("请给二级菜单选择一个图标",function(){history.back(-1);});';
			MessageBox($jsstring);
			exit;
		}

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

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

					$category3=new model_Menu();
					$category3->setSubsection('WHERE menu_id='.$parentid);
					$results3=$category3->doSelectOne();
					$category3->setChildcount($results3->getChildcount()+1);
					$category3->doUpdate();
				}
			}else{
				$exeresult='更新失败';
			}
		}else{
			$ins='内容已不存在';
			$category=new model_Menu();
			$category->setTitle($title);
			$category->setParentid($parentid);
			$category->setOrderid($orderid);
			$category->setSummary($summary);
			$category->setIsvisible($isvisible);
			$category->setTags($tags);
			$category->setImg($img);
			$category->setUrl($url);
			$results=$category->doInsert();
			if($results)
			{
				$cid=$conn->Insert_ID();
				$exeresult='插入成功';
				if($conn->Insert_ID()){
					$newParentid=',';
					$category2=new model_Menu();
					$category2->setSubsection('WHERE menu_id='.$conn->Insert_ID());
					$results2=$category2->doSelectOne();

					if($parentid){
						$category3=new model_Menu();
						$category3->setSubsection('WHERE menu_id='.$parentid);
						$results3=$category3->doSelectOne();
						if($results3->getPath())	$newParentid=$results3->getPath();
					}
					$newParentid.=$results2->getMenuId().',';
					$depth=count(explode(",",$newParentid))-2;
					$category2->setPath($newParentid);
					$category2->setDepth($depth);
					$category2->doUpdate();
				}
				//上级分类的childcount+1
				if($parentid){
					$category3=new model_Menu();
					$category3->setSubsection('WHERE menu_id='.$parentid);
					$results3=$category3->doSelectOne();
					$category3->setChildcount($results3->getChildcount()+1);
					$category3->doUpdate();
				}
			}else{
				$exeresult='插入失败';
			}
		}

		

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