<?php

  /*

      domains.php

      Domain Admin. Allows the adding editting and removing of domains
      to the system. [Commented]

      Changelog:
        2006-01-14 dave: Cleaned up code for v1.0 release
        2005-04-27 arne_sf: Replaced MySQL- and PostgreSQL-specific sequence
          ID code with call to db_next_id()
        
      ----

      Copyright (C) 2003  Central Manchester CLC
      Copyright (C) 2003  David Thorne (davethorne@gmail.com)

      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.

  */

  // Start session and populate it with data
  session_start();

  // Page security
  if ($_SESSION['_usertype'] != 'Root') {
    require_once 'system/security.php';
    ThrowOut();
  }
  
  // Includes
  require_once 'system/db.php';
  require_once 'system/lang.php';
  require_once 'system/message.php';

  // Set Language
  set_text_domain("domains");

  // Retrieve Get/Post variables
  $act = $_REQUEST['act'];
  $domain = $_POST['domain'];
  $domains = $_REQUEST['domains'];
  $defaultpriority = $_POST['defaultpriority'];

  // Action: Add a domain to the system
  if ($act == 'adddomainaction') {
    $act = 'adddomain';
    // Check that a name has been entered
    if ($domain == '') {
      $message = gettext('CORRECTION: You must give this domain a name.');
    } else {
      $domain_id = db_next_id('tbl_Domains_id');
      db_send("INSERT INTO tbl_Domains (id, domain) VALUES ($domain_id, '" . $domain . "')");

      $domains = '';
      if ($_SESSION[_domain] == '') {
        $_SESSION[_domain] = $domain_id;
        $_SESSION[_domainname] = $domain;
        $_SESSION[_domaincss] = '';
      }
      $message = 'NOTE: Domain successfully added to system.';

      $dirname = dirname($_SERVER['PHP_SELF']);
      $https = 'http'.($_SERVER['HTTPS'] ? 's' : '');
      $headertext = "Location: $https://".$_SERVER['HTTP_HOST'].$dirname.(strrpos($dirname,'/') == (strlen($dirname)-1)?'':'/').'domains.php?domains='.$_SESSION[_domain];

      header($headertext);
      exit;
    }
  }

  // Action: Edit a domain
  if ($act == 'editdomainaction') {
    $act = 'editdomain';
    // Check that a name has been entered
    if ($domain == '') {
    $message = gettext('CORRECTION: You must give this domain a name.');
    } else {
      db_send("UPDATE tbl_Domains SET domain='$domain',defaultpriority=$defaultpriority WHERE id=$domains");
      $message = gettext("NOTE: Domain successfully updated");

      $dirname = dirname($_SERVER['PHP_SELF']);
      $https = 'http'.($_SERVER['HTTPS'] ? 's' : '');
      $headertext = "Location: $https://".$_SERVER['HTTP_HOST'].$dirname.(strrpos($dirname,'/') == (strlen($dirname)-1)?'':'/').'domains.php?domains='.$domains;

      header($headertext);
      exit;
    }
  }

  // Action: Remove a domain from the system
  if ($act == 'removedomain') {
    db_send("UPDATE tbl_Domains SET active=0 WHERE id=$domains");
    $message = gettext("NOTE: Domain successfully removed.");

    $sql = "SELECT * FROM tbl_Domains WHERE id>0 AND active=1";
    $domainsRS = db_recordset($sql);

    // If it is this domain that has been deleted, find another to set as current
    if ($domains == $_SESSION['_domain']) {
      $_SESSION['_domain'] = $domainsRS[0][id];
      $_SESSION['_domainname'] = $domainsRS[0][domain];
      $_SESSION['_domaincss'] = $domainsRS[0][css];
    }
    $domains = $domainsRS[0][id];

    $dirname = dirname($_SERVER['PHP_SELF']);
    $https = 'http'.($_SERVER['HTTPS'] ? 's' : '');
    $headertext = "Location: $https://".$_SERVER['HTTP_HOST'].$dirname.(strrpos($dirname,'/') == (strlen($dirname)-1)?'':'/').'domains.php?domains='.$domains;

    header($headertext);
    exit;
  }

  // Page Title
  $ptitle = 'Domains';

  // Includes
  require 'header.php';

  // Set Language
  set_text_domain("domains");

  // Action: Request details to add a domain to the system
  if ($act == 'adddomain')
  {
    if ($message) { display(gettext($message)); }
?>

  <h1><?php echo gettext("Domain Admin")?></h1>
  <h2><?php echo gettext("Please enter the following information to add a domain:")?></h2>
  
  <div class="block">
    <form name="mainform" method="post" action="">
      <input type="hidden" name="act" value="adddomainaction" />
      <div class="labelfieldpair">
        <div class="name"><label for="domain"><?php echo gettext("Domain Name")?></label></div>
        <div class="field"><input type="text" name="domain" id="domain" size="35" maxlength="50" value="" /></div>
      </div>
      <div class="buttonpanel">
        <input name="submit" type="submit" id="submit" value="<?php echo gettext('Add Domain')?>" />
        <input name="reset" type="reset" id="reset" value="<?php echo gettext('Reset')?>" />
        <input name="cancel" type="button" id="cancel" value="<?php echo gettext('Cancel')?>" onclick="document.location='domains.php?domains=<?php echo $domains?>'" />
      </div>
    </form>
  </div>

<?php
  }

  $sql = "SELECT * FROM tbl_Domains WHERE id>0 AND active=1";
  $domainsRS = db_recordset($sql);

  if (count($domainsRS) == 0) {
    $_SESSION['_domain'] = '';
    $act = 'nodomains';
  }

  // Action: Request details to create a domain for the system
  if ($act == 'nodomains') {

    $message = gettext("WARNING: You must set up at least one domain.");
    display($message);
?>

  <h1><?php echo gettext("Domain Admin")?></h1>
  <h2><?php echo gettext("Your installation of the Helpdesk has no domains set up in it yet. You must create at least one domain to use for your helpdesk:")?></h2>
  
  <div class="block">
    <form name="mainform" method="post" action="">
      <input type="hidden" name="act" value="adddomainaction" />
      <div class="labelfieldpair">
        <div class="name"><label for="domain"><?php echo gettext("Domain Name")?></label></div>
        <div class="field"><input type="text" name="domain" id="domain" size="35" maxlength="50" value="" /></div>
      </div>
      <div class="buttonpanel">
        <input name="submit" type="submit" id="submit" value="<?php echo gettext('Add Domain')?>" />
        <input name="reset" type="reset" id="reset" value="<?php echo gettext('Reset')?>" />
      </div>
    </form>
  </div>

<?php
  }

  // Action: Request details to edit a domain
  if ($act == 'editdomain') {

    // Retrieve Domain information
    $domainsRS = db_recordset("SELECT * FROM tbl_Domains WHERE id=$domains");

    display($message);
?>

  <h1><?php echo gettext('Domain Admin')?></h1>
  <h2><?php echo gettext('Please enter the following information to edit a domain:')?></h2>
  
  <div class="block">
    <form name="mainform" method="post" action="">
      <input type="hidden" name="act" value="editdomainaction" />
      <input type="hidden" name="domains" value="<?php echo $domains?>" />
      <div class="labelfieldpair">
        <div class="name"><label for="domain"><?php echo gettext('Domain Name')?></label></div>
        <div class="field"><input type="text" name="domain" id="domain" size="35" maxlength="50" value="<?php echo $domainsRS[0][domain]?>"></div>
      </div>
<?php
    $sql = "SELECT id,priority FROM tbl_Priorities WHERE id=0 OR domain=$domains ORDER BY sortorder";
    $prioritiesRS = db_recordset($sql);
?>
      <div class="labelfieldpair">
        <div class="name"><label for="defaultpriority"><?php echo gettext('Default Priority')?></label></div>
        <div class="field"><select name="defaultpriority" id="defaultpriority">
<?php
    foreach ($prioritiesRS as $record) {
      if ($record[id] == $domainsRS[0]['defaultpriority']) {$checked = " selected=\"selected\"";} else {$checked = "";};
      print "          <option value=\"${record[id]}\"$checked>".
    preg_replace('/Ungraded/',gettext('Ungraded'),$record[priority])."</option>\n";
    }
?>
        </select></div>
      </div>
      <div class="buttonpanel">
        <input name="submit" type="submit" id="submit" value="<?php echo gettext('Submit Changes')?>" />
        <input name="reset" type="reset" id="reset" value="<?php echo gettext('Reset')?>" />
        <input name="cancel" type="button" id="cancel" value="<?php echo gettext('Cancel')?>" onclick="document.location='domains.php?domains=<?php echo $domains?>'" />
      </div>
    </form>
  </div>

<?php
  }

  // Default action: List domains and request action...
  if ($act == '') {
    // Retrieve domains
    $domainsRS = db_recordset('SELECT * FROM tbl_Domains WHERE active=1 ORDER BY domain');
    $num_domains = count($domainsRS);

    if (!$domains) $domains=$domainsRS[0][id];
    if ($message) { display(gettext($message));}
?>

<script language="javascript" type="text/javascript">
//<![CDATA[
<!--

  function mainSubmit (act) {
    var flag = true;
    if ((act == 'removedomain' || act == 'editdomain') && document.mainform.domains.value == 0) {
      alert('<?php echo gettext("There are no domains on which to action your request.")?>');
      flag = false;
    }
    if (act == 'removedomain' && flag && !confirm('<?php echo gettext("Are you sure you wish to delete this domain?")?>')) flag = false;
    if (flag) {
      document.mainform.act.value=act;
      document.mainform.submit();
    }
  }

//-->
//]]>
</script>

  <h1><?php echo gettext('Domain Admin')?></h1>
  <h2><?php echo gettext('Please use the lists below to administer the domains of issues in this system:')?></h2>
        
  <div class="block">
    <form name="mainform" id="mainform" method="post" action="">
      <input type="hidden" name="act" value="" />
      <div class="columnleft">
        <div class="labelright"><label for="domains"><?php echo gettext('Domains')?></label></div>
        <div class="columnright">
          <input type="button" value="<?php echo gettext('New')?>..." onclick="mainSubmit('adddomain')" /><br />
          <input type="button" value="<?php echo gettext('Edit')?>" onclick="mainSubmit('editdomain')" /><br />
          <input type="button" value="<?php echo gettext('Delete')?>" onclick="mainSubmit('removedomain')" />
        </div>
        <div class="columnleft">
          <select name="domains" id="domains" size="10">
<?php
    if ($num_domains == 0) {
      print ("            <option value=\"0\" selected=\"selected\">[".gettext('No Domains')."]</option>\n");
    }
    foreach ($domainsRS as $record) {
      // Set as default if it was the last to be visited.
      if ($record[id] == $domains) {$checked = " selected=\"selected\"";} else {$checked = "";};
      print ("            <option value=\"${record[id]}\"${checked}>${record[domain]}</option>\n");
    }
?>
          </select>
        </div>
      </div>
    </form>
    <hr class="hide" />
  </div>

<?php
  }

  // Include
  require 'footer.php';
?>
