<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

require_once('include/Dashlets/Dashlet.php');
require_once('include/Sugar_Smarty.php');
require_once('ChartAnnualSalesHelper.php');


class MyChartsAnnualSalesDashlet extends Dashlet { 

	var $savedText; // users's saved text
    var $height = '300'; // height of the pad 
    var $firstLoad = 'yes';
    function MyChartsAnnualSalesDashlet($id, $def = null) {
  
		global $current_user, $mod_strings, $app_strings;
		require('modules/EcmCharts/Dashlets/MyChartsAnnualSalesDashlet/MyChartsAnnualSalesDashlet.meta.php');
		require('modules/EcmCharts/language/pl_pl.lang.php');
        parent::Dashlet($id); // call parent constructor   
        
        $this->isConfigurable = true; // dashlet is configurable
        $this->hasScript = false;  // dashlet has java ipt attached to it
        $options = $this->loadOptions();

        // if no custom title, use default
        if(!$options["title"]) 
        	$options["title"] = "Wykres sprzedaży rocznej z podziałem na miesiące";
        
        $this->title = $options["title"];        

    }
	
	function display() {
		global $current_user, $mod_strings, $app_strings;

		$options = $this->loadOptions();
		$comparativeData = null;
		$options2 = null;
		$db_connection_handler = $GLOBALS["db"];
		$options = $this->loadOptions();
		// Data od
		if(!$options['year'])
			$options['year'] = date("Y");

		if(!$options["comparativeData"])
			$options["comparativeData"] = 'disabled';

		if(!$options["type"])
			$options["type"] = '%';

		if(!$options["chartType"])
			$options["chartType"] = 'column';

		if( $options["comparativeData"] == 'enabled')
		{
			$options2 = $this->loadOptions();
			$options2["year"] -= 1;
			$helper2 = new ChartAnnualSalesHelper($db_connection_handler, $options2, $mod_strings);
			$helper2->getChartOptions();
			// Pobieram dane sprzed roku
			$comparativeData = $helper2->getMonthsArray();
			$helper = new ChartAnnualSalesHelper($db_connection_handler, $options, $mod_strings, $comparativeData);
			$data = $helper->getChartOptions();
		} else {
			$helper = new ChartAnnualSalesHelper($db_connection_handler, $options, $mod_strings);
			$data = $helper->getChartOptions();
		}

		/*
		 * SMARTY
		 */
        $smarty = new Sugar_Smarty();
        $smarty->assign('chartOptions', $data);
        $smarty->assign('comparativeData', $options['comparativeData']);
        $smarty->assign('year', $options["year"]);
        $smarty->assign('chartType', $options["chartType"]);
        $smarty->assign('LANG', $mod_strings);
        if( $helper->haveComparativeData() )
        	$smarty->assign('comparative_data_on', 'true');
        else
        	$smarty->assign('comparative_data_on', 'false');
        // Pobieranie widoku
        $output = $smarty->fetch('modules/EcmCharts/Dashlets/MyChartsAnnualSalesDashlet/MyChartsAnnualSalesDashlet.tpl');   
        // return parent::display for title and smarty template
        return parent::display($this->dashletStrings['LBL_DBLCLICK_HELP']) . $output; 
    }
	
	function displayOptions() {
	    global $mod_strings,$current_user;
	    // format daty	     
		$smarty = new Sugar_Smarty();
		// Pobieram ustawienia
		$options = $this->loadOptions();
		// Data od
		if(!$options['year'])
			$options['year'] = date("Y");

		if(!$options["comparativeData"])
			$options["comparativeData"] = 'disabled';

		if(!$options["type"])
			$options["type"] = '%';

		if(!$options["chartType"])
			$options["chartType"] = 'column';

		if(!$options["title"])
			$options["title"] = "Raport sprzedaży w roku z podziałem na miesiące";

		// Generowanie lat do selektora
		$y = range(2000, date("Y"));
		$years = array_reverse($y,true);

		//The id must be assigned in all dashlet options pages
		$smarty->assign('id', $this->id); 
		// Typ dokumentu: all, normal, correct
		$smarty->assign('type', $options['type']);
		$smarty->assign('year', $options['year']);
		$smarty->assign('contractor_id', $options["contractor_id"]);;
		$smarty->assign('contractor_name', $options["contractor_name"]);
		$smarty->assign('chartType', $options["chartType"]);
		$smarty->assign('years', $years);
		$smarty->assign('LANG', $mod_strings);
		$smarty->assign('title', $options["title"]);
		// Pokazywać dane porównawcze? Tak: enabled, nie: disabled
		$smarty->assign('comparativeData', $options['comparativeData']);
		
		// Przekazuję widok opcji do metody displayOptions()
		return parent::displayOptions() . $smarty->fetch('modules/EcmCharts/Dashlets/MyChartsAnnualSalesDashlet/MyChartsAnnualSalesDashletOptions.tpl');	
	}
	
	// Zapisywanie opcji dashletu
	function saveOptions($req) {
		$options = array();
		$options["year"] = $req["year"];
		$options["comparativeData"] = $req["comparativeData"];
		$options["type"] = $req["type"];
		$options["chartType"] = $req["chartType"];
		$options["title"] = $req["title"];

		if( $req["account_name"] == '' )
		{
			$options["contractor_name"] = '';
			$options["contractor_id"] = '';
		} else {
			$options["contractor_name"] = $req['account_name'];
			$options["contractor_id"] = $req['account_id'];
		}
		return $options;
    }
}
?>