<?php
// Call in the XComponents engine and options file
define('MOD_PHP', substr(PHP_SAPI, 0, 3) != 'cgi'); // Are we running as mod_php?
define('DOCROOT', 
	MOD_PHP && isset($_SERVER['DOCUMENT_ROOT']) ? 
		$_SERVER['DOCUMENT_ROOT'] : 
		(
			MOD_PHP && isset($_SERVER['SCRIPT_FILENAME']) ? 
				substr($_SERVER['SCRIPT_FILENAME'], 0, 0-strlen($_SERVER['PHP_SELF'])) : 
				(
					substr(str_repeat('../', count(explode('/', preg_replace('_/+_', '/', $_SERVER['PHP_SELF'])))-2), 0, -1) != '' ?
						substr(str_repeat('../', count(explode('/', preg_replace('_/+_', '/', $_SERVER['PHP_SELF'])))-2), 0, -1) :
						'.'
				)
		)
	);
include(DOCROOT . '/dmx/dmx_options.php');
include(DOCROOT . '/dmx/core/php/dmx_shop_handler.php');
include(DOCROOT . '/dmx/core/php/dmx_shop_components.php');

// Abs code starts here
$abs_root = '';
@include(DOCROOT . '/abs_version_control.php');
include(DOCROOT . $abs_root . '/common/authenticate.php');
include(DOCROOT . $abs_root . '/common/header.php');
?>

<style type="text/css">
<!--

.page
{
	border-bottom: 0px;
}

.current_unapproved, .current_approved, .future_unapproved, .future_approved
{
	font-size: 11px;
	color: #000;
	margin-bottom: 1px;
	padding: 2px 10px;
}

.current_approved
{
	background-color: #D3FFCC;
}

.future_approved
{
	background-color: #FFD599;
}


.current_unapproved, .future_unapproved
{
	background-color: #FF8080
}

.current_unapproved, .current_approved
{
	margin-top: 7px;
}

.controls
{
	float: right;
	font-size: 12px;
}

.controls a
{
	color: #800A25;
	text-decoration: none;
	border-bottom : 0px;
}

.p_display
{
	margin: 10px 0px 30px 0px;
}

.loading
{
	border: 1px solid #999;
	padding: 20px;
	text-align: center;
	line-height: 150%;
}

/* Sidepages */
#sp_0, #sp_1, #sp_2, #sp_3, #sp_4, #sp_5, #sp_6, #sp_7, #sp_8, #sp_9
{
	width: 750px;
	display: block !important;
	visibility: visible !important;
}

#debug
{
	display: none !important;
}

.info_layer
{
	position: relative !important;
	top: 0px !important;
	left: 0px !important;
	z-index: 1;
	padding: 10px;
	background-image: url("images/trans_70.png");
	visibility: visible !important;
}

-->
</style>

<!-- STYLESHEET OVERRIDES FOR IN-PAGE POPUPS (or 'SIDEPAGES') -->
<!-- These are normally hidden floating layers, but are shown in this page -->
<style type="text/css">
#sp_0, #sp_1, #sp_2, #sp_3, #sp_4, #sp_5, #sp_6, #sp_7, #sp_8, #sp_9
{
	position: relative;
	top: 0px !important;
	left: 0px !important;
	z-index: 0;
}
</style>	

	
<script type="text/javascript">

if (!dmb1_get_cookie('abs_content')) set_init_abs_content();

function set_init_abs_content()
{
	var exp = dmb1_get_exp_date(1,0,0); // Set a cookie expiry 1 day from now
	// Get the directory to edit and store in cookie
	var val = '<?=$abs_opt['version'];?>__<?=$panel->today;?>__on';
	dmb1_set_cookie('abs_content', val, exp, '/');
	abs_edit_buttons = 'on';
}

// ---------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------
// Ajax interface to panel server

$panel_server_url = '../common/panel_server.php';

// ---------------------------------------------------------------------------------------------

function show_panel(id, self)
{
	
	var t = $('a_' + id);
	if (!$defined(t.firstChild))
	{
		var html = '<div class="loading"><img src="<?=$abs_root;?>/themes/<?=$abs_opt['theme'];?>/sgm/interface/ajax_loader_col1.gif"><br>fetching panel<\/div>';
		t.set('html', html);
		var panajax = new Request.HTML( { url: $panel_server_url, data: { act: 'serve', path: id + '.html' }, update: t } );
		panajax.get();
	}
	if (id.indexOf('--sb_') != -1 || id.indexOf('-side') != -1)
	{	
		t.set( { 'styles': { 'width': '250px', 'display': 'block' } } ); // So sidebar panels preview correctly
	}
	else
	{
		t.set( { 'styles': { 'display': 'block' } } );
	}
	$(self).set('text', 'hide preview');
	$(self).setProperty('onclick', 'this.blur(); hide_panel(\'' + id+ '\', this); return false;'); 
}

// ---------------------------------------------------------------------------------------------

function hide_panel(id, self)
{
	$('a_' + id).set( { 'styles': { 'display': 'none' } } );
	$(self).set('text', 'preview');
	$(self).setProperty('onclick', 'this.blur(); show_panel(\'' + id + '\', this); return false;'); 
}

// ---------------------------------------------------------------------------------------------

function approve_panel(id)
{
	var t = $('a_' + id);
	var html = '<div class="loading"><img src="<?=$abs_root;?>/themes/<?=$abs_opt['theme'];?>/sgm/interface/ajax_loader_col1.gif"><br>approving panel<\/div>';
	t.set('html', html);
	t.set( { 'styles': { 'display': 'block' } } );
	var params = {
		act : 'approve',
		path: id + '.html'
	};
	var panajax = new Request.HTML( { url: $panel_server_url, data: params, update: t.getParent().getParent() } );
	panajax.post();
}

// ---------------------------------------------------------------------------------------------

function publish_panel(id)
{
	var t = $('a_' + id);
	var html = '<div class="loading"><img src="<?=$abs_root;?>/themes/<?=$abs_opt['theme'];?>/sgm/interface/ajax_loader_col1.gif"><br>publishing panel<\/div>';
	t.set('html', html);
	t.set( { 'styles': { 'display': 'block' } } );
	var params = {
		act : 'publish',
		path: id + '.html'
	};
	var panajax = new Request.HTML( { url: $panel_server_url, data: params, update: t.getParent().getParent() } );
	panajax.post();
}

// ---------------------------------------------------------------------------------------------

function refresh_page_list(owner)
{
	var t = $(owner);
	
	var params = {
		act : 'refresh_page_list',
		owner: owner
	};
	var panajax = new Request.HTML( { url: $panel_server_url, data: params, update: t } );
	panajax.post();
}

// ---------------------------------------------------------------------------------------------

</script>
	
<!-- MAIN CONTENT AREA -->
<div id="main_area" onmouseover="sub_menu_hide();">
	<div id="main_area_content">
		
		<div class="main_column">
		<!-- ----------------- -->
		<!-- BEGIN MAIN COLUMN -->
		<!-- ----------------- -->
			
		<h1>Manage panels and panel updates</h1>
		
		<p>
		<b>This page lists all current panels, together with pending updates,
		and indicates each panel's status using a 'traffic lights' metaphor.</b>
		All colour coding is relative to the actual current date, not the edit date.
		</p>
		
		<table cellpadding="0" cellspacing="5" border="0">
			<tr>
				<td><img src="images/tr_li_red.gif" width="30" height="26" alt="Red"></td>
				<td>Red</td>
				<td>- &nbsp; Panel requires approval, and will not go live until approved.</td>
			</tr>
			<tr>
				<td><img src="images/tr_li_amber.gif" width="30" height="26" alt="Amber"></td>
				<td>Amber</td>
				<td>- &nbsp; Panel doesn't require approval, but is not yet live (its release date is in the future).</td>
			</tr>
			<tr>
				<td><img src="images/tr_li_green.gif" width="30" height="26" alt="Green"></td>
				<td>Green</td>
				<td> - &nbsp; Panel is currently live, and may be seen by visitors to the site.</td>
			</tr>
		</table>
		
		<br />
		
		<?php
		
		/*
		// OUTPUT A LIST OF PANELS
		// -------------------------------------------------------------------------------------------------
		
		(1) Compile list of distinct panels (based on owner and panel ID, discounting release date & status)
		
		(2) For each distinct panel, show the current version and pending version, with links back to page
		
		// -------------------------------------------------------------------------------------------------
		*/
		
		$panel->today = gmdate('Ymd'); // This is always done RELATIVE TO CURRENT DATE, to avoid confusion
		$panel->debug->debug = TRUE; 
		$allpanels = $panel->getall();
			
		foreach($allpanels as $owner => $panels)
		{
			$link = $owner != 'fragment' ? $abs_root . '/' . str_replace('-', '/', $owner) . '.php' : $abs_root . '/admin/fragment/';
			echo('<div id="' . $owner . '">');
			echo('<h2 class="page"><a href="' . $link . '" target="_blank" class="popup">' . $owner . '</a></h2>');
			foreach($panels as $panel_id)
			{
				$panel->request($owner, $panel_id);
				if ($panel->panel_file) create_panel_bar($panel->panel_file, $owner);
				if ($panel->panel_next) create_panel_bar($panel->panel_next, $owner);
			}
			echo('</div>');
		}
		
		// -------------------------------------------------------------------------------------------------
		
		function create_panel_bar($pan, $owner)
		{
			global $panel;
			$future = $pan['release_date'] > $panel->today;
			$class  = $future ? 'future' : 'current';
			$class .= $pan['status'] == 'p' ? '_unapproved' : '_approved';
			$id = substr(str_replace($panel->root, '', $pan['path']), 0, -5);
			?>
			<div>
				<div class="<?=$class;?>">
					<div class="controls">
						<?php if ($pan['status'] == 'p') : ?>
							<a href="#" onclick="this.blur(); approve_panel('<?=$id;?>'); return false;">approve</a>	
							|
						<?php elseif ($future) : ?>
							<a href="#" onclick="this.blur(); publish_panel('<?=$id;?>'); return false;">publish</a>	
							|
						<?php endif; ?>
						<a href="editor.php?panel_path=<?=$id;?>&return_url=close_me.php&owner=<?=$owner;?>" target="_blank">edit</a>	
						|
						<a href="#" onclick="this.blur(); show_panel('<?=$id;?>', this); return false;">preview</a>	
					</div>
					<?=$id;?>
				</div>
				<div id="a_<?=$id;?>" class="p_display" style="display: none;"></div>
			</div>
			<?php
		}
		
		// -------------------------------------------------------------------------------------------------
		
		?>

		<!-- --------------- -->
		<!-- END MAIN COLUMN -->
		<!-- --------------- -->
		</div>
		
		<div class="sidebar">
			<!-- ------- -->
			<!-- SIDEBAR -->
			<!-- ------- -->
					
			<div class="sidebar_box_heading">
				<h2>Controls</h2>
			</div>
			<div class="sidebar_box_content">
				<div>
					
					<h2>Content Management</h2>
					<p>
					<a href="index.php">Manage Panels and Panel Updates</a>
					<br>
					<a href="version_date.php">Set Edit Date &amp; Version</a>
					<br>
					<a href="adaptingbody/index.php">Show Adapting Bodies</a>
					</p>
					
					<h2>SEO</h2>
					<p>
					<a href="seo_regenerate.php">Reload Easy SEO Data</a>
					</p>
			
					<h2>Security</h2>
					<p>
					<a href="set_pw.php">Reset Username and Password</a>
					</p>
					
				</div>
			</div>

			<!-- ----------- -->
			<!-- END SIDEBAR -->
			<!-- ----------- -->
		</div>
		
	</div>
</div>
<!-- END MAIN CONTENT AREA -->

<br style="clear: both;">
<br>

<?php include(DOCROOT . $abs_root . '/common/footer.php'); ?>
