<?php

Goweb::loadClass("Goweb_Chart_Set");
Goweb::loadClass("Goweb_Chart_Categories");
Goweb::loadClass("Goweb_Chart_Dataset");
Goweb::loadClass("Goweb_Chart_TrendLine");
Goweb::loadClass("Goweb_Chart_Styles");

/**
 * Geração de gráficos com o Fusion Charts
 *
 * Chart
 *
 * @version    5.0.0
 * @category   Goweb
 * @package    Goweb_Chart
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2009 (c) Goweb (http://www.goweb.pt)
 *
 */
class Goweb_Chart {
    
    // ------------------------------------------------ //
    // PROPERTIES -------------------------- PROPERTIES //
    // ------------------------------------------------ //
    private $_chart;
    private $_options = array();
    private $_chartElement;
    private $_output = "";
    
    public $set        = null;
    public $categories = null;
    public $dataset    = null;
    public $trendLine  = null;
    public $style      = null;
    
    // ------------------------------------------------ //
    // METHODS -------------------------------- METHODS //
    // ------------------------------------------------ //
    public function __construct() {
        $this->_chart = array();
        $this->_chartElement = ""; 
        
        $this->_options = array('animation', 'palette', 'showLabels', 'labelDisplay', 'xAxisName', 'yAxisName',
                                'rotateLabels', 'slantLabels', 'labelStep', 'staggerLines', 'numberPrefix', 'shadowAlpha', 
                                'showValues', 'rotateValues', 'placeValuesInside', 'showYAxisValues', 'anchorRadius', 'decimals',
                                'showLimits', 'showDivLineValues', 'numDivLines', 'yAxisValuesStep', 'showShadow', 'yAxisValuesPadding',
                                'divLineColor', 'divLineThickness', 'divLineAlpha', 'divLineIsDashed', 'divLineDashLen', 'divLineDashGap', 'zeroPlaneColor', 'zeroPlaneThickness',
                                'adjustDiv', 'rotateYAxisName', 'yAxisNameWidth', 'clickURL',
                                'maxColWidth', 'use3DLighting', 'defaultAnimation', 'yAxisMinValue',
                                'yAxisMaxValue', 'setAdaptiveYMin', 'caption', 'subcaption', 'formatNumberScale',
                                'bgColor', 'bgAlpha', 'showBorder', 'borderColor', 'borderThickness', 'borderAlpha', 'bgRatio', 'bgAngle',
                                'canvasbgColor', 'canvasbgAlpha', 'canvasBorderColor', 'canvasBorderThickness', 'canvasBorderAlpha', 'canvasBgRatio', 'canvasBgAngle', 'showCanvasBg', 'showCanvasBase', 'canvasBaseDepth', 'canvasBgDepth');
        
        $this->set        = new Goweb_Chart_Set();
        $this->categories = new Goweb_Chart_Categories();
        $this->dataset    = new Goweb_Chart_Dataset();
        $this->style      = new Goweb_Chart_Styles();
        $this->trendLine  = new Goweb_Chart_TrendLine();
    }
    
    protected function _open() {
        $chartInput = '';
        $chartInput .= '<chart ';
        foreach ($this->_chart as $key => $value) {
            $chartInput .= $key . "='" . $value . "' ";
        }
        $chartInput .= '>';
        return $chartInput;
    }
    
    public function render() {
        $this->_output = "";
        $this->_output .= $this->_open();
        
        $this->_output .= $this->set->render();
        $this->_output .= $this->categories->render();
        $this->_output .= $this->dataset->render();
        $this->_output .= $this->trendLine->render();
        $this->_output .= $this->style->render();
        
        $this->_output .= $this->_close();
        
        return $this->_output;
    }
    
    private function _close() {
        return '</chart>';
    }
    
    // renderChartHTML function renders the HTML code for the JavaScript. This
    // method does NOT embed the chart using JavaScript class. Instead, it uses
    // direct HTML embedding. So, if you see the charts on IE 6 (or above), you'll
    // see the "Click to activate..." message on the chart.
    // $chartSWF - SWF File Name of the chart which you intend to plot
    // $strXML - If you intend to use dataXML method for this chart, pass the XML data as this parameter. Else, set it to "" (in case of dataURL method)
    // $chartId - Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id.
    // $chartWidth - Intended width for the chart (in pixels)
    // $chartHeight - Intended height for the chart (in pixels)
    // $debugMode - Whether to start the chart in debug mode
    function renderChartHTML($chartSWF, $strXML, $chartId, $chartWidth = 600, $chartHeight = 275, $debugMode = 0) {
        $baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
        $baseUrl = str_replace(array('/index.php', '/admin'), '', $baseUrl);
        $chartSWF = $baseUrl . '/admin/media/charts/' . $chartSWF;
        
        // Generate the FlashVars string based on whether dataURL has been provided
        // or dataXML.
        
        $strFlashVars = "&chartWidth=" . $chartWidth . "&chartHeight=" . $chartHeight . "&debugMode=" . $debugMode;
        //DataXML Mode
        $strFlashVars .= "&dataXML=" . $strXML;
        
        $HTML_chart = "
        <!-- START Code Block for Chart $chartId -->
        <object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width='" . $chartWidth . "' height='" . $chartHeight . "' id='" . $chartId . "'>
        <param name=\"allowScriptAccess\" value=\"always\" />
        <param name=\"movie\" value='" . $chartSWF . "'/>		
        <param name=\"FlashVars\" value=\"" . $strFlashVars . "\" />
        <param name=\"quality\" value=\"high\" />
        <embed src='" . $chartSWF . "' FlashVars=\"" . $strFlashVars . "\" quality=\"high\" width=" . $chartWidth . " height=" . $chartHeight . " name=" . $chartId . " allowScriptAccess=\"always\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />
        </object>
        <!-- END Code Block for Chart $chartId -->
        ";
        
        return $HTML_chart;
    }
    
    protected function __set($key, $value) {
        if (in_array($key, $this->_options)) {
            $this->_chart[$key] = $value;
        }
    }

}