<?php
/**
 * Latest KB Articles Added Report
 *
 * @category Report
 * @package  ClientExec
 * @author   Jason Yates <jason@clientexec.com>
 * @license  ClientExec License
 * @version  1.1
 * @link     http://www.clientexec.com
 *
 *************************************************
 *   1.1 Updated the report to use Pear Commenting & the new title handing to make app reports consistent.
 ************************************************
 */



/**
 * Latest_Articles_Added Report Class
 *
 * @category Report
 * @package  ClientExec
 * @author   Jason Yates <jason@clientexec.com>
 * @license  ClientExec License
 * @version  1.1
 * @link     http://www.clientexec.com
 */
class Latest_Articles_Added extends Report
{

    /**
     * Report Process Method
     *
     * @return null - direct output
     */
    function process()
    {

        // Set the report information
        $this->setName('Latest Knowledgebase Articles Added', true);
        $this->setVersion("1.1");
        $this->SetDescription($this->user->lang('Displays a list of the latest knowledgebase articles that have been added to ClientExec.'));

        // Output the headers before anything else
        $this->showReportHeader();
        ?>
<script>
    function viewLastestArticlesAddedSelected()
    {
        var maxItemsPerPage = document.getElementById('maxItemsPerPage').value;
        location.href='index.php?fuse=reports&view=ViewReport&report=Latest+Articles+Added&type=Knowledgebase&maxItemsPerPage=' + maxItemsPerPage;
    }
</script>

        <?php
        $aGroup = array();
        echo "<br>".$this->user->lang('Display:').' ';
        echo "<select id=maxItemsPerPage name=maxItemsPerPage onChange='viewLastestArticlesAddedSelected()'>";
        $array = array(10,20,50,70,100);
        for ($i = 0; $i < count($array); $i++) {
            if (isset($_REQUEST['maxItemsPerPage']) && ($_REQUEST['maxItemsPerPage'] == $array[$i])) {
                echo"<option value=".$array[$i]." selected>".$array[$i]."</option>";
            }else {
                echo"<option value=".$array[$i].">".$array[$i]."</option>";
            }
        }
        echo "</select><br /><br />";

        if (isset($_REQUEST['maxItemsPerPage'])) {
            $reportSQL = "SELECT id, title, UNIX_TIMESTAMP(created), rating, totalvisitors FROM  kb_articles ORDER BY created DESC LIMIT 0,".$_REQUEST['maxItemsPerPage'];
        }else {
            $reportSQL = "SELECT id, title, UNIX_TIMESTAMP(created), rating, totalvisitors FROM  kb_articles ORDER BY created DESC LIMIT 0,10";
        }

        $result = $this->db->query($reportSQL);
        while (list($id, $title, $created, $rating, $visitors) = $result->fetch()) {
            //$id of article for future links
            $aGroup[] = array("&nbsp;".$title,
                    $this->getDateDifference(NE_Lib::date_diff_hrs(date('Y-m-d H:i:s',$created),date('Y-m-d H:i:s')),$this->user),
                    '<center>'.$rating.'</center>',
                    '<center>'.$visitors.'</center>',
            );
        }
        $this->Add($aGroup,
                $this->user->lang('Latest Articles Added'),
                array ('<center>'.$this->user->lang('Article Title').'</center>',
                '<center>'.$this->user->lang('Date Created').'</center>',
                '<center>'.$this->user->lang('Article Rating').'</center>',
                '<center>'.$this->user->lang('Total Visitors').'</center>'
        ));
        $this->Show();
    }

    function convertDate($date)
    {
        $date = date("F j, Y", strtotime($date));
        return $date;
    }

    function getDateDifference(&$datearray, $user, $returnTimestamp=false)
    {
        $returnString = "";
        $tWeeks = 0;
        $tDays = 0;
        $tHours = 0;
        if ($datearray['h'] > 0) {
            $tHours += abs($datearray['h']);
            if($tHours >= 24) {
                $tDays = intval($tHours / 24);
                $tHours -= $tDays*24;
                if($tDays >= 7) {
                    $tWeeks = intval($tDays / 7);
                    $tDays -= $tWeeks*7;
                }
            }
        }

        if($tWeeks > 0) {
            if($tWeeks > 1) {
                $returnString .= $tWeeks. ' '.$user->lang('weeks') . ' ';
            } else {
                $returnString .= $tWeeks.' '.$user->lang('week') . ' ';
            }
        }

        if($tDays > 0) {
            if($tDays > 1) {
                $returnString .= $tDays.' '.$user->lang('days') . ' ';
            } else {
                $returnString .= $tDays.' '.$user->lang('day') . ' ';
            }
        }

        if ($tHours > 0) {
            if ($tHours > 1) {
                $returnString .= $tHours.' '.$user->lang('hrs').' ';
            } else {
                $returnString .= ' 1 '.$user->lang('hr').' ';
            }
            if ($datearray['m']==1) {
                $returnString .= ' '.abs($datearray['m']).' min ';
            } else {
                $returnString .= ' '.abs($datearray['m']).' mins ';
            }
        } else {
            if ($datearray['m']==1) {
                $returnString .= abs($datearray['m']).' min ';
            } else {
                $returnString .= abs($datearray['m']).' mins ';
            }
        }

        return $returnString;
    }
}
?>
