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

if('add' == $action)
{
  if(empty($faqtitle) || ADMIN_TITLE_LENGTH_FORMAT < strlen($faqtitle))
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectTitleFormat, $configJumpTime);
    exit;    
  }
  if(empty($adduser))
  {
    promptMessage("javascript:history.go(-1)", $promptAddEmptyAddUser, $configJumpTime);
    exit;
  }
  if(!checkDateFormat($addtime))
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectAddDateFormat, $configJumpTime);
    exit();
  }
  if(!checkDateFormat($updatetime))
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectUpdateDateFormat, $configJumpTime);
    exit();
  }
  
  
  if(empty($showindex)){$showindex = 0;}
  if(empty($showsub)){$showsub = 0;}
  if(empty($status)){$status = 0;}
  if(empty($secondMenuNav) || ADMIN_SECOND_NAVAGTION_ID == $secondMenuNav){$secondMenuNav = 0;}
  if(empty($thirdMenuNav)){$thirdMenuNav = 0;}
  if(empty($faqreplycontent)){$faqreplycontent = '';}
    if($kuWebsiteTemplateSelected == $skin)$skin = "";

  $strSql = "insert into {$configTableHead}faq (faqtitle, faqcontent, faqkeywords, faqdesc, banner, skin, lang, showindex, 
         showsub, adduser, addtime, updatetime, sortnum, status, type1, type2, type3, faqreplycontent) 
         value ('{$faqtitle}', '{$faqcontent}', '{$faqkeywords}', '{$faqdesc}', '{$banner}', '{$skin}', '{$lang}', {$showindex}, {$showsub}, 
         '{$adduser}', '{$addtime}', '{$updatetime}', {$sortnum}, {$status}, {$menuid}, {$secondMenuNav}, {$thirdMenuNav}, '{$faqreplycontent}');";
  if($dbInstance->query($strSql))
  {
    if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} add an faq ".CommTool::utf8ToGbk($faqtitle));         
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=faq&menu=faq_action&menuid={$menuid}", $promptAddDataSuccess  , $configJumpTime);
    exit;    
  }
  else
  {
    promptMessage("javascript:history.go(-1)", $promptAddDataFail, $configJumpTime);
    exit;    
  }
}
else if('modify' == $action)
{
  if(empty($faqtitle) || ADMIN_TITLE_LENGTH_FORMAT < strlen($faqtitle))
  {
    promptMessage("javascript:history.go(-1)", $promptEditCorrectTitleFormat, $configJumpTime);
    exit;    
  }
  if(empty($adduser))
  {
    promptMessage("javascript:history.go(-1)", $promptEditEmptyAddUser, $configJumpTime);
    exit;
  }
  if(!checkDateFormat($addtime))
  {
    promptMessage("javascript:history.go(-1)", $promptEditCorrectAddDateFormat, $configJumpTime);
    exit();
  }
  if(!checkDateFormat($updatetime))
  {
    promptMessage("javascript:history.go(-1)", $promptEditCorrectUpdateDateFormat, $configJumpTime);
    exit();
  }
  
  
  if(empty($showindex)){$showindex = 0;}
  if(empty($showsub)){$showsub = 0;}
  if(empty($status)){$status = 0;}
  if(empty($secondMenuNav)){$secondMenuNav = 0;}
  if(empty($thirdMenuNav)){$thirdMenuNav = 0;}
  if(empty($faqreplycontent)){$faqreplycontent = '';}
    if($kuWebsiteTemplateSelected == $skin)$skin = "";

  $strSql = "update {$configTableHead}faq set faqtitle='{$faqtitle}', faqcontent='{$faqcontent}', 
          faqkeywords='{$faqkeywords}', faqdesc='{$faqdesc}', banner='{$banner}', skin='{$skin}', lang='{$lang}', showindex={$showindex}, 
          showsub={$showsub}, adduser='{$adduser}', addtime='{$addtime}', updatetime='{$updatetime}', sortnum={$sortnum}, status={$status}, 
          type1={$menuid}, type2={$secondMenuNav}, type3={$thirdMenuNav}, faqreplycontent='{$faqreplycontent}' where id={$id};";
  if($dbInstance->query($strSql))
  {
    if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} add an faq ".CommTool::utf8ToGbk($faqtitle));         
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=faq&menu=faq_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}faq where id='{$value}'";
        if(!$flag = $dbInstance->query($sql))
        {
          promptMessage("javascript:history.go(-1)", $prompBatchDeleteFail, $configJumpTime);
          exit;
        }  
      }
      if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} batch delete faq");         
      promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=faq&menu=faq_action&menuid={$menuid}", $prompBatchDeleteSuccess, $configJumpTime);
      exit;    
    }
    else
    {
      $sql = "delete from {$configTableHead}faq where id='{$id}'";
      if($dbInstance->query($sql))
      {
        promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=faq&menu=faq_action&menuid={$menuid}", $promptDeleteDataSuccess, $configJumpTime);
        exit;
      }
      else
      {
        if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} delete a faq");         
        promptMessage("javascript:history.go(-1)", $promptDeleteDataFail, $configJumpTime);
        exit;
      }
    }  
  }  
}
if('secondnav' == $searchtype && !empty($secondnavid))
{
  $sql = "select count(*) as amount from {$configTableHead}faq where type2={$secondnavid} and lang='{$kuWebsiteEditVersionLanguage}'";
}
else
{
  $sql = "select count(*) as amount from {$configTableHead}faq where 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('secondnav' == $searchtype && !empty($secondnavid))
{
  $sql = "select * from {$configTableHead}faq where type2={$secondnavid} and lang='{$kuWebsiteEditVersionLanguage}' order by id desc limit $startNum, $maxNum;";
}
else
{
  $sql = "select * from {$configTableHead}faq where lang='{$kuWebsiteEditVersionLanguage}' order by id desc limit $startNum, $maxNum";  
}                                                           
$result = $dbInstance->query($sql);
while($arr = $dbInstance->fetchArray($result)) 
{
  $faqList[] = $arr;
}
$strSql = "select id, menutitle from {$configTableHead}menu where fatherid={$menuid} and lang='{$kuWebsiteEditVersionLanguage}'";
$result = $dbInstance->query($strSql);
while($arrSecondNav = $dbInstance->fetchArray($result)) 
{
  $faqSecondNavList[] = $arrSecondNav;
}
include_once template('faq_manage.html', $adminTemplateName);
?>