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

if('add' == $action)
{
  if(empty($jobtitle) || ADMIN_TITLE_LENGTH_FORMAT < strlen($jobtitle))
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectTitleFormat, $configJumpTime);
    exit;    
  }
  if(empty($jobdepart) || ADMIN_TITLE_LENGTH_FORMAT < strlen($jobdepart))
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectJobPartFormat, $configJumpTime);
    exit;    
  }
  if(empty($jobpost) || ADMIN_TITLE_LENGTH_FORMAT < strlen($jobpost))
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectJobPostFormat, $configJumpTime);
    exit;    
  }
  if(!empty($jobnumber) && !is_numeric($jobnumber))
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectJobNumberFormat, $configJumpTime);
    exit;    
  }
  if(!empty($expirytime) && !is_numeric($expirytime))
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectJobExpiryTimeFormat, $configJumpTime);
    exit;    
  }
  $arrTimePubYMD = explode("-", substr(trim($pubtime), 0, 10));    //获取年月日
  $arrTimePubHSM = explode(":", substr(trim($pubtime), -8));      //获取时分秒
  if(19 < strlen(trim($pubtime)) || empty($pubtime) || !checkdate($arrTimePubYMD[1], $arrTimePubYMD[2], $arrTimePubYMD[0]) || !ereg("^[0-9]+$", $arrTimePubHSM[0]) || !ereg("^[0-9]+$", $arrTimePubHSM[1]) || !ereg("^[0-9]+$", $arrTimePubHSM[2]) || 24 <= $arrTimePubHSM[0] || 60 <= $arrTimePubHSM[1] || 60 <= $arrTimePubHSM[1])
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectJobPublicTimeFormat, $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($status)){$status = 0;}
  if(empty($jobnumber)){$jobnumber = 0;}      
  if(empty($expirytime)){$expirytime = 0;}    
  if(empty($secondMenuNav) || ADMIN_SECOND_NAVAGTION_ID == $secondMenuNav){$secondMenuNav = 0;}
  if(empty($thirdMenuNav)){$thirdMenuNav = 0;}  
    if($kuWebsiteTemplateSelected == $skin)$skin = "";
    
  $strSql = "insert into {$configTableHead}job (jobtitle, jobdepart, jobcontent, jobpost, jobaddr, jobsalary, pubtime, 
          jobnumber, expirytime,  addtime, updatetime, sortnum, status, banner, skin, lang, type1, type2, type3, clicknum, jobkeywords, jobdesc) 
         value ('{$jobtitle}', '{$jobdepart}', '{$jobcontent}', '{$jobpost}', '{$jobaddr}', '{$jobsalary}', '{$pubtime}', 
        {$jobnumber}, '{$expirytime}', '{$addtime}', '{$updatetime}', {$sortnum}, {$status}, '{$banner}', '{$skin}', '{$lang}', {$menuid}, {$secondMenuNav}, {$thirdMenuNav}, {$clicknum}, '{$jobkeywords}', '{$jobdesc}');";
  if($dbInstance->query($strSql))
  {
    if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} add an job ".CommTool::utf8ToGbk($jobtitle));         
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=job&menu=job_action&menuid={$menuid}", $promptAddDataSuccess, $configJumpTime);
    exit;    
  }
  else
  {
    promptMessage("javascript:history.go(-1)", $promptAddDataFail, $configJumpTime);
    exit;    
  }
}
else if('modify' == $action)
{
  if(!ereg("^[0-9]+$", $id))
  {
    promptMessage("javascript:history.go(-1)", $promptEditEmptyId, $configJumpTime);
    exit;  
  }
  if(empty($jobtitle) || ADMIN_TITLE_LENGTH_FORMAT < strlen($jobtitle))
  {
    promptMessage("javascript:history.go(-1)", $promptEditCorrectTitleFormat, $configJumpTime);
    exit;    
  }
  if(empty($jobdepart) || ADMIN_TITLE_LENGTH_FORMAT < strlen($jobdepart))
  {
    promptMessage("javascript:history.go(-1)", $promptEditCorrectJobPartFormat, $configJumpTime);
    exit;    
  }
  if(empty($jobpost) || ADMIN_TITLE_LENGTH_FORMAT < strlen($jobpost))
  {
    promptMessage("javascript:history.go(-1)", $promptEditCorrectJobPostFormat, $configJumpTime);
    exit;    
  }
  if(!empty($jobnumber) && !is_numeric($jobnumber))
  {
    promptMessage("javascript:history.go(-1)", $promptEditCorrectJobNumberFormat, $configJumpTime);
    exit;    
  }
  if(!empty($expirytime) && !is_numeric($expirytime))
  {
    promptMessage("javascript:history.go(-1)", $promptEditCorrectExpiryTimeFormat, $configJumpTime);
    exit;    
  }
  $arrTimePubYMD = explode("-", substr(trim($pubtime), 0, 10));    //获取年月日
  $arrTimePubHSM = explode(":", substr(trim($pubtime), -8));      //获取时分秒
  if(19 < strlen(trim($pubtime)) || empty($pubtime) || !checkdate($arrTimePubYMD[1], $arrTimePubYMD[2], $arrTimePubYMD[0]) || !ereg("^[0-9]+$", $arrTimePubHSM[0]) || !ereg("^[0-9]+$", $arrTimePubHSM[1]) || !ereg("^[0-9]+$", $arrTimePubHSM[2]) || 24 <= $arrTimePubHSM[0] || 60 <= $arrTimePubHSM[1] || 60 <= $arrTimePubHSM[1])
  {
    promptMessage("javascript:history.go(-1)", $promptEidtCorrectJobPublicTimeFormat, $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($status)){$status = 0;}
  if(empty($jobnumber)){$jobnumber = 0;}      
  if(empty($expirytime)){$expirytime = 0;}    
  if(empty($secondMenuNav) || ADMIN_SECOND_NAVAGTION_ID == $secondMenuNav){$secondMenuNav = 0;}
  if(empty($thirdMenuNav)){$thirdMenuNav = 0;}  
    if($kuWebsiteTemplateSelected == $skin)$skin = "";

  $strSql = "update {$configTableHead}job set jobtitle='{$jobtitle}', jobdepart='{$jobdepart}', jobcontent='{$jobcontent}', 
        jobpost='{$jobpost}', jobaddr='{$jobaddr}', jobsalary='{$jobsalary}', pubtime='{$pubtime}', jobnumber={$jobnumber}, 
        expirytime='{$expirytime}', addtime='{$addtime}', updatetime='{$updatetime}', sortnum={$sortnum}, status={$status}, banner='{$banner}', skin='{$skin}', lang='{$lang}',                     
        type2={$secondMenuNav}, type3={$thirdMenuNav}, clicknum={$clicknum}, jobkeywords='{$jobkeywords}', jobdesc='{$jobdesc}' where id={$id};";
  if($dbInstance->query($strSql))
  {
    if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} modify an job ".CommTool::utf8ToGbk($jobtitle));         
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=job&menu=job_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}job where id='{$value}'";
        if(!$flag = $dbInstance->query($sql))
        {
          promptMessage("javascript:history.go(-1)", $prompBatchDeleteFail, $configJumpTime);
          exit;
        }  
      }
      if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} batch delete job");         
      promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=job&menu=job_action&menuid={$menuid}", $prompBatchDeleteSuccess, $configJumpTime);
      exit;    
    }
    else
    {
      $sql = "delete from {$configTableHead}job where id='{$id}'";
      if($dbInstance->query($sql))
      {
        if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} delete a job {$id}");         
        promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=job&menu=job_action&menuid={$menuid}", $promptDeleteDataSuccess, $configJumpTime);
        exit;
      }
      else
      {
        promptMessage("javascript:history.go(-1)", $promptDeleteDataFail, $configJumpTime);
        exit;
      }
    }  
  }  
}
$sql = "select count(*) as amount from {$configTableHead}job 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;                                                  //记录查询起始
$sql = "select * from {$configTableHead}job where lang='{$kuWebsiteEditVersionLanguage}' and type1={$menuid} order by id desc limit $startNum, $maxNum";                                                          
$result = $dbInstance->query($sql);
while($arr = $dbInstance->fetchArray($result)) 
{
  $jobList[] = $arr;
}
include_once template('job_manage.html', $adminTemplateName);
?>