<?php
/**
 *	Generated by miceCMS
 *	http://www.miceCMS.com/
 */
class article_updateDownAction extends security_login {
	function action(){
		ModuleLimit(__CLASS__);
		global $conn;
		$article_id=$_POST['article_id'];
		if($article_id){
			$article=new model_Article();
			$article->setSubsection('WHERE article_id='.$article_id);
			$results=$article->doSelectOne();
		}
		
		$miceSession = new session();
		$user_id=$miceSession->get('userid');
		$title=MiceFilterHtml($_POST['title']);
		$createdat=$_POST['cdate'].' '.$_POST['ctime'];
		$createdat=Time2Num($createdat);
		$comefrom=$_POST['comefrom'];
		$upfile=MiceFilterQuote($_POST['upfile']);
		$img=MiceFilterQuote($_POST['img']);
		$url=MiceFilterQuote($_POST['url']);
		$target=$_POST['target'];
		$tags=MiceFilterComma($_POST['tags']);
		$content=MiceFilterQuote($_POST['content']);
		$summary=MiceFilterQuote($_POST['summary']);
		$userusergroup_id=$_POST['usergroup_id'];
		$pointnum=$_POST['pointnum'];
		$istop=$_POST['istop'];
		$iscommend=$_POST['iscommend'];
		$isvisible=$_POST['isvisible'];
		$browses=$_POST['browses'];
		$filename=$_POST['filename'];
		$ip=GetRealIp();

		$category_id="";
		foreach($_POST['CategoryIdS'] as $ciditem){
			$category_id.=$ciditem;
		}

		$cid=CategoryId('micefun',$category_id);

		if($title==""){
			$jsstring='Dialog.alert("文章标题不能为空",function(){history.back(-1);});';
			MessageBox($jsstring);
			exit;
		}
		if(strlen($img)<1){
			$img=MiceFilterQuote(GetImgUrl(MiceStripslashes($_POST['content'])));
		}

		if($upfile==""){
			$jsstring='Dialog.alert("下载文件不能为空",function(){history.back(-1);});';
			MessageBox($jsstring);
			exit;
		}

		if($category_id==""){
			$jsstring='Dialog.alert("至少要选择一个分类",function(){history.back(-1);});';
			MessageBox($jsstring);
			exit;
		}

		if($results)
		{
			//改变文档所属的栏目的ishtml属性
			$Cids=explode(',',$results->getCategoryId());
			foreach($Cids as $CategoryId){
				if(is_numeric($CategoryId)){
					$category2=new model_Category();
					$category2->setSubsection('WHERE ishtml=1 AND category_id='.$CategoryId);
					$results2=$category2->doSelectOne();
					if($results2){
						$category2->setIshtml(0);
						$results3=$category2->doUpdate();
					}
				}
			}
			if($category_id!=$results->getCategoryId()){
				$Cids=explode(',',$category_id);
				foreach($Cids as $CategoryId){
					if(is_numeric($CategoryId)){
						$category2=new model_Category();
						$category2->setSubsection('WHERE ishtml=1 AND category_id='.$CategoryId);
						$results2=$category2->doSelectOne();
						if($results2){
							$category2->setIshtml(0);
							$results3=$category2->doUpdate();
						}
					}
				}
			}

			$ins='内容已存在';
			$aid=$results->getArticleId();
			$article->setCategoryId($category_id);
			$article->setUserId($user_id);
			$article->setTitle($title);
			$article->setUpfile($upfile);
			$article->setCreatedat($createdat);
			$article->setComefrom($comefrom);
			$article->setImg($img);
			$article->setContent($content);
			$article->setSummary($summary);
			$article->setTags($tags);
			$article->setPointnum($pointnum);
			$article->setUsergroupId($userusergroup_id);
			$article->setIstop($istop);
			$article->setIscommend($iscommend);
			$article->setIsvisible($isvisible);
			$article->setBrowses($browses);
			$article->setIp($ip);
			$article->setUrl($url);
			$article->setTarget($target);
			$article->setFilename($filename);
			$article->setIshtml($category_id);
			$article->setType('article2');
			$results=$article->doUpdate();
			if($results)
				$exeresult='更新成功';
			else
				$exeresult='更新失败';
		}else{
			//改变文档所属的栏目的ishtml属性
			$Cids=explode(',',$category_id);
			foreach($Cids as $CategoryId){
				if(is_numeric($CategoryId)){
					$category2=new model_Category();
					$category2->setSubsection('WHERE ishtml=1 AND category_id='.$CategoryId);
					$results2=$category2->doSelectOne();
					if($results2){
						$category2->setIshtml(0);
						$results3=$category2->doUpdate();
					}
				}
			}

			$ins='内容已不存在';
			$article=new model_Article();
			$article->setCategoryId($category_id);
			$article->setUserId($user_id);
			$article->setTitle($title);
			$article->setUpfile($upfile);
			$article->setCreatedat($createdat);
			$article->setComefrom($comefrom);
			$article->setImg($img);
			$article->setContent($content);
			$article->setSummary($summary);
			$article->setTags($tags);
			$article->setPointnum($pointnum);
			$article->setUsergroupId($userusergroup_id);
			$article->setIstop($istop);
			$article->setIscommend($iscommend);
			$article->setIsvisible($isvisible);
			$article->setBrowses($browses);
			$article->setIp($ip);
			$article->setUrl($url);
			$article->setTarget($target);
			$article->setFilename($filename);
			$article->setIshtml($category_id);
			$article->setType('article2');
			$results=$article->doInsert();
			if($results){
				$exeresult='插入成功';
				$aid=$conn->Insert_ID();
				//将组图里的临时ID改成真实ID
				$file=new model_File();
				$file->setSubsection('WHERE filecategory_id='.$_POST['maimg_id']);
				$file->setFilecategoryId($aid);
				$file->doUpdate();
				UpPostnums('micefun',$user_id);		//文章数+1
			}else
				$exeresult='插入失败';
		}

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