<?php

  /*

      statuses.php

      Status Admin. Allows the user to administer the list of statuses
      for this domain an put them in order. The order given is that of the
      life cycle of an issue (open->closed). [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 = 'Statuses';

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

  // Language selection
  set_text_domain("statuses");

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

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

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

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

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

  <h1><?php echo gettext('Status Admin')?></h1>
  <h2><?php echo gettext('Please enter the following information to add a status:')?></h2>
  
  <div class="block">
    <form name="mainform" method="post" action="">
      <input type="hidden" name="act" value="addstatusaction" />
      <div class="labelfieldpair">
        <div class="name"><label for="status"><?php echo gettext('Status Name')?></label></div>
        <div class="field"><input type="text" name="status" id="status" size="35" maxlength="50" value="<?php echo $status?>" /></div>
      </div>
      <div class="buttonpanel">
        <input name="submit" type="submit" id="submit" value="<?php echo gettext('Add Status')?>" />
        <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='statuses.php?statuses=<?php echo $statuses?>'" />
      </div>
    </form>
  </div>

<?php
  }

  // Action: Edit a status
  if ($act == 'editstatusaction') {
    $act = 'editstatus';
    // Make sure the status has a name
    if ($status == '') {
      $message = gettext("CORRECTION: You must give this status a name.");
    } else {
      // Edit the status
      db_send("UPDATE tbl_Statuses SET status='" . $status . "' WHERE id=$statuses");
      $message = gettext("NOTE: Status successfully updated.");
      $act = '';
    }
  }

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

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

    $act = "";
  }

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

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

    $act = "";
  }

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

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

    // Retrieve Status information
    $statusesRS = db_recordset("SELECT * FROM tbl_Statuses WHERE id=" . $statuses);

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

  <h1><?php echo gettext('Status Admin')?></h1>
  <h2><?php echo gettext('Please enter the following information to edit a status:')?></h2>
  
  <div class="block">
    <form name="mainform" method="post" action="">
      <input type="hidden" name="act" value="editstatusaction" />
      <input type="hidden" name="statuses" value="<?php echo $statuses?>" />
      <div class="labelfieldpair">
        <div class="name"><label for="status"><?php echo gettext('Status Name')?></label></div>
        <div class="field"><input type="text" name="status" id="status" size="35" maxlength="50" value="<?php echo $statusesRS[0][status]?>"></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='statuses.php?statuses=<?php echo $statuses?>'" />
      </div>
    </form>
  </div>

<?php
  }

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

    if (!$statuses) $statuses=$statusesRS[0][id];

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

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

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

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

  <h1><?php echo gettext('Status Admin')?></h1>
  <h2><?php echo gettext('Please use the lists below to administer the statuses of issues  in this system:')?></h2>
        
  <div class="block">
    <p><?php echo gettext("The first status in this list is the default status of a new issue. Movement through this list would be synonymous with the life cycle of the issue. The final status will be classed as 'closed' and as such will be the terminal status of issues, regardless of the actual name of the status. Note that if only one status is present, then the status will not be treated as closed.")?></p>
    <form name="mainform" id="mainform" method="post" action="">
      <input type="hidden" name="act" value="" />
      <div class="columnleft">
        <div class="labelright"><label for="statuses"><?php echo gettext('Statuses')?></label></div>
        <div class="columnright">
          <input type="button" value="<?php echo gettext('New')?>..." onclick="mainSubmit('addstatus')" /><br />
          <input type="button" value="<?php echo gettext('Edit')?>" onclick="mainSubmit('editstatus')" /><br />
          <input type="button" value="<?php echo gettext('Delete')?>" onclick="mainSubmit('removestatus')" />
        </div>
        <div class="columnleft">
          <select name="statuses" id="statuses" size="10">
<?php
    if ($num_statuses == 0) {
      print ("            <option value=\"0\" selected=\"selected\">[".gettext('No Statuses')."]</option>\n");
    }
    foreach ($statusesRS as $record) {
      // Set as default if this was previously chosen
      if ($record[id] == $statuses) {$checked = " selected=\"selected\"";} else {$checked = "";};
      if ($record[active] == 0) {$style = " class=\"inactive\"";} else {$style = "";};
      print ("            <option value=\"${record[id]}\"${checked}${style}>${record[status]}</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';
?>
