<?php

  /*

      globalpreferences.php

      Global Preferences page.
  
      Changelog:
        2006-01-14 dave: Cleaned up code for v1.0 release
  
      ----

      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();
  }

  // Page Title
  $ptitle = 'Global Preferences';

  // Includes
  require_once 'system/db.php';
  require_once 'system/lang.php';
  require_once 'system/message.php';
  require 'header.php';

  // Language selection
  set_text_domain("globalpreferences");

  // Retrieve Get/Post variables
  $act = $_REQUEST['act'];

  // Action: Edit prefs
  if ($act == 'action') {
    $prefs = db_recordset("SELECT * FROM tbl_System_Preferences WHERE system=0 ORDER BY identifier");

    $act = '';

    $flag = true;
    $message = gettext("ERROR:");

    foreach($prefs as $record) {
      if (isset($_REQUEST[$record['identifier']])) {
        if (is_numeric($_REQUEST[$record['identifier']])) {
          $value = $_REQUEST[$record['identifier']];
          db_send("UPDATE tbl_System_Preferences SET value='" . $_REQUEST[$record['identifier']] . "' WHERE id=". $record['id']);
        } else {
          $message .= " ". sprintf(gettext("Value entered for %s is not a number - preference not updated."),$record['identifier']);
          $flag = false;
        }
      }
    }
    if ($flag) $message = gettext("NOTE: Preferences successfully changed.");
  }

  // Default action: Display prefs
  if ($act == '') {

    $prefs = db_recordset("SELECT * FROM tbl_System_Preferences WHERE system=0 ORDER BY identifier");

    display($message);
?>

  <h1><?php echo gettext('Global Preferences')?></h1>
  <h2><?php echo gettext('Change the global preferences.')?></h2>
  
  <div class="block">
    <form name="mainform" id="mainform" method="post" action="">
      <input type="hidden" name="act" value="action" />
      <div class="buttonpanel">
        <input name="submit" type="submit" value="<?php echo gettext('Submit Changes')?>" />
        <input name="reset" type="reset" value="<?php echo gettext('Reset')?>" />
      </div>
<?php
    $regex = array(
      '/Page size for restriction of closed issues/',
      '/Number of hours spent on a problem that is considered excessive and should be confirmed/',
      '/Maximum file upload size in bytes/',
      '/Page size for paging of issues/'
     );
    $repl = array(
      gettext('Page size for restriction of closed issues'),
      gettext('Number of hours spent on a problem that is considered excessive and should be confirmed'),
      gettext('Maximum file upload size in bytes'),
      gettext('Page size for paging of issues')
     );

    foreach($prefs as $record) {
?>
      <div class="labelfieldpair">
        <div class="name"><?php echo $record['identifier']?>:</div>
      <div class="value"><?php echo preg_replace($regex, $repl, $record['comment'])?></div>
      </div>
      <div class="labelfieldpair">
        <div class="name">&nbsp;</div>
        <div class="field"><input type="text" name="<?php echo $record['identifier']?>" value="<?php echo $record['value']?>"></div>
      </div>
<?php
    }
?>
      <div class="buttonpanel">
        <input name="submit" type="submit" value="<?php echo gettext('Submit Changes')?>" />
        <input name="reset" type="reset" value="<?php echo gettext('Reset')?>" />
      </div>
    </form>
  </div>

<?php

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