<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期：2008-02-28
// $Id: index.php,v 1.2 2008/05/14 01:22:58 tad Exp $
// ------------------------------------------------------------------------- //

/*-----------引入檔案區--------------*/
include_once "header.php";
$xoopsOption['template_main'] = "main_tpl.html";
/*-----------function區--------------*/


//列出所有tad_player資料
function list_tad_player($pcsn=""){
	global $xoopsDB,$xoopsModule,$xoopsModuleConfig,$xoopsUser;
	
	$cate=get_tad_player_cate_all();


	$where_pcsn=(!empty($pcsn))?"where a.pcsn='{$pcsn}'":"";
	
	$order_by_sort=(empty($pcsn))?"":"a.sort ,";
	//$order_by_sort="a.sort ,";
	
	$sql = "select a.psn,a.pcsn,a.location,a.title,a.image,a.creator,a.post_date,a.counter,a.enable_group,b.title,b.of_csn from ".$xoopsDB->prefix("tad_player")." as a left join ".$xoopsDB->prefix("tad_player_cate")." as b on a.pcsn=b.pcsn $where_pcsn order by $order_by_sort a.post_date desc";
	

	

	//PageBar(資料數, 每頁顯示幾筆資料, 最多顯示幾個頁數選項);
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	$total=$xoopsDB->getRowsNum($result);

	$navbar = new PageBar($total, $xoopsModuleConfig['index_show_num'], 10);
	$mybar = $navbar->makeBar();
	$bar= sprintf(_BP_TOOLBAR,$mybar['total'],$mybar['current'])."{$mybar['left']}{$mybar['center']}{$mybar['right']}";
	$sql.=$mybar['sql'];
	
	
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

	$data="<table id='tp_index'>";
	
  $ok_cat=chk_cate_power();

  $user_group=array();
  if($xoopsUser){
    $user_group=$xoopsUser->getGroups();
	}
	
  $i=3;
	while(list($psn,$new_pcsn,$location,$title,$image,$creator,$post_date,$counter,$enable_group,$cate_title,$of_csn)=$xoopsDB->fetchRow($result)){
	
	  //查看該分類是否允許目前使用者觀看
	  if(!empty($new_pcsn) and !in_array($new_pcsn,$ok_cat)){
	      $no_power[]=$psn;
			  //continue;
		}

		$enable_group_arr=explode(",",$enable_group);
		$same=array_intersect($enable_group_arr,$user_group);
		if(!empty($enable_group) and empty($same)){
		    //$data.="<tr><td>$enable_group</td><td>$same</td></tr>";
		    continue;
		}

		if(substr($image,0,4)=='http'){
    	$image = basename($image);
		}
		
		if(empty($image) or !file_exists(_TAD_PLAYER_IMG_DIR."s_{$psn}.png")){
			$ext=substr($location,-3);
	    if($ext=="mp3"){
	      $pic="mp3.png";
			}else{
	      $pic="flv.png";
			}
			$pic="<img src='images/$pic' alt='No Pic' style='width:120px;border:1px solid gray;'>";
		}else{
      $pic="<img src='"._TAD_PLAYER_IMG_URL."s_{$psn}.png' alt='{$title}' style='width:120px;border:1px solid gray;'>";
		}

	  $data.=($i % $xoopsModuleConfig['col_num'])?"<tr>":"";
	  
    //$data.="<div style='width:120px;height:120px;float:left;margin:6px;padding:10px;background-color:black;'><a href='play.php?psn=$psn'>$pic</a><div style='width:120px;'><a href='play.php?psn=$psn' style='color:white;text-decoration: none;'>$title</a></div></div>";
    
    $img_url=(in_array($psn,$no_power))?$pic:"<a href='play.php?psn=$psn'>$pic</a>";
    $img_title=(in_array($psn,$no_power))?sprintf(_MD_TADPLAYER_NO_POWER,$title):"<a href='play.php?psn=$psn'>$title</a>";
    
    if(substr($post_date,0,2)=='20')$post_date=strtotime($post_date);
    $post_date=date("Y-m-d H:i:s",xoops_getUserTimestamp($post_date));
    $data.="
		<td class='tp_index_pic'>$img_url</td>
    <td>
		<div class='tp_index_title'>$img_title</div>
		<div class='tp_index_creator'>$creator</div>
		<div class='tp_index_date'>$post_date</div>
		<div class='tp_index_counter'>".sprintf(_MD_TADPLAYER_INDEX_COUNTER,$counter)."</div></td>
		";
		
		$data.=($i % $xoopsModuleConfig['col_num'])?"":"</tr>";
		$i++;
	}
	
	$data.="</table>";
	
	//$h1_title=(empty($cate[$pcsn]))?"":"<a href='index.php?pcsn=$pcsn&op=playlist' style='color: rgb(51,102,255);'><img src='images/playlist.png' alt='playlist.png, 5.6kB' title='Playlist' border='0' height='48' width='48' align='absmiddle' hspace=3>{$cate[$pcsn]}</a>";
	
	
	$h1_title=(!empty($pcsn))?get_pcsn_path($pcsn):"";

	//$data=div_3d($h1_title,$data,"corners","width:100%;margin-left:auto;margin-right:auto;")."<p align='center' style='width:auto;'>$bar</p>";
	$data="<div class='media_title'>{$h1_title}</div>{$data}<p align='center' style='text-align:center;'>$bar</p>";
	return $data;
}


//列出分類
function list_tad_player_cate(){
	global $xoopsDB,$xoopsModule;


	$sql = "select pcsn,count(*),psn from ".$xoopsDB->prefix("tad_player")." group by pcsn";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	while(list($pcsn,$count,$psn)=$xoopsDB->fetchRow($result)){
		$counter[$pcsn]=$count;
		$img[$pcsn]=$psn;
	}

	$sql = "select * from ".$xoopsDB->prefix("tad_player_cate")."  order by sort";

	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

  $data="";
	while(list($pcsn,$of_csn,$title,$enable_group,$sort,$width,$height)=$xoopsDB->fetchRow($result)){

		$data.="
		<div class='tp_title'>
		<table>
		<tr><td style='width:130px;'>
		<img src='".XOOPS_URL."/uploads/tad_player/img/s_{$img[$pcsn]}.png'>
		</td><td>
		<a href='index.php?op=list&pcsn=$pcsn'>{$title}</a>
		<div style='font-size:11pt;color:#666666'>".sprintf(_MA_TADPLAYER_CATE_VIDEO_NUM,$counter[$pcsn])."</div>
		</td></tr></table>
		</div>";

	}

	return $data;

}

//清單播放
function playlist($pcsn="0"){
	global $xoopsModuleConfig,$xoopsUser;
	if(empty($pcsn))$pcsn=0;
  $cate=get_tad_player_cate($pcsn);
  $ok_cat=chk_cate_power();

  $user_group=array();
  if($xoopsUser){
    $user_group=$xoopsUser->getGroups();
	}
  if(!empty($pcsn) and !in_array($pcsn,$ok_cat)){
		redirect_header("index.php",3,sprintf(_MD_TADPLAYER_NO_POWER,$cate['title']));
	}



	$playcode=play_code($cate,$pcsn,"playlist");


  $h1_title=(empty($cate[$pcsn]))?"":"{$cate[$pcsn]}";

  $main=div_3d($h1_title,$playcode,"corners","margin-left:auto;margin-right:auto;");
  //$main="<center>".div_3d($cate[$pcsn],$playcode,"raised")."</center>";
	if($xoopsUser){
		$getGroups=$xoopsUser->getGroups();
		$in_group=array_intersect($xoopsModuleConfig['show_code'],$getGroups);
	}else{
    $in_group=array();
	}
		  if(empty($width)) $width=$xoopsModuleConfig['width'];
	  if(empty($width)) $height=$xoopsModuleConfig['height'];

	  $width=(empty($file['width']))?$width:$file['width'];
		$height=(empty($file['height']))?$height:$file['height'];

	  if($xoopsModuleConfig['display']=="right"){
			$all_width=$width+$xoopsModuleConfig['display_max'];
	    $all_height=$height+20;
		}else{
		  $all_width=$width;
			$all_height=$height+$xoopsModuleConfig['display_max'];
		}
	if(sizeof($in_group) > 0){
	  $code=htmlspecialchars($playcode);
	  $main.=div_3d(_MA_TADPLAYER_CODE,"<p style='width:{$all_width}px'>"._MA_TADPLAYER_CODE_DESC."</p><textarea style='padding:4px;background-color:white;width:{$all_width}px;height:200px;font-size:10px;'>{$code}</textarea>","corners","margin-left:auto;margin-right:auto;");
	}
  
	return $main;
}


/*-----------執行動作判斷區----------*/
$op=(empty($_REQUEST['op']))?"":$_REQUEST['op'];
$psn=(empty($_REQUEST['psn']))?"":intval($_REQUEST['psn']);
$pcsn=(empty($_REQUEST['pcsn']))?"":intval($_REQUEST['pcsn']);

switch($op){
	
	case "playlist":
	$main=playlist($pcsn);
	break;
	
	//預設動作
	default:
	if($xoopsModuleConfig['index_mode']=="cate"){
    $main=list_tad_player_cate();
	}else{
		$main=list_tad_player($pcsn);
	}
	break;
}

/*-----------秀出結果區--------------*/

$admin_link="index.php";



if ($xoopsUser) {
  $module_id = $xoopsModule->getVar('mid');
  $isAdmin=$xoopsUser->isAdmin($module_id);
}

$lang['home']=_MD_HOMEPAGE;
$lang['list']=_MA_TADPLAYER_LIST;
$lang['hot']=_MA_TADPLAYER_HOT;
$lang['admin']=_TO_ADMIN_PAGE;

include XOOPS_ROOT_PATH."/header.php";
$xoopsTpl->assign( "css" , "<link rel='stylesheet' type='text/css' media='screen' href='module.css' />") ;
$xoopsTpl->assign( "content" , $main) ;
$xoopsTpl->assign( "select" , cate_select($pcsn,1)) ;
$xoopsTpl->assign( "admin_link" , $admin_link) ;
$xoopsTpl->assign( "hot_media" , hot_media()) ;
$xoopsTpl->assign( "pcsn" , $pcsn) ;
$xoopsTpl->assign( "admin" , $isAdmin) ;
$xoopsTpl->assign( "op" , $op) ;
$xoopsTpl->assign( "lang" , $lang);
include_once XOOPS_ROOT_PATH.'/footer.php';

?>
