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

if('add' == $action)
{
  
  if(!ereg("^[0-9]+$", $sortnum))
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectSortNumFormat, $configJumpTime);
    exit;
  }
  if(1 == $friendlinktype)  
  {
    if(empty($friendlinktitle) || ADMIN_TITLE_LENGTH_FORMAT < strlen($friendlinktitle))
    {
      promptMessage("javascript:history.go(-1)", $promptAddCorrectTitleFormat, $configJumpTime);
      exit;    
    }
  }
  else if(2 == $friendlinktype)  
  {
     if(empty($friendlinklogo))
    {
      promptMessage("javascript:history.go(-1)", $promptAddCorrectFriendLogoAddress, $configJumpTime);
      exit;    
    }  
  }
  
  
  if(empty($friendlinkurl) || ADMIN_TITLE_LENGTH_FORMAT < strlen($friendlinkurl))
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectFriendlinkUrl, $configJumpTime);
    exit;    
  }
  
  $strSql = "select * from {$configTableHead}friendlink where friendlinktitle='{$friendlinktitle}' and lang='{$kuWebsiteEditVersionLanguage}';";
  $row = $dbInstance->getOne($strSql);
  if(0 < $row['id'])
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectFriendlinkTitleExist, $configJumpTime);
    exit;
  }
  $strSql = "insert into {$configTableHead}friendlink (friendlinktype, friendlinktitle, friendlinkurl, friendlinklogo, friendlinkkeyword, sortnum, lang, addtime, updatetime, status, friendlinkinfo) 
         value ({$friendlinktype}, '{$friendlinktitle}', '{$friendlinkurl}', '{$friendlinklogo}', '{$friendlinkkeyword}', '{$sortnum}', '{$lang}', '{$nowTime}', '{$nowTime}', {$status}, 
         '{$friendlinkinfo}');";
  if($dbInstance->query($strSql))
  {
    if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} add an friendlink ".CommTool::utf8ToGbk($friendlinktitle));         
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=friendlink&menu=friendlink_action", $promptAddDataSuccess  , $configJumpTime);
    exit;    
  }
  else
  {
    promptMessage("javascript:history.go(-1)", $promptAddDataFail, $configJumpTime);
    exit;    
  }
}
else if('modify' == $action)
{
  
  if(!ereg("^[0-9]+$", $sortnum))
  {
    promptMessage("javascript:history.go(-1)", $promptEditCorrectSortNumFormat, $configJumpTime);
    exit;
  }
  if(1 == $friendlinktype)  
  {
    if(empty($friendlinktitle) || ADMIN_TITLE_LENGTH_FORMAT < strlen($friendlinktitle))
    {
      promptMessage("javascript:history.go(-1)", $promptAddCorrectTitleFormat, $configJumpTime);
      exit;    
    }
  }
  else if(2 == $friendlinktype)  
  {
     if(empty($friendlinklogo))
    {
      promptMessage("javascript:history.go(-1)", $promptAddCorrectFriendLogoAddress, $configJumpTime);
      exit;    
    }  
  }
  if(empty($friendlinkurl) || ADMIN_TITLE_LENGTH_FORMAT < strlen($friendlinkurl))
  {
    promptMessage("javascript:history.go(-1)", $promptEditCorrectFriendlinkUrl, $configJumpTime);
    exit;    
  }
  
  $strSql = "select * from {$configTableHead}friendlink where friendlinktitle='{$friendlinktitle}' and lang='{$kuWebsiteEditVersionLanguage}';";
  $row = $dbInstance->getOne($strSql);
  if(0 < $row['id'])
  {
    promptMessage("javascript:history.go(-1)", $promptEidtCorrectFriendlinkTitleExist, $configJumpTime);
    exit;
  }
  $strSql = "update {$configTableHead}friendlink set friendlinktype='{$friendlinktype}', friendlinktitle='{$friendlinktitle}', 
          friendlinkurl='{$friendlinkurl}', friendlinklogo='{$friendlinklogo}', friendlinkkeyword='{$friendlinkkeyword}', 
          sortnum={$sortnum}, lang='{$lang}', updatetime='{$nowTime}', status='{$status}',   friendlinkinfo='{$friendlinkinfo}'
          where id={$id}
          ;";
  if($dbInstance->query($strSql))
  {
    if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} modify an friendlink ".CommTool::utf8ToGbk($friendlinktitle));         
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=friendlink&menu=friendlink_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}friendlink where id='{$value}'";
        if(!$flag = $dbInstance->query($sql))
        {
          promptMessage("javascript:history.go(-1)", $prompBatchDeleteFail, $configJumpTime);
          exit;
        }  
      }
      if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} batch delete friendlink");         
      promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=friendlink&menu=friendlink_action", $prompBatchDeleteSuccess, $configJumpTime);
      exit;    
    }
    else
    {
      $sql = "delete from {$configTableHead}friendlink where id='{$id}'";
      if($dbInstance->query($sql))
      {
        if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} delete a friendlink");         
        promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=friendlink&menu=friendlink_action", $promptDeleteDataSuccess, $configJumpTime);
        exit;
      }
      else
      {
        promptMessage("javascript:history.go(-1)", $promptDeleteDataFail, $configJumpTime);
        exit;
      }
    }  
  }  
}


$sql = "select count(*) as amount from {$configTableHead}friendlink 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;                                                  //记录查询起始
$sql = "select * from {$configTableHead}friendlink where lang='{$kuWebsiteEditVersionLanguage}' order by id desc limit $startNum, $maxNum";                                                          
$result = $dbInstance->query($sql);
while($arr = $dbInstance->fetchArray($result)) 
{
  $friendlinkList[] = $arr;
}
include_once template('friendlink_manage.html', $adminTemplateName);
?>