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

if("add" == $action)                                                                          //前台模板添加
{
  if(empty($skinName) || empty($skinFolder))
  {
    promptMessage("javascript:history.go(-1)", $prompModelEmptyDirOrFile, $configJumpTime);
    exit;
  }
  if(!file_exists($adminServerRoot."../template/".$skinFolder))                                  //前台模板是否存在    
  {
    promptMessage("javascript:history.go(-1)", $prompModelDirNotExit, $configJumpTime);
    exit;
  }
  $sql = "select * from {$configTableHead}skin where skinname = '{$skinName}'";
  $row = $dbInstance->getOne($sql);
  if($row['id'] > 0)
  {
    promptMessage("javascript:history.go(-1)", $prompNewModelNameExit, $configJumpTime);
    exit;
  }
  $sql = "insert into {$configTableHead}skin (skinname, skinfolder, skininfo) value ('{$skinName}', '{$skinFolder}', '{$skinInfo}')";
  if($dbInstance->query($sql))
  {
    if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} add a skin ".CommTool::utf8ToGbk($skinName));         
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=system&menu=skin_manage", $promptAddDataSuccess, $configJumpTime);
    exit;
  }
  else
  {
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=system&menu=skin_manage", $promptAddDataFail, $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}skin where id='{$value}'";
        if(!$flag = $dbInstance->query($sql))
        {
          promptMessage("javascript:history.go(-1)", $prompBatchDeleteFail, $configJumpTime);
          exit;
        }  
      }
      if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} batch delete skins");         
      promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=system&menu=skin_manage", $prompBatchDeleteSuccess, $configJumpTime);
      exit;    
    }
    else                                                                    //单个删除                                                          
    {
      $sql = "delete from {$configTableHead}skin where id='{$id}'";
      if($dbInstance->query($sql))
      {
        if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} delete a skin {$id}");         
        promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=system&menu=skin_manage", $promptDeleteDataSuccess, $configJumpTime);
        exit;
      }
      else
      {
        promptMessage("javascript:history.go(-1)", $promptDeleteDataFail, $configJumpTime);
        exit;
      }
    }  
  }  
}
else if('edit' == $action)
{
  if(!file_exists($adminServerRoot."../template/".$skinFolder))                    //判断模板名称文件是否存在
  {
    promptMessage("javascript:history.go(-1)", $prompModelDirNotExit, $configJumpTime);
    exit;
  }
  $sql = "select * from {$configTableHead}skin where skinname = '{$skinName}' and id not in($id)";
  $row = $dbInstance->getOne($sql);
  if($row['id'] > 0)
  {
    promptMessage("javascript:history.go(-1)", $prompNewModelNameExit, $configJumpTime);
    exit;
  }
  $sql = "update {$configTableHead}skin set skinname='{$skinName}', skinfolder='{$skinFolder}', skininfo='{$skinInfo}' where id='{$id}';";
  if($dbInstance->query($sql))
  {
    if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} modify a skin ".CommTool::utf8ToGbk($skinName));         
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=system&menu=skin_manage", $promptEditDataSuccess, $configJumpTime);
    exit;
  }
  else
  {
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=system&menu=skin_manage", $promptEditDataFail, $configJumpTime);
    exit;
  }  
}


$sql = "select count(*) as amount from {$configTableHead}skin";
$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}skin order by id limit $startNum, $maxNum";
// $result = $dbInstance->query($sql);
// while($arr = $dbInstance->fetchArray($result)) 
// {
  // $skinList[] = $arr;
// }
include_once template('skin_manage.html', $adminTemplateName);
?>