<?php
  /*

      upgrade.0.9.3.inc.php

      Upgrades database to v.0.9.3

      Changelog:
        2005-07-15 arne_sf: Buffers output in array instead of displaying it immediately
        2005-07-15 arne_sf: Initial version, including database schema changes by Stephan Kaufhold
        
      ----

      Copyright (C) 2005  Arne (arne_sf@users.sourceforge.net)

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation version 2.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  */

      
    // Initialize array (important!)
    $qry = array();
    // Add 'lang' column to table tbl_Link_Categories
    $output[] = "Adding 'lang' column to tbl_Link_Categories ....... ";
    $qry['pgsql'] = "ALTER TABLE tbl_Link_Categories ADD COLUMN lang char(5)";
    $qry['mysql'] = "ALTER TABLE tbl_Link_Categories ADD COLUMN lang char(5) DEFAULT 'en_UK' NOT NULL";
    $qry['mssql'] = "ALTER TABLE tbl_Link_Categories ADD COLUMN lang char(5)";
    if ($qry[$db->phptype] != "") 
    {
        $res = $db->query($qry[$db->phptype]);
        $output[] = (!PEAR::isError($res) ? "Success!" : $res->getMessage()) . "<BR />\n";
    } else $output[] = "ERROR: DBMS " . $db->phptype . " not yet supported by the upgrade routine!<BR />\n";


    // Set default value for new 'lang' column in tbl_Link_Categories
    $output[] = "Setting default value for 'lang' column in tbl_Link_Categories ....... ";
    // First update all existing records with default value
    $qry = "UPDATE tbl_Link_Categories SET lang='en_UK'";
    $res = $db->query($qry);
    if (!PEAR::isError($res))
    {
        // PostgreSQL and MSSQL need a couple more ALTER TABLE's
        if ($db->phptype == 'pgsql' || $db->phptype == 'mssql')
        {
            // Set default value on the column
            $qry = "ALTER TABLE tbl_Link_Categories ALTER COLUMN lang SET DEFAULT 'en_UK'";
            $res = $db->query($qry);
            if (!PEAR::isError($res))
            {
                // Set column to not allow NULL
                $qry = "ALTER TABLE tbl_Link_Categories ALTER COLUMN lang SET NOT NULL";
                $res = $db->query($qry);
                $output[] = (!PEAR::isError($res) ? "Success!" : $res->getMessage()) . "<BR />\n";
            } else $output[] = $res->getMessage();     // Query failed
        } else $output[] = "Success!<BR />\n";
    } else $output[] = $res->getMessage();     // Query failed


    // Re-initialize array (important!)
    $qry = array();
    // Add 'lang' column to table tbl_News
    $output[] = "Adding 'lang' column to tbl_News ....... ";
    $qry['pgsql'] = "ALTER TABLE tbl_News ADD COLUMN lang char(5)";
    $qry['mysql'] = "ALTER TABLE tbl_News ADD COLUMN lang char(5) DEFAULT 'en_UK' NOT NULL";
    $qry['mssql'] = "ALTER TABLE tbl_News ADD COLUMN lang char(5)";
    if ($qry[$db->phptype] != "") 
    {
        $res = $db->query($qry[$db->phptype]);
        $output[] = (!PEAR::isError($res) ? "Success!" : $res->getMessage()) . "<BR />\n";
    } else $output[] = "ERROR: DBMS " . $db->phptype . " not yet supported by the upgrade routine!<BR />\n";


    // Set default value for new 'lang' column in tbl_News
    $output[] = "Setting default value for 'lang' column in tbl_News ....... ";
    // First update all existing records with default value
    $qry = "UPDATE tbl_News SET lang='en_UK'";
    $res = $db->query($qry);
    if (!PEAR::isError($res))
    {
        // PostgreSQL and MSSQL need a couple more ALTER TABLE's
        if ($db->phptype == 'pgsql' || $db->phptype == 'mssql')
        {
            // Set default value on the column
            $qry = "ALTER TABLE tbl_News ALTER COLUMN lang SET DEFAULT 'en_UK'";
            $res = $db->query($qry);
            if (!PEAR::isError($res))
            {
                // Set column to not allow NULL
                $qry = "ALTER TABLE tbl_News ALTER COLUMN lang SET NOT NULL";
                $res = $db->query($qry);
                $output[] = (!PEAR::isError($res) ? "Success!" : $res->getMessage()) . "<BR />\n";
            } else $output[] = $res->getMessage();     // Query failed
        } else $output[] = "Success!<BR />\n";
    } else $output[] = $res->getMessage();     // Query failed

    
    // Re-initialize array (important!)
    $qry = array();
    // Add column 'domain_id' to tbl_News
    $output[] = "Adding 'domain_id' column to tbl_News....... ";
    $qry['pgsql'] = "ALTER TABLE tbl_News ADD COLUMN domain_id int4";
    $qry['mysql'] = "ALTER TABLE tbl_News ADD COLUMN domain_id int(4)";
    $qry['mssql'] = "ALTER TABLE tbl_News ADD COLUMN domain_id int4";
    if ($qry[$db->phptype] != "") 
    {
        $res = $db->query($qry[$db->phptype]);
        $output[] = (!PEAR::isError($res) ? "Success!" : $res->getMessage()) . "<BR />\n";
    } else $output[] = "ERROR: DBMS " . $db->phptype . " not yet supported by the upgrade routine!<BR />\n";


    // For PostgreSQL and MSSQL we need to set default value to NULL (not needed for MySQL)
    if ($db->phptype == 'pgsql' || $db->phptype == 'mssql')
    {
        // Set default value for new 'domain_id' column in tbl_News
        $output[] = "Setting default value for 'domain_id' column in tbl_News ....... ";
        $qry = "ALTER TABLE tbl_News ALTER COLUMN domain_id SET DEFAULT NULL";
        $res = $db->query($qry);
        $output[] = (!PEAR::isError($res) ? "Success!" : $res->getMessage()) . "<BR />\n";
    }


    // Fill tbl_News with data after adding the new column
    $output[] = "Re-fill tbl_News with data after adding column....... ";
    $qry = "SELECT id, notice FROM tbl_Domains";        
    $data =& $db->getAll($qry);
    foreach ($data as $row)
    {
        $qry = "INSERT INTO tbl_News (domain_id, content, frontpagepara) VALUES (" . $row['id'] . ", '" . $row['notice'] . "', '3')";
        $res = $db->query($qry);
        if (PEAR::isError($res)) break;
    };
    $output[] = (!PEAR::isError($res) ? "Success!" : $res->getMessage() . $res->getDebugInfo()) . "<BR />\n";

?>
