<?php 
    # $Id: benchmarkInstance_NEW.php,v 1.8 2007/01/14 00:43:09 jarda Exp $

    /* Page for entering data for new benchmark instance creation
    */

    require_once('structuredPage.inc.php');

    class PageClass extends EditDBStructuredPage 
    {
        function getTitle()
        {
            if ($this->areSecondStageDataAvailable())
                return 'Benchmark instance addition - step two';
            else
                return 'Benchmark instance addition - step one';
        }

    
        function printContents() {
            if ($this->areSecondStageDataAvailable())
            {
                $this->printSecondStageContents();
            }
            else
            {
                $this->printFirstStageContents();
            }
        }


        function areSecondStageDataAvailable()
        {
            return ($_GET['benchmarkSetId'] && $_GET['formatId'] && $_GET['benchmarkName']);
        }


        function printFirstStageContents()
        {
            $db = connect();

            echo("<form action=\"benchmarkInstance_NEW.php\" method=\"get\">\n");
            echo "<table>\n";
            echo "<tr><td>Format:</td>";
            echo "<td><select name=\"formatId\">\n";

            $query = "SELECT * FROM format";
            
            $result = $db->Execute($query);
            
            while ($row = $result->FetchRow()) {
                echo("<option value=$row[id]>$row[name]</option>\n");
            }
            
            echo("        </select></td></tr>\n");
            echo "<br>\n";
            echo("<tr><td>Set:</td><td><select name=\"benchmarkSetId\">\n");
            
            $query = "SELECT * FROM benchmarkSet";
            $result = $db->Execute($query);

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

            echo("        </select></td></tr>\n");
            echo "<tr><td>Benchmark name:</td>";
            echo "<td><input type=\"text\" name=\"benchmarkName\"></td></tr><br>\n";
            echo "</table>\n";
            echo "<br>";
            echo "<input type=\"submit\" value=\"Next step\">";
            echo "<input type=\"submit\" value=\"Cancel\">";
            echo("</form>\n");
        }

        function printSecondStageContents() {
            $formatId = $_GET[formatId];

            $db = connect();

            $this->insertSecondStageInfo($db);

            # form header {{{
            echo("<form enctype=\"multipart/form-data\" action=\"benchmarkInstance_ADD.php\" method=\"post\">\n");
            echo("<input type=\"hidden\" name=\"formatId\" value=\"$formatId\">");
            echo("<input type=\"hidden\" name=\"benchmarkSetId\" value=\"$_GET[benchmarkSetId]\">");
            echo("<input type=\"hidden\" name=\"benchmarkName\" value=\"$_GET[benchmarkName]\">");
            # }}}

            echo "<hr>\n";

            $this->insertBenchmarkDescription($db);
            
            echo "<hr>\n";

            # instance description {{{
            echo "<table><tr>\n";
            echo "<td><table><tr><td>Instance description:</td></tr><tr><td>&nbsp;</td></tr></table></td>";
            echo "<td><textarea name=\"benchmarkInstanceDescription\"></textarea></td>";
            echo "</tr></table>\n";
            echo "<br>\n";
            # }}}

            echo "<hr>\n";

            # file to upload {{{
            echo "File: <input name=\"uploadedFile\" type=\"file\"><br>\n";
            # }}}
            
            # file to upload {{{
            echo "Read attribute values from file: <input name=\"parse\" type=\"checkbox\"><br>\n";
            # }}}
            
            echo "<hr>\n";

            # attributes {{{
            echo("    <table border=\"1\">\n");
            echo("    <tr><th>Name</th><th>Value</th>\n");
            $query = "SELECT * FROM formatAttribute, attribute WHERE formatId=$formatId AND formatAttribute.attributeId = attribute.id";
            $result = $db->Execute($query);
            while ($row = $result->FetchRow()) {
                echo "        <tr><td>$row[name]</td><td><input type=\"text\" name=\"attribute_$row[attributeid]\"></td></tr>";
            }
            echo("    </table>\n");
            # }}}

        
            echo "<br>\n";

            # buttons {{{
            echo "<input type=\"submit\" name=\"OK\" value=\"OK\">";
            echo "<input type=\"submit\" name=\"Cancel\" value=\"Cancel\">\n";
            # }}}
            
            echo("</form>\n");
        }

        function checkEnvironment()
        {
            if (!$this->areSecondStageDataAvailable())
                return True;

            $db = connect();

            $recordSet = $db->Execute("SELECT * FROM format WHERE id=$_GET[formatId]");
            if (!$recordSet->RecordCount())
                return False;

            $recordSet = $db->Execute("SELECT * FROM benchmarkSet WHERE id=$_GET[benchmarkSetId]");
            if (!$recordSet->RecordCount())
                return False;
            
            return True;
        }

        function insertSecondStageInfo($db)
        {
            echo "<table>\n";
            
            echo"<tr><td>Benchmark name:</td><td>$_GET[benchmarkName]</td></tr>\n";
            
            $recordSet = $db->Execute("SELECT * FROM format WHERE id=$_GET[formatId]");
            $row = $recordSet->FetchRow();
            echo"<tr><td>Format:</td><td>$row[name]</td></tr>\n";
            

            $recordSet = $db->Execute("SELECT * FROM benchmarkSet WHERE id=$_GET[benchmarkSetId]");
            $row = $recordSet->FetchRow();
            echo"<tr><td>Set:</td><td>$row[name]</td></tr>\n";
            
            echo "</table>\n";
        }

        function insertBenchmarkDescription($db) 
        {
            $benchmarkName = $_GET['benchmarkName']; 

            if (!$benchmarkName) {
                insertEditableDescription();
                return;
            }

            $query = "SELECT description FROM benchmark WHERE name='$benchmarkName'";
            $recordSet = $db->Execute($query);
            
            if (!$recordSet->RecordCount()) {
                $this->insertEditableDescription();
            }
            else {
                $row = $recordSet->FetchRow();
                $this->insertNonEditableDescription($row['description']);
            }
        }

        function insertEditableDescription()
        {
            echo "<table><tr>\n";
            echo "<td><table><tr><td>Benchmark description:</td></tr><tr><td>&nbsp;</td></tr></table></td>";
            echo "<td><textarea name=\"benchmarkDescription\"></textarea></td>";
            echo "</tr></table>\n";
            echo "<br>\n";
        }

        function insertNonEditableDescription($description)
        {
            echo "<table>\n";
            echo "<tr><td>Benchmark description:</td><td>$description</td></tr>\n";
            echo "</table>\n";
        }
        
    }

    launchPage();

    # vim:enc=iso8859-2:fenc=iso8859-2:
?>
