<?php
	
//neillibrary_classmenu	
//班級年級下拉選單
function neillibrary_classmenu($classid="",$status="",$defaultval=""){	
//下拉選單設定
//讀取neillibrary_class資料表 
$dbneme="neillibrary_class";  //資料表名稱

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



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

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

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

$defaultval['classstatus']=$status;	
//第一層
$defaultval['classselectmenu1']=$of_classid[0];
//第二層
$defaultval['classselectmenu2']=$of_classid[1];

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

//下拉選單
foreach($option as $key=> $val){
$optionone.=optionselect($dataid=$option[$key]['classid'],$optionname=$option[$key]['classtitle'],$oid="".$of_classid[0]."",$value=$option[$key]['classid']);
}

$defaultval['classoptionone']=$optionone;

return $defaultval;
}
	
	
	
	
//班級管理
function neillibrary_classdefaultn(){
 global $xoopsModule,$status,$i,$classid,$i;
 
 
if(empty($classid)){ //新增
$classbox['1']="<h3>"._MA_CLASSADMINPHP02."</h3>";
$classbox['2']=_MA_CLASSADMINPHP04;
$classList['enable']='1';
$buttonvar="classinsert";
 }else{  //編輯
 
//引入模組DB函數-初始化設定
//批次轉換htmlspecialchars
$classList=htmlspecialcharsbatch($allowvar=array("classtitle"),$eachvar=moduledb($dbneme="neillibrary_class",$where=" where `classid` = '".$classid."'"));
 
$classbox['1']="<h3>"._MA_CLASSADMINPHP07."</h3>";
$classbox['2']=_MA_CLASSADMINPHP08;
$hidden="<input type='hidden' name='classid' value='".$classid."'><input type='hidden' name='i' value='".$i."'>"; 
$buttonvar="classupdate";


 }
 


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


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


//讀取function.php中sortselect(函數)
if(!empty($sortselect['of_classid'])){
$sortselectbox.=sortselect($dbneme,$classid="".$sortselect['of_classid']."",$basid="",$enfunction="",$type="",$DBval="classid",$keyvar="of_classid");

}

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


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

//第一層
$selectmenu1=$ofclassid[0];




 
 //遞回開始
function retorted($dbneme="",$classid="",$i="",$classidboxvar="",$boxtopid=""){
 global $xoopsModule;	
$where="  where `of_classid` = '".$classid."' 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){
	

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


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


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

}

return $sortvar;
}

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

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

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

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




//顯示選單功能標籤
$enableforeach=enable($envar=$sort[$key]['enable']);	
$sortvar.="<tr id='deletetr{$sort[$key]['classid']}' class=".$colorstyle[$key].">
<td>".$sortidsort."</td>	
<td><span class='glyphicon glyphicon-option-horizontal' aria-hidden='true'></span>&nbsp;".$sort[$key]['classtitle']."".$blockbtn."</td>

<td>".$enableforeach."</td>		
  <td>
       <a href='" .XOOPS_URL. "/modules/". $xoopsModule->getVar("dirname")."/admin/classadmin.php?classid=".$sort[$key]['classid']."&status=edit&i=top' class='btn  btn-warning btn-xs active'>"._MS_SHARED08."</a>  
       <a id='deletebotton{$sort[$key]['classid']}'  href='#NO' name='66,".$sort[$key]['classid'].",".$sort[$key]['classtitle'].","._MA_CLASSADMINPHP09.",neillibrary_class,,,,,".$xoopsModule->getVar("dirname").",classid,2' class='btn  btn-danger active btn-xs deleteeachArr'>"._MS_SHARED09."</a>  
    
    </td>	
</tr>";	

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

}
 
 
 
 
 
 
//班級名稱
$classbox['3']=inputtext($class="",$name="classtitle",$value="".$classList['classtitle']."",$placeholder="".$classList['classtitle'].""); 

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


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

//下拉選單
foreach($classoption as $key=> $val){
$classbox['5'].=optionselect($dataid=$classoption[$key]['classid'],$optionname=$classoption[$key]['classtitle'],$oid="".$selectmenu1."",$value=$classoption[$key]['classid']);
}



$main="<div class='container'>
  <h4>"._MA_CLASSADMINPHP01."&nbsp;<label for='sel2'>Class management</label>&nbsp; <a class='btn btn-warning active'  href='" . XOOPS_URL . "/modules/".$xoopsModule->getVar("dirname")."/admin/classadmin.php'>"._MS_SHARED25."</a>
      </h4>
   <h5 class='red'>"._MA_CLASSADMINPHP14."</h5>	
          <hr />
    <div class='form-group col-md-12'>
<div class='row'>

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

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

	 <div class='col-sm-8'>
	 <select id=classdefault class='form-control' name='of_classid' >
    <option   data-id='0' value='0'>"._MA_CLASSADMINPHP03."</option>
    	".$classbox['5']."   
  </select>	 <br />
    

   	

  	

  	</div> 

</div> 


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

	
<div class='form-group'>
<label class='col-sm-4'>"._MS_SHARED05."</label>	
	 <div class='col-sm-8'> ".$classbox['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: 50%;'>"._MA_CLASSADMINPHP06."</th> 	
         <th  style='width: 13%;'>"._MS_SHARED10."</th>
        <th style='width: 15%;'>"._MS_SHARED119."</th>
      </tr>
    </thead>
    <tbody>
   ".$sortvar." 
 
    </tbody>
  </table> 	   
 	   
 <input type='hidden' name='op' value='classsortupdate'>  	
 <button  type='submit' class='btn btn-primary btn-lg btn-block button'>"._MS_SHARED12."</button>
	
		
		
		  </form> 	   
 	   
 	   	  </div>    
      
       </div> </div>  
  	  
  	  </div>  
    <script>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_classinsert(){
  global $xoopsDB;   

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

$valuesvar="(of_classid,classtitle,sortidsort,enable) values('{$_POST['of_classid']}','{$_POST['classtitle']}','0','{$_POST['enable']}')";	  
$classid=insert($dbname="neillibrary_class",$values=$valuesvar);

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

}

	
	
//編輯班級
function neillibrary_classupdate(){
  global $xoopsDB,$classid;   

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

//去資料表標籤-去除空白跟HTML標籤
$_POST=strip_tagsremovesqlArr($allowvar="",$postvar=$_POST,$sqltags=array("classtitle"));  
  
$setvar="set of_classid='{$_POST['of_classid']}'  , classtitle='{$_POST['classtitle']}' , enable='{$_POST['enable']}'   where classid='".$classid."'";
update($dbname="neillibrary_class",$set=$setvar);

}


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

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

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

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

}


}	
	
	
?>