<?php
require_once "../../maincore.php";
require_once "infusion_db.php";

if(!FUSION_V6) {
	// hide left an right side panels?
	//$settings['exclude_left']="/infusions/".$sp_folder."/sendeplan_overview.php";
	$settings['exclude_right']="/infusions/".$sp_folder."/sendeplan_overview.php";
	require_once THEMES."templates/header.php";	
}
else {
	require_once BASEDIR."subheader.php";
	require_once BASEDIR."side_left.php";
}

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

require_once $sp_install_folder."sp_functions.php";

if(!checkgroup($sp_setting['dj_group']) && !checkgroup($sp_setting['dj_admingroup']) && !iSUPERADMIN) redirect("../../index.php");

// convert current timestamp using global offset settings
$timestamp = (time() + (3600*$settings['timeoffset']));
if(IsSet($settings['serveroffset'])) { $timestamp += (3600*$settings['serveroffset']); }
// IMPORTTANT: 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;

for ($s=(-30);$s<=14;$s++) {	
  	if(!tblexists_day($newday+($s*(3600*24)))) create_day($newday+($s*(3600*24)));
}
  
// delete old entries
$result=@dbquery("DELETE from `".$sendeplan_db['table_schedules']."` WHERE `day` < ".($timestamp-(60*((date("w")+1)*(60*60*24)))));

// 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";
}

// 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 "\n<script type=\"text/javascript\" src=\"https://www.systemweb.de/supportfiles/sendeplan_v3/sp_tooltip.js\"></script>\n";

opentable($splocale['overview_title'].($sp_channelname!="" ? " &raquo; ".htmlentities($sp_channelname) : ''));
include $sp_install_folder."dj_headermenu.php";

// read saved show presets
$result = dbquery("SELECT * FROM `".$sendeplan_db['table_shows']."` WHERE `user_id` = '".$userdata['user_id']."' ORDER BY `title` ASC");
$rows = dbrows($result);
if ($rows != 0) {
	$myshowtitle = array();
	$myshowid = array();
	while ($show_entry = dbarray($result)) {
		$myshowtitle[] = $show_entry['title'];
		$myshowid[] = $show_entry['ID'];
	}
}
elseif(iSUPERADMIN || checkgroup($sp_setting['dj_admingroup'])) {
	echo "<br /><div class='admin-message'>".$splocale['no_own_show_admin']."</div>\n";
}
else {
	echo "<br /><div class='admin-message'>".$splocale['error'].": ".$splocale['no_own_show']."</div>\n";
}

if((IsSet($_POST['edit']) || IsSet($_POST['editsave'])) && IsSet($_POST['day']) && IsNum($_POST['day']) && (IsSet($myshowid) || iSUPERADMIN || checkgroup($sp_setting['dj_admingroup']))) {
	$result = dbquery("SELECT * FROM `".$sendeplan_db['table_schedules']."` WHERE `day` ='".stripinput($_POST['day'])."'");
	$rows = dbrows($result);
	if ($rows == 0) { redirect(FUSION_SELF); exit; }
	$tmp = dbarray($result);
	
	if(IsSet($_POST['editsave'])) {
		$insert = "";
		for($z=0; $z<=23; $z++) {
			if(IsSet($_POST[$z])) {
				if($tmp[$z]!="") { $data = explode(".", $tmp[$z]); }
				
					if((IsSet($data[0]) && ($data[0] == $userdata['user_id'] || iSUPERADMIN || checkgroup($sp_setting['dj_admingroup']))) || !IsSet($data[0]) || $data[0]==0) { $insert.=($insert!="" ? ", " : "")."`".$z."` = '".stripinput($_POST[$z])."'"; }
					else { $sp_error = "<strong>".$splocale['error'].":</strong> ".$splocale['time_shortly_used']; }
			}
		}
		if(!IsSet($sp_error)) {
			$result = dbquery("UPDATE `".$sendeplan_db['table_schedules']."` SET $insert WHERE `day` = '".stripinput($_POST['day'])."' LIMIT 1");
			redirect(FUSION_SELF);
			exit;
			//echo $insert;
		}
	}
	
	if(IsSet($sp_error)) { echo "<div class='admin-message'>$sp_error</div>"; }
	echo "<p>&nbsp;</p>
	<h3 align='center'>".$splocale['edit_showtimes']." ".$splocale['date_at']." ".get_sp_weekday(date("w",$_POST['day'])).", ".date('d.m.Y',$_POST['day'])."</h3>
	<br />
	<p align='center'>[<a href='".FUSION_SELF."'>".$splocale['back']."</a>]</p>
	<br />
	<form method='post' action='".FUSION_SELF."'>
			<table align='center' class='tbl-border' cellspacing='1' cellpadding='0' border='0'>
				<tr>
					<td class='tbl2' align='center' width='60'>".$splocale['clocktime']."</td><td class='tbl2' align='left' width='1'>".$splocale['radioshow']."</td><td class='tbl2' width='100'>".$splocale['entry_made']."</td>
				</tr>";
				for($h=0; $h<=23; $h++) {
				if(($h>$sp_setting['entry_endtime'] && $h>=$sp_setting['entry_starttime'] && $sp_setting['entry_starttime']>$sp_setting['entry_endtime']) || ($sp_setting['entry_starttime'] == $sp_setting['entry_endtime']) || ($h<$sp_setting['entry_endtime'] && $h<=$sp_setting['entry_starttime'] && $sp_setting['entry_starttime']>$sp_setting['entry_endtime']) || ($h<$sp_setting['entry_endtime'] && $h>=$sp_setting['entry_starttime'] && $sp_setting['entry_starttime']<$sp_setting['entry_endtime']) || ($h<$sp_setting['entry_starttime'] && $h<$sp_setting['entry_endtime'] && $sp_setting['entry_starttime']>$sp_setting['entry_endtime']) || (iSUPERADMIN) || (checkgroup($sp_setting['dj_admingroup']))) {
				echo "
				<tr>
					<td class='tbl1' align='center'>".($h<10 ? "0" : "").$h."-".(($h+1)<10 ? "0" : "").($h+1<24 ? $h+1 : "00")."</td>";
					if($tmp[$h]=="") { echo "<td class='tbl1' align='left' style='padding: 0px;'>".getDropDown($h, "", $_POST['day']); }
					else {
						$showinfo = explode(".", $tmp[$h]);
						if($showinfo[0]>0 && $showinfo[0]!=$userdata['user_id'] && !iSUPERADMIN && !checkgroup($sp_setting['dj_admingroup'])) {
							$getshow = dbquery("SELECT * FROM `".$sendeplan_db['table_shows']."` WHERE `ID` = '".$showinfo[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='tbl2' align='center'>
							<a href='".BASEDIR."profile.php?lookup=".$tmp[0]."' title='".getusernick($showinfo[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:".$showdata['font-color'].";><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 style=color:".$showdata['font-color'].";><table width=100% border=0 align=left cellpadding=2 cellspacing=0 class=spshowtitle style=color:".$showdata['font-color'].";><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" ? utf8_decode($show['title']) : htmlentities($show['title']))."</div><div align=".$sp_setting['align_showdescription']." class=spshowdescription style=color:".$showdata['font-color'].";>".string2js($show['description'])."</div></div></div>\");'>
							".getusernick($showinfo[0])."</a>";							
						}
						else { echo "<td class='tbl2' align='center'>".getDropDown($h, $tmp[$h], stripinput($_POST['day'])); }
					}
			echo "</td><td class='tbl1'>";
			if(is_weekly($h,$_POST['day'], $tmp[$h]) && $tmp[$h]!="") {
				echo $splocale['made_by_repeat'];
			}
			elseif($tmp[$h]!="") { echo $splocale['made_by_hand']; }
			else { echo "-"; }

		echo "</td>
		</tr>";
	}}
echo "
		<tr>
			<td class='tbl2' colspan='3' align='center'>
				<input type='hidden' name='day' value='".$_POST['day']."' />
				<input type='submit' class='button' name='editsave' value='".$splocale['save']."' />
			</td>
		</tr>
		</table>

</form>";		
}

else {
echo "
<br />
<p align='center'>[<a href='sendeplan.php'>".$splocale['back']."</a>]</p>
<br />
<h3 align='center'>".$splocale['overview_viewable']." ".$splocale['date_from']." ".date('d.m.Y',($newday-($tmp_offset*3600*24)))." ".$splocale['date_to']." ".date('d.m.Y',$newday+((6-$tmp_offset)*(3600*24)))."</h3>

<table width='100%' align='center' class='tbl-border' cellspacing='1' cellpadding='0' border='0'>
	<tr>
		<td class='tbl2'>&nbsp;</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' align='center'>".($i<10 ? "0" : "").$i."-".(($i+1)<10 ? "0" : "").($i+1)."</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;'>-frei-"; }
			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:".$showdata['font-color'].";><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 style=color:".$showdata['font-color'].";><table width=100% border=0 align=left cellpadding=2 cellspacing=0 class=spshowtitle style=color:".$showdata['font-color'].";><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:".$showdata['font-color'].";>".str_replace("\r\n","",nl2br((strtolower($locale['charset'])=="utf-8" ? htmlentities(utf8_decode($show['description'])) : htmlentities($show['description']))))."</div></div></div>\");'>".trimlink(getusernick($tmp[0]),10)."</a>
							";
				}
				else { echo "<td class='tbl1' align='left'>".trimlink($sp_setting['autodj_name'],10); }
			}
			echo "</td>";
		}
		echo "
		</tr>";
	}
	if(iSUPERADMIN || checkgroup($sp_setting['dj_admingroup']) || checkgroup($sp_setting['dj_signin'])) {
	echo "<tr>
			<td class='tbl2'>&nbsp;</td>";	
	for($i=(0-$tmp_offset); $i<=(6-$tmp_offset); $i++) {
		echo "
		<td class='tbl2' align='center'>".($i>=0 ? "<form method='post'><input type='hidden' name='day' value='".($newday+($i*(3600*24)))."' /><input name='edit' type='submit' class='button' value='".$splocale['edit']."' /></form>" : "-")."</td>";
	}}
	echo "
	</tr>
</table>

<p>&nbsp;</p><h3 align='center'>".$splocale['overview_notviewable']." ".$splocale['date_from']." ".date('d.m.Y',($newday+((7-$tmp_offset)*3600*24)))." ".$splocale['date_to']." ".date('d.m.Y',$newday+((13-$tmp_offset)*(3600*24)))."</h3>
<table width='100%' align='center' class='tbl-border' cellspacing='1' cellpadding='0' border='0'>
	<tr>
		<td class='tbl2'>&nbsp;</td>";
		for($i=(7-$tmp_offset); $i<=(13-$tmp_offset); $i++) {
			echo "<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' align='center'>".($i<10 ? "0" : "").$i."-".(($i+1)<10 ? "0" : "").($i+1)."</td>";
		for($d=(7-$tmp_offset); $d<=(13-$tmp_offset); $d++) {
			if($day[$newday+($d*(3600*24))][$i]=="") { echo "<td class='tbl1' align='center' style='padding: 0px;'>-frei-"; }
			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:".$showdata['font-color'].";><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 style=color:".$showdata['font-color'].";><table width=100% border=0 align=left cellpadding=2 cellspacing=0 class=spshowtitle style=color:".$showdata['font-color'].";><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:".$showdata['font-color'].";>".str_replace("\r\n","",nl2br((strtolower($locale['charset'])=="utf-8" ? htmlentities(utf8_decode($show['description'])) : htmlentities($show['description']))))."</div></div></div>\");'>".trimlink(getusernick($tmp[0]),10)."</a>
							";
				}
				else { echo "<td class='tbl1' align='left'>".trimlink($sp_setting['autodj_name'],10); }
			}
			echo "</td>";
		}
		echo "
		</tr>";
	}
	if(iSUPERADMIN || checkgroup($sp_setting['dj_admingroup']) || checkgroup($sp_setting['dj_signin'])) {
	echo "<tr>
			<td class='tbl2'>&nbsp;</td>";	
	for($i=(7-$tmp_offset); $i<=(13-$tmp_offset); $i++) {
		echo "
		<td class='tbl2' align='center'><form method='post'><input type='hidden' name='day' value='".($newday+($i*(3600*24)))."' /><input name='edit' type='submit' class='button' value='".$splocale['edit']."' /></form></td>";
	}}
	echo "
	</tr>
</table>
";
}

closetable();

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