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

    /* Makes change to a benchmark instance
    */

    require_once('simplePage.inc.php');

    class PageClass extends EditDBSimplePage {

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

            return True;
        }


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


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

            # start the transaction
            $db->StartTrans();

            # prepare benchmark if name is going to be changed {{{
            if ($_POST['name'])
            {
                $benchmarkId = $this->getBenchmarkId($db, $name);

                if (!$benchmarkId)
                    $benchmarkId = $this->createBenchmark($db, $_POST['name'], $_POST['benchmarkDescription']);
            }
            # }}}

            # get benchmark instance info {{{
            $query = "SELECT * FROM benchmarkInstance WHERE id = $_POST[id]";

            $recordSet = $db->Execute($query);

            $benchmarkInstance = $recordSet->FetchRow();
            # }}}

            # change the benchmark instance data {{{
            $query = "UPDATE benchmarkInstance SET description = '$_POST[instanceDescription]'";
            
            if ($_POST['benchmarkSetId'])
                $query .= ", benchmarkSetId = $_POST[benchmarkSetId]";

            if ($_POST['name'])
                $query .= ", benchmarkId = $benchmarkId";
            
            $query .= " WHERE id = $_POST[id]";

            $db->Execute($query);
            # }}}

            # change the benchmark description
            $this->changeBenchmarkDescription($db, $benchmarkInstance['benchmarkid']);

            # process all the benchmark instance attributes
            $this->processAttributes($db);

            # complete the transaction
            $db->CompleteTrans();

            header("Location: benchmarkInstance_SHOW.php?id=$_POST[id]"); /* Redirect browser */
        }

        
        /* Changes common description of the benchmark
        */
        function changeBenchmarkDescription($db, $benchmarkInstanceId)
        {
            # change the common description {{{
            $query = "UPDATE benchmark SET description = '$_POST[benchmarkDescription]' " .
                     " WHERE id = $benchmarkInstanceId";

            $db->Execute($query);
            # }}}
        }


        /* Creates, changes or removes benchmark instance attributes
        */
        function processAttributes($db)
        {
            foreach ($_POST as $key => $value) {
                if (preg_match('/attribute_(\d+)/', $key, $results)) {
                    if (!$value) {
                        $query = "DELETE FROM benchmarkInstanceAttribute WHERE benchmarkInstanceId=$_POST[id] " .
                                 "AND attributeId = $results[1]";

                        $db->Execute($query);
                    }
                    else {
                        $query = "SELECT * FROM benchmarkInstanceAttribute WHERE benchmarkInstanceId = $_POST[id] " . 
                                 "AND attributeId = $results[1]";

                        $db->Execute($query);

                        $recordSet = $db->Execute($query);

                        $row = $recordSet->FetchRow();

                        # if there is a row for the specified benchmark instance attribute, change its value {{{
                        if ($row) {
                            $query = "UPDATE benchmarkInstanceAttribute " .
                                     "SET value = '$value' " .
                                     "WHERE benchmarkInstanceID = $_POST[id] AND attributeId = $results[1]";

                            $db->Execute($query);
                            
                        }
                        # }}}
                        # otherwise create the row {{{
                        else {
                            $query = "INSERT INTO benchmarkInstanceAttribute (benchmarkInstanceId, attributeId, value) ".
                                     "VALUES ($_POST[id], $results[1], $value)";

                            $db->Execute($query);
                        }
                        # }}}
                    }
                }
            }
        }


        /* Returns benchmark ID by name
        */
        function getBenchmarkId($db, $name)
        {
            $recordSet = $db->Execute("SELECT * FROM benchmark WHERE name='$name'");
            if (!$recordSet->RecordCount())
                return NULL;

            $row = $recordSet->FetchRow();

            return $row['id'];
        }


        /* Creates a benchmark table entry
        */
        function createBenchmark($db, $name, $description)
        {
            $benchmarkId = $db->GenId("benchmark_id_seq"); 
            
            $query = "INSERT INTO benchmark (id, name, description) VALUES ($benchmarkId, '$name', '$description')";

            $result = $db->Execute($query);

            return $benchmarkId;
        }
    }


    launchPage();

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