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

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

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

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

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

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

if(IsSet($_POST['save'])) {
	unset($error);
	if(!IsSet($_POST['show']) || $_POST['show']=="") { $sp_error['show'] = true; }
	if(!IsSet($_POST['stunde']) || $_POST['stunde']=="") { $sp_error['stunde'] = true; }
	if(!IsSet($_POST['tag']) || $_POST['tag']=="") { $sp_error['tag'] = true; }
	
	$check = dbarray(dbquery("SELECT * FROM `".$sendeplan_db['table_weekly']."` WHERE `weekday` = '".(stripinput($_POST['tag'])<7 ? stripinput($_POST['tag']) : '0')."' AND `".stripinput($_POST['stunde'])."` = ''"));
	if(!$check) { $sp_error['not_empty'] = true;}
	if(!IsSet($sp_error)) {
		$sql_q = "UPDATE `".$sendeplan_db['table_weekly']."` SET `".stripinput($_POST['stunde'])."` = '".stripinput($_POST['show'])."' WHERE `weekday` = '".(stripinput($_POST['tag'])<7 ? stripinput($_POST['tag']) : '0')."'";
		$result=dbquery($sql_q);
		// add this show in current scheduler times if free
		$check2 = dbquery("SELECT * FROM `".$sendeplan_db['table_schedules']."` WHERE `".stripinput($_POST['stunde'])."` = ''");
		while($tmpdata = dbarray($check2)) {
			$tmptag = (stripinput($_POST['tag'])<7 ? stripinput($_POST['tag']) : '0');
			if(date("w",$tmpdata['day']) == $tmptag) {
				$result=dbquery("UPDATE `".$sendeplan_db['table_schedules']."` SET `".stripinput($_POST['stunde'])."` = '".stripinput($_POST['show'])."' WHERE `day` = '".$tmpdata['day']."';");
			}
		}
		redirect(FUSION_SELF."?success=added");
		exit;
	}
}

elseif(IsSet($_POST['delete'])) {
	$tag = stripinput($_POST['tag']);
	if($tag==7) { $tag='0'; }
	$stunde = stripinput($_POST['stunde']);
	$show = stripinput($_POST['show']);
	if(!IsNum($stunde) || !IsNum($tag)) { redirect(FUSION_SELF); exit; }
	$sql_q = "UPDATE `".$sendeplan_db['table_weekly']."` SET `".$stunde."` = '' WHERE `weekday` = '".$tag."';";
	$result=@dbquery($sql_q);
	// delete this show in current scheduler times if set
		$result2 = @dbquery("SELECT * FROM `".$sendeplan_db['table_schedules']."` WHERE `".$stunde."` = '".$show."'");
		while($tmpdata = @dbarray($result2)) {
			if(date("w",$tmpdata['day']) == $tag && ($timestamp+(3600*24*$sp_setting['signoff_forbid'])) < mktime($stunde,0,0,date("m",$tmpdata['day']),date("d",$tmpdata['day']),date("Y",$tmpdata['day'])) || iSUPERADMIN || (checkgroup($sp_setting['dj_admingroup']))) {
				$sql_q = "UPDATE `".$sendeplan_db['table_schedules']."` SET `".stripinput($_POST['stunde'])."` = '' WHERE `day` = '".$tmpdata['day']."';";
				$result3=dbquery($sql_q);
			}
		}
	redirect(FUSION_SELF.'?success=deleted');
	exit;
}

echo "
<script type='text/javascript'>
function RemoveItem(content) {
	return confirm(\"'\" + content + \"' wirklich loeschen?\");
}
</script>
";

opentable($splocale['weekly_shows_overview'].($sp_channelname!="" ? " &raquo; ".htmlentities($sp_channelname) : ''));
include $sp_install_folder."dj_headermenu.php";
echo "<br />\n";

if(IsSet($error['not_empty'])) {
	echo "<div class='admin-message'>".$splocale['error'].": ".$splocale['weekly_already_used']."</div>";
}
elseif(IsSet($_GET['success']) && $_GET['success']=='added') {
	echo "<br /><div align='center'>".$splocale['weekly_successfully_saved']."</div><br />";
}
elseif(IsSet($_GET['success']) && $_GET['success']=='deleted') {
	echo "<br /><div align='center'>".$splocale['weekly_successfully_deleted']."</div><br />";
}

if(iSUPERADMIN || checkgroup($sp_setting['dj_admingroup']) || $sp_setting['showrepeats_dj'] == 1) {
echo "
<form name='newrepeat' method='post' action='".FUSION_SELF."'>
	<table class='tbl-border' cellspacing='1' cellpadding='0' border='0' align='center'>
		<tr><td class='tbl1'><strong>".$splocale['day']."</strong></td><td class='tbl1'><strong>".$splocale['clocktime']."</strong></td><td class='tbl1'><strong>".$splocale['radioshow']."</strong></td></tr>
		<tr><td class='tbl2'><select name='tag' class='textbox'>";
		for($wochentag=1;$wochentag<=7;$wochentag++) {
			echo "<option value='".($wochentag==7 ? '0' : $wochentag)."'>".get_sp_weekday($wochentag==7 ? '0' : $wochentag)."</option>";
		}
		echo "</select></td><td class='tbl2'><select name='stunde' class='textbox'>";
		for($s=0;$s<=23;$s++) {
			if(iSUPERADMIN || checkgroup($sp_setting['dj_admingroup']) || ($s>$sp_setting['entry_endtime'] && $s>=$sp_setting['entry_starttime'] && $sp_setting['entry_starttime']>$sp_setting['entry_endtime']) || ($sp_setting['entry_starttime'] == $sp_setting['entry_endtime']) || ($s<$sp_setting['entry_endtime'] && $s<=$sp_setting['entry_starttime'] && $sp_setting['entry_starttime']>$sp_setting['entry_endtime']) || ($s<$sp_setting['entry_endtime'] && $s>=$sp_setting['entry_starttime'] && $sp_setting['entry_starttime']<$sp_setting['entry_endtime'])) {
				echo "<option value='".$s."'>".($s<10 ? "0".$s : $s)."-".(($s+1)<10 ? "0".($s+1) : ($s+1))."</option>";
			}
		}
		echo "</select></td><td class='tbl2'>".getWeeklyDropDown()."<input type='submit' name='save' class='button' value='".$splocale['save']."' /></td></tr>
	</table>
	<br />
	<div align='center'></div>
</form>
";
echo "<hr />\n";
}

for($wochentag=1;$wochentag<=7;$wochentag++) {
	$z = 0;
	$repeat = dbarray(dbquery("SELECT * FROM `".$sendeplan_db['table_weekly']."` WHERE `weekday` = '".($wochentag<7 ? $wochentag : 0)."'"));
	echo "<br /><h3 align='center'>".$splocale['repeats']." ".$splocale['date_at']." ".get_sp_weekday($wochentag==7 ? '0' : $wochentag)."</h3>
	<table class='tbl-border' width='400' cellspacing='1' cellpadding='0' border='0' align='center'>
		<tr>
			<td class='tbl2' width='60' align='center'><strong>".$splocale['clocktime']."</strong></td><td class='tbl2'><strong>".$splocale['radioshow']."</strong></td><td class='tbl2' width='60' align='center'>&nbsp;</td>
		</tr>";
		for($s=0;$s<=23;$s++) {
			if($repeat[$s]!="") {
				$z++;
				$tmp = explode(".",$repeat[$s]);
				$showdata = dbarray(dbquery("SELECT * FROM `".$sendeplan_db['table_shows']."` WHERE `ID` = '".$tmp[1]."'"));
				echo "
				<tr>
					<td class='tbl1' align='center'>".($s<10 ? "0".$s : $s)."<sup>oo</sup>-".(($s+1)<10 ? "0".($s+1) : ($s+1))."<sup>oo</sup></td><td class='tbl1'>".getusernick($tmp[0]).": ".$showdata['title']."</td>
					<td class='tbl1' align='center'>";
					if(iSUPERADMIN || checkgroup($sp_setting['dj_admingroup']) || ($sp_setting['showrepeats_dj'] == '1' && $tmp[0]==$userdata['user_id'])) {
						echo "<form name='delete_repeat' method='post' action='".FUSION_SELF."'><input type='hidden' name='show' value='".$repeat[$s]."' /><input type='hidden' name='tag' value='".$wochentag."' /><input type='hidden' name='stunde' value='".$s."' /><input type='submit' class='button' name='delete' value='".$splocale['delete']."' /></form>";
					}
					else { echo "&nbsp;"; }
					echo "</td>
				</tr>";
			}
		}
		if($z==0) {
			echo "
			<tr>
				<td class='tbl1' align='center'>-</td><td class='tbl1' align='center'><em>".$splocale['no_repeats_found']."</em></td><td class='tbl1' align='center'>&nbsp;</td>
			</tr>";
		}
		echo "
	</table>
	
	";
}
closetable();

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