<?php
//自訂按鈕共用function

//模組反安裝移除按鈕資料
function neillibrary_deletefunction_menuall($modulesid=""){
//刪除按鈕
$dbneme="neillibrary_modulesmenu";
$where="  where   modulesid = '".$modulesid."'   order by  menuid  DESC";  //where數值 
$delettimeArr=databasetablewhile($dbneme,$where);  
foreach($delettimeArr as $key=> $val){	
//刪除neillibrary_modulesmenu資料
deletefunction($where=" where `menuid` = '".$delettimeArr[$key]['menuid']."'",$DBname="neillibrary_modulesmenu");
}
}


//迴圈刪除自訂按鈕
function neillibrary_memudeletfunction($pageid="",$menutype="",$modulesid=""){
global $xoopsDB;	
$memudeletArr=databasetablewhile($dbneme="neillibrary_modulesmenu",$where=" where `modulesid` = '".$modulesid."'  &&  `menutype` = '".$menutype."'   &&  `pageid` = '".$pageid."' ");
foreach($memudeletArr as $key=> $i){
deletefunction($where=" where `menuid` = '".$memudeletArr[$key]['menuid']."'",$dbneme="neillibrary_modulesmenu");
}
}



//前台讀取自訂按鈕
function neillibrary_sort_memureadfunction($pageid="",$menutype=""){
global $xoopsDB,$xoopsModule;	

$dbneme="neillibrary_modulesmenu";
$where=" where  `modulesid` = '".$xoopsModule->getVar("dirname")."'  &&  `menutype` = '".$menutype."' &&  `pageid` = '".$pageid."'  &&  `enable` = '1'   order by  btnsort ASC"; 
$modulesmenuArr=databasetablewhile($dbneme,$where);  

foreach($modulesmenuArr as $key=> $val){	
$target[$key]=pagemethod($types=$modulesmenuArr[$key]['target']);	
$modulesmenuArr[$key]['target']=$target[$key]['way'];	
}


return $modulesmenuArr;	
}





//判斷自訂按鈕是否存在
function neillibrary_sort_memuexistfunction($type='',$pageid="",$menutype=""){
global $xoopsDB,$xoopsModule;	
	
//判斷自訂按鈕
$dbneme="neillibrary_modulesmenu";
$where=" where   `modulesid` = '".$xoopsModule->getVar("dirname")."'  &&  `menutype` = '".$menutype."' &&  `pageid` = '".$pageid."'  order by  btnsort ASC"; 
	
switch($type){
  case "1":
  //執行對應函數
  break;
  
  default:
$countvar = is_array(databasetablewhile($dbneme,$where)) ? count(databasetablewhile($dbneme,$where)) : 0 ;
if(!empty($countvar)) $memuexist=" <span title='"._MS_MENU_SHARED13."' class='glyphicon glyphicon-menu-hamburger red' aria-hidden='true'></span>";
}	
 return $memuexist;	
}




//自訂按鈕分類管理介面
function neillibrary_custommenufunctionback(){
//需global變數 $pageid,$menutype,$title,$menuid;		
global $xoopsDB,$xoopsModule,$pageid,$menutype,$title,$menuid,$typesid,$sortid,$suid;	



//表頭設定(在模組的function.php)



switch($xoopsModule->getVar("dirname")){
  case "neilofficebriefing":
$menutypetext=neilofficebriefing_Buttonparameters();
  break;
  
  case "neilmfdcms":
$menutypetext=neilmfdcms_Buttonparameters();
  break;
  
  case "neilsignedup_c":
$menutypetext=neilsignedup_c_Buttonparameters();
  break;
  
  case "neilsignedup_a":
$menutypetext=neilsignedup_a_Buttonparameters();
  break;
  
  case "neilperiodical":
$menutypetext=neilperiodical_Buttonparameters();
  break;
  
  case "neilfilefolder":
$menutypetext=neilfilefolder_Buttonparameters();
  break;
  
  case "neilalbum":
$menutypetext=neilalbum_Buttonparameters();
  break;
  
  case "neilproduct":
$menutypetext=neilproduct_Buttonparameters();
  break;
  
  
  default:
  //預設動作
}




if(!empty($typesid)) $custommenu_table_form['9'].="<input type='hidden' name='typesid' value='".$typesid."'>";
if(!empty($sortid)) $custommenu_table_form['9'].="<input type='hidden' name='sortid' value='".$sortid."'>";
if(!empty($suid)) $custommenu_table_form['9'].="<input type='hidden' name='suid' value='".$suid."'>";

if(empty($menuid)){  //新增
$custommenu_table_form['7']="<h3>"._MS_MENU_SHARED06."</h3>";
//POST類型

$menutypetext[8]=!empty($menutypetext[8]) ? $menutypetext[8] : 'custommenusave_news'; // get TRUE


$custommenu_table_form['9'].="
<input type='hidden' name='modulesid' value='".$xoopsModule->getVar("dirname")."'>
<input type='hidden' name='menutype' value='".$menutype."'>	
<input type='hidden' name='pageid' value='".$pageid."'>	
<input type='hidden' name='title' value='".$title."'>	
<input type='hidden' name='op' value='".$menutypetext[8]."'>";
$menulist['enable']=1;
$menulist['target']=0;



}else{  //編輯
$custommenu_table_form['7']="<h3>"._MS_MENU_SHARED07."</h3>";

//開啟neothemesflash資料表
$dbneme="neillibrary_modulesmenu";
$where="  where `menuid` = '".$menuid."'";  //where數值
//批次轉換htmlspecialchars
$menulist=htmlspecialcharsbatch($allowvar=array("btntitle","url"),$eachvar=moduledb($dbneme,$where));

$menutypetext[9]=!empty($menutypetext[9]) ? $menutypetext[9] : 'custommenuupdated'; // get TRUE
	
//POST類型
$custommenu_table_form['9'].="
<input type='hidden' name='menuid' value='".$menulist['menuid']."'>
<input type='hidden' name='modulesid' value='".$xoopsModule->getVar("dirname")."'>
<input type='hidden' name='menutype' value='".$menutype."'>	
<input type='hidden' name='pageid' value='".$pageid."'>	
<input type='hidden' name='title' value='".$title."'>	
<input type='hidden' name='op' value='".$menutypetext[9]."'>";
}


//if(!empty($sortid)) $sortidvar="&sortid=".$sortid."";


//按鈕名稱
$custommenu_table_form['0']=inputtext($class="",$name="btntitle",$value="".$menulist['btntitle']."",$placeholder=""._MS_SHARED142.""._MS_MENU_SHARED08."",$id="");	
//按鈕連結
$custommenu_table_form['1']=inputtext($class="",$name="url",$value="".$menulist['url']."",$placeholder=""._MS_SHARED142.""._MS_MENU_SHARED09."",$id="");	

//按鈕開啟方式
$custommenu_table_form['2']=radioinline($class="",$name="target",$text=array(_MS_SHARED31,_MS_SHARED32),$checked="".$menulist['target'].""); 
//是否啟用
$custommenu_table_form['4']=radioinline($class="",$name="enable",$text=array(_MS_SHARED04,_MS_SHARED03),$checked="".$menulist['enable'].""); 
 

//TABLE區塊
$dbneme="neillibrary_modulesmenu";
$where=" where   `modulesid` = '".$xoopsModule->getVar("dirname")."'  &&  `menutype` = '".$menutype."' &&  `pageid` = '".$pageid."'  order by  btnsort ASC"; 
$menuArr=databasetablewhile($dbneme,$where);  
	
foreach($menuArr as $key=> $val){
$td['enable']=enable($envar=$menuArr[$key]['enable']);
//頁面開啟方式
$td['target']=pagemethod($types=$menuArr[$key]['target']);

if($menuArr[$key]['menuid']==$menuid){
$colorstyle[$key]="color1";
}



	
$custommenu_table_form['6'].="<tr class='deletetr{$key} tr ".$colorstyle[$key]."' id='".$menuArr[$key]['menuid']."'  >
        <td>".$menuArr[$key]['menuid']." </td>
        <td><div id='btntitlebox'>".$title."</div></td>
        <td><a  target='_blank' href='".$menuArr[$key]['url']."'>".$menuArr[$key]['btntitle']."</a></td>
        <td>".$td['target']['text']."</td>
        <td>".$td['enable']."</td>
        <td><a href='" .XOOPS_URL. "/modules/". $xoopsModule->getVar("dirname")."/".$menutypetext[7]."&pageid=".$pageid."&menutype=".$menutype."&title=".$title."&menuid=".$menuArr[$key]['menuid'].$sortidvar."'  class='btn btn-sm btn-info  active'>"._MS_SHARED08."</a>   
     <a id='deletebotton{$key}' type='button' mane='165,".$menuArr[$key]['menuid'].",".$menuArr[$key]['title'].","._MS_MENU_SHARED12.",,neillibrary_modulesmenu,".$xoopsModule->getVar("dirname").",,menuid,".$menutype.",".$pageid.",".$typesid.",".$sortid.",".$suid."'  class='btn btn-sm btn-danger   deleteeach'>"._MS_SHARED09."</a>     
      </tr>";
}
 
$custommenu_table_form['5']="
<table class='table sortable basictable' name='164,".$xoopsModule->getVar("dirname").",".$menutype.",".$pageid.",".$typesid.",".$sortid.",".$suid."'>
    <thead>
      <tr >
      
        <th style='width: 5%'>ID</th>
        <th style='width: 25%'>".$menutypetext[0]."</th>        	
           <th style='width: 15%'>"._MS_MENU_SHARED08."</th>
           <th style='width: 12%'>"._MS_SHARED43."</th>
               <th style='width: 10%'>"._MS_SHARED10."</th>  
                 <th style='width: 15%'>"._MS_SHARED07."</th>  
      </tr>
    </thead>
    <tbody>
   ".$custommenu_table_form['6']."
     
    </tbody>
  </table>
";
 
$main.="<div class='".$menutypetext[4]."'>
	
".$menutypetext[5]."
  

    
    
    <div class='row'>
    ";
//左邊區塊
$main.="
<form method='post'  action='{$_SERVER['PHP_SELF']}' >	
  <div class='col-md-4'>".$custommenu_table_form['7']."<hr />
 
 
 
<div  class='form-group'>	
<label class='col-sm-3 control-label'>"._MS_MENU_SHARED08."</label>
 		 <div class='col-sm-9'> ".$custommenu_table_form['0']." 
  	   <br /></div> 
 	 <hr />	</div> 	
 
 
<div  class='form-group'>	
<label class='col-sm-3 control-label'>"._MS_MENU_SHARED09."</label>
 		 <div class='col-sm-9'> ".$custommenu_table_form['1']." 
  	   <br /></div> 
 	 <hr />	</div> 	

<div  class='form-group'>	
<label class='col-sm-3 control-label'>"._MS_SHARED43."</label>
 		 <div class='col-sm-9'> ".$custommenu_table_form['2']." 
  	   <br /><br /></div> 
 	 <hr />	</div> 	

			   	  
  	  
<div  class='form-group'>	
<label class='col-sm-3 control-label'>"._MS_SHARED44."</label>
 		 <div class='col-sm-9'> ".$custommenu_table_form['4']." 
  	   <br /><br /></div> 
 	 <hr />	</div> 	
  	  
 
  ".$custommenu_table_form['9']."	
 <button  type='submit' class='btn btn-primary btn-lg btn-block button'>"._MS_SHARED01."</button>
  	  
  	  </div>
   </form> 	<br />  
  	  
    ";
//右邊區塊
$main.="
  <div class='col-md-8'><h3>"._MS_MENU_SHARED10."</h3><hr />
  	  ".$custommenu_table_form['5']."
  <span id='helpBlock' class='help-block text-center'>"._MS_SHARED143."</span>	  
    	  </div>

    
    ".$menutypetext[6]."
    <script>var itemsvar='".$menutypetext[3]."'; dragmenufunction();</script>  	  
    ";
$main.="
</div>
<script>
    $('.basictable').basictable({
        breakpoint: 640,
        orceResponsive: true,
    });
</script> 	
	";



$afterid=!empty($menutype) ? '#xo-content .block-title' : '#buttonbar_mod'; // get TRUE
	
if(!empty(modulesmidvar($modulesid="xLanguage",$var="isactive"))){
$main.="<script>neillibrary_llanguagetext(afterid='{$afterid}');</script>";
}
 return $main;
}



//新增自訂按鈕
function neillibrary_custommenusave_news(){
  global $xoopsDB,$pageid,$menutype,$title;   
  
$httphosturl=httphosturl($keywordid="");
//填寫按鈕名稱  	  
if(empty($_POST['btntitle']))   redirect_header($httphosturl['request_uri'].'?op=custommenu'.combinationvar($Arrvar=array("typesid","pageid","sortid","suid","menutype","title")),0 , _MS_MENU_SHARED16);   
  
  
//去資料表標籤-去除空白跟HTML標籤
$_POST=strip_tagsremovesqlArr($allowvar=array("url"),$postvar=$_POST,$sqltags=array("btntitle"));  
//新增資料
$valuesvar="
(modulesid,menutype,pageid,btntitle,url,btnsort,target,enable) 
values
('{$_POST['modulesid']}','{$_POST['menutype']}','{$_POST['pageid']}','{$_POST['btntitle']}','{$_POST['url']}','0','{$_POST['target']}','{$_POST['enable']}')";	  
$menuid=insert($dbname="neillibrary_modulesmenu",$values=$valuesvar);
//寫入排序
$setvar="set btnsort='".$menuid."'   where menuid='".$menuid."'";
update($dbname="neillibrary_modulesmenu",$set=$setvar); 
return combinationvar($Arrvar=array("pageid","sortid","menutype","title"));
}



//更新自訂按鈕
function  neillibrary_custommenuupdated(){
global $xoopsDB,$menuid,$pageid,$menutype,$title;  	

$httphosturl=httphosturl($keywordid="");
//填寫按鈕名稱  	  
if(empty($_POST['btntitle']))   redirect_header($httphosturl['request_uri'].'?op=custommenu'.combinationvar($Arrvar=array("typesid","pageid","sortid","suid","menutype","title")),0 , _MS_MENU_SHARED16);   
  

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

$setvar="set btntitle='{$_POST['btntitle']}' , url='{$_POST['url']}' , target='{$_POST['target']}' , enable='{$_POST['enable']}'  where menuid='".$menuid."'";
update($dbname="neillibrary_modulesmenu",$set=$setvar);
return combinationvar($Arrvar=array("pageid","sortid","menutype","title"));

}



?>