<?php 
    # $Id: benchmarkInstance_EDIT.php,v 1.6 2007/01/14 15:09:31 jarda Exp $

    require_once('structuredPage.inc.php');

    class PageClass extends EditDBStructuredPage {

        function checkEnvironment()
        {
            if (!$_GET['id'])
            {
                return False;
            }

            return True;
        }


        function handleInvalidEnvironment()
        {
            header("Location: index.php"); /* Redirect browser */
            exit();
        }


        function getTitle()
        {
            return 'Benchmark instance editing';
        }
        

        function printContents() {
            $db = connect();
            $query = "SELECT benchmark.name AS name, benchmarkInstance.description AS instancedescription, " .
                "benchmarkSet.name AS setName, " .
                "benchmark.description AS benchmarkdescription, " .
                "benchmarkInstance.formatId AS formatId " .
                "FROM benchmark, benchmarkInstance, benchmarkSet " .
                "WHERE benchmarkInstance.id = $_GET[id] AND " .
                "benchmarkInstance.benchmarkSetId = benchmarkSet.id AND " .
                "benchmarkInstance.benchmarkId = benchmark.id";
            $recordSet = $db->Execute($query);
            $row = $recordSet->FetchRow();

            echo("<form action=\"benchmarkInstance_CHANGE.php\" method=\"post\">\n");
            echo("<input type=\"hidden\" name=\"id\" value=\"$_GET[id]\">");
            echo("    <table>\n");
            echo("        <tr><td>Name:</td><td>$row[name]</td></tr>\n");
            echo("        <tr><td>New name:</td><td><input type=\"text\" name=\"name\"></td></tr>\n");
            echo("        <tr><td>Benchmark set:</td><td>$row[setname]</td></tr>\n");
            echo("        <tr><td>Move to set:</td><td>");
            $this->insertBenchmarkSetSelection($db);
            echo("</td></tr>\n");
            echo("        <tr><td>General description:</td><td><textarea name=\"benchmarkDescription\">$row[benchmarkdescription]</textarea></td></tr>\n");
            echo("        <tr><td>Instance description:</td><td><textarea name=\"instanceDescription\">$row[instancedescription]</textarea></td></tr>\n");
            echo("        <tr><td>Format:</td>");

            $formatId = $row['formatid'];

            # format {{{
            $query = "SELECT * FROM format WHERE id=$formatId";
            $recordSet = $db->Execute($query);
            $row = $recordSet->FetchRow();
            echo "<td>$row[name]</td></tr>\n";
            echo("    </table><p>\n");
            # }}}
            #

            # attributes {{{
            echo("    <strong>Benchmark attributes</strong>\n");
            echo("    <table border=\"1\">\n");
            echo("    <tr><th>Name</th><th>Value</th>\n");
            $query = "SELECT * FROM formatAttribute, attribute WHERE formatAttribute.formatId = $formatId AND formatAttribute.attributeId = attribute.id";
            $recordSet = $db->Execute($query);
            while ($row = $recordSet->FetchRow()) {
                $valueQuery = "SELECT value FROM benchmarkInstanceAttribute " .
                              "WHERE attributeId = $row[attributeid] " .
                              "AND benchmarkInstanceId = $_GET[id]";
                $valueRecordSet = $db->Execute($valueQuery);
                $valueRow = $valueRecordSet->FetchRow();
                if ($valueRow)
                    $value = $valueRow['value'];
                else
                    $value = '';
                echo "        <tr><td>$row[name]</td><td><input type=\"text\" name=\"attribute_$row[attributeid]\" value=\"$value\"></td></tr>";
            }
            # }}}

            //$query = "SELECT * FROM attribute, formatAttribute WHERE benchmarkId=$_GET[id] AND ";

                echo("        <tr><td></td></tr>\n");
            echo("        </td></tr>\n");
            echo("    </select>\n");
            echo("    </table>\n");
            echo("    <p>\n");
            echo("    <p>\n");
            echo("    <input type=\"submit\" name=\"OK\" value=\"OK\">\n");
            echo("    <input type=\"submit\" name=\"Cancel\" value=\"Cancel\">\n");
            echo("    </p>\n");
            echo("</form>");
        }


        function insertBenchmarkSetSelection($db)
        {
            echo("<select name=\"benchmarkSetId\">\n");
            echo("<option value=\"\"></option>\n");

            $query = "SELECT * FROM benchmarkSet";
            $result = $db->Execute($query);

            while ($row = $result->FetchRow()) {
                echo "<option value=\"$row[id]\"";
                if ($row['id'] == $_GET['benchmarkSetId'])
                    echo " selected";
                echo ">$row[name]</option>\n";
            }

            echo("</select>");
        }
    }

    launchPage();
    
    # vim:enc=iso8859-2:fenc=iso8859-2:
?>
