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

if('quickreply' == $action)
{
  $strSql = "select * from {$configTableHead}feedback where id={$id} and lang='{$kuWebsiteEditVersionLanguage}'";
  $result = $dbInstance->query($strSql);
  $feedbackRow = $dbInstance->fetchArray($result);

  $feedbackType = $arrAllModule[$feedbackRow['feedbacktype']];
  $strSql = "select * from {$configTableHead}{$feedbackType} where id={$feedbackRow['objectid']}";
  $objectRow = $dbInstance->getOne($strSql);
  $objectTitle = $objectRow[$feedbackType.'title'];
  $objectUrl = "{$kuHttpPathUrl}{$feedbackType}/{$feedbackType}.php?lang={$kuWebsiteEditVersionLanguage}&itemid={$feedbackRow['objectid']}";

  include_once '../inc/mailer.class.php';

  
  $strSendName = $kuWebsiteTitle;
  $strSubject = $feedbackRow['feedbacktitle'] ?  $feedbackRow['feedbacktitle'] : 'An reply from '.$objectTitle;
  $strEmail = $kuWebsiteFeedbackAutoRevertEmailAddress; 
  $strTel = '0755-36943966';
  $strFeedback = $feedbackreplycontent ? $feedbackreplycontent : 'Thanks for your message, we will contact you asap. Any question pls visit: '.$kuHttpPath;
  
  $strHeader = array("To: " . $feedbackRow['feedbackemail'], "From: " . $strEmail, "Cc: ", "Bcc: ");
  $strHeaderString = join("\r\n", $strHeader);
  if(1 == $kuWebsiteMailType)
  {
    KuwebsMailer::smtpMail($strEmail, $feedbackRow['feedbackemail'], $strSubject, $strFeedback);
  }
  else
  {
    @mail($feedbackRow['feedbackemail'], $strSubject, $strFeedback, $strHeaderString);
  }
  
  $nowTime = date('Y-m-d H:i:s', time());
  $strSql = "update {$configTableHead}feedback set feedbackreplycontent='{$feedbackreplycontent}', status=2, updatetime='$nowTime' where id={$id};";
  if($dbInstance->query($strSql))
  {
    if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} reply a feedback {$id}");         
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=plus&menu=feedback_action", $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}feedback where id='{$value}'";
        if(!$flag = $dbInstance->query($sql))
        {
          promptMessage("javascript:history.go(-1)", $prompBatchDeleteFail, $configJumpTime);
          exit;
        }  
      }
      if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} batch delete feedback");         
      promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=plus&menu=feedback_action", $prompBatchDeleteSuccess, $configJumpTime);
      exit;    
    }
    else                                                                               
    {
      $sql = "delete from {$configTableHead}feedback where id='{$id}'";
      if($dbInstance->query($sql))
      {
        if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} delete a feedback {$id}");         
        promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=plus&menu=feedback_action", $promptDeleteDataSuccess, $configJumpTime);
        exit;
      }
      else
      {
        promptMessage("javascript:history.go(-1)", $promptDeleteDataFail, $configJumpTime);
        exit;
      }
    }  
  }  
}
else if('feedbackset' == $action)
{
  $kuWebsiteFeedbackRevertAudit = $_POST['kuWebsiteFeedbackRevertAudit'];
  $kuWebsiteFeedbackPostEmail = $_POST['kuWebsiteFeedbackPostEmail'];
  $kuWebsiteFeedbackAutoRevert = $_POST['kuWebsiteFeedbackAutoRevert'];
  include_once 'system/saveconfig.php';                                            
  if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} change feedback settings");         
  promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=plus&menu=feedback", $promptEditDataSuccess, $configJumpTime);
  exit;
}

if('all' == $searchtype || empty($searchtype))                                      
{
  $sql = "select count(*) as amount from {$configTableHead}feedback where lang='{$kuWebsiteEditVersionLanguage}'";
}
else                                                  
{
  $sql = "select count(*) as amount from {$configTableHead}feedback where status='{$searchtype}' 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 || '' == $searchtype)                                      //所有记录：查询相应数量的记录条数    
{
  $sql = "select * from {$configTableHead}feedback where lang='{$kuWebsiteEditVersionLanguage}' order by id desc limit $startNum, $maxNum";
}
else                                                   
{
  $sql = "select * from {$configTableHead}feedback where status='{$searchtype}' and lang='{$kuWebsiteEditVersionLanguage}' order by id desc  limit $startNum, $maxNum;";
}
$result = $dbInstance->query($sql);
while($arr = $dbInstance->fetchArray($result)) 
{
  $feedbackList[] = $arr;
}
include_once template('feedback_manage.html', $adminTemplateName);
?>