<?php
require_once "../../maincore.php";
require_once "infusion_db.php";
require_once $sp_install_folder."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";
}

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

if(IsSet($_POST['save'])) {
	unset($error);
	if(!IsSet($_POST['showid']) || !IsNum($_POST['showid'])) { $error['image'] = true; }
	if(!IsSet($_POST['showtitle']) || $_POST['showtitle']=="" || strlen($_POST['showtitle'])>22) { $error['title'] = true; }
	if(!IsSet($_POST['showdescription']) || $_POST['showdescription']=="") { $error['description'] = true; }
	if(!IsSet($_POST['is_nsv']) || $_POST['is_nsv']=="") { $error['nsv'] = true; }
	if(!IsSet($_POST['studiobox']) || $_POST['studiobox']=="") { $error['studiobox'] = true; }
	
	if(!IsSet($error)) {
		$result=dbquery("INSERT INTO `".$sendeplan_db['table_shows']."` (`user_id`, `showid`, `title`, `description`, `studiobox`, `is_nsv`) VALUES ('".$userdata['user_id']."', '".$_POST['showid']."', '".stripinput($_POST['showtitle'])."', '".stripinput($_POST['showdescription'])."', '".stripinput($_POST['is_nsv'])."', '".stripinput($_POST['studiobox'])."')");
		redirect(FUSION_SELF."?success=added");
		exit;
	}
}

elseif(IsSet($_POST['delete'])) {
	unset($error);
	$showid = stripinput($_POST['showid']);
	if(!IsNum($showid)) { redirect(FUSION_SELF); exit; }
	unset($error);
	$result=dbquery("SELECT * FROM `".$sendeplan_db['table_shows']."` WHERE `ID` = '".$showid."' AND `user_id` = '".$userdata['user_id']."'");
	$rows = dbrows($result);
	if ($rows == 0) { redirect(FUSION_SELF); exit; }
	else {
		// check if show is used in stored schedule entries
		$result2=dbquery("SELECT * FROM `".$sendeplan_db['table_schedules']."` WHERE 
		`0` = '".$userdata['user_id'].".".$showid."' OR 
		`1` = '".$userdata['user_id'].".".$showid."' OR 
		`2` = '".$userdata['user_id'].".".$showid."' OR 
		`3` = '".$userdata['user_id'].".".$showid."' OR 
		`4` = '".$userdata['user_id'].".".$showid."' OR 
		`5` = '".$userdata['user_id'].".".$showid."' OR 
		`6` = '".$userdata['user_id'].".".$showid."' OR 
		`7` = '".$userdata['user_id'].".".$showid."' OR 
		`8` = '".$userdata['user_id'].".".$showid."' OR 
		`9` = '".$userdata['user_id'].".".$showid."' OR 
		`10` = '".$userdata['user_id'].".".$showid."' OR 
		`11` = '".$userdata['user_id'].".".$showid."' OR 
		`12` = '".$userdata['user_id'].".".$showid."' OR 
		`13` = '".$userdata['user_id'].".".$showid."' OR 
		`14` = '".$userdata['user_id'].".".$showid."' OR 
		`15` = '".$userdata['user_id'].".".$showid."' OR 
		`16` = '".$userdata['user_id'].".".$showid."' OR 
		`17` = '".$userdata['user_id'].".".$showid."' OR 
		`18` = '".$userdata['user_id'].".".$showid."' OR 
		`19` = '".$userdata['user_id'].".".$showid."' OR 
		`20` = '".$userdata['user_id'].".".$showid."' OR 
		`21` = '".$userdata['user_id'].".".$showid."' OR 
		`22` = '".$userdata['user_id'].".".$showid."' OR 
		`23` = '".$userdata['user_id'].".".$showid."'");
		$rows = dbrows($result2);
		if ($rows != 0) {
			$error['delete'] = true;
		}
		else {
			$result=dbquery("DELETE FROM `".$sendeplan_db['table_shows']."` WHERE `ID` = '".$showid."' AND `user_id` = '".$userdata['user_id']."'");
			redirect(FUSION_SELF.'?success=deleted');
			exit;
		}
	}
}

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

if((IsSet($_POST['edit']) || IsSet($_POST['editsave'])) && IsSet($_POST['showid']) && IsNum($_POST['showid'])) {
	opentable($splocale['scheduler_myshows'].($sp_channelname!="" ? " &raquo; ".htmlentities($sp_channelname) : ''));
	
	if(IsSet($_POST['editsave'])) {
		unset($error);
		if(!IsSet($_POST['newid']) || !IsNum($_POST['newid'])) { $error['image'] = true; }
		if(!IsSet($_POST['showtitle']) || $_POST['showtitle']=="" || strlen($_POST['showtitle'])>22) { $error['title'] = true; }
		if(!IsSet($_POST['showdescription']) || $_POST['showdescription']=="") { $error['description'] = true; }
		if(!IsSet($_POST['is_nsv']) || $_POST['is_nsv']=="") { $error['nsv'] = true; }
		if(!IsSet($_POST['studiobox']) || $_POST['studiobox']=="") { $error['studiobox'] = true; }
	
		if(!IsSet($error)) {
			$result=dbquery("UPDATE `".$sendeplan_db['table_shows']."` SET `showid`='".stripinput($_POST['newid'])."', `title`='".stripinput($_POST['showtitle'])."', `description`='".stripinput($_POST['showdescription'])."', `is_nsv`='".stripinput($_POST['is_nsv'])."', `studiobox`='".stripinput($_POST['studiobox'])."' WHERE `user_id` = '".$userdata['user_id']."' AND `ID` = '".stripinput($_POST['showid'])."' LIMIT 1");
		}
	}
		
	$result = dbquery("SELECT * FROM `".$sendeplan_db['table_shows']."` WHERE `ID` = '".stripinput($_POST['showid'])."' AND `user_id` = '".$userdata['user_id']."'");
	$rows = dbrows($result);
	if ($rows == 0) { redirect(FUSION_SELF); exit; }
	$show = dbarray($result);
	echo "
	<br /><p align='center'>[<a href='".FUSION_SELF."'>".$splocale['back']."</a>]</p><br />

			";
			if(IsSet($error)) { echo "<div class='admin-message'><strong>".$splocale['error'].":</strong> ".$splocale['marked_not_filled']."</div>\n"; }
			
			$result2 = dbquery("SELECT * FROM `".$sendeplan_db['table_showdata']."` WHERE `ID` = '".$show['showid']."'");
							$rows = dbrows($result2);
							if ($rows != 0) {
							$showdata = dbarray($result2);
							echo "
							<div style='padding: 10px; width: ".($sp_setting['showbox_width']-20)."px; height: ".($sp_setting['showbox_height']-20)."px; background-repeat: no-repeat; background-position: center; background-color: transparent; color: ".$showdata['font-color'].";' align='center'>
							<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 none; height: 50px; overflow: hidden;'><div style='width: auto; font-family: ".$sp_setting['font_showtitle']."; font-size: ".$sp_setting['fontsize_showtitle']."px; font-weight: ".($sp_setting['bold_showtitle']==true ? "bold" : "normal").";' align='".$sp_setting['align_showtitle']."'><table width='100%' border='0' align='left' cellpadding='2' cellspacing='0' style='font-family: ".$sp_setting['font_showtitle']."; font-size: ".$sp_setting['fontsize_showtitle']."px; color: ".$showdata['font-color']."';><tr><td><strong>XX:00".($sp_setting['view_endtime']=="1" ? "-XX:00" : "")."</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!' title='Sendung wird per Video &uuml;bertragen' /></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!' title='Sendung wird NICHT per Video &uuml;bertragen' /></td>" : "")."</tr></table>".$show['title']."</div>
							<div style='font-family: ".$sp_setting['font_showdescription']."; font-size: ".$sp_setting['fontsize_showdescription']."px;color: ".$showdata['font-color']."; font-weight: ".($sp_setting['bold_showdescription']==true ? "bold" : "normal").";' align='".$sp_setting['align_showdescription']."'>".nl2br($show['description'])."</div>
							</div>
							</div>
							<br />
							";
							}
			
			
			echo "<form method='post' action='".FUSION_SELF."?action=editsave' name='showpreset'>\n";
		echo "<table class='tbl-border' cellspacing='1' cellpadding='0' border='0' align='center'>";
		echo "<tr><td class='tbl1'>".$splocale['showtitle']."</td><td class='tbl1'><input name='showtitle' type='text' class='textbox' value='".$show['title']."' size='25'".(IsSet($error['title']) ? " style='background-color: #fbb; color: #000; border; 1px solid #f00;'" : "")." /></td></tr>\n";
		echo "<tr><td class='tbl2'>".$splocale['showdescription']."</td><td class='tbl2'><textarea name='showdescription' class='textbox' size='25' style='width: ".($sp_setting['showbox_width']-20)."px; height: ".($sp_setting['showbox_height']-70)."px;".(IsSet($error['title']) ? "background-color: #fbb; color: #000; border; 1px solid #f00;" : "")."'>".$show['description']."</textarea></td></tr>";		


		echo "</td></tr>\n";
		echo "</table>\n";
		echo "<br /><div align='center'><input type='hidden' name='showid' value='".$_POST['showid']."' /><input type='submit' class='button' name='editsave' value='".$splocale['save']."' /></div>\n";
		echo "</form>\n<br />";
}

elseif(IsSet($_GET['action']) && $_GET['action']=="new") {
	opentable($splocale['scheduler_myshows'].($sp_channelname!="" ? " &raquo; ".htmlentities($sp_channelname) : '')." &raquo; ".$splocale['add_new_show']);
	echo "
	<br /><p align='center'>[<a href='".FUSION_SELF."'>".$splocale['back']."</a>]</p><br />
		<form method='post' action='".FUSION_SELF."?action=new'>
			";
			if(IsSet($error)) { echo "<div class='admin-message'><strong>".$splocale['error'].":</strong> ".$splocale['marked_not_filled']."</div>\n"; }
			echo "<table class='tbl-border' cellpadding='0' cellspacing='1' border='0' align='center'>
				<tr>
					<td class='tbl1'>".$splocale['showtitle']."</td><td class='tbl1' align='left' valign='top' height='1%'>
						<input type='text' name='showtitle' size='25' class='textbox' style='".(IsSet($error['title']) ? " background-color: #fbb; color: #000; border; 1px solid #f00;" : "")."' value='".(IsSet($_POST['showtitle']) ? $_POST['showtitle'] : "")."' />
					</td>
				</tr>
				<tr>
					<td class='tbl2' align='left' valign='top' height='1%'>
						".$splocale['showdescription']."
					</td>
				
					<td class='tbl2' align='left' valign='top' height='1%'>
						<textarea name='showdescription' class='textbox' style='width: ".($sp_setting['showbox_width']-20)."px; height: ".($sp_setting['showbox_height']-70)."px;".(IsSet($error['description']) ? " background-color: #fbb; color: #000; border; 1px solid #f00;" : "")."'>".(IsSet($_POST['showdescription']) ? $_POST['showdescription'] : "")."</textarea>
					</td>
				</tr>
				<tr>
					<td class='tbl1'>Video-&Uuml;bertragung?</td>
					<td class='tbl1' align='left' style='".(IsSet($error['nsv']) ? " background-color: #fbb; color: #000; border; 1px solid #f00;" : "")."'>
						".($sp_setting['use_nsv']==true ? "<input name='is_nsv' class='textbox' value='1' type='radio'".(IsSet($_POST['is_nsv']) && $_POST['is_nsv']=='1' ? " checked" : "").">".$splocale['yes']."&nbsp;&nbsp;&nbsp;&nbsp;<input name='is_nsv' class='textbox' value='0' type='radio'".(IsSet($_POST['is_nsv']) && $_POST['is_nsv']=='0' ? " checked" : "").">".$splocale['no'] : "<em>- Dieses Feature ist nicht aktiv-</em><input type='hidden' name='is_nsv' value='0' />")."
					</td>
				</tr>
				<tr>
					<td class='tbl2'>Studiobox-Einstellung</td>
					<td class='tbl2'align='left'>
						".($sp_setting['use_studiobox']==true ? "
						<select name='studiobox' class='textbox' style='".(IsSet($error['studiobox']) ? " background-color: #fbb; color: #000; border; 1px solid #f00;" : "")."'>
							<option value=''".(IsSet($_POST['studiobox']) && $_POST['studiobox']=='' ? " selected" : "").">- bitte ausw&auml;hlen:</option>
							<option value='0'".(IsSet($_POST['studiobox']) && $_POST['studiobox']=='0' ? " selected" : "").">Keine Gr&uuml;&szlig;e/W&uuml;nsche</option>
							<option value='1'".(IsSet($_POST['studiobox']) && $_POST['studiobox']=='1' ? " selected" : "").">Nur Gr&uuml;&szlig;e/ keine W&uuml;nsche</option>
							<option value='2'".(IsSet($_POST['studiobox']) && $_POST['studiobox']=='2' ? " selected" : "").">Keine Gr&uuml;&szlig;e/ nur W&uuml;nsche</option>
							<option value='3'".(IsSet($_POST['studiobox']) && $_POST['studiobox']=='3' ? " selected" : "").">Gr&uuml;&szlig;e &amp; W&uuml;nsche</option>
						</select>" : "<em>- Dieses Feature ist nicht aktiv-</em><input type='hidden' name='studiobox' value='0' />")."
					</td>
				</tr>
				";
				echo "<tr><td class='tbl1'>".$splocale['showpreset']."</td><td class='tbl1'>";
		$result3 = dbquery("SELECT * FROM `".$sendeplan_db['table_showdata']."` WHERE `owner` = 'all' OR `owner` = '".$userdata['user_id']."' ORDER BY `title` ASC");
		$rows = dbrows($result3);
		if ($rows != 0) {
			echo "
			<select name='showid' size='1' class='textbox' ".(IsSet($error['showid']) ? " style='background-color: #fbb; color: #000; border; 1px solid #f00;'" : "").">";
			while ($showdata2 = dbarray($result3)) {
				echo "
				<option value='".$showdata2['ID']."'".(IsSet($show['showid']) && $showdata2['ID']==$show['showid'] ? " selected" : "").">".$showdata2['title']."</option>";
			}
			echo "
			</select>";
		}
		else { echo $splocale['error']." ".$splocale['no_preset_available']; }
		echo "</td></tr>\n";
				echo "<tr>
					<td class='tbl1' colspan='2' align='center'>
						<input type='submit' name='save' value='".$splocale['save']."' class='button' />
					</td>
				</tr>
			</table>
			<br />
			</div>
		</form>		
		";
	
	
}

else {
opentable($splocale['scheduler_myshows'].($sp_channelname!="" ? " &raquo; ".htmlentities($sp_channelname) : ''));
include $sp_install_folder."dj_headermenu.php";
if(IsSet($error['delete'])) { echo "<br /><div class='admin-message'><strong>".$splocale['error']."</strong> ".$splocale['used_show_not_deletable']."</div>\n"; }
elseif(IsSet($_GET['success']) && $_GET['success']=="added") { echo "<br /><div class='admin-message'><strong>".$splocale['show_successfully_saved']."</div>\n"; }
elseif(IsSet($_GET['success']) && $_GET['success']=="deleted") { echo "<br /><div class='admin-message'><strong>".$splocale['show_deleted']."</div>\n"; }
echo "
<br />
";
$result=@dbquery("SELECT * FROM `".$sendeplan_db['table_shows']."` WHERE `user_id` = '".$userdata['user_id']."'");
$rows = @dbrows($result);
if ($rows == 0) {
	echo "<br />
	<div class='admin-message'>".$splocale['no_own_show_found']."</div>
	<br />
	";
}
else {
	echo "
	<table width='100%' align='center' class='tbl-border' cellspacing='1' cellpadding='0' border='0'>
		<tr>
			<td class='tbl2' style='padding:2px;'>";
			$i=0;while ($show = dbarray($result)) {				 
				$result2=dbquery("SELECT * FROM `".$sendeplan_db['table_showdata']."` WHERE `ID` = '".$show['showid']."'");
				$rows = dbrows($result2);
				if ($rows != 0) {
						$showdata = dbarray($result2);
						$i++;
						if($i>1) { echo "\n<hr />\n"; }
						
						echo "
						<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'].";' align='center'>
							<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 none; height: ".($sp_setting['showbox_height']-30)."px; overflow: hidden;'><div style='width: auto; font-family: ".$sp_setting['font_showtitle']."; font-size: ".$sp_setting['fontsize_showtitle']."px; font-weight: ".($sp_setting['bold_showtitle']==true ? "bold" : "normal").";' align='".$sp_setting['align_showtitle']."'><table width='100%' border='0' align='left' cellpadding='2' cellspacing='0' style='font-family: ".$sp_setting['font_showtitle']."; font-size: ".$sp_setting['fontsize_showtitle']."px; color: ".$showdata['font-color']."';><tr><td><strong>XX:00".($sp_setting['view_endtime']=="1" ? "-XX:00" : "")."</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!' title='Sendung wird per Video &uuml;bertragen' /></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!' title='Sendung wird NICHT per Video &uuml;bertragen' /></td>" : "")."</tr></table>".$show['title']."</div>
							<div style='font-family: ".$sp_setting['font_showdescription']."; font-size: ".$sp_setting['fontsize_showdescription']."px;color: ".$showdata['font-color']."; font-weight: ".($sp_setting['bold_showdescription']==true ? "bold" : "normal").";' align='".$sp_setting['align_showdescription']."'>".nl2br($show['description'])."</div></div></div>
							<div align='center'><form method='post' action='".FUSION_SELF."'><input type='hidden' name='showid' value='".$show['ID']."' /><input type='submit' name='delete' class='button' value='".$splocale['delete']."' onclick='return RemoveItem(\"".$show['title']."\");'/>&nbsp;<input type='submit' name='edit' class='button' value='".$splocale['edit']."' /></form></div>						
						";						
				}
				echo "</div>";
			}
	echo "
			</td>		
		</tr>
	</table>
	";	
}

echo "<hr />
<div align='center'>[<a href='".FUSION_SELF."?action=new'>".$splocale['add_new_show']."</a>]</div>
<br />
";
}
closetable();

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