<?php

  /*

      priorities.php

      Priority Admin. Allows the user to administer the list of priorities
      for this domain an put them in order. The order given is from lowest
      severity to highest. [Commented]
      
      Changelog:
        2006-01-14 dave: Cleaned up code for v1.0 release
  
      ## PAGE CONTAINS HANDYANDY MYSQL MODS ##
      ## Copyright (C) 2005 Andy Deakin (handyandy.org.uk) ##

      ----

      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'] != 'Administrator'
      && $_SESSION['_usertype'] != 'Domain Administrator'
      && $_SESSION['_usertype'] != 'Root') {
    require_once 'system/security.php';
    ThrowOut();
  }

  // Page Title
  $ptitle = 'Priorities';
  
  // Includes
  require_once 'system/db.php';
  require_once 'system/lang.php';
  require_once 'system/message.php';
  require 'header.php';

  // Language selection
  set_text_domain("priorities");

  // Retrieve Get/Post variables
  $act = $_REQUEST['act'];
  $priority = $_POST['priority'];
  $class = $_POST['class'];
  $priorities = $_REQUEST['priorities'];

  // Action: Add a priority to the system
  if ($act == 'addpriorityaction') {
    $act = 'addpriority';
    // Make sure the priority has a name
    if ($priority == '') {
    $message = gettext("CORRECTION: You must give this priority a name.");
    } else {
      $prioritycount = db_recordset("SELECT * FROM tbl_Priorities WHERE active=1 AND domain=$_SESSION[_domain] AND id>0");
      $priorities = '';
      // Add the priority
      db_send("INSERT INTO tbl_Priorities (priority,class,domain,sortorder) VALUES ('" . $priority . "'," . $class . "," . $_SESSION['_domain'] . ',' . count($prioritycount) . ")");

      $act = '';
      $message = gettext("NOTE: Priority successfully added to system.");
    }
  }

  // Action: Request details to add a priority to the system
  if ($act == 'addpriority') {

    if ($message) { display($message); }
?>

  <h1><?php echo gettext('Priority Admin')?></h1>
  <h2><?php echo gettext('Please enter the following information to add a priority:')?></h2>
  
  <div class="block">
    <form name="mainform" method="post" action="">
      <input type="hidden" name="act" value="addpriorityaction" />
      <div class="labelfieldpair">
        <div class="name"><label for="priority"><?php echo gettext('Priority Name')?></label></div>
        <div class="field"><input type="text" name="priority" id="priority" size="35" maxlength="50" value="<?php echo $priority?>" /></div>
      </div>
      <div class="labelfieldpair">
        <div class="name">
          <label for="class"><?php echo gettext('Tag Colour')?>:</label>
        </div>
        <div class="field">
          <select name="class" id="class">
            <option value="2"><?php echo gettext('Red')?></option>
            <option value="1"><?php echo gettext('Yellow')?></option>
            <option value="0" selected="selected"><?php echo gettext('Green')?></option>
          </select>
        </div>
      </div>
      <div class="buttonpanel">
        <input name="submit" type="submit" id="submit" value="<?php echo gettext('Add Priority')?>" />
        <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='priorities.php?priorities=<?php echo $priorities?>'" />
      </div>
    </form>
  </div>

<?php
  }

  // Action: Edit a priority
  if ($act == 'editpriorityaction') {
    $act = 'editpriority';
    // Make sure the priority has a name
    if ($priority == '') {
    $message = gettext("CORRECTION: You must give this priority a name.");
    } else {
      // Edit the priority
      db_send("UPDATE tbl_Priorities SET priority='" . $priority . "',class=" . $class . " WHERE id=$priorities");
      $message = gettext("NOTE: Priority successfully updated.");
      $act = '';
    }
  }

  // Action: Move a priority up
  if ($act == 'up') {
    // Find this priority's sort order
    $priority = db_recordset("SELECT sortorder,active FROM tbl_Priorities WHERE id=$priorities");

    // Only change the ordering of active priorities
    if ($priority[0][active] == 1) {
      // Only move a priority up if it isn't at the top already
      if ($priority[0][sortorder] > 0) {
        // Find the priority that is one in line above it
        $newpriority = db_recordset("SELECT id FROM tbl_Priorities WHERE domain=$_SESSION[_domain] AND active=1 AND sortorder=" . ($priority[0][sortorder]-1));
        // Swap the sort orders for the two priorities
        db_send("UPDATE tbl_Priorities SET sortorder=" . ($priority[0][sortorder]-1) . " WHERE id=$priorities");
        db_send("UPDATE tbl_Priorities SET sortorder=" . $priority[0][sortorder] . " WHERE id=" . $newpriority[0][id]);
      }
    } else {
    $message = gettext("CORRECTION: You cannot change the precedence of an inactive item.");
    }

    $act = '';
  }

  // Action: Move a priority down
  if ($act == 'down') {
    // Find the lowest priority
    $prioritiesRS = db_recordset("SELECT * FROM tbl_Priorities WHERE domain=$_SESSION[_domain] AND active=1");
    // Find this priority's sort order
    $priority = db_recordset("SELECT sortorder,active FROM tbl_Priorities WHERE id=$priorities");

    // Only change the ordering of active priorities
    if ($priority[0][active] == 1) {
      // Only move a priority down if it isn't at the bottom already
      if ($priority[0][sortorder] < (count($prioritiesRS)-1)) {
        // Find the priority that is one in line below it
        $newpriority = db_recordset("SELECT id FROM tbl_Priorities WHERE domain=$_SESSION[_domain] AND active=1 AND sortorder=" . ($priority[0][sortorder]+1));
        // Swap the sort orders for the two priorities
        db_send("UPDATE tbl_Priorities SET sortorder=" . ($priority[0][sortorder]+1) . " WHERE id=$priorities");
        db_send("UPDATE tbl_Priorities SET sortorder=" . $priority[0][sortorder] . " WHERE id=" . $newpriority[0][id]);
      }
    } else {
    $message = gettext("CORRECTION: You cannot change the precedence of an inactive item.");
    }

    $act = '';
  }

  // Action: Remove a priority from the system
  if ($act == 'removepriority') {
    // Find the sort order of this priority
    $priority = db_recordset("SELECT sortorder FROM tbl_Priorities WHERE id=$priorities");
    // deactivate it and negate the sort order
    db_send("UPDATE tbl_Priorities SET active=0,sortorder=-1 WHERE id=$priorities");
    // change all the higher sort orders in order to fill the gap that has now been made
    db_send("UPDATE tbl_Priorities SET sortorder=sortorder-1 WHERE domain=$_SESSION[_domain] AND active=1 AND sortorder>" . $priority[0][sortorder]);
    $act = '';
    $priorities = '';
    $message = gettext("NOTE: Priority successfully removed.");
  }

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

    // Retrieve Priority information
    $prioritiesRS = db_recordset("SELECT * FROM tbl_Priorities WHERE id=" . $priorities);

    if ($message) { display($message); }
?>

  <h1><?php echo gettext('Priority Admin')?></h1>
  <h2><?php echo gettext('Please enter the following information to edit a priority:')?></h2>
  
  <div class="block">
    <form name="mainform" method="post" action="">
      <input type="hidden" name="act" value="editpriorityaction" />
      <input type="hidden" name="priorities" value="<?php echo $priorities?>" />
      <div class="labelfieldpair">
        <div class="name"><label for="priority"><?php echo gettext('Priority Name')?></label></div>
        <div class="field"><input type="text" name="priority" id="priority" size="35" maxlength="50" value="<?php echo $prioritiesRS[0][priority]?>"></div>
      </div>
      <div class="labelfieldpair">
        <div class="name">
          <label for="class"><?php echo gettext('Tag Colour')?>:</label>
        </div>
        <div class="field">
<?php
  if ($prioritiesRS[0]['class'] == 0) $class_green=' selected="selected"';
  if ($prioritiesRS[0]['class'] == 1) $class_yellow=' selected="selected"';
  if ($prioritiesRS[0]['class'] == 2) $class_red=' selected="selected"';
?>
          <select name="class" id="class">
            <option value="2"<?php echo $class_red?>><?php echo gettext('Red')?></option>
            <option value="1"<?php echo $class_yellow?>><?php echo gettext('Yellow')?></option>
            <option value="0"<?php echo $class_green?>><?php echo gettext('Green')?></option>
          </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='priorities.php?priorities=<?php echo $priorities?>'" />
      </div>
    </form>
  </div>

<?php
  }

  // Default action: Show priorities and invite user to choose action...
  if ($act == '') {
    // Retrieve priorities
    $sql = "SELECT * FROM tbl_Priorities WHERE domain=$_SESSION[_domain]";
    if ($_SESSION['_usertype'] != 'Root') {
      $sql .= " AND active=1";
    }
    $sql .= " ORDER BY active DESC,sortorder";
    $prioritiesRS = db_recordset($sql);
    $num_priorities = count($prioritiesRS);

    if (!$priorities) $priorities=$prioritiesRS[0][id];

    if ($message) { display($message); }
?>

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

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

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

  <h1><?php echo gettext('Priority Admin')?></h1>
  <h2><?php echo gettext('Please use the lists below to administer the priorities of issues in this system:')?></h2>
        
  <div class="block">
    <p><?php echo gettext('The first priority in this list is of lowest priority, and movement through the list would be synonymous with an increase in severity.')?></p>
    <form name="mainform" id="mainform" method="post" action="">
      <input type="hidden" name="act" value="" />
      <div class="columnleft">
        <div class="labelright"><label for="priorities"><?php echo gettext('Priorities')?></label></div>
        <div class="columnright">
          <input type="button" value="<?php echo gettext('New')?>..." onclick="mainSubmit('addpriority')" /><br />
          <input type="button" value="<?php echo gettext('Edit')?>" onclick="mainSubmit('editpriority')" /><br />
          <input type="button" value="<?php echo gettext('Delete')?>" onclick="mainSubmit('removepriority')" />
        </div>
        <div class="columnleft">
          <select name="priorities" id="priorities" size="10">
<?php
    if ($num_priorities == 0) {
      print ("            <option value=\"0\" selected=\"selected\">[".gettext('No Priorities')."]</option>\n");
    }
    foreach ($prioritiesRS as $record) {
      // Set as default if this was previously chosen
      if ($record[id] == $priorities) {$checked = " selected=\"selected\"";} else {$checked = '';};
      if ($record[active] == 0) {$style = " class=\"inactive\"";} else {$style = '';};
      print ("            <option value=\"${record[id]}\"${checked}${style}>${record[priority]}</option>\n");
    }
?>
          </select>
        </div>
      </div>
      <div class="columnleft">
        <div class="labelleft">&nbsp;</div>
        <div class="columnleft">
          <input type="button" value="<?php echo gettext('Up')?>" onclick="mainSubmit('up')" /><br />
          <input type="button" value="<?php echo gettext('Down')?>" onclick="mainSubmit('down')" /><br />
        </div>
      </div>
    </form>
    <hr class="hide" />
  </div>

<?php
  }

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