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

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

// Load studiobox settings
$sb_setting = @dbarray(dbquery("SELECT `default_sp`,`guests_allowed`,`infotext` FROM `".$sb_db['table_settings']."` WHERE `profile` = 'default' LIMIT 1"));

if(!FUSION_V6) { 
	if (!checkrights("SWSB") || !defined("iAUTH") || $_GET['aid'] != iAUTH) { redirect("../../index.php"); }
	require_once THEMES."templates/admin_header.php";
	include LOCALE.LOCALESET."admin/infusions.php";
}
else {
	if (!checkrights("IP")) fallback("../../index.php");
	require_once BASEDIR."subheader.php";
	require_once ADMIN."navigation.php";
}

// save new settings
if(IsSet($_POST['savesettings'])) {	
		unset($save_error);
		//$connect = explode("|",stripinput($_POST['sp_connect']));
		$connect = (stripinput($_POST['sp_connect'])*1);
		$query = "UPDATE `".$sb_db['table_settings']."` SET
		`default_sp` = '".$connect."',
		`guests_allowed` = '".($_POST['guests_allowed']*1)."',
		`infotext` = '".addslash($_POST['infotext'])."'		
		WHERE `profile` = 'default' LIMIT 1";
		$result = dbquery($query);
		redirect(FUSION_SELF.$aidlink."&status=success");
		exit;
}

// look for scheduler folders in infusions directory
if ($handle = opendir(INFUSIONS)) {
	$sp_folders = array();
	$sp_install_numbers = array();
	$sp_names = array();
	$s = 0;
    	while (false !== ($file = readdir($handle))) {		
        	if ($file != "." && $file != ".." && is_dir(INFUSIONS.$file) && file_exists(INFUSIONS.$file."/sendeplan.php") && file_exists(INFUSIONS.$file."/infusion_db.php")) {
			// check if installed and right version
			unset($sp_install_number,$sp_channelname,$tmp);
			include(INFUSIONS.$file."/infusion_db.php");
			if(IsSet($sp_install_number)) {
				$result = dbquery("SHOW TABLES LIKE '".DB_PREFIX."sendeplan".($sp_install_number>0 ? $sp_install_number : '')."_settings'");
				if (dbrows($result)== 1) {
					$tmp = dbarray(dbquery("SELECT `version` FROM `".DB_PREFIX."sendeplan".($sp_install_number>0 ? $sp_install_number : '')."_settings` WHERE `profile` = 'default' LIMIT 1"));
					if(IsSet($tmp['version']) && $tmp['version']>=3.02) {				
						$sp_folders[$s] = $file;
						$sp_install_numbers[$s] = $sp_install_number;
						$sp_names[$s] = $sp_channelname;
						$s++;
					}
				}
			}
		}
    	}
    	closedir($handle);
}

opentable($sb_locale['studiobox_admintitle']);
if(IsSet($_GET['status']) && $_GET['status']=="success") {
	echo "<div class='admin-message'>".$sb_locale['successfully_saved']."</div>";
}

if(count($sp_folders)==0) {
	echo "<div class='admin-message'>".$sb_locale['no_scheduler_found']."<br />".$sb_locale['get_more_infos'].": [<a href='http://www.systemweb.de/' target='_blank' title='Systemweb.de'>www.systemweb.de</a>]</div>\n";
	$sw_requirements=false;
}
else {
	$sw_requirements=true;
}
if(IsSet($save_error)) {
	echo "<div class='admin-message'>".$save_error."</div>\n";
}

if($sw_requirements==true) {

echo "
<form method='post' action='".FUSION_SELF."?aid=".iAUTH."'>
<table width='100%' class='tbl-border' cellpadding='0' cellspacing='1' border='0' align='center'>
	<tr>
		<td>
			<div align='center'>
				<h4>".$sb_locale['studiobox_presets']."</h4>
			</div>
			 <table width='100%' cellspacing='0' cellpadding='0' border='0'>
			 	<tr>
					<td class='tbl2'>
						".$sb_locale['which_scheduler']."<br />
                        			<small>".$sb_locale['which_scheduler_hint']."</small>
                    			</td>
					<td class='tbl2' style='width: 280px;'>
						<select name='sp_connect' class='textbox' size='1' style='width: 220px;'>";
							for($s=0;$s<count($sp_install_numbers);$s++) {
								echo "
								<option value='".($sp_install_numbers[$s]!='' ? $sp_install_numbers[$s] : '0')."'".($sb_setting['default_sp']==($sp_install_numbers[$s]!='' ? $sp_install_numbers[$s] : '0') ? " selected" : "").">Sendeplan ".($sp_install_numbers[$s]=='' ? '' : $sp_install_numbers[$s])." ".(IsSet($sp_names[$s]) && $sp_names[$s]!="" ? "(".$sp_names[$s].")" : "")."</option>";
							}							
							echo "
						</select>
                     		</td>
				</tr>
				<tr>
					<td class='tbl1'>
						".$sb_locale['posts_allowed_for']."<br />
						<small>".$sb_locale['posts_allowed_for_hint']."</small>
                    			</td>
					<td class='tbl1'>
						<input type='radio' name='guests_allowed' class='textbox' value='0'".($sb_setting['guests_allowed']=='0' ? ' checked="checked"' : '').">".$sb_locale['members_only']."
						<br />
						<input type='radio' name='guests_allowed' class='textbox' value='1'".($sb_setting['guests_allowed']=='1' ? ' checked="checked"' : '').">".$sb_locale['guests_too'] ."
                     		</td>
				</tr>
                		<tr>
					<td class='tbl2'>
						".$sb_locale['studiobox_infotext']."<br />
                        			<small>".$sb_locale['studiobox_infotext_hint']."</small>                       
                    			</td>
					<td class='tbl2'>
						<textarea name='infotext' class='textbox' type='text' style='width: 220px;height:100px;'>".stripslashes($sb_setting['infotext'])."</textarea>						  
                     		</td>
				</tr>
      			</table>
		
             <div align='center' style='padding: 10px;'><input type='submit' value='".$sb_locale['studiobox_save_settings']."' name='savesettings' class='button' /></div>		
		</td>
	</tr>
</table>
</form>
";
}
closetable();

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

?>
