<?php
require_once('inc/logincheck.php');
CommTool::checkIncludeByAdminPhp();

if('modify' == $action)
{
    $nowTime = date('Y-m-d H:i:s', time());
  if(empty($status)){$status = 0;}
  $strSql = "update {$configTableHead}message set messagetitle='{$messagetitle}', messagecontent='{$messagecontent}', messagereplycontent='{$messagereplycontent}', status={$status}, updatetime='$nowTime' where id={$id};";
  if($dbInstance->query($strSql))
  {
    if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} modify a message {$id}");         
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=message&menu=message_action&menuid={$menuid}", $promptEditDataSuccess, $configJumpTime);
    exit;    
  }
  else
  {
    promptMessage("javascript:history.go(-1)", $promptEditDataFail, $configJumpTime);
    exit;    
  }
}
else if('delete' == $action)
{
  if(empty($id) || ereg("/^d+$/", $id))
  {
    promptMessage("javascript:history.go(-1)", $promptDeleteEmptyId, $configJumpTime);
    exit;
  }
  else
  {
    if(is_array($id))                                                                
    {
      foreach($id as $key=>$value)
      {
        $sql = "delete from {$configTableHead}message where id='{$value}'";
        if(!$flag = $dbInstance->query($sql))
        {
          promptMessage("javascript:history.go(-1)", $prompBatchDeleteFail, $configJumpTime);
          exit;
        }  
      }
      if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} batch delete message");         
      promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=message&menu=message_action&menuid={$menuid}", $prompBatchDeleteSuccess, $configJumpTime);
      exit;    
    }
    else                                                                               
    {
      $sql = "delete from {$configTableHead}message where id='{$id}'";
      if($dbInstance->query($sql))
      {
        if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} delete a message {$id}");         
        promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=message&menu=message_action&menuid={$menuid}", $promptDeleteDataSuccess, $configJumpTime);
        exit;
      }
      else
      {
        promptMessage("javascript:history.go(-1)", $promptDeleteDataFail, $configJumpTime);
        exit;
      }
    }  
  }  
}
else if('messageset' == $action)
{
  $kuWebsiteMessageRevertAudit = $_POST['kuWebsiteMessageRevertAudit'];
  $kuWebsiteMessagePostEmail = $_POST['kuWebsiteMessagePostEmail'];
  $kuWebsiteMessageAutoRevert = $_POST['kuWebsiteMessageAutoRevert'];
  include_once 'system/saveconfig.php';                                            
  if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} change message settings");         
  promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=message&menu=message&menuid={$menuid}", $promptEditDataSuccess, $configJumpTime);
  exit;
}

if('all' == $searchtype || empty($searchtype))                                      
{
  $sql = "select count(*) as amount from {$configTableHead}message where lang='{$kuWebsiteEditVersionLanguage}'";
}
else if('notrevert' == $searchtype)                                                  
{
  $sql = "select count(*) as amount from {$configTableHead}message where status=0 and lang='{$kuWebsiteEditVersionLanguage}'";
}
else if('reverted' == $searchtype)                                                  
{
  $sql = "select count(*) as amount from {$configTableHead}message where status=1 and lang='{$kuWebsiteEditVersionLanguage}'";  
}
$result = $dbInstance->query($sql);
$row = $dbInstance->fetchArray($result);
$totalNumber = $row['amount'];                                                      //总记录数
$maxNum = $adminPagingPageNumber;                                                    //每页显示的记录数 
!empty($page) ? ($page = intval($page)) : $page =1;
$actionDisplayNumber = $adminPagingActionsDisplayNumber;                             //活动显示页数 
$type = $adminPagingType;                                                           //分页样式0-5 
$isAlwaysShowFirstOrLast = $adminPagingIsAlwaysShowFirstOrLast;                      //是否绍终显示第一页和最后一页
$startNum = ($page - 1) * $maxNum;                                                  //记录查询起始
if('all' == $searchtype || empty($searchtype))                                      //所有记录：查询相应数量的记录条数    
{
  $sql = "select * from {$configTableHead}message where lang='{$kuWebsiteEditVersionLanguage}' order by id desc limit $startNum, $maxNum";
}
else if('notrevert' == $searchtype)                                                  
{
  $sql = "select * from {$configTableHead}message where status=0 and lang='{$kuWebsiteEditVersionLanguage}' order by id desc  limit $startNum, $maxNum;";
}
else if('reverted' == $searchtype)                                                  
{
  $sql = "select * from {$configTableHead}message where status=1 and lang='{$kuWebsiteEditVersionLanguage}' order by id desc  limit $startNum, $maxNum;";
}                                                        
$result = $dbInstance->query($sql);
while($arr = $dbInstance->fetchArray($result)) 
{
  $messageList[] = $arr;
}
include_once template('message_manage.html', $adminTemplateName);
?>