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

if('add' == $action)
{
  if(empty($downloadtitle) || ADMIN_TITLE_LENGTH_FORMAT < strlen($downloadtitle))
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectTitleFormat, $configJumpTime);
    exit;    
  }
  if(empty($fileurl))
  {
    promptMessage("javascript:history.go(-1)", $promptAddEmptyDownloadFileUrl, $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;    
  }
  
  
  for($i = ADMIN_DOWNLOAD_PARA_START; $i <= ADMIN_DOWNLOAD_PARA_END; $i++)
  {
    $parameter = 'para'.$i;
    $para[] =  $$parameter;
  }
  
  
  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}download (downloadtitle, downloadcontent, downkeywords, downdesc, banner, skin, lang, showindex, 
         showsub, clicknum, isrecommend, addtime, updatetime, sortnum, status, type1, type2, type3, 
         para1, para2, para3, para4, para5, para6, para7, para8, para9, para10, fileurl, filesize) 
         value ('{$downloadtitle}', '{$downloadcontent}', '{$downkeywords}', '{$downdesc}', '{$banner}', '{$skin}', '{$lang}', {$showindex}, {$showsub}, 
         '{$clicknum}', '{$isrecommend}', '{$addtime}', '{$updatetime}', {$sortnum}, {$status}, {$menuid}, {$secondMenuNav}, {$thirdMenuNav}, '{$para[0]}', '{$para[1]}', '{$para[2]}', '{$para[3]}', '{$para[4]}', 
         '{$para[5]}', '{$para[6]}', '{$para[7]}', '{$para[8]}', '{$para[9]}', '{$fileurl}', {$filesize})";

  if($dbInstance->query($strSql))
  {
    if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} add an download ".CommTool::utf8ToGbk($downloadtitle));         
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=download&menu=download_action&menuid={$menuid}", $promptAddDataSuccess  , $configJumpTime);
    exit;    
  }
  else
  {
    promptMessage("javascript:history.go(-1)", $promptAddDataFail, $configJumpTime);
    exit;    
  }
}
else if('modify' == $action)
{
  if(empty($downloadtitle) || ADMIN_TITLE_LENGTH_FORMAT < strlen($downloadtitle))
  {
    promptMessage("javascript:history.go(-1)", $promptEditCorrectTitleFormat, $configJumpTime);
    exit;    
  }
  if(empty($fileurl))
  {
    promptMessage("javascript:history.go(-1)", $promptEidtEmptyDownloadFileUrl, $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;    
  }
  
  
  for($i = ADMIN_DOWNLOAD_PARA_START; $i <= ADMIN_DOWNLOAD_PARA_END; $i++)
  {
    $parameter = 'para'.$i;
    $para[] =  $$parameter;
  }
  
  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 = "update {$configTableHead}download set downloadtitle='{$downloadtitle}', downloadcontent='{$downloadcontent}', downkeywords='{$downkeywords}', 
        downdesc='{$downdesc}', banner='{$banner}', skin='{$skin}', lang='{$lang}', showindex={$showindex}, showsub={$showsub}, clicknum='{$clicknum}', isrecommend='{$isrecommend}', 
        addtime='{$addtime}', updatetime='{$updatetime}', sortnum={$sortnum}, status={$status}, type2={$secondMenuNav}, type3={$thirdMenuNav}, para1='{$para[0]}', para2='{$para[1]}', 
        para3='{$para[2]}', para4='{$para[3]}', para5='{$para[4]}', para6='{$para[5]}', para7='{$para[6]}', para8='{$para[7]}', para9='{$para[8]}', para10='{$para[9]}', fileurl='{$fileurl}', filesize='{$filesize}' where id={$id};";
  
  if($dbInstance->query($strSql))
  {
    if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} update an download ".CommTool::utf8ToGbk($downloadtitle));         
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=download&menu=download_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}download where id='{$value}'";
        if(!$flag = $dbInstance->query($sql))
        {
          promptMessage("javascript:history.go(-1)", $prompBatchDeleteFail, $configJumpTime);
          exit;
        }  
      }
      if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} batch delete download");         
      promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=download&menu=download_action&menuid={$menuid}", $prompBatchDeleteSuccess, $configJumpTime);
      exit;    
    }
    else
    {
      $sql = "delete from {$configTableHead}download where id='{$id}'";
      if($dbInstance->query($sql))
      {
        if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} delete a download");         
        promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=download&menu=download_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}download where type2={$secondnavid} and lang='{$kuWebsiteEditVersionLanguage}'";
}
else
{
  $sql = "select count(*) as amount from {$configTableHead}download 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}download where lang='{$kuWebsiteEditVersionLanguage}' and type2={$secondnavid} order by id desc  limit $startNum, $maxNum;";
}
else
{
  $sql = "select * from {$configTableHead}download where lang='{$kuWebsiteEditVersionLanguage}' and type1={$menuid} order by id desc limit $startNum, $maxNum";  
}                                                           
$result = $dbInstance->query($sql);
while($arr = $dbInstance->fetchArray($result)) 
{
  $downloadList[] = $arr;
}
$strSql = "select id, menutitle from {$configTableHead}menu where fatherid={$menuid} and lang='{$kuWebsiteEditVersionLanguage}'";
$result = $dbInstance->query($strSql);
while($arrSecondNav = $dbInstance->fetchArray($result)) 
{
  $downloadSecondNavList[] = $arrSecondNav;
}
include_once template('download_manage.html', $adminTemplateName);
?>