<?php

  /*

      problemcategroies.php

      Problem categorisation Admin. [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 = 'Problem Categories';

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

  // Language selection
  set_text_domain("problemcategories");

  // Retrieve Get/Post variables
  $act = $_REQUEST['act'];
  $description = $_POST['description'];
  $details = $_REQUEST['details'];
  $categories = $_REQUEST['categories'];

  // Action: Add a category to the system
  if ($act == 'addcategoryaction') {
    $act = 'addcategory';
    // Make sure the category has a name
    if ($description == '') {
    $message = gettext("CORRECTION: You must give this problem category a name.");
    } else {
      $categories = '';
      db_send("INSERT INTO tbl_ProblemCategories (description,domain) VALUES ('" . $description . "',$_SESSION[_domain])");

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

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

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

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

<?php
  }

  // Action: Edit a category
  if ($act == 'editcategoryaction') {
    $act = 'editcategory';
    // Make sure the category has a name
    if ($description == '') {
    $message = gettext("CORRECTION: You must give this problem category a name.");
    } else {
      db_send("UPDATE tbl_ProblemCategories SET description='" . $description . "' WHERE ID=$categories");
      $message = gettext("NOTE: Problem category successfully updated.");
      $act = '';
    }
  }

  // Action: Remove a category
  if ($act == 'removecategory') {
    db_send("UPDATE tbl_ProblemDetails SET active=0 WHERE category=$categories");
    db_send("UPDATE tbl_ProblemCategories SET active=0 WHERE id=$categories");
    $act = "";
    $categories = "";
    $message = gettext("NOTE: Problem category successfully removed.");
  }

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

    // Retrieve Problem Category information
    $categoriesRS = db_recordset("SELECT * FROM tbl_ProblemCategories WHERE id=" . $categories);

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

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

<?php
  }

  // Action: Add a detail to the system
  if ($act == 'addaction') {
    $act = 'add';
    if ($description == '') {
    $message = gettext("CORRECTION: You must give this problem detail a description.");
    } else {
      $details = '';
      db_send("INSERT INTO tbl_ProblemDetails (description,category,domain) VALUES ('" . $description . "'," . $categories . ",$_SESSION[_domain])");

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

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

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

  <h1><?php echo gettext('Problem Category Admin')?></h1>
  <h2><?php echo gettext('Please enter the following information to add a problem detail:')?></h2>
  
  <div class="block">
    <form name="mainform" method="post" action="">
      <input type="hidden" name="act" value="addaction" />
      <input type="hidden" name="categories" value="<?php echo $categories?>" />
      <input type="hidden" name="details" value="<?php echo $details?>" />
      <div class="labelfieldpair">
        <div class="name"><label for="description"><?php echo gettext('Description')?></label></div>
        <div class="field"><input type="text" name="description" id="description" size="35" maxlength="50" value="<?php echo $description?>" /></div>
      </div>
      <div class="buttonpanel">
        <input name="submit" type="submit" id="submit" value="<?php echo gettext('Add Detail')?>" />
        <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='problemcategories.php?categories=<?php echo $categories?>'" />
      </div>
    </form>
  </div>

<?php
  }

  // Action: Edit a category
  if ($act == 'editaction') {
    $act = 'edit';
    if ($description == '') {
    $message = gettext("CORRECTION: You must give this problem detail a description.");
    } else {
      db_send("UPDATE tbl_ProblemDetails SET description='" . $description . "' WHERE ID=$details");
      $message = gettext("NOTE: Problem detail successfully updated.");
      $act = '';
    }
  }

  // Action: Remove a category
  if ($act == 'remove') {
    $act = '';
    db_send("UPDATE tbl_ProblemDetails SET active=0 WHERE id=$details");
    $details = '';
    $message = gettext("NOTE: Problem detail successfully removed.");
  }

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

    // Retrieve Problem information
    $detailsRS = db_recordset("SELECT * FROM tbl_ProblemDetails WHERE id=" . $details);

    display($message);
?>

  <h1><?php echo gettext('Problem Category Admin')?></h1>
  <h2><?php echo gettext('Please enter the following information to edit a problem detail:')?></h2>
  
  <div class="block">
    <form name="mainform" method="post" action="">
      <input type="hidden" name="act" value="editaction" />
      <input type="hidden" name="categories" value="<?php echo $categories?>" />
      <input type="hidden" name="details" value="<?php echo $details?>" />
      <div class="labelfieldpair">
        <div class="name"><label for="description"><?php echo gettext('Description')?></label></div>
        <div class="field"><input type="text" name="description" id="description" size="35" maxlength="50" value="<?php echo $detailsRS[0][description]?>" /></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='problemcategories.php?categories=<?php echo $categories?>&details=<?php echo $details?>'" />
      </div>
    </form>
  </div>

<?php
  }

  // Default action: Show categories and details and choose action...
  if ($act == '') {
    // Retrieve problem categories
    $categoriesRS = db_recordset("SELECT * FROM tbl_ProblemCategories WHERE domain=$_SESSION[_domain] AND active=1 ORDER BY description");
    $num_categories = count($categoriesRS);

    // Retrieve problem detail information
    if ($num_categories > 0)
      $details = db_recordset("SELECT * FROM tbl_ProblemDetails WHERE domain=$_SESSION[_domain] AND active=1 ORDER BY description");
    if (!$categories) $categories=$categoriesRS[0][id];

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

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

<?php

    /*

      Dynamically create a set of javascript arrays that will be used for the
      client side interface for choosing between the heirarchical mappings of
      categories and details.

      The arrays holds the 'children' of each top level category. By choosing
      the appropriate category using a <select> list the following SetupDetails
      function causes the details list to be rebuilt, on the fly.

  */

    if ($num_categories > 0) {
      $index = 0;
      foreach ($categoriesRS as $record) {
        $current = filter_records($details,"category",$record[id]);
        $flag = TRUE;
        print ("  var problems_$index = new Array('");
        if (count($current) == 0) {
          print ("0','[No Details]");
        } else {
          foreach ($current as $res_record) {
            if ($flag) {
              $flag = FALSE;
            } else {
              print ("','");
            }
          print ($res_record[id] . "','" . addslashes($res_record[description]));
          }
        }
        print ("');\n");
        $index++;
      }
    }
?>

  function SetupDetails () {
    var noDetails = document.mainform.categories.options.length;
    var DetailsList = document.mainform.details.options;
    var strTemp = '';
    DetailsList.length = 0;
    for (i=0;i<noDetails;i++) {
      if (document.mainform.categories.options[i].selected && eval('problems_' + i + '[0]') != '') {
        for (loop=0;loop<eval('problems_' + i + '.length')/2;loop++) {
          DetailsList[loop] = new Option(eval('problems_' + i + '[loop*2+1]'));
          DetailsList[loop].value = eval('problems_' + i + '[loop*2]');
        }
      }
    }
    DetailsList[0].selected = true;
  }

  function mainSubmit (act) {
    var flag = true;
    if ((act == 'removecategory' || act == 'editcategory' || act == 'add') && document.mainform.categories.value == 0) {
      alert('<?php echo gettext('There are no categories on which to action your request.')?>');
      flag = false;
    }
    if (act == 'removecategory' && flag && !confirm('<?php echo gettext('Are you sure you wish to delete this problem category?')?>')) flag = false;
    if ((act == 'remove' || act == 'edit') && document.mainform.details.value == 0) {
      alert('<?php echo gettext('There are no details on which to action your request.')?>');
      flag = false;
    }
    if (act == 'remove' && flag && !confirm('<?php echo gettext('Are you sure you wish to delete this problem detail?')?>')) flag = false;
    if (flag) {
      document.mainform.act.value=act;
      document.mainform.submit();
    }
  }

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

  <h1><?php echo gettext('Problem Category Admin')?></h1>
  <h2><?php echo gettext('Please use the lists below to administer the categorisation of problems in this system:')?></h2>
        
  <div class="block">
    <form name="mainform" id="mainform" method="post">
      <input type="hidden" name="act" value="" />
      <div class="columnleft"> <div class="labelright">
       <label for="categories"><?php echo gettext('Categories')?></label></div>
        <div class="columnright">
          <input type="button" value="<?php echo gettext('New')?>..." onclick="mainSubmit('addcategory')" /><br />
          <input type="button" value="<?php echo gettext('Edit')?>" onclick="mainSubmit('editcategory')" /><br />
          <input type="button" value="<?php echo gettext('Delete')?>" onclick="mainSubmit('removecategory')" />
        </div>
        <div class="columnleft">
          <select name="categories" id="categories" size="10" onchange="SetupDetails()">
<?php
    if ($num_categories == 0) {
      print ("            <option value=\"0\" selected=\"selected\">[".gettext('No Categories')."]</option>\n");
    }
    foreach ($categoriesRS as $record) {
      // Set as default if this was previously chosen
      if ($record[id] == $categories) {$checked = " selected=\"selected\"";} else {$checked = "";};
      print ("            <option value=\"${record[id]}\"${checked}>${record[description]}</option>\n");
    }
?>
          </select>
        </div>
      </div>
      <div class="columnleft">
        <div class="columnleft"><label for="details"><?php echo gettext('Details')?></label></div><br />
        <div class="columnleft">
          <select name="details" id="details" size="10">
      <option value="0" selected="selected">[<?php echo gettext('No Details')?>]</option>
          </select>
        </div>
        <div class="columnleft">
          <input type="button" value="<?php echo gettext('New')?>..." onclick="mainSubmit('add')" />
          <br />
          <input type="button" value="<?php echo gettext('Edit')?>" onclick="mainSubmit('edit')" />
          <br />
          <input type="button" value="<?php echo gettext('Delete')?>" onclick="mainSubmit('remove')" />
        </div>
      </div>
    </form>
    <hr class="hide" />
  </div>

<?php
    if ($num_categories > 0) {
?>
<script language="javascript" type="text/javascript">
//<![CDATA[
<!--

  SetupDetails();

//-->
//]]>
</script>
<?php
    }
  }

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