<?php
require_once 'modules/reports/models/Reports.php';    
require_once 'modules/reports/models/Report.php';
require_once 'modules/admin/models/DashboardPlugin.php';

class PluginReportgraphs extends DashboardPlugin
{
    var $hasTab = false;
    var $hasPanel = true;
    var $name = "Report Graphs";
    var $description = /*T*/"This plugin adds a dashboard panel displaying various graphs from the reports module."/*/T*/;
    var $default = true;

    function getTab()
    {
        return '';
    }
    
    function getPanel()
    {
        $this->tpl->define(array('graphs' => '../../plugins/dashboard/reportgraphs/PluginReportgraphs.tpl'));
        
        $reports = new Reports();
        $reportNames = $reports->getDashboardReports($this->user);    
        $lastName = false;
        
        for ($i = 0; $i < count($reportNames); $i++) {
            // Sometimes the reports are stored in the database with all lower case characters
            list($class, $type) = explode('-', $reportNames[$i]);
            $class = str_replace(' ', '_', ucwords(str_replace('_', ' ', $class)));
            $type = ucfirst($type);
            $reportNames[$i] = $class.'-'.$type;                
            if (is_file(dirname(__FILE__)."/../../reports/".$type."/".$class.".php")) {
                require_once dirname(__FILE__)."/../../reports/".$type."/".$class.".php";
                $report = new $class();
                if (!$report->companySupportsReport()) {
                    continue;
                }
                
                $friendlyName = str_replace("_", " ", $class);
                if ($reportNames[$i] == $this->user->getDashboardLastUsedGraph()) {
                    $selected = ' selected="selected"';
                } else {
                    $selected = '';
                }                                
                $this->tpl->setKey(array(
                        'option_value'      => $report->getGraphType()."-".$reportNames[$i],
                        'option_selected'   => $selected,
                        'option_name'       => $friendlyName
                ));
                $this->tpl->parse('graph_option_out', '.graph_option');
                $lastName = $reportNames[$i];
            }
        }
        
        if (!in_array($this->user->getDashboardLastUsedGraph(), $reportNames) && $lastName) {
            $this->user->setDashboardLastUsedGraph($lastName);
            $this->user->save();
        }
        $lastUsed = explode("-", $this->user->getDashboardLastUsedGraph());
        
        //check to see if report exists
        if (is_file(dirname(__FILE__)."/../../reports/".$lastUsed[1]."/".$lastUsed[0].".php")) {
            //get last report shown and get url to pass with lazy loading option
            $url = "index.php?fuse=reports&indashboard=1&noheaderfooter=1&view=ViewReport&report=".$lastUsed[0]."&type=".$lastUsed[1]."&graphdata=1&nolog=1";
        
            require_once dirname(__FILE__)."/../../reports/".$lastUsed[1]."/".$lastUsed[0].".php";
            $report = new $lastUsed[0]();
            if (!$report->companySupportsReport()) {
        	    $this->tpl->assign('current_graph', $this->user->lang('You have no permission to see this graph'));
            } else {
                $report->setName($lastUsed[0]."-".$lastUsed[1].".php");
                $report->setUser($this->user);
                $report->PopulateSettings();
                ob_start();
                $report->ShowGraph(1,$url,"center");
                $this->tpl->assign('current_graph', ob_get_contents());
            }
        	ob_end_clean();
        }
        
        $this->tpl->parse('graphs_out', 'graphs');
        return $this->tpl->fetch('graphs_out');
    }
}

?>
