<?php

  /*

      myissues.php

      My Unresolved and pending Issues. If the 'closed' querystring value
      is true then this shows only closed issues, limiting to a maximum of
      $global_prefs[closedpagesize] issues per page and sorting on the
      closed date. If the closed value doesn't equal true then it shows
      only open issues and shows no limits on page size or sorting.
      [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'] == '' || $_SESSION['_usertype'] == 'Root') {
    require_once 'system/security.php';
    ThrowOut();
  }

  // Page Title
  $ptitle = 'My Issues';

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

  // Language selection
  set_text_domain("myissues");

  // Retrieve Get/Post variables
  $act = $_GET['act'];
  $orderby = $_GET['orderby'];
  $orderdir = $_GET['orderdir'];
  $orderby2 = $_GET['orderby2'];
  $orderdir2 = $_GET['orderdir2'];
  $closed = $_GET['closed']; // Should this page show the closed issues?

  // Default action: Show this user's issues
  if ($act == '') {
    if ($closed != 'true') {
      // If this is the open list then...
      if ($_SESSION['_usertype'] == 'Domain Administrator' || $_SESSION['_usertype'] == 'Administrator' || $_SESSION['_usertype'] == 'Support Agent') {
        // Set up sorting and grouping values for the unresolved issue list
        if ($orderby == '' && $_SESSION['_orderby-myissues'] != '') {
            // Use stored ordering unless another selection has been made
            $orderby = $_SESSION['_orderby-myissues'];
            $orderdir = $_SESSION['_orderdir-myissues'];
        }
        if ($orderby == '') {
      if ($conf_db['dsn']['phptype'] == 'pgsql') {
        $grouporderby = 'id';
        $orderby = 'sortorder DESC,vw_Issues.id DESC';
      } else {
        $grouporderby = 'tbl_Issues.id';
        $orderby = 'tbl_Priorities.sortorder DESC,tbl_Issues.id DESC';
        $orderdir = '';
      }
        } else {
          // Save ordering in session
          session_register('_orderby-myissues');
          session_register('_orderdir-myissues');
          $_SESSION['_orderby-myissues'] = $orderby;
          $_SESSION['_orderdir-myissues'] = $orderdir;

          $grouporderby = $orderby;
        }
      }

      // Set up sorting and grouping values for the pending issue list
      if ($orderby2 == '' && $_SESSION['_orderby-myissues2'] != '') {
        // Use stored ordering unless another selection has been made
        $orderby2 = $_SESSION['_orderby-myissues2'];
        $orderdir2 = $_SESSION['_orderdir-myissues2'];
      }
      if ($orderby2 == '') {
        if ($conf_db['dsn']['phptype'] == 'pgsql') {
           $grouporderby2 = 'id';
           $orderby2 = 'sortorder DESC,vw_Issues.id DESC';
        } else {
          $grouporderby2 = 'tbl_Issues.id';
          $orderby2 = 'tbl_Priorities.sortorder DESC,tbl_Issues.id DESC';
          $orderdir2 = '';
        }
      } else {
        // Save ordering in session
        session_register('_orderby-myissues2');
        session_register('_orderdir-myissues2');
        $_SESSION['_orderby-myissues2'] = $orderby2;
        $_SESSION['_orderdir-myissues2'] = $orderdir2;
        $grouporderby2 = $orderby2;
      }
    } else {
      // ... otherwise limit the sorting and grouping
      $orderby = 'closedon';
      $grouporderby = 'closedon';
      $orderdir = 'DESC';
      $orderby2 = 'closedon';
      $grouporderby2 = 'closedon';
      $orderdir2 = 'DESC';
    }

    // Find the highest status (the terminal status) and treat it as closed
    $closedstatus = get_closed_status($_SESSION['_domain']);
?>

  <h1><?php echo gettext("My")?> <?php if ($closed == 'true') { print gettext("Closed").' '; } ?><?php echo gettext("Issues")?></h1>
  <h2>
<?php
    if ($closed == 'true') {
?>
    <?php echo gettext("Below is a reference list of all your recently resolved issues")?>:
<?php
    } else {
?>
    <?php echo gettext("Below is a list of all your unresolved and pending issues")?>:
<?php
    }
?>
  </h2>
  <div class="block">
<?php
    if ($_SESSION['_usertype'] == 'Domain Administrator' || $_SESSION['_usertype'] == 'Administrator' || $_SESSION['_usertype'] == 'Support Agent') {
      // Retrieve issues
    if ($conf_db['dsn']['phptype'] == 'pgsql') {
      $sql = "SELECT * FROM vw_Issues WHERE domain=$_SESSION[_domain] AND assignedto=$_SESSION[_id]";
      // Choose open or closed
      if ($closed == 'true') {
        $pagesize = $global_prefs[closedpagesize];
        $sql .= ' AND status=' . $closedstatus;
      } else {
        $pagesize = 'ALL';
        $sql .= ' AND status<>' . $closedstatus;
      }
    } else {
      $sql = "SELECT DISTINCT $aselects FROM $afrom WHERE tbl_Issues.domain=$_SESSION[_domain] AND tbl_Issues.assignedto=$_SESSION[_id]";
      // Choose open or closed
      if ($closed == 'true') {
        $pagesize = $global_prefs[closedpagesize];
        $sql .= ' AND tbl_Issues.status=' . $closedstatus;
      } else {
        $pagesize = '500';
        $sql .= ' AND tbl_Issues.status<>' . $closedstatus;
      }
    }
    $sql .= " ORDER BY $orderby $orderdir LIMIT $pagesize";
    $issuesRS = db_recordset($sql);
    $num_issues = count($issuesRS);
?>

<?php
      if ($closed == 'true') {
?>
      <p><?php printf (gettext('Each list is limited to %s pagesize entries. For more, please use the <a href="find.php">find</a> page.'), $pagesize);?>
  </p>
    <h3><?php echo gettext('Resolved Issues (Resolved by you)')?></h3>
<?php
      } else {
?>
    <h3><?php echo gettext('Unresolved Issues (Assigned to you)')?></h3>
<?php
      }
?>

    <table width="100%" border="0" align="center" cellpadding="1" cellspacing="0" summary="<?php echo gettext('This table summarises the unresolved issues for this user.')?>">
    <thead>
<?php
      if ($closed == 'true') {
        // Do not allow sorting
?>
      <tr>
        <th align="left" class="issueheader">#</th>
        <th align="left" class="issueheader"><?php echo gettext('Resolved')?></th>
        <th align="left" class="issueheader"><?php echo gettext('Reported By')?></th>
        <th align="left" class="issueheader"><?php echo gettext('Site')?></a></th>
        <th align="left" class="issueheader"><?php echo gettext('Summary')?></a></th>
        <th align="center" class="issueheader"><?php echo gettext('Priority')?></a></th>
      </tr>
<?php
      } else {
        // Allow sorting on each attribute
?>
      <tr>
        <th align="left" class="issueheader"><a href="myissues.php?closed=<?php echo $closed?>&amp;<?php echo makeordering('id',$orderby,$orderdir)?>&amp;orderby2=<?php echo $orderby2?>&amp;orderdir2=<?php echo $orderdir2?>">#</a><?php echo makesymbol('id',$orderby,$orderdir)?></th>
        <th align="left" class="issueheader"><a href="myissues.php?closed=<?php echo $closed?>&amp;<?php echo makeordering('createdon',$orderby,$orderdir)?>&amp;orderby2=<?php echo $orderby2?>&amp;orderdir2=<?php echo $orderdir2?>"><?php echo gettext('Reported')?></a><?php echo makesymbol('createdon',$orderby,$orderdir)?></th>
        <th align="left" class="issueheader"><a href="myissues.php?closed=<?php echo $closed?>&amp;<?php echo makeordering('reportedbyname',$orderby,$orderdir)?>&amp;orderby2=<?php echo $orderby2?>&amp;orderdir2=<?php echo $orderdir2?>"><?php echo gettext('Reported By')?></a><?php echo makesymbol('reportedbyname',$orderby,$orderdir)?></th>
        <th align="left" class="issueheader"><a href="myissues.php?closed=<?php echo $closed?>&amp;<?php echo makeordering('sitename',$orderby,$orderdir)?>&amp;orderby2=<?php echo $orderby2?>&amp;orderdir2=<?php echo $orderdir2?>"><?php echo gettext('Site')?></a><?php echo makesymbol('sitename',$orderby,$orderdir)?></th>
        <th align="left" class="issueheader"><a href="myissues.php?closed=<?php echo $closed?>&amp;<?php echo makeordering('summary',$orderby,$orderdir)?>&amp;orderby2=<?php echo $orderby2?>&amp;orderdir2=<?php echo $orderdir2?>"><?php echo gettext('Summary')?></a><?php echo makesymbol('summary',$orderby,$orderdir)?></th>
        <th align="center" class="issueheader"><a href="myissues.php?closed=<?php echo $closed?>&amp;<?php echo makeordering('sortorder',$orderby,$orderdir)?>&amp;orderby2=<?php echo $orderby2?>&amp;orderdir2=<?php echo $orderdir2?>"><?php echo gettext('Priority')?></a><?php echo makesymbol('sortorder',$orderby,$orderdir)?></th>
      </tr>
<?php
      }
?>
      </thead>
<?php
      if ($num_issues == 0) {
        // If there are no issues, then say as much...
        print "      <tr>\n";
        if ($closed == 'true') {
          print "        <td colspan=\"6\" align=\"center\" class=\"issuecell\">".gettext('No Resolved Issues.')."</td>\n";
        } else {
          print "        <td colspan=\"6\" align=\"center\" class=\"issuecell\">".gettext('No Unresolved Issues.')."</td>\n";
        }
        print "      </tr>\n";
      }
      // Alternately shade the rows on the sorted attribute by keeping track
      // of the previously shown value. If this value != last value then the
      // colour can be changed

      // flag shows whether to shade or not - starts being 'NO SHADING'
      $flag = 1;
      // Nullify the last value
      $lastvalue = "\0";
      foreach ($issuesRS as $record) {
        if ($lastvalue != $record[$grouporderby]) {
          // If different, toggle shading
          $flag = 1 - $flag;
          $lastvalue = $record[$grouporderby];
        }
        if ($flag == 0) {
          $class = 'issuecell';
        } else {
          $class = 'issuecelldark';
        }

        if ($closed == 'true' || $record['recalled'] == 1) {
          $priority = 'prioritydefunct';
        } else {
          switch ($record['class']) {
            case 0: $priority='prioritylow';
              break;
            case 1: $priority='prioritymedium';
              break;
            case 2: $priority='priorityhigh';
              break;
          }
        }

        // Show attributes
        print "      <tr valign=\"top\">\n";
        print "        <td class=\"$class\"><a href=\"issue.php?id=${record[id]}\">${record[id]}</a></td>\n";
        if ($closed == 'true') {
          print "          <td class=\"$class\">";
          if (strftime('%d/%m/%y',$record[closedon]) == strftime('%d/%m/%y')) {
            print strftime('%H:%M',$record[closedon]);
          } else {
            print strftime('%d/%m/%y',$record[closedon]);
          }
          print "</td>\n";
        } else {
          print "          <td class=\"$class\">";
          if (strftime('%d/%m/%y',$record[createdon]) == strftime('%d/%m/%y')) {
            print strftime('%H:%M',$record[createdon]);
          } else {
            print strftime('%d/%m/%y',$record[createdon]);
          }
          print "</td>\n";
        }
        print "        <td class=\"$class\">".$record[reportedbyname]."</td>\n";
        print "        <td class=\"$class\">".$record[sitename]."</td>\n";
        print "        <td class=\"$class\"><a href=\"issue.php?id=${record[id]}\">".$record[summary]."</a></td>\n";
        print "        <td align=\"center\" class=\"$priority\">";
        if ($record['recalled'] == 1) {
          print gettext('Recalled');
        } else {
          if ($record[priorityname] == "Ungraded") {
            print gettext('Ungraded');
          } else {
            print $record[priorityname];
          }
        }
        print "</td>\n";
        print "      </tr>\n";

      }
?>
    </table>

<?php
      if ($closed == 'true') {
?>
    <h3><?php echo gettext('Resolved Issues (Submitted by you)')?></h3>
<?php
      } else {
?>
    <h3><?php echo gettext('Pending Issues (Submitted by you)')?></h3>
<?php
      }
?>

<?php
    }
    // Retrieve issues
    if ($conf_db['dsn']['phptype'] == 'pgsql') {
      $sql = "SELECT * FROM vw_Issues
              WHERE domain=$_SESSION[_domain]
                AND (createdby=$_SESSION[_id] OR reportedby=$_SESSION[_id])
                AND assignedto!=$_SESSION[_id]";
      // Choose open or closed
      if ($closed == 'true') {
        $sql .= ' AND status=' . $closedstatus;
      } else {
        $sql .= ' AND status<>' . $closedstatus;
      }
    } else {
      $sql = "SELECT DISTINCT $aselects
              FROM $afrom
              WHERE tbl_Issues.domain=$_SESSION[_domain]
                AND (tbl_Issues.createdby=$_SESSION[_id] OR tbl_Issues.reportedby=$_SESSION[_id])
                AND tbl_Issues.assignedto!=$_SESSION[_id]";
      // Choose open or closed
      if ($closed == 'true') {
        $sql .= ' AND tbl_Issues.status=' . $closedstatus;
      } else {
        $sql .= ' AND tbl_Issues.status<>' . $closedstatus;
      }
    }
    $sql .= " ORDER BY $orderby2 $orderdir2";
    $issuesRS = db_recordset($sql);
    $num_issues = count($issuesRS);
?>

    <table width="100%" border="0" align="center" cellpadding="1" cellspacing="0" summary="<?php echo gettext('This table summarises the pending issues for this user.')?>">
    <thead>
<?php
    if ($closed == 'true') {
      // Do not allow sorting
?>
      <tr>
        <th align="left" class="issueheader">#</th>
        <th align="left" class="issueheader"><?php echo gettext('Resolved')?></th>
        <th align="left" class="issueheader"><?php echo gettext('Resolved By')?></th>
        <th align="left" class="issueheader"><?php echo gettext('Site')?></a></th>
        <th align="left" class="issueheader"><?php echo gettext('Summary')?></a></th>
        <th align="center" class="issueheader"><?php echo gettext('Priority')?></a></th>
      </tr>
<?php
    } else {
      // Allow sorting on each attribute
?>
      <tr>
        <th align="left" class="issueheader"><a href="myissues.php?closed=<?php echo $closed?>&amp;<?php echo makeordering2('id',$orderby2,$orderdir2)?>&amp;orderby=<?php echo $orderby?>&amp;orderdir=<?php echo $orderdir?>">#</a><?php echo makesymbol('id',$orderby2,$orderdir2)?></th>
        <th align="left" class="issueheader"><a href="myissues.php?closed=<?php echo $closed?>&amp;<?php echo makeordering2('createdon',$orderby2,$orderdir2)?>&amp;orderby=<?php echo $orderby?>&amp;orderdir=<?php echo $orderdir?>"><?php echo gettext('Reported')?></a><?php echo makesymbol('createdon',$orderby2,$orderdir2)?></th>
        <th align="left" class="issueheader"><a href="myissues.php?closed=<?php echo $closed?>&amp;<?php echo makeordering2('assignedtoname',$orderby2,$orderdir2)?>&amp;orderby=<?php echo $orderby?>&amp;orderdir=<?php echo $orderdir?>"><?php echo gettext('Assigned To')?></a><?php echo makesymbol('assignedtoname',$orderby2,$orderdir2)?></th>
        <th align="left" class="issueheader"><a href="myissues.php?closed=<?php echo $closed?>&amp;<?php echo makeordering2('sitename',$orderby2,$orderdir2)?>&amp;orderby=<?php echo $orderby?>&amp;orderdir=<?php echo $orderdir?>"><?php echo gettext('Site')?></a><?php echo makesymbol('sitename',$orderby2,$orderdir2)?></th>
        <th align="left" class="issueheader"><a href="myissues.php?closed=<?php echo $closed?>&amp;<?php echo makeordering2('summary',$orderby2,$orderdir2)?>&amp;orderby=<?php echo $orderby?>&amp;orderdir=<?php echo $orderdir?>"><?php echo gettext('Summary')?></a><?php echo makesymbol('summary',$orderby2,$orderdir2)?></th>
        <th align="center" class="issueheader"><a href="myissues.php?closed=<?php echo $closed?>&amp;<?php echo makeordering2('sortorder',$orderby2,$orderdir2)?>&amp;orderby=<?php echo $orderby?>&amp;orderdir=<?php echo $orderdir?>"><?php echo gettext('Priority')?></a><?php echo makesymbol('sortorder',$orderby2,$orderdir2)?></th>
      </tr>
<?php
    }
?>
    </thead>
<?php
    if ($num_issues == 0) {
      // If there are no issues, then say as much...
      print "      <tr>\n";
      if ($closed == 'true') {
        print "        <td colspan=\"6\" align=\"center\" class=\"issuecell\">".gettext('No Resolved Issues.')."</td>\n";
      } else {
        print "        <td colspan=\"6\" align=\"center\" class=\"issuecell\">".gettext('No Pending Issues.')."</td>\n";
      }
      print "      </tr>\n";
    }
    // Alternately shade the rows on the sorted attribute by keeping track
    // of the previously shown value. If this value != last value then the
    // colour can be changed

    // flag shows whether to shade or not - starts being 'NO SHADING'
    $flag = 1;
    // Nullify the last value
    $lastvalue = "\0";
    foreach ($issuesRS as $record) {
      if ($lastvalue != $record[$grouporderby2]) {
        // If different, toggle shading
        $flag = 1 - $flag;
        $lastvalue = $record[$grouporderby2];
      }
      if ($flag == 0) {
        $class = 'issuecell';
      } else {
        $class = 'issuecelldark';
      }

      // For the priority column, choose the colouring
      if ($closed == 'true' || $record['recalled'] == 1) {
        $priority = 'prioritydefunct';
      } else {
        switch ($record['class']) {
          case 0: $priority='prioritylow';
            break;
          case 1: $priority='prioritymedium';
            break;
          case 2: $priority='priorityhigh';
            break;
        }
      }

      // Show attributes
      print "      <tr valign=\"top\">\n";
      print "        <td class=\"$class\"><a href=\"issue.php?id=${record[id]}\">${record[id]}</a></td>\n";
      if ($closed == 'true') {
        print "          <td class=\"$class\">";
        if (strftime('%d/%m/%y',$record[closedon]) == strftime('%d/%m/%y')) {
          print strftime('%H:%M',$record[closedon]);
        } else {
          print strftime('%d/%m/%y',$record[closedon]);
        }
        print "</td>\n";
        print "        <td class=\"$class\">".$record[assignedtoname]."</td>\n";
      } else {
        print "          <td class=\"$class\">";
        if (strftime('%d/%m/%y',$record[createdon]) == strftime('%d/%m/%y')) {
          print strftime('%H:%M',$record[createdon]);
        } else {
          print strftime('%d/%m/%y',$record[createdon]);
        }
        print "</td>\n";
        if ($record[assignedtoname] == "* unassigned *") {
          print "        <td class=\"$class\">".gettext("* unassigned *")."</td>\n";
        } else {
          print "        <td class=\"$class\">".$record[assignedtoname]."</td>\n";
        }
      }
      print "        <td class=\"$class\">".$record[sitename]."</td>\n";
      print "        <td class=\"$class\"><a href=\"issue.php?id=${record[id]}\">".$record[summary]."</a></td>\n";
      print "        <td align=\"center\" class=\"$priority\">";
      if ($record['recalled'] == 1) {
        print gettext('Recalled');
      } else {
        if ($record[priorityname] == "Ungraded") {
          print gettext('Ungraded');
        } else {
          print $record[priorityname];
        }
      }
      print "</td>\n";
      print "      </tr>\n";

    }
?>
    </table>

<?php
    if ($closed == 'true') {
?>
    <h3><a href="myissues.php"><?php echo gettext('Open Issues')?></a></h3>

    <p><?php echo gettext('Please click here to see your <a href="myissues.php">open issues</a>.')?></p>
<?php
    } else {
?>
    <h3><a href="myissues.php?closed=true"><?php echo gettext('Closed Issues')?></a></h3>

    <p><?php echo gettext('Please click here to see your <a href="myissues.php?closed=true">closed issues</a>.')?></p>
<?php
    }
?>
  </div>

<?php
  }

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