<?php
/**
 * Phoebe pedagogic planner
 *
 * PHP version 5
 *
 * LICENSE: The GNU General Public License (GPL), version 2. See LICENSE.txt
 *
 * Copyright (c) 2006-2008 JISC/HEFCE/University of Oxford
 *
 * @author      David Balch <david.balch@conted.ox.ac.uk>
 * @license     http://opensource.org/licenses/gpl-license.php GPL2
 * @link        http://phoebe-project.conted.ox.ac.uk/
 */

$content['settings'] = 'view';
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/core.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/session.php');

// Get design as XML
include($_SERVER['DOCUMENT_ROOT'].'/db/getDesign.php');
$designXml = getDesignXml();
// Prepare DOM objects
$proc = new XSLTProcessor;
$designXsl = new DOMDocument;
$designXmlDoc = new DOMDocument;
$designXmlDoc->loadXML($designXml);

// Get title from design
$xp = new DomXPath($designXmlDoc); 
$res = $xp->query("/data/designInfo/title");
$designTitle = $res->item(0)->textContent;
$designTitle = str_replace(' ', '_', $designTitle);
//echo $designTitle;

// Set desired presentation format
switch ($_GET['mode']) {
    case 'XML':
        $presMode = 'XML';
        $returnXML = true;
        break;

    case 'HTML_H':
        $presMode = 'HTML_H';
        $xslFile = $CNF->root."view/xml2html.xsl";
        $xslTransform = true;
        break;

    default:
        $presMode = 'HTML_T';
        $xslFile = $CNF->root."view/xml2htmlTables.xsl";
        $xslTransform = true;
        break;
}

if($_GET['hide_empty_fields'] == 'on') {
    $hideEmpty = true;
} else {
    $hideEmpty = false;
}

if($hideEmpty) {
    // Transform XMl to remove empty fields
    $trimXsl = new DOMDocument;
    $trimXslFile = $CNF->root."view/trimEmpty.xsl";
    $trimXsl->load($trimXslFile);

    // Configure the transformer
    $proc->importStyleSheet($trimXsl); // Attach the XSL rules

    // Transform
    $designXmlDoc = $proc->transformToDoc($designXmlDoc);
    $designXml = $designXmlDoc->saveHTML();
}

if ($returnXML) {
    // Return the design as XML download
    header("Content-Type: text/xml");
    header('Content-Disposition: attachment; filename="Phoebe_'.
    $designTitle.'.xml"');
    //header("Content-Type: text/plain");
    echo $designXml;

} else if($xslTransform) {
    // Return the design as a webpage
    // Prepare standard View page content
    $content['windowTitle'] = 'View design' . ' | ' . $CNF->appName;
    $content['main'] = '<h1 id="mainTitle"><a href="/" class="titleLink">Phoebe</a></h1>';
    $content['main'] .= $topNav;

    $viewModes = array('HTML_T' => 'HTML tables', 'HTML_H' => 'HTML hierarchy', 'XML' => 'XML');

    // View mode
    $viewForm .= '<form action="" method="GET" id="viewChooser">';
    $viewForm .= '<input type="hidden" name="design_id" value="'.$design_id.'">';
    $viewForm .= '<label>View format: <select name="mode">';
    foreach($viewModes as $mode => $modeName) {
        if ($mode == $presMode) {
            $selected = ' selected="selected"';
        } else {$selected = '';}
        $viewForm .= '<option id="view'.$mode.'" value="'.$mode.'"'.$selected.'>';
        $viewForm .= $modeName.'</option>';
    }
    $viewForm .= '</select></label>';
    if($hideEmpty) {$emptyChecked = ' checked="checked"';}
    $viewForm .= ' | <label>Hide empty fields:<input type="checkbox" name="hide_empty_fields"'.$emptyChecked.'/></label>';
    $viewForm .= ' | <input type="submit" value="Change format"/></form>';

    $content['main'] .= $viewForm;
    if (userOwnsDesign($_GET['design_id'], $_SESSION['userid'])) {
        $content['main'] .= '<p><a href="/editDesign.php?design_id='.$_GET['design_id'].'">Edit design <img src="/inc/img/edit.gif" alt="Edit" title="Edit this design" height="16" width="16"></a></p>';
    }
    $printLink = '<p class="printLink"><a href="#" onclick="print(); return false">Print design</a></p>';
    $content['main'] .= $printLink;

    // Configure the transformer
    $designXsl->load($xslFile);
    $proc->importStyleSheet($designXsl); // attach the xsl rules

    // Transform
    $designHtml = $proc->transformToXML($designXmlDoc);

    $content['main'] .= $designHtml;
    $content['main'] .= $printLink;
    //$content['main'] .= '<hr>'.$designXml;

    // Write via template
    include_once($CNF->template);
}
?>
