<?php
require_once '../phpbean/News.class.php';
require_once '../service/NewsService.class.php';

//创建了NewsService实例
$newsService = new NewsService();

//根据action参数决定用户要执行什么样的操作
if(!empty($_REQUEST['action'])) {
	//接收action值
	$action = $_REQUEST['action'];
	if($action == "add") {
		//说明用户希望执行添加新闻信息信息
		$news = new News();
		$array_news = $_POST["news"];
		/*处理图片上传*/
		require_once("../util/upload.class.php");
		require_once("../util/util.php");

		$news->setNewsId($array_news['newsId']);
		$news->setNewsTitle($array_news['newsTitle']);
		$news->setNewsContent($array_news['newsContent']);
		$news->setNewsDate($array_news['newsDate']);
		$newsPhoto = "/upload/NoImage.jpg";
		if ($_FILES['newsPhoto']['name'] != ''){
			/*--  实例化上传类  --*/
			$file = $_FILES['newsPhoto'];
			$upload_path = '../upload';
			$allow_type = array('jpg','bmp','png','gif','jpeg');
			$max_size=2048000;
			$upload = new upFiles($file, $upload_path, $max_size, $allow_type);
			$upload->upload();
			$pic = $upload->getSaveFileInfo();
			$newsPhoto = substr($pic['path'], 2)."/".$pic['savename'];
		}
		$news->setNewsPhoto($newsPhoto);


		//完成添加->数据库
		$res = $newsService->AddNews($news);
		if($res==1) {
			header("Location: /ok.php");
			exit();
		} else {
			//失败
			header("Location: /error.php");
			exit();
		}
	} else if($action == "query") {
		//查询新闻信息信息
		require_once '../service/NewsService.class.php';
		require_once '../service/FenyePage.class.php';
		//获取查询参数
		$newsTitle = !empty($_POST['newsTitle']) ? $_POST['newsTitle'] : "";
		$newsDate = !empty($_POST['newsDate']) ? $_POST['newsDate'] : "";
		//创建NewsService实例
		$newsService = new NewsService();

		//创建一个FenyePage对象实例
		$fenyePage = new FenyePage();
		//给fenyePage指定必须的参数
		$fenyePage->pageNow = 1;
		$fenyePage->pageSize = 5;
		$fenyePage->gotoUrl = "NewsAction.php";

		//在此我们需要根据用户的点击来修改pageNow的值，不要判断是否有这个pageNow发送
		if(!empty($_REQUEST['pageNow'])){
			$fenyePage->pageNow=$_REQUEST['pageNow'];
		}

		//调用getFenyePage，该方法可以把fenyePage完成
		$newsService->getFenyePage($fenyePage,$newsTitle,$newsDate);
		include '../News/newsManage.php';
	} else if($action == "frontQuery") {
		//查询新闻信息信息
		require_once '../service/NewsService.class.php';
		require_once '../service/FenyePage.class.php';
		//获取查询参数
		$newsTitle = !empty($_POST['newsTitle']) ? $_POST['newsTitle'] : "";
		$newsDate = !empty($_POST['newsDate']) ? $_POST['newsDate'] : "";
		//创建NewsService实例
		$newsService = new NewsService();

		//创建一个FenyePage对象实例
		$fenyePage = new FenyePage();
		//给fenyePage指定必须的参数
		$fenyePage->pageNow = 1;
		$fenyePage->pageSize = 5;
		$fenyePage->gotoUrl = "NewsAction.php";

		//在此我们需要根据用户的点击来修改pageNow的值，不要判断是否有这个pageNow发送
		if(!empty($_REQUEST['pageNow'])){
			$fenyePage->pageNow=$_REQUEST['pageNow'];
		}

		//调用getFenyePage，该方法可以把fenyePage完成
		$newsService->getFenyePage($fenyePage,$newsTitle,$newsDate);
		include '../News/newsFrontQuery.php';
	} else if($action == "update") {
		//说明用户希望执行更新新闻信息信息
		$array_news = $_POST["news"];
		$news = $newsService->GetNews($array_news['newsId']);
		/*处理图片上传*/
		require_once("../util/upload.class.php");
		require_once("../util/util.php");

		$news->setNewsId($array_news['newsId']);
		$news->setNewsTitle($array_news['newsTitle']);
		$news->setNewsContent($array_news['newsContent']);
		$news->setNewsDate($array_news['newsDate']);
		if ($_FILES['newsPhoto']['name'] != ''){
			/*--  实例化上传类  --*/
			$file = $_FILES['newsPhoto'];
			$upload_path = '../upload';
			$allow_type = array('jpg','bmp','png','gif','jpeg');
			$max_size=2048000;
			$upload = new upFiles($file, $upload_path, $max_size, $allow_type);
			$upload->upload();
			$pic = $upload->getSaveFileInfo();
			$newsPhoto = substr($pic['path'], 2)."/".$pic['savename'];
		  $news->setNewsPhoto($newsPhoto);
		}

		//完成修改->数据库
		$res = $newsService->UpdateNews($news);
		if($res != 0) {
			header("Location: /ok.php");
			exit();
		} else {
			//失败
			header("Location: /error.php");
			exit();
		}
	} else if($action == "del") {
		//这时我们知道要删除新闻信息信息
		$newsId = $_GET['newsId'];
		if($newsService->DeleteNews($newsId) == 1) {
			//成功
			header("Location: /ok.php");
			exit();
		} else {
			//失败
			header("Location: /error.php");
			exit();
		}
	}
}
?>
