<?php
/**
 * Customer Languages Report
 *
 * @category Report
 * @package  ClientExec
 * @author   Juan D. Bolívar <juan@clientexec.com>
 * @license  ClientExec License
 * @version  1.0
 * @link     http://www.clientexec.com
 *
 *************************************************
 *   1.0 Initial Report Released.  - Juan D. Bolívar
 ************************************************
 */

/**
 * Customer_Languages Report Class
 *
 * @category Report
 * @package  ClientExec
 * @author   Juan D. Bolívar <juan@clientexec.com>
 * @license  ClientExec License
 * @version  1.0
 * @link     http://www.clientexec.com
 */
class Customer_Languages extends Report
{

    var $m_hasgraph = true;
    var $m_graphType = "Pie2D.swf";
    var $friendlyName = 'Customer Languages';
    var $colorArray = array('#DFC329','#6699FF','#CC9933','#CC0033','#000099','#990033','#006600','#FFFF99','#FF6699');

    /**
     * Report Process Method
     *
     * @return null - direct output
     */
    function process()
    {
        // Set the report information
        $this->setName('Customer Languages');
        $this->setVersion("1.0");
        $this->SetDescription($this->user->lang('Displays total users using each language.'));

        //DETERMINE IF THIS IS A GRAPH CALL
        if (!get_magic_quotes_gpc()) {
            $graphdata = addslashes(@$_GET['graphdata']);
        } else {
            $graphdata = @$_GET['graphdata'];
        }

        //SQL to generate the the result set of the report
        $sql = "SELECT ucuf.`value`, "
              ."COUNT(ucuf.`value`) AS total "
              ."FROM `user_customuserfields` ucuf "
              ."WHERE ucuf.`customid` = (SELECT cuf.`id` "
              ."FROM `customuserfields` cuf "
              ."WHERE cuf.`name` = 'Language') "
              ."GROUP BY ucuf.`value` "
              ."ORDER BY COUNT(ucuf.`value`) DESC ";

        $result = $this->db->query($sql);

        $totalElements = 0;

        while(list($element, $total) = $result->fetch()) {
            $aGroup[$element] = array($this->user->lang($element), $total);
            $totalElements += $total;
        }

        if($graphdata && isset($aGroup)) {
            //this supports lazy loading and dynamic loading of graphs
            echo $this->GraphData($aGroup, $totalElements);
            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.
        $url = "index.php?noheaderfooter=1&graphdata=1&view=".$_REQUEST['view']."&fuse=".$_REQUEST['fuse']."&report=".urlencode($_REQUEST['report'])."&type=".urlencode($_REQUEST['type']);
        $this->ShowGraph(0,$url);
        
        if (isset($aGroup)) {
            $this->Add($aGroup,'',array($this->user->lang('Language'),$this->user->lang('Customers')));
            unset($aGroup);
        }
        
        //Display Report
        $this->Show();
    }

    /**
     * Function to output the xml for the graph data
     *
     * @return XML - graph data
     */
    function GraphData($aGroups, $totalElements) {

        // 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');

        $showborder="showborder='0'";
        $bgColor="bgColor='FFFFFF'";
        $title = "";
        $subcaption = "";

        if($this->viewingFromDashboard()) {
            //do not want to see title in dashboard since we already show from dropdown
            $legend="showLegend='0'";
            $labelstep="labelStep='3'";
            $clickurl="clickURL='index.php?fuse=reports&report=Customer+Languages&type=Accounts&view=ViewReport'";
        }else {
            $clickurl="";
            $legend="";
            $labelstep="";
        }

        $strXML  = "<chart ".$clickurl." ".$bgColor." ".$showborder." ".$labelstep." ".$legend." useRoundEdges='1' caption='".$title."' subcaption='".$subcaption."' xAxisName='' yAxisName='' yAxisMinValue='' numberPrefix='' showValues='0' alternateHGridColor='FCB541' alternateHGridAlpha='20' divLineColor='FCB541' divLineAlpha='50' canvasBorderColor='666666' baseFontColor='666666' lineColor='FCB541'>\n";

        //get largest so that we can slice it when not viewing from dashboard
        $largest=0;
        $largestIndex=0;
        $gIndex=0;
        foreach($aGroups as $group) {
            if($group[1] > $largest) {
                $largest = $group[1];
                $largestIndex = $gIndex;
            }
            $gIndex++;
        }

        $index=0;
        foreach($aGroups as $group) {
            $issliced = "";
            if ( ($index==$largestIndex) && (!$this->viewingFromDashboard()) ) {
                $issliced = "isSliced='1'";
            }
            $strXML .= "<set value='".$this->ReturnAverage($group[1], $totalElements)."' label='".$group[0]."' color='".$this->colorArray[$index]."' ".$issliced." />\n";
            $index++;
        }

        $strXML .= "</chart>\n";
        $this->logger->log(4,print_r("in graph data",true) );
        return $strXML;


    }

    /**
     * Function to return the average, used for making the pie chart
     *
     * @return num - average value
     */
    function ReturnAverage($count,$totalCount) {
        $avg = ($count/$totalCount);
        $avg =  $avg * 100;
        return ceil($avg);
    }

}
?>
