<?php
require_once "../../maincore.php";
require_once "infusion_db.php";
require_once THEMES."templates/header.php";

$sp_setting = dbarray(dbquery("SELECT * FROM `".$sendeplan_db['table_settings']."` WHERE `profile` = 'default' LIMIT 1"));

// Check if locale file is available matching the current site locale setting.
if (file_exists($sp_install_folder."locale/".$settings['locale'].".php")) {
	// Load the locale file matching the current site locale setting.
	include $sp_install_folder."locale/".$settings['locale'].".php";
} else {
	// Load the infusion's default locale file.
	include $sp_install_folder."locale/English.php";
}

if(!FUSION_V6) {	
	if(($sp_setting['default_view']<2 && (!IsSet($_GET['view']) || $_GET['view']!='weekly')) || $sp_setting['default_view']>1 && (IsSet($_GET['view']) && $_GET['view']=='daily')) { }
	else {
		// hide side panels?
		//$settings['exclude_left']="/infusions/".$sp_folder."/sendeplan.php";
		$settings['exclude_right']="/infusions/".$sp_folder."/sendeplan.php";
	}
	require_once THEMES."templates/header.php";
	add_to_title(" &raquo; ".$splocale['linkname']);
}
else {
	require_once BASEDIR."subheader.php";
	require_once BASEDIR."side_left.php";
}

require_once $sp_install_folder."sp_functions.php";

if($sp_setting['view_changeable']!='1' && !iADMIN && !checkgroup($sp_setting['dj_group']) && !checkgroup($sp_setting['dj_admingroup']) && !iSUPERADMIN) {
	unset($_GET['view']);
}

// convert current timestamp using global offset settings
$timestamp = (time() + (3600*$settings['timeoffset']));
if(IsSet($settings['serveroffset'])) { $timestamp += (3600*$settings['serveroffset']); }
// IMPORTANT: generate dateformat in gmt for db tables (without MESZ changes!)
$requestday = gmmktime(12,0,0,date("m",$timestamp),date("d",$timestamp),date("Y",$timestamp));

$newday = $requestday;
if($sp_setting['default_view']==1 || $sp_setting['default_view']==3) {
	if(date("w")==0) { $tmp_offset=6; }
	else { $tmp_offset=(date("w")-1); }
}
else $tmp_offset = 0;

if($sp_setting['default_view']>1 || (IsSet($_GET['view']) && $_GET['view']=='weekly')) {
	echo "\n<script type=\"text/javascript\" src=\"https://www.systemweb.de/supportfiles/sendeplan_v3/sp_tooltip.js\"></script>\n";
}

for ($s=(-30);$s<=14;$s++) {	
  	if(!tblexists_day($newday+($s*(3600*24)))) create_day($newday+($s*(3600*24)));
}

opentable($splocale['linkname'].($sp_channelname!="" ? " &raquo; ".htmlentities($sp_channelname) : ''));
$page = 0;
if(!IsSet($_GET['page'])) { $page = 0; }
elseif($_GET['page']*1!=$_GET['page'] || $_GET['page']<(0-$tmp_offset) || $_GET['page']>(6-$tmp_offset)) { $page = 0; }
else { $page = $_GET['page']; }

if(($sp_setting['default_view']<2 && (!IsSet($_GET['view']) || $_GET['view']!='weekly')) || $sp_setting['default_view']>1 && (IsSet($_GET['view']) && $_GET['view']=='daily')) {
	echo "
	<table width='100%' cellspacing='0' cellpadding='0' border='0'>
	<tr>
	";
	for($t=(0-$tmp_offset); $t<=(6-$tmp_offset); $t++) {
		if($t!=$page) { echo "<td class='splan' align='center' width='14%'><a class='asplan' href='".FUSION_SELF."?page=$t&amp;view=daily'>".($t==0 ? $splocale['today'] : get_sp_weekday(date("w",($newday+($t*(3600*24))))))."</td>\n"; }
		else { echo "<td class='splanaktiv' align='center' width='14%'><strong>".($t==0 ? $splocale['today'] : get_sp_weekday(date("w",($newday+($t*(60*60*24))))))."</td>\n"; }
	}
	echo "</tr>
	</table>
	<br />
	";
}

if($sp_setting['view_changeable']=='1' || iADMIN || checkgroup($sp_setting['dj_group']) || checkgroup($sp_setting['dj_admingroup']) || iSUPERADMIN) {
	echo "<div align='center'>";
	
	if(checkgroup($sp_setting['dj_group']) || checkgroup($sp_setting['dj_admingroup']) || iSUPERADMIN) {
		echo "<a class='button' href='".$sp_install_folder."sendeplan_overview.php'>".$splocale['entry']."</a>";
	}
	echo "</div><br/>\n";
}

unset($showstart,$showend,$showid,$djid);
$currentshow = "";


// Output daily view
if(($sp_setting['default_view']<2 && (!IsSet($_GET['view']) || $_GET['view']!='weekly')) || $sp_setting['default_view']>1 && (IsSet($_GET['view']) && $_GET['view']=='daily')) {
?>
<div class='tbl-border tbl2' style='width:540px;height:20px;margin-left: auto;margin-right: auto;'>
	<div style="width: 110px; float: left; text-align: center; font-family: Arial,Helvetica,sans serif; font-size: 14px; font-weight: bold;">Uhrzeit</div>
	<div style="width: 320px; float: left; text-align: center; font-family: Arial,Helvetica,sans serif; font-size: 14px; font-weight: bold;">Sendungsname</div>
	<div style="width: 110px; float: left; text-align: center; font-family: Arial,Helvetica,sans serif; font-size: 14px; font-weight: bold;">Moderator</div>
</div>
<table cellpadding='0' cellspacing='1' border='0' align='center'>
	<tr>
		<td align='center'>
			<?php
			// request available entries and store them in array
			$result = @dbquery("SELECT * FROM `".$sendeplan_db['table_schedules']."` WHERE `day` = ".($newday+($page*(60*60*24))));
			$rows = @dbrows($result);
			if ($rows != 0) {
				$entry = @dbarray($result);
				$showstart = array();
				$showend = array();
				$showid = array();
				$djid = array();
				$current_show = "-";
				for($i=0; $i<=23; $i++) {
					if($current_show=="OFF") { $current_show="-"; }
					if($entry[$i]=="" && $sp_setting['autodj_hidden']=="2" && (($sp_setting['entry_starttime']<$sp_setting['entry_endtime'] && $i<$sp_setting['entry_starttime'] && $i<$sp_setting['entry_endtime']) || ($sp_setting['entry_starttime']>$sp_setting['entry_endtime'] && $i>=$sp_setting['entry_endtime'] && $i<$sp_setting['entry_starttime']) || ($i>$sp_setting['entry_starttime'] && $i>=$sp_setting['entry_endtime'] && $sp_setting['entry_starttime']<$sp_setting['entry_endtime']))) {
						$current_show = "OFF";
						$djid[] = 0;
						$showid[] = '';
						$showstart[] = $i;
						$showend[] = $i;
					}
					elseif(($i==0 && $entry[$i]=="" && $current_show!="OFF") || ($current_show!=$entry[$i] && $entry[$i]=="") && $sp_setting['autodj_hidden']=='0') {
						// no entry = autostream
						$djid[] = 0;
						$showid[] = 1;
						$current_show = "";
						$showstart[] = $i;
						$showend[] = $i;
					}
					elseif($entry[$i]!=$current_show) { 
						
						if($entry[$i]!="") {
							$showdata = explode(".",$entry[$i]);
							$djid[] = $showdata[0];
							$showid[] = $showdata[1];
							$current_show = $entry[$i];
							$showstart[] = $i;
							$showend[] = $i;						
						}
						elseif($current_show!="OFF") {
							// no entry = autostream
							$djid[] = 0;
							$showid[] = 1;
							$current_show = "";
							$showstart[] = $i;
							$showend[] = $i;
						}
					}					
				}
				$showstart[] = 0;

				if(count($showstart)>1 || $sp_setting['autodj_hidden']!=1) {
					if(count($showstart)==1) { $showstart[1] = '0'; $djid[] = 0; $showid[] = 1; }
					for($i=0; $i<(count($showstart)-1); $i++) {
						if($djid[$i]!=0 || ($djid[$i]==0 && $sp_setting['autodj_hidden']!=1) && $showid[$i]!='' ) {
						$result = @dbquery("SELECT * FROM `".$sendeplan_db['table_shows']."` WHERE `ID` = '".$showid[$i]."'");
						$rows = @dbrows($result);
						$z=0;
						if ($rows != 0) {
							$show = @dbarray($result);
							$result2 = @dbquery("SELECT * FROM `".$sendeplan_db['table_showdata']."` WHERE `ID` = '".$show['showid']."'");
							$rows = @dbrows($result2);
							if ($rows != 0) {
							$showdata = @dbarray($result2);
							$z++; 
							$result3 = @dbquery("SELECT * FROM `".DB_USERS."` WHERE `user_id` = '".$djid[$i]."'");
								$rows = @dbrows($result3);
								if ($rows != 0) { $djdata = @dbarray($result3); }
								else {
									$djdata['user_name'] = $sp_setting['autodj_name'];
								}									
								$result4 = @dbquery("SELECT * FROM `".$sendeplan_db['table_djinfos']."` WHERE `user_id` = '".$djid[$i]."'");
								$rows = @dbrows($result4);
								if ($rows != 0) { $djinfo = @dbarray($result4); }
								if(IsSet($djdata) && (IsSet($djinfo) || $djdata['user_id']==0)) {
							echo "
							<table align='center' width='550' cellspacing='0' cellpadding='0' border='0' class='tbl-border'>
							<tr>
							<td width='1%' class='".(IsNum($z/2) ? "tbl1" : "tbl2")."' style='padding: 10px;position:relative;'>
								<div style='position: absolute;margin: 0;top: 50%;-ms-transform: translateY(-50%);transform: translateY(-50%);'>
								<div style='width: 100px; float: left; text-align: center; font-family: ".$sp_setting['font_showtitle']."; font-size: ".$sp_setting['fontsize_showtitle']."px; font-weight: ".($sp_setting['bold_showtitle']==true ? "bold" : "normal").";'>
									".($showstart[$i]<10 ? "0" : "").$showstart[$i].":00".($sp_setting['view_endtime']=="1" ? "-".($showstart[$i+1]<10 ? "0" : "").$showstart[$i+1].":00" : "").(date("H",$timestamp)>=$showstart[$i] && (date("H",$timestamp)<$showstart[$i+1] || $showstart[$i+1]=='0') && $page==0 ? "" : "")."
								</div>
								<div style='width: 325px; float: left; text-align: center; font-family: ".$sp_setting['font_showtitle']."; font-size: ".$sp_setting['fontsize_showtitle']."px; font-weight: ".($sp_setting['bold_showtitle']==true ? "bold" : "normal").";'>
									".(IsSet($locale['charset']) && strtolower($locale['charset'])=="utf-8" ? $show['title'] : utf8_decode($show['title']))."
								</div>
								</div>
								<div style='float:right;padding-right:25px;'>";
									if(IsSet($djinfo) && $djinfo['user_avatar']!="") { echo "<img src='".$sp_install_folder."images/dj-avatars/".$djinfo['user_avatar']."' border='0' alt='".$djdata['user_name']."' style='max-width: ".$sp_setting['dj_avatar_maxwidth']."px; max-height: ".$sp_setting['dj_avatar_maxheight']."px;' />"; }											
									else { echo "<img src='".$sp_install_folder."images/dj-avatars/nopic.png' border='0' alt='kein Bild'  style='max-width: ".$sp_setting['dj_avatar_maxwidth']."px; max-height: ".$sp_setting['dj_avatar_maxheight']."px;'/>"; }}
							echo "
							</td>
							</tr>
							</table>
							";
							}
						}
						}
					}
				}
				
			}
?>
<?php
if($sp_setting['autodj_hidden']==1 && count($showstart)==2 && $djdata['user_name'] = $sp_setting['autodj_name']) { echo "<div align='center'><em>".$splocale['no_planned_shows_found']."</em></div>"; }
?>
		</td>
	</tr>
</table>
<?php
}
// output weekly view
else {
	// request available entries and store them in array
	$result = dbquery("SELECT * FROM `".$sendeplan_db['table_schedules']."` ORDER BY `day` ASC");
	$rows = dbrows($result);
	if ($rows != 0) {
		$day = array();
		while ($sp_entry = dbarray($result)) {
			//$day[] = $sp_entry['day'];
			for($i=0; $i<=23; $i++) {
				$day[$sp_entry['day']][$i] = $sp_entry[$i];
			}
					
		}
	}
	echo "<table width='100%' align='center' class='tbl-border' cellspacing='1' cellpadding='0' border='0'>
	<tr>
		<td class='tbl2' style='text-align: center;'>Tag/<br />Uhrzeit</td>";
		for($i=(0-$tmp_offset); $i<=(6-$tmp_offset); $i++) {
			echo " ".($i==0 ? "<td class='tbl1' align='center'>".$splocale['today'] : "<td class='tbl2' align='center'>".get_sp_weekday(date("w",($newday+($i*(3600*24)))))).",<br />
			".date('d.m.Y',$newday+($i*(3600*24)))."</td>\n";
		}
		echo "
	</tr>
	";
	for($i=0; $i<=23; $i++) {
		
		echo "
		<tr>
			<td class='tbl2' style='text-align: center;'>".($i<10 ? "0" : "").$i.":00</td>";
		for($d=(0-$tmp_offset); $d<=(6-$tmp_offset); $d++) {
			if($day[$newday+($d*(3600*24))][$i]=="") { 
				echo "<td class='tbl1' align='center' style='padding: 0px;'>";
				if($sp_setting['autodj_hidden']=="2" && (($sp_setting['entry_starttime']<$sp_setting['entry_endtime'] && $i<$sp_setting['entry_starttime'] && $i<$sp_setting['entry_endtime']) || ($sp_setting['entry_starttime']>$sp_setting['entry_endtime'] && $i>=$sp_setting['entry_endtime'] && $i<$sp_setting['entry_starttime']) || ($i>$sp_setting['entry_starttime'] && $i>=$sp_setting['entry_endtime'] && $sp_setting['entry_starttime']<$sp_setting['entry_endtime']))) { echo "-"; }
				elseif($sp_setting['entry_starttime']==$sp_setting['entry_endtime'] && $sp_setting['autodj_hidden']!='1') {
					$djinfo = @dbarray(dbquery("SELECT * FROM `".$sendeplan_db['table_djinfos']."` WHERE `user_id` = '0'"));
					if(IsSet($djinfo) && $djinfo['user_avatar']!="") {
						echo "<img src='".$sp_install_folder."images/dj-avatars/".$djinfo['user_avatar']."' border='0' alt='Picture' style='max-width: 50px; max-height: 50px;' /><br />\n";
					}											
					else {
						echo "<img src='".$sp_install_folder."images/dj-avatars/nopic.png' border='0' alt='kein Bild'  style='max-width: 50px; max-height: 50px;'/><br />\n";
					}
					echo trimlink($sp_setting['autodj_name'],10);
				}
				elseif($sp_setting['autodj_hidden']=='1') { echo '-'; }
				else {					
					$djdata = @dbarray(dbquery("SELECT * FROM `".$sendeplan_db['table_djinfos']."` WHERE `user_id` = '0'"));
							if(IsSet($djdata) && $djdata['user_avatar']!="") {
								echo "<img src='".$sp_install_folder."images/dj-avatars/".$djdata['user_avatar']."' border='0' alt='DJ' style='max-width: 50px; max-height: 50px;' /><br />";
							}											
							else {
								echo "<img src='".$sp_install_folder."images/dj-avatars/nopic.png' border='0' alt='kein Bild'  style='max-width: 50px; max-height: 50px;'/><br />\n";
							}
					echo trimlink($sp_setting['autodj_name'],10);			
				}
			}
			else {
				$tmp = explode(".", $day[$newday+($d*(3600*24))][$i]);
				if($tmp[0]>0) {
					$getshow = dbquery("SELECT * FROM `".$sendeplan_db['table_shows']."` WHERE `ID` = '".$tmp[1]."'");
					$rows = dbrows($getshow);
					if ($rows != 0) { $show = dbarray($getshow); }
					$getshowdata = dbquery("SELECT * FROM `".$sendeplan_db['table_showdata']."` WHERE `ID` = '".$show['showid']."'");
					$rows = dbrows($getshowdata);
					if ($rows != 0) { $showdata = dbarray($getshowdata); }
					echo "<td class='tbl1' align='center'>
							<a href='".BASEDIR."profile.php?lookup=".$tmp[0]."' title='".getusernick($tmp[0])."' onmouseover='Tip(\"<div style=padding:10px;width:".($sp_setting['showbox_width']-20)."px;height:".($sp_setting['showbox_height']-20)."px;background-image:url(".$sp_install_folder."images/shows/".$showdata['image'].");background-repeat:no-repeat;background-position:center;background-color:transparent;color:#333333;><div style=margin:0px;padding:5px;padding-top:3px;".($showdata['textbg']!="" && file_exists($sp_install_folder."images/bg_trans/".$showdata['textbg']) ? "background-image:url(".$sp_install_folder."images/bg_trans/".$showdata['textbg'].");" : "")."border:0px;height:".($sp_setting['showbox_height']-30)."px;overflow:hidden;><div align=".$sp_setting['align_showtitle']." class=spshowtitle><table width=100% border=0 align=left cellpadding=2 cellspacing=0 class=spshowtitle style=color:#333333;><tr><td>&nbsp;</td>".($sp_setting['use_nsv']==true && $show['is_nsv']==false ? "<td width=1><img src=".$sp_install_folder."images/camera_off.png width=25 height=25 border=0 alt=Cam_off! /></td>" : "").($sp_setting['use_nsv']==true && $show['is_nsv']==true ? "<td width=1><img src=".$sp_install_folder."images/camera_on.png width=25 height=25 border=0 alt=Cam_on! /></td>" : "")."</tr></table>".(strtolower($locale['charset'])=="utf-8" ? htmlentities(utf8_decode($show['title'])) : htmlentities($show['title']))."</div><div align=".$sp_setting['align_showdescription']." class=spshowdescription style=color:#333333;>".str_replace("\r\n","",nl2br((strtolower($locale['charset'])=="utf-8" ? htmlentities(utf8_decode($show['description'])) : htmlentities($show['description']))))."</div></div></div>\");' style='text-decoration: none;'>";
							$djdata = @dbarray(dbquery("SELECT * FROM `".$sendeplan_db['table_djinfos']."` WHERE `user_id` = '".$tmp[0]."'"));
							if(IsSet($djdata) && $djdata['user_avatar']!="") {
								echo "<img src='".$sp_install_folder."images/dj-avatars/".$djdata['user_avatar']."' border='0' alt='DJ' style='max-width: 50px; max-height: 50px;' /><br />";
							}											
							else {
								echo "<img src='".$sp_install_folder."images/dj-avatars/nopic.png' border='0' alt='kein Bild'  style='max-width: 50px; max-height: 50px;'/><br />\n";
							}
							echo trimlink(getusernick($tmp[0]),10)."</a>
							";
				}
				else {
					$djdata = @dbarray(dbquery("SELECT * FROM `".$sendeplan_db['table_djinfos']."` WHERE `user_id` = '0'"));
							if(IsSet($djdata) && $djdata['user_avatar']!="") {
								echo "<img src='".$sp_install_folder."images/dj-avatars/".$djdata['user_avatar']."' border='0' alt='".$djdata['user_name']."' style='max-width: 50px; max-height: 50px;' /><br />";
							}											
							else {
								echo "<img src='".$sp_install_folder."images/dj-avatars/nopic.png' border='0' alt='kein Bild'  style='max-width: 50px; max-height: 50px;'/><br />\n";
							}
					echo "<td class='tbl1' align='left'>".trimlink($sp_setting['autodj_name'],10);
				}
			}
			echo "</td>";
		}
		echo "
		</tr>";
	}
	echo "
	</tr>
</table>

";
}
?>
<br />
<div align='right'><a href='https://www.systemweb.de' target='_blank' title='<?php echo $splocale['linkname'];?> v3.03 by Systemweb.de' style='text-decoration:none;'>&copy;</a></div>
<?php

closetable();

if(!FUSION_V6) { require_once THEMES."templates/footer.php"; }
else {
	//require_once BASEDIR."side_right.php";
	require_once BASEDIR."footer.php";
}
?>
