<?php
	
//地址組合
function neillibrary_addresscombination($address="",$citytype=""){	
$addressArr=phpconversion($conversion=$address,$type="unserialize");

//開啟縣市鄉鎮資料表
$dbneme="neillibrary_citydate";  //資料表名稱
$where="  where `cityid` = '".$addressArr['of_cityid']."'  order by  of_cityid  DESC";  //where數值
$city1=moduledb($dbneme,$where);

if($addressArr['of_cityid']!=$addressArr['of_cityid1']){
$where="  where `cityid` = '".$addressArr['of_cityid1']."'  order by  of_cityid  DESC";  //where數值
$city2=moduledb($dbneme,$where);
}
//組合資料

switch($citytype){
  case "1":
  //縣市區
$address=$city1['citytitle'].$city2['citytitle'];  
  break;

  default:
//完整地址  
$address=$city1['citytitle'].$city2['citytitle'].$addressArr['of_cityid2'];    	  

}



return  $address;

}
	
	
//縣市鄉鎮下拉選單
function neillibrary_countiesajaxmenu($cityid="",$status="",$defaultval=""){	
//下拉選單設定
//讀取neillibrary_citydate資料表 
//取出cityid值按照排列
$dbneme="neillibrary_citydate";  //資料表名稱

//編輯下拉選單取值
//第一層
$where="  where `cityid` = '".$cityid."' &&  `enable` = '1'  order by  of_cityid  DESC";  //where數值
$sortselect=moduledb($dbneme,$where);
$sortselectbox.=$sortselect['cityid'].",";



//讀取function.php中sortselect(函數)
if(!empty($sortselect['of_cityid'])){
$sortselectbox.=sortselect($dbneme,$cityid="".$sortselect['of_cityid']."",$basid="",$enfunction="",$type="3");
}

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

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

$defaultval['countiesstatus']=$status;	
//第一層
$defaultval['countiesselectmenu1']=$ofcityid[0];
//第二層
$defaultval['countiesselectmenu2']=$ofcityid[1];

//下拉選單設定
$cityoption=databasetablewhile($dbneme="neillibrary_citydate",$where="  where `of_cityid` = '0'  order by  sortidsort  ASC");  

//下拉選單
foreach($cityoption as $key=> $val){
$optionone.=optionselect($dataid=$cityoption[$key]['cityid'],$optionname=$cityoption[$key]['citytitle'],$oid="".$ofcityid[0]."",$value=$cityoption[$key]['cityid']);
}

$defaultval['countiesoptionone']=$optionone;

return $defaultval;
}
	
	
	
	
	
	
	

//城市鄉鎮管理
function neillibrary_cityfunction(){
 global $xoopsModule,$status,$i,$cityid,$i;
 
 
if(empty($cityid)){ //新增
$citymemu['1']="<h4>"._MS_CITY_MENU02."</h4>";
$citymemu['2']=_MS_CITY_MENU05;
$citylist['enable']='1';
$buttonvar="cityinsert";
 }else{  //編輯
 
//引入模組DB函數-初始化設定
//批次轉換htmlspecialchars
$citylist=htmlspecialcharsbatch($allowvar=array("citytitle"),$eachvar=moduledb($dbneme="neillibrary_citydate",$where=" where `cityid` = '".$cityid."'"));
 
$citymemu['1']="<h4>"._MS_CITY_MENU03."</h4>";
$citymemu['2']=_MS_CITY_MENU06;
$hidden="<input type='hidden' name='cityid' value='".$cityid."'><input type='hidden' name='i' value='".$i."'>"; 
$buttonvar="cityupdate";


 }
 


//讀取neillibrary_citydate資料表 
//取出cityid值按照排列
$dbneme="neillibrary_citydate";  //資料表名稱
$where="  where `of_cityid` = '0'  order by  sortidsort  ASC";  //where數值
$sort=databasetablewhile($dbneme,$where); 


//編輯下拉選單取值
//第一層
$where="  where `cityid` = '".$cityid."'  order by  of_cityid  DESC";  //where數值
$sortselect=moduledb($dbneme,$where);
$sortselectbox.=$sortselect['cityid'].",";


//讀取function.php中sortselect(函數)
if(!empty($sortselect['of_cityid'])){
$sortselectbox.=sortselect($dbneme,$cityid="".$sortselect['of_cityid']."",$basid="",$enfunction="",$type="3");
}

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



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

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



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

empty($i) ? $i=1 : '';  
$arrowspan="<i class='fa-solid fa-arrow-right'></i> ";
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){
	

//排序
$sortidsort=inputtext($class="",$name="sortidsort{$sortArr[$key]['cityid']}",$value="".$sortArr[$key]['sortidsort']."",$placeholder="".$sortArr[$key]['sortidsort']."");
  	
$enableforeach=enable($envar=$sortArr[$key]['enable']);


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


$sortvar.="<tr id='deletetr{$sortArr[$key]['cityid']}' ".$colorstyle2[$key].">
<td class='".$boxtopid."'>".$sortidsort."</td>
<td  class='".$boxtopid."'>".$arrow."".$sortArr[$key]['citytitle']."</td>
<td  class='".$boxtopid."'>".$enableforeach."</td>	
  <td  class='".$boxtopid."'>
       <a href='" .XOOPS_URL. "/modules/". $xoopsModule->getVar("dirname")."/admin/settings.php?op=city&cityid=".$sortArr[$key]['cityid']."&status=edit&i=".$i."' class='btn  btn-warning btn-sm '>"._MS_SHARED08."</a>  
       <a id='deletebotton{$sortArr[$key]['cityid']}'  href='#NO'   name='66,".$sortArr[$key]['cityid'].",".$sortArr[$key]['citytitle'].","._MS_CITY_MENU09.",neillibrary_citydate,,,,,".$xoopsModule->getVar("dirname").",cityid,1' class='btn  btn-danger btn-sm deleteeachArr'>"._MS_SHARED09."</a>  
    </td>	
</tr>
	
	";
if($i<2){ //限制迴圈階層數2層	 
$sortvar.=retorted($dbneme,$cityid="".$sortArr[$key]['cityid']."",$i+1,$cityidboxvar=$cityidboxvar,$boxtopid=$boxtopid);
}

}

return $sortvar;
}

//第一層迴圈
foreach($sort as $key=> $val){
	
//排序
$sortidsort=inputtext($class="",$name="sortidsort{$sort[$key]['cityid']}",$value="".$sort[$key]['sortidsort']."",$placeholder="".$sort[$key]['sortidsort']."");

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

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

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

//顯示選單功能標籤

$enableforeach=enable($envar=$sort[$key]['enable']);	
$sortvar.="<tr id='deletetr{$sort[$key]['cityid']}' class=".$colorstyle[$key].">
<td>".$sortidsort."</td>	
<td><i class='fa-solid fa-grip'></i>&nbsp;".$sort[$key]['citytitle']."".$blockbtn."</td>
<td>".$enableforeach."</td>		
  <td>
       <a href='" .XOOPS_URL. "/modules/". $xoopsModule->getVar("dirname")."/admin/settings.php?op=city&cityid=".$sort[$key]['cityid']."&status=edit&i=top' class='btn btn-sm btn-warning  '>"._MS_SHARED08."</a>  
       <a id='deletebotton{$sort[$key]['cityid']}'  href='#NO' name='66,".$sort[$key]['cityid'].",".$sort[$key]['citytitle'].","._MS_CITY_MENU09.",neillibrary_citydate,,,,,".$xoopsModule->getVar("dirname").",cityid,1' class='btn  btn-danger btn-sm  deleteeachArr'>"._MS_SHARED09."</a>  
    
    </td>	
</tr>";	

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

}
 
 
 
 
 
 
//城市名稱
$citymemu['3']=inputtext($class="",$name="citytitle",$value="".$citylist['citytitle']."",$placeholder="".$citylist['citytitle'].""); 

//狀態
$citymemu['4']=radioinline($class="",$name="enable",$text=array(_MS_SHARED04,_MS_SHARED03),$checked=$citylist['enable']); 


//下拉選單設定
$cityoption=databasetablewhile($dbneme="neillibrary_citydate",$where="  where `of_cityid` = '0'  order by  sortidsort  ASC");  

//下拉選單
foreach($cityoption as $key=> $val){
$citymemu['5'].=optionselect($dataid=$cityoption[$key]['cityid'],$optionname=$cityoption[$key]['citytitle'],$oid="".$selectmenu1."",$value=$cityoption[$key]['cityid']);
}


  //<span id='cityselectboxname' name='".$selectmenu1.",".$status.",".$selectmenu2.",of_cityid'></span>	   	<div id='cityselectbox'></div>	
$main="<div class='container'>
  <h4>"._MS_CITY_MENU01."&nbsp;<label for='sel2'>City and town admin</label>&nbsp; <a class='btn btn-warning '  href='" . XOOPS_URL . "/modules/".$xoopsModule->getVar("dirname")."/admin/settings.php?op=city'>"._MS_SHARED25."</a>  </h4>	
          <hr />
    <div class='form-group col-md-12'>
<div class='row'>

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

	<form method='post'  action='{$_SERVER['PHP_SELF']}'> 
	
	".$citymemu['1']."<hr />
		
<div class='row'>		
<label class='col-sm-4'>"._MS_CITY_MENU04."</label>	

	 <div class='col-sm-8'>
	 <select id=citydefault class='form-control' name='of_cityid' >
    <option   data-id='0' value='0'>"._MS_CITY_MENU04."</option>
    	".$citymemu['5']."   
  </select>	 <br />
    

   	

  	

  	</div> 

</div> 


	
	
<div class='row'>
<label class='col-sm-4'>".$citymemu['2']."</label>	
	 <div class='col-sm-8'> ".$citymemu['3']."    
  	<br /></div> 
</div> 	

	
<div class='row'>
<label class='col-sm-4'>"._MS_SHARED05."</label>	
	 <div class='col-sm-8'> ".$citymemu['4']."    
  	<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-7'>
 	   	<form method='post'  action='{$_SERVER['PHP_SELF']}'> 
  <table class='table basictable'>
    <thead>
      <tr>
        <th style='width: 11%;'>"._MS_SHARED11."</th>
        <th style='width: 45%;'>"._MS_CITY_MENU07."</th> 	
         <th  style='width: 13%;'>"._MS_SHARED10."</th>
        <th style='width: 18%;'>"._MS_SHARED119."</th>
      </tr>
    </thead>
    <tbody>
   ".$sortvar." 
 
    </tbody>
  </table> 	   
 	   
 <input type='hidden' name='op' value='citysortupdate'>  	
 <button  type='submit' class='btn btn-primary btn-lg btn-block button'>"._MS_SHARED12."</button>
	
		
		
		  </form> 	   
 	   
 	   	  </div>    
      
       </div> </div>  
  	  
  	  </div>  
    <script>citymenufunction();menucollapsefunction();deletecategory();</script>  	
   <script>
    $('.basictable').basictable({
        breakpoint: 640,
        orceResponsive: true,
    });
</script> 	 	    
  "; 
 
if(!empty(modulesmidvar($modulesid="xLanguage",$var="isactive"))){
$main.="<script>neillibrary_llanguagetext(afterid='#buttonbar_mod');</script>";
}
 
  
 return $main;
	
	

}



//新增城市
function neillibrary_cityinsert(){
  global $xoopsDB;   

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

$valuesvar="(of_cityid,country,citytitle,enable) values('{$_POST['of_cityid']}','0','{$_POST['citytitle']}','{$_POST['enable']}')";	  
$cityid=insert($dbname="neillibrary_citydate",$values=$valuesvar);

//存入排序
$setvar="set sortidsort='".$cityid."'    where cityid='".$cityid."'";
update($dbname="neillibrary_citydate",$set=$setvar);

}

//編輯城市
function neillibrary_cityupdate(){
  global $xoopsDB,$cityid;   

//讀取父分類
$dbneme="neillibrary_citydate";
$where="  where `cityid` = '".$cityid."'  order by  of_cityid  DESC";  //where數值
$sortselect=moduledb($dbneme,$where);
	
$_POST['of_cityid']=$cityid!=$_POST['of_cityid'] ? $_POST['of_cityid']: $sortselect['of_cityid']; 

//去資料表標籤-去除空白跟HTML標籤
$_POST=strip_tagsremovesqlArr($allowvar="",$postvar=$_POST,$sqltags=array("citytitle"));  
   
   
$setvar="set of_cityid='{$_POST['of_cityid']}' , country='0' , citytitle='{$_POST['citytitle']}' , enable='{$_POST['enable']}'   where cityid='".$cityid."'";
update($dbname="neillibrary_citydate",$set=$setvar);

}


//更新排序
function  neillibrary_citysortupdate(){
   global $xoopsDB; 

$sort=databasetablewhile($dbneme="neillibrary_citydate",$where="");
foreach($sort as $key=> $val){
$digital= 'sortidsort'.$sort[$key]['cityid'];   

//判斷排序是否有值傳入
$_POST[$digital]=!empty($_POST[$digital]) ? $_POST[$digital] : $sort[$key]['sortidsort']; // get TRUE

$setvar="set sortidsort='{$_POST[$digital]}'   where cityid='".$sort[$key]['cityid']."'";
update($dbname="neillibrary_citydate",$set=$setvar); 

}


}



?>