<?php  
include_once 'header.php'; //XOOPS檔頭
include_once "../function.php";	
include_once "../class/themeset.php";




//允許傳送變數
$allowvar =array("op","sortid","status","i","url","blocktitle","groupid");

//接收變數區
foreach($_REQUEST as $var=> $value){	
if(in_array($var,$allowvar)){
 $$var=sqlinjectionfound($val=trim(isset($value)?$value:""));
} 
}



switch($op){
  case "sortmemuinsert":
if(empty($_POST['sorttitle']))   redirect_header($_SERVER['PHP_SELF'],0 , _MS_SHARED90);   //空值判斷 	 
redirect_header(XOOPS_URL. "/modules/".$xoopsModule->getVar("dirname")."/admin/menutable.php?op=default".sortmemuinsert()  ,0 , _MS_SHARED02);    
  break;
  
  case "sortmemuupdate":
if(!empty($i)) $ivar='&i='.$i.'';    	  
if(empty($_POST['sorttitle']))   redirect_header($_SERVER['PHP_SELF'].'?sortid='.$sortid.'&status=edit'.$ivar ,0 , _MS_SHARED90);   //空值判斷 	
redirect_header(XOOPS_URL. "/modules/".$xoopsModule->getVar("dirname")."/admin/menutable.php?op=default".sortmemuupdate()  ,0 , _MS_SHARED02);    
  break;    
  
  
 
  case "sortidsortupdate":
redirect_header(XOOPS_URL. "/modules/".$xoopsModule->getVar("dirname")."/admin/menutable.php?op=default".sortidsortupdate()  ,0 , _MS_SHARED02);    
  break; 
 

 
  default:
$main=sortmemu();
}




//-- 所有函數 --//
function sortmemu(){
 global $xoopsDB,$xoopsModule,$sortid,$status,$i,$url,$blocktitle,$groupid;
 

//判斷傳入值
if(!empty($url)) $sortlimit['url']=base64_decode($url);
if(!empty($blocktitle)) $sortlimit['sorttitle']=$blocktitle; 
 

if(!empty($sortid)){ //編輯設定
//讀取neilshopinvoicedonation資料表
//引入模組DB函數-初始化設定
$dbneme="neothemesmenu";
$where=" where `sortid` = '".$sortid."'";
//批次轉換htmlspecialchars
$sortlimit=htmlspecialcharsbatch($allowvar=array("sorttitle","url"),$eachvar=moduledb($dbneme,$where));


$buttonvar="sortmemuupdate";
$hidden="<input type='hidden' name='groupid' value='".$groupid."'><input type='hidden' name='sortid' value='".$sortid."'><input type='hidden' name='i' value='".$i."'>"; 
$sorttitletext=_MA_MENUTABLEPHP07;
//取得上層捲單數值
//$sortidnamedefault=$sortid;
$sortmemu['1']="<h3>"._MA_MENUTABLEPHP05."</h3>";
$sortidboxvar=$sortid;

if(empty($sortlimit['browsegroup'])){
//預設群組
$groupArr = array(); //建構陣列
$groupArr[0]='2'; 
$groupArr[1]='3';  
}

$ivar=$i;  
}else{ //新增設定
$hidden="<input type='hidden' name='groupid' value='".$groupid."'>"; 

$buttonvar="sortmemuinsert";	
$sortlimit['enable']='1';
$sortlimit['target']='0';
$sorttitletext=_MA_MENUTABLEPHP06;
$sortmemu['1']="<h3>"._MA_MENUTABLEPHP04."</h3>";

//預設群組
$groupArr = array(); //建構陣列
$groupArr[0]='2'; 
$groupArr[1]='3';  

$sortlimit['displaytype']='1';

}  
//讀取neothemesmenu資料表 
//取出sortid值按照排列

$wherevar=!empty($groupid) ? "`sortid` = '".$groupid."'" : "`of_sortid` = '0' "; // get TRUE


$dbneme="neothemesmenu";  //資料表名稱	
$where="  where ".$wherevar." order by  sortidsort  ASC";  //where數值
	
//下拉選單
foreach($sortmenu=databasetablewhile($dbneme,$whereval="where `of_sortid` = '0' order by  sortidsort  ASC")  as $key=> $val){
$menudate['sortmenubox'].=optionselect($dataid=$sortmenu[$key]['sortid'],$optionname=$sortmenu[$key]['sorttitle'],$oid=$groupid,$value="".XOOPS_URL."/modules/".$xoopsModule->getVar("dirname")."/admin/menutable.php?groupid=".$sortmenu[$key]['sortid']."",$id="");
} 
	
	
	
$sort=databasetablewhile($dbneme,$where); 


//編輯下拉選單取值
//第一層
$sortid=(!empty($groupid) && empty($sortid)) ? $groupid : $sortid; // get TRUE
$where="  where `sortid` = '".$sortid."'  order by  of_sortid  DESC";  //where數值
$sortselect=moduledb($dbneme,$where);
$sortselectbox.=$sortselect['sortid'].",";


//讀取function.php中sortselect(函數)
if(!empty($sortselect['of_sortid'])){
$sortselectbox.=tplsortselect($dbneme,$sortid="".$sortselect['of_sortid']."");
}

//去除最後一個字元
$ofsortid=cuttingfunction($cutting=substr($sortselectbox,0,-1),$sign=",");



//反轉陣列順序
$ofsortid=array_reverse($ofsortid);
	
	

//第一層
$selectmenu1=$ofsortid[0];
//第二層
$selectmenu2=$ofsortid[1];
//第三層
//$selectmenu3=$ofsortid[2];
//第四層
//$selectmenu4=$ofsortid[3];


 
//下拉選單設定
$where="  where `of_sortid` = '0'  order by  sortidsort  ASC";  //where數值 
$sortoption=databasetablewhile($dbneme,$where);  

//遞回開始
function sortidretorted($dbneme="",$sortid="",$i=""){ 
$where=" where `of_sortid` = '".$sortid."' order by sortidsort ASC";   //where數值
$sortArr=databasetablewhile($dbneme,$where);
empty($i) ? $i=1 : '';  
foreach($sortArr as $key=> $val){
$sortidvar.=$sortArr[$key]['sortid'].",";
if($i<3){ //限制迴圈階層數3層	 
$sortidvar.=sortidretorted($dbneme,$sortid="".$sortArr[$key]['sortid']."",$i+1);
}
}
return $sortidvar;
}

foreach($sort as $key=> $val){
$sortidvar.=$sort[$key]['sortid'].","; 
$sortidvar.=sortidretorted($dbneme,$sortid="".$sort[$key]['sortid']."");

} 
 
$sortidvar=substr($sortidvar,0,-1);
 
 


//遞回開始
function retorted($dbneme="",$sortid="",$i="",$sortidboxvar="",$boxtopid=""){
 global $xoopsModule,$groupid;	
$where="  where `of_sortid` = '".$sortid."' order by  sortidsort  ASC";	
$sortArr=databasetablewhile($dbneme,$where);

empty($i) ? $i=1 : '';  
$arrowspan="<span class='glyphicon glyphicon-arrow-right' aria-hidden=".true."></span> ";
switch($i){
  case "1":
$arrow=$arrowspan;
$stylebox='color2';  
  break;	
  case "2":
$arrow=$arrowspan.$arrowspan;
$stylebox='color3';    
  break;
  case "3":
$arrow=$arrowspan.$arrowspan.$arrowspan;
$stylebox='color4';      
  break;  
  default:
}




foreach($sortArr as $key=> $val){
	
//頁面開啟方式
$td['target']=pagemethod($types=$sortArr[$key]['target']);	

//排序	
$sortidsort=inputtext($class="",$name="sortidsort".$sortArr[$key]['sortid']."",$value="".$sortArr[$key]['sortidsort']."",$placeholder="");
$menudate['sortArr']="<input type='hidden' name='sortArr[]' value='".$sortArr[$key]['sortid']."'>";

  	
$enableforeach=enable($envar=$sortArr[$key]['enable']);


if($sortArr[$key]['sortid']==$sortidboxvar){
$colorstyle2[$key]="class='color1'";
}else{
$colorstyle2[$key]="class='".$stylebox." ".$boxtopid."'";
}

$titlebox[$key]=!empty($sortArr[$key]['url']) ? "<a class='black' target='_blank' href=".$sortArr[$key]['url'].">".$sortArr[$key]['sorttitle']."</a>" : $sortArr[$key]['sorttitle']; // get TRUE


if(!empty($groupid)) $groupid_box[$key]="&groupid=".$groupid."";


$sortvar.="<tr id='deletetr{$sortArr[$key]['sortid']}' ".$colorstyle2[$key].">
<td class='".$boxtopid."'>".$sortidsort."</td>
<td  class='".$boxtopid."'>".$arrow."".$titlebox[$key]."</td>
<td>".groupslabel($Arr=phpconversion($conversion=$sortArr[$key]['browsegroup'],$type="unserialize"),$type="",$style="label-danger")."</td>   	
<td>".neothemesadmin_labeltextfunction($textArr=$sortArr[$key]['displaytype'])."</td>	
<td  class='".$boxtopid."'>".$enableforeach."</td>	
<td  class='".$boxtopid."'>".$td['target']['text']."</td>		
  <td  class='".$boxtopid."'>
       <a href='" .XOOPS_URL. "/modules/". $xoopsModule->getVar("dirname")."/admin/menutable.php?sortid=".$sortArr[$key]['sortid'].$groupid_box[$key]."&status=edit&i=".$i."' class='btn btn-sm  btn-warning  '>"._MS_SHARED08."</a>  
       <a id='deletebotton{$sortArr[$key]['sortid']}'  type='button'   mane='85,".$sortArr[$key]['sortid'].",".$sortArr[$key]['sorttitle'].","._MA_MENUTABLEPHP12."' class='btn  btn-danger btn-sm  deleteeachArr'>"._MS_SHARED09."</a>  
    </td>	
</tr>
".$menudate['sortArr']."		
	
	";
if($i<2){ //限制迴圈階層數2層	 
$sortvar.=retorted($dbneme,$sortid="".$sortArr[$key]['sortid']."",$i+1,$sortidboxvar=$sortidboxvar,$boxtopid=$boxtopid);
}

}

return $sortvar;
}

//第一層迴圈
foreach($sort as $key=> $val){
//頁面開啟方式
$td['target']=pagemethod($types=$sort[$key]['target']);		

//排序	
$sortidsort=inputtext($class="",$name="sortidsort".$sort[$key]['sortid']."",$value="".$sort[$key]['sortidsort']."",$placeholder="");
$menudate['sortArr']="<input type='hidden' name='sortArr[]' value='".$sort[$key]['sortid']."'>";




if($sort[$key]['sortid']==$sortidboxvar){
$colorstyle[$key]="color1";
}else{
$colorstyle[$key]="levelone";
}
$boxtopid="box".$key."";	
$boxtopid2="boxbutton".$key."";	

//判斷子層
$where="  where `of_sortid` = '".$sort[$key]['sortid']."'  order by  of_sortid  DESC";  //where數值
$neoblockmenubuttonList=moduledb($dbneme,$where);

if(!empty($neoblockmenubuttonList)){
$blockbtn="&nbsp;<a type='button' class='".$boxtopid2." btn btn-sm btn-info  boxbuteach'>"._MS_SHARED46."</a>";
}else{
$blockbtn="&nbsp;<p class='boxbuteach'>";
}

$titlebox[$key]=!empty($sort[$key]['url']) ? "<a class='black' target='_blank' href=".$sort[$key]['url'].">".$sort[$key]['sorttitle']."</a>" : $sort[$key]['sorttitle']; // get TRUE

if(!empty($groupid)) $groupid_box[$key]="&groupid=".$groupid."";


$enableforeach=enable($envar=$sort[$key]['enable']);	
$sortvar.="<tr id='deletetr{$sort[$key]['sortid']}' class=".$colorstyle[$key].">
<td>".$sortidsort."</td>	
<td><span class='glyphicon glyphicon-option-horizontal' aria-hidden='true'></span>&nbsp;".$titlebox[$key].$blockbtn."</td>
<td>".groupslabel($Arr=phpconversion($conversion=$sort[$key]['browsegroup'],$type="unserialize"),$type="",$style="label-danger")."</td>    	
<td>".neothemesadmin_labeltextfunction($textArr=$sort[$key]['displaytype'])."</td>	
<td>".$enableforeach."</td>	
<td>".$td['target']['text']."</td>		
  <td>
       <a href='" .XOOPS_URL. "/modules/". $xoopsModule->getVar("dirname")."/admin/menutable.php?sortid=".$sort[$key]['sortid'].$groupid_box[$key]."&status=edit&i=top' class='btn  btn-sm btn-warning  '>"._MS_SHARED08."</a>  
       <a id='deletebotton{$sort[$key]['sortid']}'  type='button' mane='85,".$sort[$key]['sortid'].",".$sort[$key]['sorttitle'].","._MA_MENUTABLEPHP12."' class='btn  btn-danger  btn-sm deleteeachArr'>"._MS_SHARED09."</a>  
    
    </td>	
</tr>
".$menudate['sortArr']."	
	";	

$sortvar.=retorted($dbneme,$sortid="".$sort[$key]['sortid']."",$i="",$sortidboxvar=$sortidboxvar,$boxtopid=$boxtopid);

}

//下拉選單
foreach($sortoption as $key=> $val){
$optionone.=optionselect($dataid=$sortoption[$key]['sortid'],$optionname=$sortoption[$key]['sorttitle'],$oid="".$selectmenu1."",$value=$sortoption[$key]['sortid']);
}




$displaytypeArr = array(); //建構陣列
$i=0;
$displaytypeArr[$i]['id']='1';  //顯示主選單
$displaytypeArr[$i]['title']=_MA_MENUTABLEPHP13;
$i++;
$displaytypeArr[$i]['id']='2';  //顯示底區塊選單
$displaytypeArr[$i]['title']=_MA_MENUTABLEPHP14;



foreach($displaytypeArr as $key=> $val){
//比對陣列	
if(!empty(cuttingfunction($cutting=$sortlimit['displaytype'],$sign=",")) && in_array($displaytypeArr[$key]['id'], cuttingfunction($cutting=$sortlimit['displaytype'],$sign=","))){
 $goid2[$key]=$displaytypeArr[$key]['id'];	
	}else{
 $goid2[$key]=0;		
	}
	
$displaytypebox.=checkbox($dataid="".$displaytypeArr[$key]['id']."",$name="displaytype[]",$value="".$displaytypeArr[$key]['id']."",$optionname="".$displaytypeArr[$key]['title']."",$oid="".$goid2[$key]."");  
}




//可觀看群組
$groupsArr=neillibrary_permissionmenu($types='2',$admingroup="",$browsegroup=$sortlimit['browsegroup'],$groupArr);

//可觀看群組
$groups['2']=$groupsArr[2];

 

//分類名稱
$sorttitle=inputtext($class="",$name="sorttitle",$value="".$sortlimit['sorttitle']."",$placeholder="".$sortlimit['sorttitle'].""); 

//狀態
$enable=radioinline($class="",$name="enable",$text=array(_MS_SHARED04,_MS_SHARED03),$checked=$sortlimit['enable']); 
//按鈕連結
$url=inputtext($class="",$name="url",$value="".$sortlimit['url']."",$placeholder="".$sortlimit['url'].""); 
//按鈕連結開啟方式
$target=radioinline($class="",$name="target",$text=array(_MS_SHARED31,_MS_SHARED32),$checked="".$sortlimit['target'].""); 


//引入模組DB函數-初始化設定
$configList=moduledb($dbneme="neothemesconfig",$where=" where `nsn` = '1'");
//字串轉陣列
$fieldsplit=phpconversion($conversion=$configList['field'],$type="unserialize"); 

//可建立按鈕數量
$upperlimit=sprintf(_MA_MENUTABLEPHP02,$fieldsplit[15],$fieldsplit[16]);


$main="<div id='menutablebox'>
<h2>"._MA_MENUTABLEPHP01."&nbsp;<label for='sel2'>themes button admin</label>&nbsp; <a class='btn btn-warning '  href='" . XOOPS_URL . "/modules/".$xoopsModule->getVar("dirname")."/admin/menutable.php'>"._MS_SHARED25."</a>  </h2>	
<span>".$upperlimit."</span>
          <hr />
    <div class='form-group container'>
<div class='row'>

<div class='col-md-4'>	

	<form method='post'  action='{$_SERVER['PHP_SELF']}'> 
	
	".$sortmemu['1']."<hr />
<div id='modulesid' name='".$xoopsModule->getVar("dirname").",,,,".$ivar."'></div>		
<div class='row'>					
<label class='col-sm-4'>"._MA_MENUTABLEPHP03."</label>	
	 <div class='col-sm-8'>
	 <select id=default class='form-control' name='of_sortid' >
    <option   data-id='0' value='0'>"._MA_MENUTABLEPHP03."</option>
    	".$optionone."   
  </select>	 <br />
      <span id='selectboxname' mane='".$selectmenu1.",".$status.",".$selectmenu2.",neothemesmenu'></span>
   	<div id='selectbox'></div>	
   	

  	

  	</div> 

</div> 


	
	
<div class='row'>
<label class='col-sm-4'>".$sorttitletext."</label>	
	 <div class='col-sm-8'> ".$sorttitle."    
  	<br /></div> 
</div> 	

<div class='row'>
<label class='col-sm-4'>"._MA_MENUTABLEPHP08."</label>	
	 <div class='col-sm-8'> ".$url."    
  	<br /></div> 
</div> 
	
<div class='row'>
<label class='col-sm-4 control-label'>"._MS_SHARED91."</label>	
	 <div class='col-sm-8'>
	<select  class='form-control' id='sel1'  name='browsegroup[]' multiple  >
    	".$groups['2']."   
  </select>		  
  	<br />
  <span id='helpBlock' class='help-block'>"._MS_SHARED92."</span><br /><br />
  	</div> 
</div> 	



<div class='row'>
<label class='col-sm-4 control-label'>"._MA_MENUTABLEPHP15."</label>	
	 <div class='col-sm-8'>
".$displaytypebox."	  
  	<br />
  <span id='helpBlock' class='help-block'>"._MA_MENUTABLEPHP16."</span>  <br /><br />
  	</div> 
</div> 	
	
	
	
	
<div class='row'>
<label class='col-sm-4'>"._MA_MENUTABLEPHP09."</label>	
	 <div class='col-sm-8'> ".$target."    
  	<br /><br /></div> 
</div> 			
	

	
<div class='row'>
<label class='col-sm-4'>"._MS_SHARED05."</label>	
	 <div class='col-sm-8'> ".$enable."    
  	<br /><br /><br /></div> 
</div> 	
	
".$hidden."	 
<input type='hidden' name='op' value='".$buttonvar."'>  	
 <button  type='submit' class='btn btn-primary btn-lg btn-block button'>"._MS_SHARED01."</button>
	
		
		
		  </form> <br />

   </div> 
 <div class='col-md-8'>
	<select  id='FriendLink1' class='form-control' >
    <option value='".XOOPS_URL."/modules/".$xoopsModule->getVar("dirname")."/admin/menutable.php'  >"._MA_MENUTABLEPHP20."</option>
   ".$menudate['sortmenubox']."
  </select><br />

 	   
 	   	<form method='post'  action='{$_SERVER['PHP_SELF']}'> 
 	   <div id='deletename' mane='".$sortidvar."'></div>
  <table class='table basictable'>
    <thead>
      <tr>
        <th style='width: 10%;'>"._MS_SHARED11."</th>
        <th style='width: 42%;'>"._MA_MENUTABLEPHP10."</th>
        <th style='width: 4%;'>"._MS_SHARED94."</th>
          <th style='width: 4%;'>"._MA_MENUTABLEPHP17."</th>      	
         <th  style='width: 8%;'>"._MS_SHARED10."</th>
                 <th style='width: 13%;'>"._MS_SHARED43."</th>   
        <th style='width: 19%;'>"._MS_SHARED119."</th>
      </tr>
    </thead>
    <tbody>
   ".$sortvar." 
 
    </tbody>
  </table> 	   
  
 <input type='hidden' name='groupid' value='".$groupid."'>  	
 <input type='hidden' name='op' value='sortidsortupdate'>  	
 <button  type='submit' class='btn btn-primary btn-lg btn-block button'>"._MS_SHARED12."</button>
	
		
		
		  </form> 	   
 	   
 	   	  </div>    
      
       </div> </div>  
  	  
  	  </div>  
 <script src='".XOOPS_URL."/modules/neillibrary/js/menujs/newbase.js'></script> 
<script>
    $('.basictable').basictable({
        breakpoint: 640,
        orceResponsive: true,
    });
</script> 	 		  	  	   
  "; 
if(!empty(modulesmidvar($modulesid="xLanguage",$var="isactive"))){
$main.="<script>neillibrary_llanguagetext(afterid='#buttonbar');</script>";
}
 return $main;
}

//更新分類
function sortmemuupdate(){
  global $xoopsDB,$sortid,$groupid;   ;   

//讀取父分類
$dbneme="neothemesmenu";
$where="  where `sortid` = '".$sortid."'  order by  of_sortid  DESC";  //where數值
$sortselect=moduledb($dbneme,$where);

$_POST['of_sortid']=$sortid!=$_POST['of_sortid'] ? $_POST['of_sortid']: $sortselect['of_sortid']; 

//去資料表標籤-去除空白跟HTML標籤
$_POST=strip_tagsremovesqlArr($allowvar=array("url"),$postvar=$_POST,$sqltags=array("sorttitle"));  

$setvar="set of_sortid='{$_POST['of_sortid']}' , sorttitle='{$_POST['sorttitle']}' , url='{$_POST['url']}' , target='{$_POST['target']}', enable='{$_POST['enable']}', browsegroup='".phpconversion($conversion=$_POST['browsegroup'],$type="serialize")."' , displaytype='".cuttingfunction($cutting=$_POST['displaytype'],$sign=",",$type="2")."'  where sortid='".$sortid."'";
update($dbname="neothemesmenu",$set=$setvar);

return combinationvar($Arrvar=array("groupid"));
}


//更新排序
function  sortidsortupdate(){
   global $xoopsDB,$groupid; 
//取得sortid值
/*$dbneme="neothemesmenu";

$sort=databasetablewhile($dbneme,$where);
foreach($sort as $key=> $val){
$digital= 'sortidsort'.$sort[$key]['sortid'];   
$setvar="set sortidsort='{$_POST[$digital]}'   where sortid='".$sort[$key]['sortid']."'";
update($dbname="neothemesmenu",$set=$setvar); 

}*/

foreach($_POST['sortArr'] as  $val){
$keyname[$val]="sortidsort".$val;	
 $setvar="set sortidsort='".$_POST[$keyname[$val]]."'   where sortid='".$val."'";
update($dbname="neothemesmenu",$set=$setvar); 
}


return combinationvar($Arrvar=array("groupid")); 



//die();

}



//新增按鈕分類
function sortmemuinsert(){
global $xoopsDB,$groupid;   

//去資料表標籤-去除空白跟HTML標籤
$_POST=strip_tagsremovesqlArr($allowvar=array("url"),$postvar=$_POST,$sqltags=array("sorttitle"));  

$valuesvar="(of_sortid,sortidsort,sorttitle,url,target,enable,browsegroup,displaytype) values('{$_POST['of_sortid']}','0','{$_POST['sorttitle']}','{$_POST['url']}','{$_POST['target']}','{$_POST['enable']}','".phpconversion($conversion=$_POST['browsegroup'],$type="serialize")."','".cuttingfunction($cutting=$_POST['displaytype'],$sign=",",$type="2")."')";	  
insert($dbname="neothemesmenu",$values=$valuesvar);


return combinationvar($Arrvar=array("groupid"));
}



loadModuleAdminMenu(6);
echo $main;

include_once "footer.php"; //XOOPS檔尾	
	
	?>