<?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'] = 'dbWrite,structureEdit';
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/core.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/utils.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/session.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/user.php');

// Get info for this design
$sql = 'SELECT * FROM designs '.
       'WHERE design_id = '.$mdb2->quote($_GET['design_id']);
//echo $sql.'<br>';
$design = $mdb2->queryRow($sql);
if (PEAR::isError($design)) {
    die($design->getMessage());
}

// Sanitize
foreach($design as $name=>$val) {
    $html[$name] = htmlspecialchars($val);
}

// Get field types
$sql = 'SELECT * FROM field_types';
$field_types = $mdb2->queryAll($sql);
if (PEAR::isError($field_types)) {
    die($field_types->getMessage());
}
$newFieldTypeSelect = '<label>Field type <select id="fieldType">';
$fieldTypesJS = '<script type="text/javascript">fieldTypes = new Array('."\n";
foreach($field_types as $type) {
    $newFieldTypeSelect .= ' <option title="'.$type['description'].'" value="'.$type['field_type_id'].'">'.$type['field_type_name'].'</option>';
    $fieldTypesJS .= '  new Object("description": "'.$type['description'].'", "field_type_id": "'.$type['field_type_id'].'", "field_type_name": "'.$type['field_type_name']."\"),\n";
}
$fieldTypesJS .= ");</script>\n";
$newFieldTypeSelect .= '</select></label> | ';
unset($field_types);

// Data area
$content['windowTitle'] = 'Design structure editor' . ' | ' . $CNF->appName;
$content['main'] = '<div id="unsupportedMsg">
        <h1>Phoebe pedagogic planner</h1>
        <p>Sorry, your browser configuration is not supported at this time.
        We recommend a current version of <a href="http://www.mozilla.com/">Firefox</a> with Javascript enabled.</p>
    </div>
    <div id="phoebeArea" class="hidden editStruct">';

$content['main'] .= '
        <!-- Toolbar -->
        <div id="phoebeToolbar">
            <h1 id="editorTitle"><a href="/" class="titleLink" title="Return to the Phoebe homepage">Phoebe</a> : <a href="/templates.php" title="Return to Design templates">Design templates</a> : '.$design['title'].'</h1>
            <ul class="buttons">
                <li class="sepButtons"><div id="outlineContainer" class="hidden"><div id="oviewTrig">Outline <img width="16" height="16" src="/inc/img/arrow-d.gif" alt="Open below"/></div></div></li>
                <li><a href="/help/" target="phoebeHelp"><img src="/inc/img/qmark.gif" alt="" title="Help" width="16" height="16"></a></li>
            </ul>
        </div>';


$content['main'] .= '
        <!-- Controls area -->
        <div id="designMeta">
        </div>
        <div id="designRoot" class="node editStructure">
        </div>

        <form id="editNodes" class="hidden" action="" onsubmit="createNewProp(this.form); return false;">';
$content['main'] .= '<input type="hidden" value="'.$html[design_id].'" id="desId"/>';
$content['main'] .= '<h2>Edit fields:</h2>
            <fieldset>
                <legend>Add property</legend>
                <label>Title: <input type="text" id="newPropTitle"/></label> | 
';
$content['main'] .= $newFieldTypeSelect;
$content['main'] .= '<label>Allow clones in design: <input type="checkbox" name="multiplicity" value="1"/></label> | ';
$content['main'] .= '<input type="button" value="Add" onclick="createNewProp(this.form); return false;"/>
            </fieldset>
        </form>

        <div id="hiddenNodes" class="hidden">
            <h2>Hidden fields:</h2>
            <span id="noHidden">There are no hidden fields.</span>
        </div>

            <div id="mask" class="hidden"> </div>
        </div>';

// Add field types object to page, for reference by javascript
$content['main'] .= $fieldTypesJS;

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