<?php

// panel_server.php
// -------------------------------------------------------------------------------------------------
// Designed to serve dmABS panel requests made using AJAX
//
// Project    > dmClubNewYear08:C37
// Copyright  > (C) Digital Mail Limited (2005-2008)
// Author     > RAK for DML
// -------------------------------------------------------------------------------------------------

$panel_server_debugging = FALSE;

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

// 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_root = '';
@include(DOCROOT . '/abs_version_control.php');

// include_once('abs_tech_options.php');
include_once('abs_lib.php');
// include_once('abs_run.php');

include(DOCROOT . '/dmx/core/php/class/debug.class.php');
$abs_debug = new debug(0);

// Set overall mode of site operation
// Valid values are 'AFFILIATE', 'ADAPTED'
$abs_sitemode = 'ADAPTED';

// Get site options
$abs_opt = abs_get_site_options($abs_sitemode);

// Resolve version (so we know which panels directory to use)
$abs_content = abs_resolve_version();

// Analyse URL
$abs_path = abs_analyse_path($_SERVER['PHP_SELF']);

// Try to determine page subject
$abs_subject = abs_get_page_subject();

// Include substitution class, and generate substitutions
require_once(DOCROOT . '/dmx/core/php/class/substitutions.class.php');
$abs_glos = new Substitutions();
abs_compile_substitutions();

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

// NOW FOR THE PANEL SERVING...

$act   = isset($_REQUEST['act'])   ? $_REQUEST['act'] 				: NULL;
$path  = isset($_REQUEST['path'])  ? urldecode($_REQUEST['path']) 	: NULL;
$today = isset($_REQUEST['today']) ? $_REQUEST['today'] 			: gmdate('Ymd');

// Include panel class, and initialise it
require_once(DOCROOT . '/dmx/core/php/class/panel.class.php');
$panel = new Panel(
	DOCROOT . $abs_root . '/_CONTENT',
	$abs_content['version'],
	$abs_content['default'],
	$today
);
$panel->pan_globals  = array('abs_root', 'abs_opt', 'abs_subject', 'abs_number'); // Place these variables in scope of panel renderer
$panel->js_interface = 'abs_editable'; // Push panel names into 'abs_editable' javascript object
$panel->debug->debug = $panel_server_debugging; 

switch($act)
{
	case 'approve':
		$panel->approve_panel($path);
		refresh_page_list($panel->panel_file['owner']);
		break;
		
	case 'publish':
		$panel->publish_panel($path);
		refresh_page_list($panel->panel_file['owner']);
		break;

	case 'refresh_page_list':
		sleep(1);
		refresh_page_list($_POST['owner']);
		break;

	case 'serve':
		// INPUT: PANEL TO GET
		$owner 	  = isset($_POST['owner']) 	  ? urldecode($_POST['owner'])    : NULL;
		$panel_id = isset($_POST['panel_id']) ? urldecode($_POST['panel_id']) : NULL;
		ob_start(array($abs_glos, 'substitute'));
		if ($owner && panel_id)
		{
			$panel->render($owner, $panel_id);
		}
		else if ($path)
		{
			$panel->render_absolute($path);
		}
		else
		{
			echo('<p>panel_server says bad input</p>');
		}
		ob_end_flush();
		break;

}

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

if ($panel_server_debugging) $panel->debug->display();

exit;

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

function refresh_page_list($owner)
{
	global $panel, $abs_root;
	$panel->today = gmdate('Ymd'); // This is always done relative to current date, to avoid confusion
	$allpanels = $panel->getall($owner . '--*');
	
	foreach($allpanels as $owner => $panels)
	{
		$link = $owner != 'fragment' ? $abs_root . '/' . str_replace('-', '/', $owner) . '.php' : $abs_root . '/admin/fragment/';
		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);
		}
	}
}

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

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
}

?>