<?php
/*
 * @package Reports
 */
class Customer_Status extends Report {
	var $m_hasgraph = true;
	var $m_graphType = 'Bar2D.swf';
	var $friendlyName = 'Customer Status';
	
	protected function _getStatusList () {
        $statusList = array();
        $statusList[] = array(1, $this->user->lang('Active'));
        $statusList[] = array(0, $this->user->lang('Pending'));
        $statusList[] = array(-1, $this->user->lang('Suspended'));
        $statusList[] = array(-2, $this->user->lang('Cancelled'));
        $statusList[] = array(-3, $this->user->lang('Fraud'));
        
        return $statusList;
	}
	
	protected function _getTotal($status, $totals) {
		return isset($totals[$status])? $totals[$status] : 0;
	}
	
	public function GraphData ($totals) {
        // These headers must be set or IE will show an error.
        // IE + SSL = BAD
        // http://www.fusioncharts.com/KB/article.aspx?id=10091
        header('Cache-Control: cache, must-revalidate');
        header('Pragma: public');
        
        $xmlOutput = '';
        $xmlOutput .= '<chart caption="" yAxisName="" xAxisName="" showPlotBorder="1" plotBorderColor="666666" plotFillAlpha="90" plotBorderThickness="1" plotFillRatio="100" useRoundEdges="0" showLegend="1" bgColor="FFFFFF" labelStep="1" chartLeftMargin="0" yAxisValuesPadding="3" legendShadow="0" legendBorderAlpha="30" canvasPadding="5" chartTopMargin="20" canvasBorderAlpha="0" showShadow="0" canvasBorderThickness="0" showBorder="0" borderThickness="0" lineThickness="3" anchorBorderThickness="3" xAxisName="" yAxisName="" yAxisMinValue="" numberPrefix="" showValues="0" alternateHGridColor="dfdfdf" alternateHGridAlpha="20" divLineColor="dddddd" divLineAlpha="50" canvasBorderColor="666666" baseFontColor="666666" lineColor="FCB541">';
        
        $colorIndex = 0;
        $colorArray = array('#6699FF','#DFC329','#CC9933','#CC0033','#000099','#990033','#006600','#FFFF99','#FF6699');
        
        $statusList = $this->_getStatusList();
        
        foreach ($statusList as $row) {
            $label = $row[1];
            $value = $this->_getTotal($row[0], $totals);
            $color = $colorArray[$colorIndex];
            
            $xmlOutput .= "<set label=\"{$label}\" value=\"{$value}\" color=\"{$color}\" />";
            $colorIndex++;
        }
        
        $xmlOutput .= '<styles>';
        $xmlOutput .= '<definition>';
        $xmlOutput .= '    <style name="myToolTipFont" borderColor="dddddd" leftMargin="20" type="font" font="Verdana" size="10" color="333333"/>';
        $xmlOutput .= '    <style name="myLegendFont" type="font" font="Verdana" size="10" color="333333" bold="0" />';
        $xmlOutput .= '</definition>';
        $xmlOutput .= '<application>';
        $xmlOutput .= '    <apply toObject="ToolTip" styles="myToolTipFont" />';
        $xmlOutput .= '    <apply toObject="Legend" styles="myLegendFont" />';
        $xmlOutput .= '</application>';
        $xmlOutput .= '</styles>';
        $xmlOutput .= '</chart>';
        
        return $xmlOutput;
	}
	
	public function process () {
		// Set the report information
        $this->setName('Customer by Status', true);
        $this->setVersion("1.1");
        $this->SetDescription($this->user->lang('Displays the customer status.'));
        
	    // DETERMINE IF THIS IS A GRAPH CALL
        if (!get_magic_quotes_gpc()) {
            $graphdata = addslashes(@$_GET['graphdata']);
        } else {
            $graphdata = @$_GET['graphdata'];
        }
        
        $query = "SELECT COUNT(*) as total, status FROM users WHERE groupid=1 GROUP BY status";
        $result = $this->db->query($query);
        
		$totals = array();
        
        while ($row = $result->fetch()) {
        	$totals[$row['status']] = $row['total'];
        }
        
        $statusList = $this->_getStatusList();
        
        if ($graphdata) {
            //this supports lazy loading and dynamic loading of graphs
            echo $this->GraphData($totals);
            exit;
        }
        
        // Output the headers before anything else
        $this->showReportHeader();
        
        // create a lazy loading chart so that our showgraph will call ajax
        // call to retrieve chart and populate it after load.  This should
        // increase performance.
        $view = $_REQUEST['view'];
        $fuse = $_REQUEST['fuse'];
        $report = urlencode($_REQUEST['report']);
        $type = urlencode($_REQUEST['type']);
        $url = "index.php?noheaderfooter=1&graphdata=1&view={$view}&fuse={$fuse}&report={$report}&type={$type}";
        $this->ShowGraph(0, $url);
        
        $aGroup = array();
        $aLabel = array($this->user->lang('Status'), $this->user->lang('Count'));
        
        foreach ($statusList as $row) {
        	$label = $row[1];
            $value = $this->_getTotal($row[0], $totals);
            
            $group = array($label, $value);
            
            $aGroup[] = $group;
        }
        
        $this->Add($aGroup, null, $aLabel);
        
        echo "<br/><br/>";
        $this->Show();
	}
}