<?php 

    /* Page that makes changes to a format data
    */

    require_once('simplePage.inc.php');

    class PageClass extends EditDBSimplePage
    {
        function createPage()
        {
            if ($_POST["OK"])
            {
                $db = connect();

                # update the description {{{
                $query = "UPDATE format SET description='$_POST[description]' WHERE id = $_POST[formatId]";
                $db->Execute($query);
                # }}}

                $query = "SELECT * FROM formatAttribute WHERE formatId=$_POST[formatId]";
                $result = $db->Execute($query);

                $supportedAttributes = array();
                while ($row = $result->FetchRow()) {
                    $supportedAttributes[$row['attributeid']] = 1;
                }

                $query = "SELECT * FROM attribute";

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

                if ($_POST["attributes"])
                    $attributes = $_POST["attributes"];
                else
                    $attributes = array();

                    while ($row = $result->FetchRow()) {
                    if (in_array($row['id'], $attributes))
                    {
                        if ($supportedAttributes[$row['id']] != 1)
                        {
                            $db->Execute("INSERT INTO formatAttribute (formatId, attributeId) VALUES($_POST[formatId], $row[id])");
                        }
                    }
                    else
                    {
                        if ($supportedAttributes[$row['id']] == 1)
                        {
                            $db->Execute("DELETE FROM formatAttribute WHERE formatId = $_POST[formatId] AND attributeId = $row[id]");
                        }
                    }
                }

            }

            header("Location: formats_LISTING.php"); /* Redirect browser */ 
        }
    }


    launchPage();

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