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

if ('add' == $action)
{
  if (empty($newstitle) || ADMIN_TITLE_LENGTH_FORMAT < strlen($newstitle))
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectTitleFormat, $configJumpTime);
    exit();
  }
  if (empty($author))
  {
    promptMessage("javascript:history.go(-1)", $promptAddNewsEmptyAuthor, $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 (! ereg("^[0-9]+$", $clicknum))
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectClickNumFormat, $configJumpTime);
    exit();
  }
  
  
  if (empty($isrecommend))
  {
    $isrecommend = 0;
  }
  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($kuWebsiteTemplateSelected == $skin)$skin = "";

  $strSql = "insert into {$configTableHead}news (newstitle, newscontent, newskeywords, newsdesc, banner, skin, lang, showindex, 
         showsub, author, copyfrom, clicknum, isrecommend, addtime, updatetime, sortnum, status, type1, type2, type3) 
         value ('{$newstitle}', '{$newscontent}', '{$newskeywords}', '{$newsdesc}', '{$banner}', '{$skin}', '{$lang}', {$showindex}, {$showsub}, '{$author}', 
         '{$copyfrom}', '{$clicknum}', '{$isrecommend}', '{$addtime}', '{$updatetime}', {$sortnum}, {$status}, {$menuid}, {$secondMenuNav}, {$thirdMenuNav})";
  if ($dbInstance->query($strSql))
  {
    if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} add an news ".CommTool::utf8ToGbk($newstitle));         
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=news&menu=news_action&menuid={$menuid}", $promptAddDataSuccess, $configJumpTime);
    exit();
  }
  else
  {
    promptMessage("javascript:history.go(-1)", $promptAddDataFail, $configJumpTime);
    exit();
  }
}
else if ('modify' == $action)
{
  if (empty($newstitle) || ADMIN_TITLE_LENGTH_FORMAT < strlen($newstitle))
  {
    promptMessage("javascript:history.go(-1)", $promptEditCorrectTitleFormat, $configJumpTime);
    exit();
  }
  if (empty($author))
  {
    promptMessage("javascript:history.go(-1)", $promptUpdateNewsEmptyAuthor, $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 (! ereg("^[0-9]+$", $clicknum))
  {
    promptMessage("javascript:history.go(-1)", $promptEditCorrectClickNumFormat, $configJumpTime);
    exit();
  }
  
  if (empty($isrecommend))
  {
    $isrecommend = 0;
  }
  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($kuWebsiteTemplateSelected == $skin)$skin = "";

  $strSql = "update {$configTableHead}news set newstitle='{$newstitle}', newscontent='{$newscontent}', 
          newskeywords='{$newskeywords}', newsdesc='{$newsdesc}', banner='{$banner}', skin='{$skin}', lang='{$lang}', showindex={$showindex}, 
          showsub={$showsub}, author='{$author}', copyfrom='{$copyfrom}', clicknum='{$clicknum}', 
          isrecommend='{$isrecommend}', addtime='{$addtime}', updatetime='{$updatetime}', sortnum={$sortnum}, status={$status}, 
          type2={$secondMenuNav}, type3={$thirdMenuNav} where id={$id};";
  
  if ($dbInstance->query($strSql))
  {
    if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} modify an news ".CommTool::utf8ToGbk($newstitle));         
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=news&menu=news_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}news where id='{$value}'";
        if (! $flag = $dbInstance->query($sql))
        {
          promptMessage("javascript:history.go(-1)", $prompBatchDeleteFail, $configJumpTime);
          exit();
        }
      }
      if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} batch delete news");         
      promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=news&menu=news_action&menuid={$menuid}", $prompBatchDeleteSuccess, $configJumpTime);
      exit();
    }
    else
    {
      $sql = "delete from {$configTableHead}news where id='{$id}'";
      if ($dbInstance->query($sql))
      {
        if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} delete a news {$id}");         
        promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=news&menu=news_action&menuid={$menuid}", $promptDeleteDataSuccess, $configJumpTime);
        exit();
      }
      else
      {
        promptMessage("javascript:history.go(-1)", $promptDeleteDataFail, $configJumpTime);
        exit();
      }
    }
  }
}

if ('secondnav' == $searchtype && ! empty($secondnavid))
{
  $sql = "select count(*) as amount from {$configTableHead}news where type2={$secondnavid} and lang='{$kuWebsiteEditVersionLanguage}'";
}
else
{
  $sql = "select count(*) as amount from {$configTableHead}news  where lang='{$kuWebsiteEditVersionLanguage}' and type1={$menuid};";
}
$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}news where type2={$secondnavid} and lang='{$kuWebsiteEditVersionLanguage}' order by id desc limit $startNum, $maxNum;";
}
else
{
  $sql = "select * from {$configTableHead}news where lang='{$kuWebsiteEditVersionLanguage}' and type1={$menuid} order by id desc limit $startNum, $maxNum";
}
$result = $dbInstance->query($sql);
while($arr = $dbInstance->fetchArray($result))
{
  $newsList [] = $arr;
}
$strSql = "select id, menutitle from {$configTableHead}menu where fatherid={$menuid} and lang='{$kuWebsiteEditVersionLanguage}'";
$result = $dbInstance->query($strSql);
while($arrSecondNav = $dbInstance->fetchArray($result))
{
  $newsSecondNavList [] = $arrSecondNav;
}

include_once template('news_manage.html', $adminTemplateName);
?>