<?php

  /*

      mysitesissues.php

      My Sites Unresolved Issues. Shows the users' unresolved issues
      and allows sorting of the paged list. [Commented]

      Changelog:
        2006-01-14 dave: Cleaned up code for v1.0 release
        2005-07-03 arne_sf: Replaced all instances of column name 'user' for table tbl_UserSites with 'userid'
        
      ## 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'] == 'Client' || $_SESSION['_usertype'] == 'Root') {
    require_once 'system/security.php';
    ThrowOut();
  }

  // Page Title
  $ptitle = 'My Sites 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("mysitesissues");

  // Retrieve Get/Post variables
  $act = $_GET['act'];
  $orderby = $_GET['orderby'];
  $orderdir = $_GET['orderdir'];
  $page = $_GET['page'];

  // Default action: Show this user's issues
  if ($act == '') {
    if ($conf_db['dsn']['phptype'] == 'pgsql') {
      $is_pgsql = true;
    }
    // Set up sorting and grouping values for the unresolved issue list
    if ($orderby == '' && $_SESSION['_orderby-site'] != '') {
      // Use stored ordering unless another selection has been made
      $orderby = $_SESSION['_orderby-site'];
      $orderdir = $_SESSION['_orderdir-site'];
    }

    if ($orderby == '') {
      $grouporderby = 'id';
      $orderby = 'sortorder DESC,id DESC';
      $orderdir = '';
    } else {
      // Save ordering in session
      session_register('_orderby-site');
      session_register('_orderdir-site');
      $_SESSION['_orderby-site'] = $orderby;
      $_SESSION['_orderdir-site'] = $orderdir;
      $grouporderby = $orderby;
    }

    // Find the highest status (the terminal status) and treat it as closed
    $closedstatus = get_closed_status($_SESSION['_domain']);
    if ($is_pgsql) {
      $sql = "SELECT count(vw_Issues.id) AS num FROM vw_UserTypes,vw_Issues
              WHERE vw_UserTypes.id=" . $_SESSION['_id'] . "
                AND vw_UserTypes.usertypesortorder>0
                AND vw_Issues.site=vw_UserTypes.site
                AND vw_Issues.status<>" . $closedstatus . " AND vw_Issues.domain=" .
                  $_SESSION['_domain'];
    } else {
      $sql = "SELECT DISTINCT $aselects FROM $afrom
              WHERE tbl_UserSites.userid=".$_SESSION['_id']."
                AND tbl_UserTypes.sortorder >0
                AND tbl_Issues.status<>$closedstatus
                AND tbl_Issues.domain=".$_SESSION['_domain'];
    }
    $issuesRS = db_recordset($sql);

    // How many issues would be in this category
    if ($is_pgsql) {
      $num_issues = $issuesRS[0][num];
    } else {
      $num_issues = count($issuesRS);
    }
    // Set page size and limit issues to that number
    $pagesize = $global_prefs[pagesize];
    $num_pages = ceil($num_issues/$pagesize);
    if ($is_pgsql) {
      $sql = "SELECT DISTINCT vw_Issues.id, vw_Issues.createdon, vw_Issues.reportedbyname, vw_Issues.assignedto, vw_Issues.recalled, vw_Issues.assignedtoname, vw_Issues.sitename, vw_Issues.summary, vw_Issues.sortorder, vw_Issues.priorityname, vw_Issues.class
              FROM vw_UserTypes, vw_Issues
              WHERE vw_UserTypes.id=" . $_SESSION['_id'] . "
                AND vw_UserTypes.usertypesortorder>0
                AND vw_Issues.site=vw_UserTypes.site
                AND vw_Issues.status<>" . $closedstatus . "
                AND vw_Issues.domain=" . $_SESSION['_domain'] . "
              ORDER BY $orderby $orderdir";
    } else {
      $sql = "SELECT DISTINCT $aselects FROM $afrom
              WHERE tbl_UserSites.userid=".$_SESSION['_id']."
                AND tbl_UserTypes.sortorder >0
                AND tbl_Issues.status<>$closedstatus
                AND tbl_Issues.domain=".$_SESSION['_domain']."
                ORDER BY $orderby $orderdir";
    }
  
    if ($page == '') $page = 0;
    
    if ($page <= 0) {
      $page = 0;
    } elseif ($page >= $num_pages) {
      $page = $num_pages - 1;
    }
    $sql .= " LIMIT $pagesize OFFSET " . ($page * $pagesize);
    // Retrieve issues (no more than $global_prefs[pagesize])
    $issuesRS = db_recordset($sql);
?>

  <h1><?php echo gettext("My Sites' Issues")?></h1>
  <h2><?php echo gettext("Below is a list of all your sites' unresolved issues")?>:</h2>
  
  <div class="block">

    <table width="100%" border="0" align="center" cellpadding="1" cellspacing="0" summary="<?php echo gettext("This table summarises the unresolved issues for this user's sitestes.")?>">
      <thead>
        <tr>
          <th align="left" class="issueheader"><a href="mysitesissues.php?page=<?php echo $page?>&amp;<?php echo makeordering('id',$orderby,$orderdir)?>">#</a><?php echo makesymbol('id',$orderby,$orderdir)?></th>
          <th align="left" class="issueheader"><a href="mysitesissues.php?page=<?php echo $page?>&amp;<?php echo makeordering('createdon',$orderby,$orderdir)?>"><?php echo gettext("Reported")?></a><?php echo makesymbol('createdon',$orderby,$orderdir)?></th>
          <th align="left" class="issueheader"><a href="mysitesissues.php?page=<?php echo $page?>&amp;<?php echo makeordering('reportedbyname',$orderby,$orderdir)?>"><?php echo gettext("Reported By")?></a><?php echo makesymbol('reportedbyname',$orderby,$orderdir)?></th>
          <th align="left" class="issueheader"><a href="mysitesissues.php?page=<?php echo $page?>&amp;<?php echo makeordering('assignedtoname',$orderby,$orderdir)?>"><?php echo gettext("Assigned To")?></a><?php echo makesymbol('assignedtoname',$orderby,$orderdir)?></th>
          <th align="left" class="issueheader"><a href="mysitesissues.php?page=<?php echo $page?>&amp;<?php echo makeordering('sitename',$orderby,$orderdir)?>"><?php echo gettext("Site")?></a><?php echo makesymbol('sitename',$orderby,$orderdir)?></th>
          <th align="left" class="issueheader"><a href="mysitesissues.php?page=<?php echo $page?>&amp;<?php echo makeordering('summary',$orderby,$orderdir)?>"><?php echo gettext("Summary")?></a><?php echo makesymbol('summary',$orderby,$orderdir)?></th>
          <th align="center" class="issueheader"><a href="mysitesissues.php?page=<?php echo $page?>&amp;<?php echo makeordering('sortorder',$orderby,$orderdir)?>"><?php echo gettext("Priority")?></a><?php echo makesymbol('sortorder',$orderby,$orderdir)?></th>
        </tr>
      </thead>
<?php
    if ($num_issues > 0) {
?>
      <tfoot>
        <tr>
<td align="right" colspan="7"><?php printf(gettext("Page %s of %s : %s record(s)"), $page+1, $num_pages, $num_issues); ?></td>
        </tr>
<?php
    if ($num_pages > 1) {
      // If there is more than one page then show links to the other pages
?>
        <tr>
          <td align="right" colspan="7">
<?php
      $paging = '            ';
      $begin = $page-4;
      $end = $page+5;
      if ($begin < 0) $end -= $begin;
      if ($end >= $num_pages) $begin -= ($end - $num_pages);
      if ($begin < 0) $begin = 0;
      if ($end >= $num_pages) $end = $num_pages;
      for ($i=$begin;$i<$end;$i++) {
        if ($i != $begin) $paging .= ' &bull; ';
        if ($i != $page) {
          $paging .= '<a href="mysitesissues.php?page=' . $i . '&amp;orderby=' . $orderby . '&amp;orderdir=' . $orderdir . '" title="' .gettext("Goto page"). ' ' . ($i+1) . '">';
        }
        $paging .= $i+1;
        if ($i != $page) {
          $paging .= '</a>';
        }
      }
      if ($page > 0) {
        $paging .= ' &nbsp; <a href="mysitesissues.php?page=0&amp;orderby=' . $orderby . '&amp;orderdir=' . $orderdir . '" title="' .gettext("First"). '">|&larr;</a>';
        $paging .= ' <a href="mysitesissues.php?page=' . ($page-1) . '&amp;orderby=' . $orderby . '&amp;orderdir=' . $orderdir . '" title="' .gettext("Previous"). '">&larr;</a>';
      } else {
        $paging .= ' &nbsp; |&larr; &larr;';
      }
      if ($page < ($num_pages-1)) {
        $paging .= ' <a href="mysitesissues.php?page=' . ($page+1) . '&amp;orderby=' . $orderby . '&amp;orderdir=' . $orderdir . '" title="' .gettext("Next"). '">&rarr;</a>';
        $paging .= ' <a href="mysitesissues.php?page=' . ($num_pages-1) . '&amp;orderby=' . $orderby . '&amp;orderdir=' . $orderdir . '" title="' .gettext("Last"). '">&rarr;|</a>';
      } else {
        $paging .= ' &rarr; &rarr;|';
      }
      print $paging;
?>
          </td>
        </tr>
<?php
    }
?>
      </tfoot>
<?php
  }
?>
      <tbody>
<?php
  if ($num_issues == 0) {
    // If there are no issues, then say as much...
    print "        <tr>\n";
    print "          <td colspan=\"7\" align=\"center\" class=\"issuecell\">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';
    }

    // For the priority column, choose the colouring
    if ($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";
    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\">".preg_replace('/\* unassigned \*/',gettext('* unassigned *'),$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 {
      print preg_replace('/Ungraded/',gettext('Ungraded'),$record[priorityname]);
    }
    print "</td>\n";
    print "        </tr>\n";

  }
?>
      </tbody>
    </table>

  </div>

<?php
  }

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