Location: PHPKode > projects > Cowtacular > cowtacular-0.51/scripts/upgradedatabase.php
<?php
// ******************************************************************
//  Cowtacular
//  (c) 2010 - Cowtacular LLC
//
//  http://www.cowtacular.com
//
// ******************************************************************

  // Change the working directory to the proper location
  $scriptspath = $_SERVER["PWD"] ."/". $_SERVER["PHP_SELF"];
  if (! file_exists($scriptspath))
    die("\n\nStopping!  Could not find script at ". $scriptspath ."\n\n");
  $scriptspath = str_replace("/scripts/". basename($scriptspath), "", $scriptspath);
  chdir($scriptspath);

  echo "\n\n";
  echo "Running upgrade script\n";
  echo "============================================\n";
  $db_ignoreversion = 1;

  // Load the configuration
  include_once('cow-config.php');
              
  // Include the global library
  include_once('global/lib.php');

  // Get the current DB version
  $currentversion = db_getconfigvalue("DBVERSION");
  echo "Current DB version: ". $currentversion ."\n";

  // What if we can't get the database version?
  if ($currentversion == "") {
    echo "Error - could not determine database version\n\n";
    die();
  }

  // 0.40 to 0.41
  if ($currentversion == "0.40") {
    upgrade_040_to_041();
    db_readconfig();
    $currentversion = db_getconfigvalue("DBVERSION");
  }
  
  // 0.41 to 0.42
  if ($currentversion == "0.41") {
    upgrade_041_to_042();
    db_readconfig();
    $currentversion = db_getconfigvalue("DBVERSION");
  }
  
  // 0.42 to 0.43
  if ($currentversion == "0.42") {
    db_readconfig();
    upgrade_042_to_043();
    $currentversion = db_getconfigvalue("DBVERSION");
  }   
  
  // 0.43 to 0.44
  if ($currentversion == "0.43") {
    db_readconfig();
    upgrade_043_to_044();
    $currentversion = db_getconfigvalue("DBVERSION");
  }
  
  // 0.44 to 0.45
  if ($currentversion == "0.44") {
    db_readconfig();
    upgrade_044_to_045();
    $currentversion = db_getconfigvalue("DBVERSION");
  }

  // 0.45 to 0.46
  if ($currentversion == "0.45") {
    db_readconfig();
    upgrade_045_to_046();
    $currentversion = db_getconfigvalue("DBVERSION");
  }
  
  // 0.46 to 0.47
  if ($currentversion == "0.46") {
    db_readconfig();
    upgrade_046_to_047();
    $currentversion = db_getconfigvalue("DBVERSION");
  }
  
  // 0.47 to 0.48
  if ($currentversion == "0.47") {
    db_readconfig();
    upgrade_047_to_048();
    $currentversion = db_getconfigvalue("DBVERSION");
  }
                    
  // 0.48 to 0.49
  if ($currentversion == "0.48") {
    db_readconfig();
    upgrade_048_to_049();
    $currentversion = db_getconfigvalue("DBVERSION");
  }
  
  // 0.49 to 0.50
  if ($currentversion == "0.49") {
    db_readconfig();
    upgrade_049_to_050();
    $currentversion = db_getconfigvalue("DBVERSION");
  }
  
  // 0.50 to 0.51
  if ($currentversion == "0.50") {
    db_readconfig();
    upgrade_050_to_051();
    $currentversion = db_getconfigvalue("DBVERSION");
  }
      
  // Close the databases
  echo "Done\n\n";
  db_writeobjectcache();
  db_close();
          

  // ******************************************************************
  // upgrade 0.40 to 0.41
  // ******************************************************************
  function upgrade_040_to_041() {
    echo "Upgrading database from 0.40 to 0.41\n";
    
    echo "  Adjusting the TICKET object definitions\n";
    //  TICKET.FIRSTNAME, LASTNAME, LOCATION, PHONE  minimum length 0
    db_setconfigfieldvalue("FIRSTNAME", "TICKET", "MINVALUE", "0");
    db_setconfigfieldvalue("LASTNAME", "TICKET", "MINVALUE", "0");
    db_setconfigfieldvalue("LOCATION", "TICKET", "MINVALUE", "0");
    db_setconfigfieldvalue("PHONE", "TICKET", "MINVALUE", "0");
  
    // Set the TICKET.PRIVATE and TICKETNOTE.PRIVATE default values to 0
    db_setconfigfieldvalue("PRIVATE", "TICKET", "CURRENTVALUE", "0");
    db_setconfigfieldvalue("PRIVATE", "TICKET", "DEFAULTVALUE", "0");
    db_setconfigfieldvalue("PRIVATE", "TICKETNOTE", "CURRENTVALUE", "0");
    db_setconfigfieldvalue("PRIVATE", "TICKETNOTE", "DEFAULTVALUE", "0");
  
    echo "  Adjusting the REP object definitions\n";
    // Set the REP.DASHBOARD refresh min/max to 0 thru 60  
    db_setconfigfieldvalue("REFRESHTIMEOUT", "REP", "MINVALUE", "0");
    db_setconfigfieldvalue("REFRESHTIMEOUT", "REP", "MAXVALUE", "60");
    db_setconfigfieldvalue("FIRSTNAME", "REP", "MINVALUE", "0");
    db_setconfigfieldvalue("LASTNAME", "REP", "MINVALUE", "0");

    echo "  Adding the email checking configuraiton options\n";
    // Add the configuration options for email checking and sending
    $tempid=db_createconfigvalue(0, "INCOMINGMAILSERVER", "GLOBAL", "LINE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 64);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Incoming mail server");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "INCOMINGMAILPROTOCOL", "GLOBAL", "LINE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 64);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "pop3");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "pop3");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Incoming mail protocol");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "INCOMINGMAILSSL", "GLOBAL", "BOOLEAN", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 64);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Incoming mail using ssl");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "INCOMINGMAILPORT", "GLOBAL", "INTEGER", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 64);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "110");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "110");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Incoming mail port");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "INCOMINGMAILUSERNAME", "GLOBAL", "LINE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 64);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Incoming mail username");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "INCOMINGMAILPASSWORD", "GLOBAL", "LINE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 64);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Incoming mail password");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "INCOMINGMAILAUTOGENERATETICKET", "GLOBAL", "BOOLEAN", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 1);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Auto-create new tickets from emails");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "OUTGOINGMAILSERVER", "GLOBAL", "LINE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 64);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Outgoing mail server");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "OUTGOINGMAILPROTOCOL", "GLOBAL", "LINE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 64);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "smtp");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "smtp");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Outgoing mail protocol");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "OUTGOINGMAILSSL", "GLOBAL", "BOOLEAN", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 64);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Outgoing mail using ssl");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "OUTGOINGMAILPORT", "GLOBAL", "INTEGER", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 64);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "25");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "25");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Ougoing mail port");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "OUTGOINGMAILUSERNAME", "GLOBAL", "LINE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 64);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Outgoing mail username");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "OUTGOINGMAILPASSWORD", "GLOBAL", "LINE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 64);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Outgoing mail password");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }

    // Timezone
    echo "  Creating Timezone settings\n";
    $tempid=db_createconfigvalue(0, "TIMEZONE", "GLOBAL", "LINE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 30);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "UTC");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "UTC");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Timezone");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }


    // Create email queue
    echo "  Creating the MAILQUEUE Object\n";
    $tempid=db_createconfigvalue(0, "DATECREATED", "MAILQUEUE", "DATE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MAXVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "DEFAULTVALUE", $now);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "CURRENTVALUE", $now);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TITLE", "Date Created");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "LASTUPDATE", "MAILQUEUE", "DATE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MAXVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "DEFAULTVALUE", $now);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "CURRENTVALUE", $now);   
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TITLE", "Last Update");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "LASTUPDATEBYID", "MAILQUEUE", "REPID", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MAXVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "DEFAULTVALUE", 0);      
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "CURRENTVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TITLE", "Last update by");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "TOEMAIL", "MAILQUEUE", "LINE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MAXVALUE", 80);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "DEFAULTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "CURRENTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TITLE", "To"); 
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "FROMEMAIL", "MAILQUEUE", "LINE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MINVALUE", 0); 
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MAXVALUE", 80);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "DEFAULTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "CURRENTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TITLE", "From");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "SUBJECT", "MAILQUEUE", "LINE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "ARCHIVED", 0); 
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MINVALUE", 0); 
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MAXVALUE", 80);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "DEFAULTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "CURRENTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TITLE", "Subject");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "BODY", "MAILQUEUE", "BLOCK", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "ARCHIVED", 0); 
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MINVALUE", 0); 
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MAXVALUE", 8000);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "DEFAULTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "CURRENTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TITLE", "Body");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TOOLTIPS", "");
    }
                                             
    // Set the DB Version
    echo "  Setting the DB Version to 0.41\n";
    db_setconfigvalue("DBVERSION", "0.41");
    db_writeobjectcache();
  }
  
  
  // ******************************************************************
  // Upgrade 0.41 to 0.42
  // ******************************************************************
  function upgrade_041_to_042() {
    
    echo "Upgrading database from 0.41 to 0.42\n";

    // Adding fields to the MAILQUEUE
    echo "  Adding fields to the MAILQUEUE object\n";
    $tempid=db_createconfigvalue(0, "SENT", "MAILQUEUE", "BOOLEAN", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MAXVALUE", 1);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "DEFAULTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "CURRENTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TITLE", "Email has been sent");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TOOLTIPS", "");
    }
    
    $tempid=db_createconfigvalue(0, "DATESENT", "MAILQUEUE", "DATE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "MAXVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "DEFAULTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "CURRENTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TITLE", "Date Email was sent");
      db_setconfigfieldvalue((int)$tempid, "MAILQUEUE", "TOOLTIPS", "");
    }

    
    // Adding email sent retention limits
    echo "  Adding email sent retention limit field\n";
    $tempid=db_createconfigvalue(0, "EMAILSENTRETENTION", "GLOBAL", "INTEGER", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 1);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 365);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "7");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "7");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Days to retain sent emails");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }


    // Adjusting TICKETUSER
    echo "  Adjusting the TICKET object definitions\n";
    //  TICKETUSER.FIRSTNAME, LASTNAME, LOCATION, PHONE  minimum length 0
    db_setconfigfieldvalue("FIRSTNAME", "TICKETUSER", "MINVALUE", "0");
    db_setconfigfieldvalue("LASTNAME", "TICKETUSER", "MINVALUE", "0");
    db_setconfigfieldvalue("LOCATION", "TICKETUSER", "MINVALUE", "0");
    db_setconfigfieldvalue("PHONE", "TICKETUSER", "MINVALUE", "0");
    db_setconfigfieldvalue("PHONE", "TICKETUSER", "MAXVALUE", "20");
        
    // Set the DB Version
    echo "  Setting the DB Version to 0.42\n";
    db_setconfigvalue("DBVERSION", "0.42");
    db_writeobjectcache();
  
  }



  // ******************************************************************
  // Upgrade 0.42 to 0.43
  // ******************************************************************
  function upgrade_042_to_043() {
        
    echo "Upgrading database from 0.42 to 0.43\n";
    
    // TICKETTEMPLATE
    echo "  Adjusting the TICKETTEMPLATE object definitions\n";        
    $tempid=db_createconfigvalue(0, "HEADER", "TICKETTEMPLATE", "BLOCK", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MAXVALUE", 8000);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "DEFAULTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "CURRENTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TITLE", "Template Header");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TOOLTIPS", "");
    }
    db_setconfigfieldvalue("TITLE", "TICKETTEMPLATE", "MAXVALUE", 40);

	// Create a default ticket template if none exists
	$defaulttickettemplateid = db_getconfigvalue("DEFAULTTICKETTEMPLATEID");
	if (! db_readobject($defaulttickettemplateid, "TICKETTEMPLATE")) {
  		echo "  Creating the default ticket template \n";
  		$tickettemplateid = db_createobject("TICKETTEMPLATE");
  		if ($tickettemplateid > 0) {
    			db_setobjectvalue($tickettemplateid, "TICKETTEMPLATE", "TITLE", "Default ticket template");
    
    			$descriptionfieldid = db_createobject("TICKETTEMPLATEFIELD");
    			if ($descriptionfieldid > 0) {
    				db_setobjectvalue($descriptionfieldid, "TICKETTEMPLATEFIELD", "TICKETTEMPLATEID", $tickettemplateid);
  				db_setobjectvalue($descriptionfieldid, "TICKETTEMPLATEFIELD", "TITLE", "Description");
  				db_setobjectvalue($descriptionfieldid, "TICKETTEMPLATEFIELD", "REQUIRED", 1);
  				db_setobjectvalue($descriptionfieldid, "TICKETTEMPLATEFIELD", "DATATYPE", "BLOCK");
  				db_setobjectvalue($descriptionfieldid, "TICKETTEMPLATEFIELD", "MAX", 8000);
  			}
  			db_setconfigvalue("DEFAULTTICKETTEMPLATEID", $tickettemplateid);
  		}
	}

    // Set the DB Version
    echo "  Setting the DB Version to 0.43\n";
    db_setconfigvalue("DBVERSION", "0.43");
    db_writeobjectcache();
     
  }

  // ******************************************************************
  // Upgrade 0.43 to 0.44
  // ******************************************************************
  function upgrade_043_to_044() {
  
    echo "Upgrading database from 0.43 to 0.44\n";
    
    echo "  Adjusting the RegKey minimum and maximums\n";  
    db_setconfigfieldvalue("REGKEY", "SOFTWARE", "MINVALUE", 0);
    db_setconfigfieldvalue("REGKEY", "SOFTWARE", "MAXVALUE", 120);

    echo "  Adjusting the Software title minumm and maximums\n";
    db_setconfigfieldvalue("NAME", "SOFTWARE", "MINVALUE", 3);
    db_setconfigfieldvalue("NAME", "SOFTWARE", "MAXVALUE", 60);
          
    // Set the DB Version
    echo "  Setting the DB Version to 0.44\n";
    db_setconfigvalue("DBVERSION", "0.44");
    db_writeobjectcache();
                
  }
  
  // ******************************************************************
  // Upgrade 0.44 to 0.45
  // ******************************************************************
  function upgrade_044_to_045() {
    echo "Upgrading database from 0.44 to 0.45\n";
    
    // Set the DB Version
    echo "  Setting the DB Version to 0.45\n";
    db_setconfigvalue("DBVERSION", "0.45");
    db_writeobjectcache();
    
  }

  // ******************************************************************
  // Upgrade 0.45 to 0.46
  // ******************************************************************
  function upgrade_045_to_046() {
    echo "Upgrading database from 0.45 to 0.46\n";

    
    // Add category selection to ticket templates
    echo "  Adding category selection to ticket templates\n";
    $tempid=db_createconfigvalue(0, "ALLOWALLCATEGORIES", "TICKETTEMPLATE", "BOOLEAN", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MAXVALUE", 1);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "DEFAULTVALUE", "1");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "CURRENTVALUE", "1");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TITLE", "Allow all categories");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "ALLOWEDCATEGORYIDS", "TICKETTEMPLATE", "MULTISELECTID", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "PARRENTID", 60);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MAXVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "DEFAULTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "CURRENTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TITLE", "Allowed Categories");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TOOLTIPS", "");
    }

    // Add a default category option to the template
    echo "  Adding default category option to the ticket templates\n";
    $tempid=db_createconfigvalue(0, "DEFAULTCATEGORYID", "TICKETTEMPLATE", "SELECTID", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "PARRENTID", 60);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MAXVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "DEFAULTVALUE", "61");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "CURRENTVALUE", "61");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TITLE", "Default Category");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TOOLTIPS", "");
    }
            
    // Add a block field for ticket template instructions
    echo "  Adding instructions field to ticket templates\n";
    $tempid=db_createconfigvalue(0, "INSTRUCTIONS", "TICKETTEMPLATE", "BLOCK", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MAXVALUE", 4000);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "DEFAULTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "CURRENTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TITLE", "Instructions");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TOOLTIPS", "");
    }
    
    // Add DEFAULTGROUPID
    echo "  Adding default group to the ticket templates\n";
    $tempid=db_createconfigvalue(0, "DEFAULTGROUPID", "TICKETTEMPLATE", "SELECTID", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "PARRENTID", 20);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MAXVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "DEFAULTVALUE", "21");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "CURRENTVALUE", "21");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TITLE", "Default Ticket Group");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TOOLTIPS", "");
    }

        
    // Add DEFAULTPRIORITYID
    echo "  Adding default priority to the ticket templates\n";
    $tempid=db_createconfigvalue(0, "DEFAULTPRIORITYID", "TICKETTEMPLATE", "SELECTID", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "PARRENTID", 50);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MAXVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "DEFAULTVALUE", "51");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "CURRENTVALUE", "51");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TITLE", "Default Ticket Priority");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TOOLTIPS", "");
    }
    
    
    // Add DEFAULTSTATUSID
    echo "  Adding default status to the ticket templates\n";
    $tempid=db_createconfigvalue(0, "DEFAULTSTATUSID", "TICKETTEMPLATE", "SELECTID", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "PARRENTID", 70);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MAXVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "DEFAULTVALUE", "71");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "CURRENTVALUE", "71");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TITLE", "Default Ticket Status");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TOOLTIPS", "");
    }
    

    // Email and paging templates
    echo "  Adding email and paging templates to the ticket templates\n";
    $tempid=db_createconfigvalue(0, "EMAILSUBJECT", "TICKETTEMPLATE", "LINE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MINVALUE", 3);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MAXVALUE", 60);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "DEFAULTVALUE", "Cowtacular Ticket");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "CURRENTVALUE", "Cowtacular Ticket");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TITLE", "Email subject line");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "EMAILNEWTICKET", "TICKETTEMPLATE", "BLOCK", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MINVALUE", 3);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MAXVALUE", 4000);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "DEFAULTVALUE", "Hi %firstname%,\n\nThanks for submitting a ticket.  It has been assigned #%ticketid%.  We will try to respond in a timely matter.  If you wish to update this ticket, please reply to this email or click on the link below.\n\n%ticketlink%\n\nTicket ID: %ticketid%\nStatus: %status%\nPriority: %priority%\nAssigned to: %assignedto%\nCategory: %category%\nSubject: %subject%\n\nDescription:\n------------------------------------\n%description%\n------------------------------------\n\n- Support");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "CURRENTVALUE", "Hi %firstname%,\n\nThanks for submitting a ticket.  It has been assigned #%ticketid%.  We will try to respond in a timely matter.  If you wish to update this ticket, please reply to this email or click on the link below.\n\n%ticketlink%\n\nTicket ID: %ticketid%\nStatus: %status%\nPriority: %priority%\nAssigned to: %assignedto%\nCategory: %category%\nSubject: %subject%\n\nDescription:\n------------------------------------\n%description%\n------------------------------------\n\n- Support");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TITLE", "New Ticket Email to Submitter");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "EMAILUPDATETICKET", "TICKETTEMPLATE", "BLOCK", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MINVALUE", 3);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MAXVALUE", 4000);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "DEFAULTVALUE", "Hello %firstname%, \n\nYour ticket #%ticketid% has been updated with the following information:\n\n===============================================\n%recentnote%\n===============================================\n\nCurrent Ticket Status and Original Ticket ID: %ticketid%\nStatus: %status%\nPriority: %priority%\nAssigned to: %assignedto%\nCategory: %category%\nSubject: %subject%\n\nDescription:\n------------------------------------\n%description%\n------------------------------------\n\nYou can view, close or re-open this ticket using the following link:\n\n%ticketlink%\n\n- Support");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "CURRENTVALUE", "Hello %firstname%, \n\nYour ticket #%ticketid% has been updated with the following information:\n\n===============================================\n%recentnote%\n===============================================\n\nCurrent Ticket Status and Original Ticket ID: %ticketid%\nStatus: %status%\nPriority: %priority%\nAssigned to: %assignedto%\nCategory: %category%\nSubject: %subject%\n\nDescription:\n------------------------------------\n%description%\n------------------------------------\n\nYou can view, close or re-open this ticket using the following link:\n\n%ticketlink%\n\n- Support");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TITLE", "Updated Ticket Email to Submitter");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "PAGENEWTICKET", "TICKETTEMPLATE", "BLOCK", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MINVALUE", 3);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MAXVALUE", 4000);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "DEFAULTVALUE", "NEW TICKET#%ticketid% - %priority% - %firstname% %lastname% - %phone% - %description%");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "CURRENTVALUE", "NEW TICKET#%ticketid% - %priority% - %firstname% %lastname% - %phone% - %description%");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TITLE", "Rep Page for New Ticket");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "PAGEASSIGNEDTICKET", "TICKETTEMPLATE", "BLOCK", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MINVALUE", 3);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MAXVALUE", 4000);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "DEFAULTVALUE", "ASSIGNED TICKET#%ticketid% - %priority% - %firstname% %lastname% - %phone% - %description%");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "CURRENTVALUE", "ASSIGNED TICKET#%ticketid% - %priority% - %firstname% %lastname% - %phone% - %description%");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TITLE", "Rep Page for Assigned Ticket");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TOOLTIPS", "");
    }

    
    // Removing some ticket global settings
    echo "  Removing some ticket global settings\n";
    $tempdefaultcategoryid = (int)db_getconfigvalue("DEFAULTTICKETCATEGORYID");
    $tempdefaultgroupid = (int)db_getconfigvalue("DEFAULTTICKETGROUPID");
    $tempdefaultpriorityid = (int)db_getconfigvalue("DEFAULTTICKETPRIORITYID");
    $tempdefaultstatusid = (int)db_getconfigvalue("DEFAULTTICKETSTATUSID");
    $tempemailnew = db_getconfigvalue("EMAILNEWTICKET");
    $tempemailupdate = db_getconfigvalue("EMAILUPDATETICKET");
    $temppagenew = db_getconfigvalue("PAGENEWTICKET");
    $temppageassigned = db_getconfigvalue("PAGEASSIGNEDTICKET");
    $tempemailsubject = db_getconfigvalue("EMAILSUBJECT");
    db_deleteconfigvalue("ENABLETICKETTEMPLATES", "GLOBAL");
    db_deleteconfigvalue("ALLOWUSERSSETTICKETCATEGORY", "GLOBAL");
    db_deleteconfigvalue("DEFAULTTICKETGROUPID", "GLOBAL");
    db_deleteconfigvalue("DEFAULTTICKETCATEGORYID", "GLOBAL");
    db_deleteconfigvalue("DEFAULTTICKETPRIORITYID", "GLOBAL");
    db_deleteconfigvalue("DEFAULTTICKETSTATUSID", "GLOBAL");
    db_deleteconfigvalue("DEFAULTHELPPAGETITLE", "GLOBAL");
    db_deleteconfigvalue("EMAILNEWTICKET", "GLOBAL");
    db_deleteconfigvalue("EMAILUPDATETICKET", "GLOBAL");
    db_deleteconfigvalue("PAGENEWTICKET", "GLOBAL");
    db_deleteconfigvalue("PAGEASSIGNEDTICKET", "GLOBAL");
    db_deleteconfigvalue("EMAILSUBJECT", "GLOBAL");
    
    // Updating existing templates with new values
    echo "  Updating existing templates with new values\n";
    db_newobjectsearch("TICKETTEMPLATE", "", "");
    db_addobjectsearchcriteria("ID", "gt", "-1");
    $templateids = db_runobjectsearch();
    foreach ($templateids as $templateid) {
      db_setobjectvalue($templateid, "TICKETTEMPLATE", "ALLOWALLCATEGORIES", "1");
      db_setobjectvalue($templateid, "TICKETTEMPLATE", "INSTRUCTIONS", "");
      db_setobjectvalue($templateid, "TICKETTEMPLATE", "DEFAULTCATEGORYID", $tempdefaultcategoryid);
      db_setobjectvalue($templateid, "TICKETTEMPLATE", "DEFAULTGROUPID", $tempdefaultgroupid);
      db_setobjectvalue($templateid, "TICKETTEMPLATE", "DEFAULTPRIORITYID", $tempdefaultpriorityid);
      db_setobjectvalue($templateid, "TICKETTEMPLATE", "DEFAULTSTATUSID", $tempdefaultstatusid);      
      db_setobjectvalue($templateid, "TICKETTEMPLATE", "EMAILNEWTICKET", $tempemailnew);
      db_setobjectvalue($templateid, "TICKETTEMPLATE", "EMAILUPDATETICKET", $tempemailupdate);
      db_setobjectvalue($templateid, "TICKETTEMPLATE", "PAGENEWTICKET", $temppagenew);
      db_setobjectvalue($templateid, "TICKETTEMPLATE", "PAGEASSIGNEDTICKET", $temppageassigned);
      db_setobjectvalue($templateid, "TICKETTEMPLATE", "EMAILSUBJECT", $tempemailsubject);
    }


    // Create a ticket template field for each ticket
    echo "  Adding ticket template id field to each ticket\n";
    $tempid=db_createconfigvalue(0, "TICKETTEMPLATEID", "TICKET", "TICKETTEMPLATEID", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "TICKET", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKET", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKET", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKET", "MAXVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKET", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKET", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKET", "DEFAULTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "TICKET", "CURRENTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "TICKET", "TITLE", "Template used");
      db_setconfigfieldvalue((int)$tempid, "TICKET", "TOOLTIPS", "");
    }
    
    
    // Set the DB Version
    echo "  Setting the DB Version to 0.46\n";
    db_setconfigvalue("DBVERSION", "0.46");
    db_writeobjectcache();

  }


  // ******************************************************************
  // Upgrade 0.46 to 0.47
  // ******************************************************************
  function upgrade_046_to_047() {
    echo "Upgrading database from 0.46 to 0.47\n";
    
    // Unique ID
    echo "  Adding unique ID field\n";
    $tempid=db_createconfigvalue(0, "UNIQUEID", "GLOBAL", "LINE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 40);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Unique ID");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    
    // Nightly maintenance window
    echo "  Adding daily maintenace window\n";
    $tempid=db_createconfigvalue(0, "DAILYMAINTENANCEWINDOW", "GLOBAL", "INTEGER", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 24);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "1");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "1");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Daily maintenance window");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    
    // Last nightly maintenance run
    echo "  Adding last daily maintenance run\n";
    $tempid=db_createconfigvalue(0, "LASTDAILYMAINTENANCERUN", "GLOBAL", "DATE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", $now);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", $now);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Last daily maintenance run");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    
    // Enable version checking
    echo "  Adding version checking switch\n";
    $tempid=db_createconfigvalue(0, "ENABLEVERSIONCHECKING", "GLOBAL", "BOOLEAN", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 1);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "1");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "1");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Enable version checking");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    
    // Last version received from the version checking
    echo "  Adding last version checked field\n";
    $tempid=db_createconfigvalue(0, "LASTVERSIONCHECKED", "GLOBAL", "LINE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 12);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Last version checked");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    
    // Set the DB Version
    echo "  Setting the DB Version to 0.47\n";
    db_setconfigvalue("DBVERSION", "0.47");
    db_writeobjectcache();
                  
  }


  // ******************************************************************
  // Upgrade 0.47 to 0.48
  // ******************************************************************
  function upgrade_047_to_048() {
    echo "Upgrading database from 0.47 to 0.48\n";

    // Uppercase the SERIALNUM field in devices
    echo "  Forcing all serial numbers to upper case\n";
    db_query("UPDATE DEVICE SET SERIALNUM=UPPER(SERIALNUM)");
    db_query("UPDATE ARCHIVED.DEVICE SET SERIALNUM=UPPER(SERIALNUM)");
    
    // Lowercase all EMAIL fields
    echo "  Forcing all email fields to lower case\n";
    db_query("UPDATE TICKET SET EMAIL=LOWER(EMAIL)");
    db_query("UPDATE REP SET EMAIL=LOWER(EMAIL)");
    db_query("UPDATE TICKETUSER SET EMAIL=LOWER(EMAIL)");
    db_query("UPDATE ARCHIVED.TICKET SET EMAIL=LOWER(EMAIL)");
    db_query("UPDATE ARCHIVED.REP SET EMAIL=LOWER(EMAIL)");
    db_query("UPDATE ARCHIVED.TICKETUSER SET EMAIL=LOWER(EMAIL)");
            
    // Set the DB Version
    echo "  Setting the DB Version to 0.48\n";
    db_setconfigvalue("DBVERSION", "0.48");
    db_writeobjectcache();

  }

  // ******************************************************************
  // Upgrade from 0.48 to 0.49
  // ******************************************************************
  function upgrade_048_to_049() {
    echo "Upgrading database from 0.48 to 0.49\n";
    
    // Ticket template - Rep page for ticket update
    echo "  Adding ticket template field for Rep page template\n";
    $tempid=db_createconfigvalue(0, "PAGEUPDATETICKET", "TICKETTEMPLATE", "BLOCK", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "MAXVALUE", 8000);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "DEFAULTVALUE", "UPDATED TICKET#%ticketid% - %priority% - %firstname% %lastname% - %phone% - %recentnote%");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "CURRENTVALUE", "UPDATED TICKET#%ticketid% - %priority% - %firstname% %lastname% - %phone% - %recentnote%");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TITLE", "Rep page for updated ticket");
      db_setconfigfieldvalue((int)$tempid, "TICKETTEMPLATE", "TOOLTIPS", "");
    }
    db_newobjectsearch("TICKETTEMPLATE", "", "");
    db_addobjectsearchcriteria("ID", "gt", "0");
    $tickettemplateids = db_runobjectsearch();
    foreach ($tickettemplateids as $tickettemplateid)
      db_setobjectvalue($tickettemplateid, "TICKETTEMPLATE", "PAGEUPDATETICKET", "UPDATED TICKET#%ticketid% - %priority% - %firstname% %lastname% - %phone% - %recentnote%");
    db_clearobjectcache();
    
        
    // Last time backgroundprocess.php was run via command line
    echo "  Adding a field to check when the last time backgroundprocess.php was run from cron/command line\n";
    $tempid=db_createconfigvalue(0, "LASTBACKGROUNDPROCESSRUN", "GLOBAL", "DATE", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "MAXVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "DEFAULTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "CURRENTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TITLE", "Last time the background process was run");
      db_setconfigfieldvalue((int)$tempid, "GLOBAL", "TOOLTIPS", "");
    }
    
    
    // Rep hours till stale for My Ticks
    echo "  Adding ability for reps to set a staleness value for My Tickets\n";
    $tempid=db_createconfigvalue(0, "MYTICKETSHOURSTILLSTALE", "REP", "INTEGER", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "REP", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "MAXVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "REQUIRED", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "DEFAULTVALUE", "72");
      db_setconfigfieldvalue((int)$tempid, "REP", "CURRENTVALUE", "72");
      db_setconfigfieldvalue((int)$tempid, "REP", "TITLE", "My Tickets - hours till stale");
      db_setconfigfieldvalue((int)$tempid, "REP", "TOOLTIPS", "");
    }
    db_newobjectsearch("REP", "", "");
    db_addobjectsearchcriteria("ID", "gt", "0");
    $repids = db_runobjectsearch();
    foreach ($repids as $repid) {
      db_setobjectvalue($repid, "REP", "MYTICKETSHOURSTILLSTALE", "72");
    }
    db_clearobjectcache();
    

    // Rep email for ticket updates
    echo "  Adding new rep ticket notification preferences\n";
    $tempid=db_createconfigvalue(0, "EMAILWHENUPDATED", "REP", "BOOLEAN", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "REP", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "MAXVALUE", 1);
      db_setconfigfieldvalue((int)$tempid, "REP", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "REP", "DEFAULTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "REP", "CURRENTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "REP", "TITLE", "Email me when my tickets are updated");
      db_setconfigfieldvalue((int)$tempid, "REP", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "PAGEWHENUPDATED", "REP", "BOOLEAN", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "REP", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "MAXVALUE", 1); 
      db_setconfigfieldvalue((int)$tempid, "REP", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "REP", "DEFAULTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "REP", "CURRENTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "REP", "TITLE", "Page me when my tickets are updated");
      db_setconfigfieldvalue((int)$tempid, "REP", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "EMAILWHENASSIGNEDNONBUSHRS", "REP", "BOOLEAN", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "REP", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "MAXVALUE", 1);
      db_setconfigfieldvalue((int)$tempid, "REP", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "REP", "DEFAULTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "REP", "CURRENTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "REP", "TITLE", "Email me when assigned a ticket after business hours");
      db_setconfigfieldvalue((int)$tempid, "REP", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "PAGEWHENASSIGNEDNONBUSHRS", "REP", "BOOLEAN", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "REP", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "MAXVALUE", 1);
      db_setconfigfieldvalue((int)$tempid, "REP", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "REP", "DEFAULTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "REP", "CURRENTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "REP", "TITLE", "Page me when assigned a ticket after business hours");
      db_setconfigfieldvalue((int)$tempid, "REP", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "EMAILWHENUPDATEDNONBUSHRS", "REP", "BOOLEAN", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "REP", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "MAXVALUE", 1);
      db_setconfigfieldvalue((int)$tempid, "REP", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "REP", "DEFAULTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "REP", "CURRENTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "REP", "TITLE", "Page me when my tickets are updated after business hours");
      db_setconfigfieldvalue((int)$tempid, "REP", "TOOLTIPS", "");
    }
    $tempid=db_createconfigvalue(0, "PAGEWHENUPDATEDNONBUSHRS", "REP", "BOOLEAN", 1);
    if ($tempid > 0) {
      db_setconfigfieldvalue((int)$tempid, "REP", "PARRENTID", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "ARCHIVED", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "MINVALUE", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "MAXVALUE", 1);
      db_setconfigfieldvalue((int)$tempid, "REP", "POSITION", 0);
      db_setconfigfieldvalue((int)$tempid, "REP", "REQUIRED", 1);
      db_setconfigfieldvalue((int)$tempid, "REP", "DEFAULTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "REP", "CURRENTVALUE", "0");
      db_setconfigfieldvalue((int)$tempid, "REP", "TITLE", "Page me when my tickets are updated after business hours");
      db_setconfigfieldvalue((int)$tempid, "REP", "TOOLTIPS", "");
    }
    db_newobjectsearch("REP", "", "");
    db_addobjectsearchcriteria("EMAILWHENASSIGNED", "eq", 1);
    $repids = db_runobjectsearch();
    foreach ($repids as $repid)
      db_setobjectvalue($repid, "REP", "EMAILWHENASSIGNEDNONBUSHRS", 1);
    db_newobjectsearch("REP", "", "");
    db_addobjectsearchcriteria("PAGEWHENASSIGNED", "eq", 1);
    $repids = db_runobjectsearch();
    foreach ($repids as $repid)
      db_setobjectvalue($repid, "REP", "PAGEWHENASSIGNEDNONBUSHRS", 1);
    db_clearobjectcache();
    
    // Increase the maximum SERIALNUM to 50 characters
    echo "  Increasing the SERIALNUM and MACHINENAME fields to 50 characters\n";
    db_setconfigfieldvalue("SERIALNUM", "DEVICE", "MAXVALUE", 50);
    db_setconfigfieldvalue("MACHINENAME", "DEVICE", "MAXVALUE", 50);
    db_newobjectsearch("DEVICE", "", "");
    db_addobjectsearchcriteria("ID", "gt", "0");
    $deviceids = db_runobjectsearch();
    foreach ($deviceids as $deviceid) {
      $serialnum = db_getobjectvalue($deviceid, "DEVICE", "SERIALNUM");
      $machinename = db_getobjectvalue($deviceid, "DEVICE", "MACHINENAME");
      
      $serialnum = strtoupper($serialnum);
      $serialnum = ereg_replace("[^A-Za-z0-9-]", "", $serialnum);
      $serialnum = substr($serialnum, 0, db_getconfigfieldvalue("SERIALNUM", "DEVICE", "MAXVALUE"));
      $machinename = strtoupper($machinename);
      $machinename = ereg_replace("[^A-Za-z0-9-]", "", $machinename);
      $machinename = substr($machinename, 0, db_getconfigfieldvalue("MACHINENAME", "DEVICE", "MAXVALUE"));

      db_setobjectvalue($deviceid, "DEVICE", "SERIALNUM", $serialnum);
      db_setobjectvalue($deviceid, "DEVICE", "MACHINENAME", $machinename);
      db_clearobjectcache();
    }
    
            
    // set the DB version
    echo "  Setting the DB Version to 0.49\n";
    db_setconfigvalue("DBVERSION", "0.49");
    db_writeobjectcache();
  }
  
  
  // ******************************************************************
  // Upgrade from 0.49 to 0.50
  // ******************************************************************
  function upgrade_049_to_050() {
  
    echo "Upgrading database from 0.49 to 0.50\n";
    
    // set the DB version
    echo "  Setting the DB Version to 0.50\n";
    db_setconfigvalue("DBVERSION", "0.50");
    db_writeobjectcache();
                 
  
  }
  
  // ******************************************************************
  // Upgrade from 0.50 to 0.51
  // ******************************************************************
  function upgrade_050_to_051() {

    global $datalocation;
    $processerrors = 0;
    
    echo "Upgrading database from 0.50 to 0.51\n";

    // Reset the unique ID if necessary
    $uniqueid = db_getconfigvalue("UNIQUEID");
      if ($uniqueid == "7c5f740cbbb952fea2b9b194865b0ea3")
        db_setconfigvalue("UNIQUEID", "");
                  
                  
    // Extract data from filestore.db to the new filestore location
    if (file_exists($datalocation ."filestore.db")) {
      echo "  Exporting attachments from database to file system filestore\n";
      db_query("ATTACH '". $datalocation . "/filestore.db' as FILESTORE"); 
      $fileids = db_query("SELECT FILEID FROM FILESTORE.FILEDATA");
      foreach ($fileids as $fileid_detail) {
        $directory = "0";
        $filedata = "";
        $fileid = $fileid_detail[0];
        if (strlen($fileid) > 3)
          $directory = substr($fileid, 0, strlen($fileid) - 3);
        echo "  - ". $datalocation ."filestore/". $directory ."/". $fileid .".dat\n";
        if (!file_exists($datalocation ."filestore/". $directory))
          if (!mkdir($datalocation ."filestore/". $directory))
            $processerrors++;
          else
            chmod($datalocation ."filestore/". $directory, 0777);
        $found = db_query("SELECT BINARYDATA FROM FILESTORE.FILEDATA WHERE FILEID=". (int)$fileid);
        if (count($found) > 0)
          $filedata = $found[0][0];
        $fh = fopen($datalocation ."filestore/". $directory ."/". $fileid .".dat", "w");
        fwrite($fh, $filedata);
        fclose($fh);
        chmod($datalocation ."filestore/". $directory ."/". $fileid .".dat", 0666);
      }
      if ($processerrors == 0) {
        // Delete the old filestore database
        db_query("DETACH FILESTORE;");
        unlink($datalocation . "filestore.db");
      }
    }
        
    // Finish
    if ($processerrors > 0) {
      lib_safeend("Upgrade failed due to errors");
    } else {
      echo "  Setting the DB Version to 0.51\n";
      db_setconfigvalue("DBVERSION", "0.51");
      db_writeobjectcache();
    }
  }

    
             
?>
Return current item: Cowtacular