<?php

  /*

      recent.php

      Recent Knowledge Base Entries page.

      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 Title
  $ptitle = 'Recent Issues';
  
  // Includes
  require_once 'system/db.php';
  require_once 'system/lang.php';
  require_once 'system/user_preferences.php';
  require_once 'system/global_preferences.php';
  require 'header.php';

  // Set Language
  set_text_domain("recent");

  // How many recent items to display (max is defined in system preferences)
  if (isset($_REQUEST['amount'])) {
    $amount=$_REQUEST['amount'];
    if ($amount > $global_prefs['pagesize']) {
        $amount = $global_prefs['pagesize'];
    }
  } else {
    $amount=10;
  }

  $sql = "SELECT tbl_Issues.id, tbl_Issues.publishedon, tbl_Issues.summary, tbl_Users.name AS reportedbyname, tbl_Sites.site AS sitename FROM tbl_Issues, tbl_Users, tbl_Sites WHERE published=1 AND tbl_Sites.domain=$_SESSION[_domain] AND tbl_Issues.site=tbl_Sites.id AND tbl_Issues.reportedby=tbl_Users.id ORDER BY publishedon DESC LIMIT $amount";
  $issuesRS = db_recordset($sql);

  // Do we display the sites/usernames?
  $user_prefs = get_user_prefs($_SESSION['_id']);
  $hidesu = ($user_prefs['pub-hide-su'] == 'true');
?>

  <h1><?php echo gettext("Most Recent")?></h1>
  <h2><?php echo gettext("Below is a list of the most recently published issues.")?></h2>
  
  <div class="block">
    <table width="100%" border="0" align="center" cellpadding="1" cellspacing="0"
        summary="<?php echo gettext("This table lists the most recent issues.")?>">
    <thead>
      <tr>
        <th align="left" class="issueheader">#</th>
        <th align="left" class="issueheader"><?php echo gettext("Published")?></th>
<?php
    if (!$hidesu) {
?>
        <th align="left" class="issueheader"><?php echo gettext("Reported By")?></th>
        <th align="left" class="issueheader"><?php echo gettext("Site")?></a></th>
<?php
    }
?>
        <th align="left" class="issueheader"><?php echo gettext("Summary")?></a></th>
      </tr>
    </thead>
    <tbody>
<?php
    if (count($issuesRS) == 0) {
      print "      <tr>\n";
      print "        <td colspan=\"5\" align=\"center\" class=\"issuecell\">".gettext("No knowledgebase articles have been published.")."</td>\n";
      print "      </tr>";
    } else {
      // 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['id']) {
          // If different, toggle shading
          $flag = 1 - $flag;
          $lastvalue = $record['id'];
        }
        if ($flag == 0) {
          $class = 'issuecell';
        } else {
          $class = 'issuecelldark';
        }

        // 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[publishedon]) == strftime('%d/%m/%y')) {
          print strftime('%H:%M',$record[publishedon]);
        } else {
          print strftime('%d/%m/%y',$record[publishedon]);
        }
        print "</td>\n";
        if (!$hidesu) {
          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 "      </tr>\n";
      }
    }
?>
    </tbody>
    </table>
  </div>
<?php

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