<?php
defined('IN_YUNYECMSAdmin') or exit('No permission.');
core::load_admin_class('admin');
class replay extends YUNYE_admin
{
	function __construct()
	{
		parent::__construct();
		$this->db = core::load_model('common_model', 'replay');
	}
	//加载首页
	public function init()
	{
		$parnav = '<li class=\"breadcrumb-item\"><a href=\"' . url_admin('init', "replay") . '\" target=\"maincontent\">系统</a></li><li class=\"breadcrumb-item\"><a href=\"' . url_admin('init', 'replay') . '\" target=\"maincontent\">回复管理</a></li><li class=\"breadcrumb-item active\">回复列表</li>';

		$pagesize = 20;
		$sqlquery = "select * from `#yunyecms_replay`  ";
		$where = " where 1=1 and content<>'' ";
		$sqlcnt = " select count(*) from `#yunyecms_replay` ";
		$order = " order by `addtime` desc ";

		if (isset($_REQUEST)) {
			if (!empty($_REQUEST["searchkey"])) {
				$searchkey = usafestr(trim($_REQUEST["searchkey"]));
				$where = $where . " and (`title`  like '%{$searchkey}%' )";
			}
			if (!empty($_REQUEST["plid"])) {
				$plid = intval(trim($_REQUEST["plid"]));
				$where = $where . " and ( `plid`  = '{$plid}' )";
			}
			if (!empty($_REQUEST["catid"])) {
				$catid = intval(trim($_REQUEST["catid"]));
			}
			if (!empty($_REQUEST["infoid"])) {
				$infoid = intval(trim($_REQUEST["infoid"]));
				$where = $where . " and ( `infoid`  = '{$infoid}' )";
			}
		}

		if ($plid) $ccomment = $this->db->getbyid($plid, 'comment');
		$infoid = $ccomment['infoid'];
		$breadcumb = getbreadcumb($catid);
		$breadcumbstr = "";
		foreach ($breadcumb as $kk => $vv) {
			$breadcumbstr = $breadcumbstr . '<li><a href=\"' . url_admin('lists', 'content', array("catid" => $vv['id'])) . '\" target=\"maincontent\">' . $vv['title'] . '</a></li>';
		}
		$parnav = $breadcumbstr . '<li><a href=\"' . url_admin('init', 'comment', array('id' => $infoid, 'catid' => $catid)) . '\" target=\"maincontent\">评论</a></li><li><a href=\"' . url_admin('init', 'replay', array('plid' => $plid, 'catid' => $catid)) . '\" target=\"maincontent\">回复</a></li>';
		$pagearr = $this->db->pagelist($sqlcnt, $sqlquery, $where, $order, $pagesize);
		if ($pagearr["count"] != 0) {
			$list = $pagearr["query"];
			foreach ($list as $key => $var) {
				$list[$key]["info"] = GetContentById($var['infoid'], $var['catid']);
				$list[$key]["comment"] = $this->db->getbyid($var['plid'], "comment");
				$list[$key]["member"] = $this->db->getbyid($var['userid'], "member");
			}
			$page = $pagearr["page"];
		}
		require tpl_adm('replay_list');
	}





	public function add()
	{
		if (!empty($_REQUEST["plid"])) {
			$plid = intval(trim($_REQUEST["plid"]));
			$ccomment = $this->db->getbyid($plid, 'comment');
			if (empty($ccomment)) {
				messagebox("该评论不存在", $_SERVER['HTTP_REFERER']);
			}
		} else {
			messagebox("该评论不存在", $_SERVER['HTTP_REFERER']);
		}
		$catid = $ccomment['catid'];
		$infoid = $ccomment['infoid'];
		if ($infoid) {
			$info = GetContentById($infoid, $catid);
		} else {
			messagebox("所属信息不存在", $_SERVER['HTTP_REFERER']);
		}
		if (!empty($_GET["id"])) {
			$parnav = '<li class=\"breadcrumb-item\"><a href=\"' . url_admin('init', 'replay') . '\" target=\"maincontent\">回复管理</a></li><li class=\"breadcrumb-item active\">修改回复</li>';
			$id = trim($_GET["id"]);
			if (!is_numeric($id)) {
				messagebox("回复参数错误", url_admin('init'));
			}
			$rsinfo = $this->db->find("select * from `#yunyecms_replay` where `id`= {$id}");
			if (empty($rsinfo)) {
				messagebox("回复不存在", $_SERVER['HTTP_REFERER']);
			}
			$yyact = "edit";
		} else {
			$yyact = yyact_get("add");
			if ($plid) $ccomment = $this->db->getbyid($plid, 'comment');
			$infoid = $ccomment['infoid'];
			$breadcumb = getbreadcumb($catid);
			$breadcumbstr = "";
			foreach ($breadcumb as $kk => $vv) {
				$breadcumbstr = $breadcumbstr . '<li><a href=\"' . url_admin('lists', 'content', array("catid" => $vv['id'])) . '\" target=\"maincontent\">' . $vv['title'] . '</a></li>';
			}
			$parnav = $breadcumbstr . '<li><a href=\"' . url_admin('init', 'comment', array('id' => $infoid, 'catid' => $catid)) . '\" target=\"maincontent\">评论</a></li><li><a href=\"' . url_admin('init', 'replay', array('plid' => $plid, 'catid' => $catid)) . '\" target=\"maincontent\">回复</a></li><li class=\"breadcrumb-item active\">增加回复</li>';
		}
		if (isset($_POST["yyact"])) {
			$_POST = ustripslashes($_POST);
			$data["isgood"] = empty($_POST["isgood"]) ? 0 : usafestr(trim($_POST["isgood"]));
			$data["title"] = usafestr(trim($_POST["title"]));
			$data["summary"] = usafestr(trim($_POST["summary"]));
			$data["infoid"] = $infoid;
			$data["plid"] = $plid;
			$data["catid"] = $catid;
			$data["admin_userid"] = $this->admuser["userid"];
			$data["status"] = 1;
			if (isset($_POST["content"])) {
				$data["content"] = uhtmlspecialchars(trim($_POST["content"]));
			}
			if (empty($data["content"])) {
				messagebox("回复内容不能为空，谢谢!", $_SERVER['HTTP_REFERER']);
			}
			if ($_POST["yyact"] == "add") {
				$data["addtime"] = time();
				$retres = $this->db->insert($data, 'replay');
				if ($retres) {
					$doing = "添加回复—" . $data["title"];
					$yyact = "Add_replay";
					insert_admlogs($doing, $yyact);
					messagebox("添加回复成功！", url_admin('init', 'replay', array("plid" => $plid)), "success");
				} else {
					messagebox("添加回复失败！", url_admin('init', 'replay', array("plid" => $plid)), "error");
				}
			}
			if ($_POST["yyact"] == "edit") {
				$id = $_POST["id"];
				if (!$this->check_exist($id)) {
					messagebox("该回复不存在！", url_admin('init'), "warn");
				}
				$data["addtime"] = strtotime(trim($_POST["addtime"]));
				$retres = $this->db->update($data, "replay", array('id' => $id));
				if ($retres) {
					$doing = "更新回复—" . $data["title"];
					$yyact = "Update_replay";
					insert_admlogs($doing, $yyact);
					messagebox("回复更新成功！", url_admin('init', 'replay', array("plid" => $plid)), "success");
				} else {
					messagebox("回复更新失败！", url_admin('init', 'replay', array("plid" => $plid)), "error");
				}
			}
		}
		require tpl_adm('replay_add');
	}

	public function finaldelete()
	{
		if (!getroot('info', 'del')) {
			messagebox(Lan('no_permission'), 'back');
		}
		$id = $_REQUEST["id"];
		if (!is_array($id)) {
			$id = compact('id');
		}
		$idarray = $id;
		foreach ($idarray as $key => $var) {
			if (!is_numeric($var)) {
				messagebox("错误的参数！", 'back', "warn");
			}
			$var = usafestr($var);
			$idarray[$key] = $var;
			$curmodelnr = $this->db->find("select * from `#yunyecms_replay` where `id`= {$var}");
			if ($curmodelnr) {
				$ct_content = $curmodelnr["content"];
				$ct_content_img = getcontentimg($ct_content);
				if (!empty($thumb)) {
					file_delete($thumb);
				}
				foreach ($ct_content_img as $key3 => $var3) {
					file_delete($var3);
				}
			}
		}

		if (isset($idarray)) {
			$idarray = implode(",", $idarray);
			$retres = $this->db->delete("id in ({$idarray})", "replay");
			if ($retres !== false) {
				$yyact = "replay_Del";
				$logcontent['tablename'] = "replay";
				$logcontent['action'] = $yyact;
				$logcontent['ids'] = serialize($id);
				$logcontent = serialize($logcontent);
				$doing = "删除回复：{$idarray} ";
				insert_admlogs($doing, $yyact, $logcontent);
				messagebox(Lan('admin_delete_success'), $_SERVER['HTTP_REFERER'], "success");
			} else {
				messagebox(Lan('admin_delete_error'), $_SERVER['HTTP_REFERER'], "warn");
			}
		} else {
			messagebox(Lan('admin_delete_error'), $_SERVER['HTTP_REFERER'], "warn");
		}
	}





	public function check()
	{
		parent::infocheck("replay", $this->db);
	}

	public function nocheck()
	{
		parent::infonocheck("replay", $this->db);
	}

	private function check_exist($id)
	{
		$id = trim($id);
		if (empty($id)) {
			return false;
		} else {
			if (!is_numeric($id)) {
				return false;
			}
			if ($this->db->find("select count(*) as cnt from `#yunyecms_replay` where `id`= {$id}")) {
				return true;
			}
		}
	}
}
