<?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('ChartSalesHelper.php');


class MyChartsSalesDashlet extends Dashlet { 

	var $savedText; // users's saved text
    var $height = '300'; // height of the pad 
    var $firstLoad = 'yes';
    function MyChartsSalesDashlet($id, $def) {
  
		global $current_user, $mod_strings, $app_strings;
		require('modules/EcmCharts/Dashlets/MyChartsSalesDashlet/MyChartsSalesDashlet.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( !$options["title"] )
			$options["title"] = "Wykres sprzedaży z podziałem na kategorie/podkategorie";

        // if no custom title, use default
   		$this->title = $options["title"];
                
    }
	
	function display() {
		global $current_user, $mod_strings, $app_strings;

		$options = $this->loadOptions();

		// Data od
		if(!$options['date_from'])
			$options['date_from'] = date("01.m.Y");

		// Data do
		if(!$options['date_to'])
			$options['date_to'] = date("d.m.Y");

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

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

		if(!$options['detail'])
			$options['detail'] = "category";

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

		if(!$options["title"])
			$options["title"] = "Wykres sprzedaży z podziałem na kategorie/podkategorie";

		$optionsForComparativeData = $options;
		$optionsForComparativeData["date_from"] = date("Y-m-d",strtotime(date("Y-m-d", strtotime($optionsForComparativeData["date_from"]))." -1 year"));
		$optionsForComparativeData["date_to"] = date("Y-m-d",strtotime(date("Y-m-d", strtotime($optionsForComparativeData["date_to"]))." -1 year"));

		$db_connection_handler = $GLOBALS["db"];
		
		/*
		 * DATA
		 */
		$cd = null;

		if( $options["comparativeData"] == "enabled")
		{
			$comparativeData = new ChartSalesHelper( $db_connection_handler, $optionsForComparativeData);
			if( $options["detail"] == 'category')
			{
				$cd = $comparativeData->getAllCategories();
			} else {
				$cd = $comparativeData->getAllSubcategories();
			}
		}

		$helper = new ChartSalesHelper( $db_connection_handler, $options, $cd );
		$data = $helper->renderGoogleChartOptions();
		
		/*
		 * SMARTY
		 */
        $smarty = new Sugar_Smarty();
		//$ss->assign('account_id',$optionsArray['account_id']);
        $smarty->assign('id', $this->id);
        $smarty->assign('height', $this->height);
        $smarty->assign('date_from', $options["date_from"]);
        $smarty->assign('date_to', $options["date_to"]);
        $smarty->assign('chartOptions', $data);
        $smarty->assign('detail', $options["detail"]);
        $smarty->assign('firstLoad', $this->firstLoad);
        $smarty->assign('comparativeData', $options['comparativeData']);
        if( $helper->haveComparativeData() )
        	$smarty->assign('comparative_data_on', 'true');
        else
        	$smarty->assign('comparative_data_on', 'false');
        $smarty->assign('chartType', $options['chartType']);
        $smarty->assign('LANG', $mod_strings);


        // Pobieranie widoku
        $output = $smarty->fetch('modules/EcmCharts/Dashlets/MyChartsSalesDashlet/MyChartsSalesDashlet.tpl');   
        // return parent::display for title and smarty template
        return parent::display($this->dashletStrings['LBL_DBLCLICK_HELP']) . $output; 
    }
	
	function displayOptions() {
	    global $current_user, $mod_strings, $app_strings;
	    // format daty	     
		$smarty = new Sugar_Smarty();
		// Pobieram ustawienia
		$options = $this->loadOptions();
		
		// Data od
		if(!$options['date_from'])
			$options['date_from'] = date("01.m.Y");

		// Data do
		if(!$options['date_to'])
			$options['date_to'] = date("d.m.Y");

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

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

		if(!$options['detail'])
			$options['detail'] = "category";

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

		if(!$options["title"])
			$options["title"] = "Wykres sprzedaży z podziałem na kategorie/podkategorie";

		// Format daty
		$Calendar_daFormat = str_replace("d","%d",str_replace("m","%m",str_replace("Y","%Y",$GLOBALS['timedate']->get_date_format())));
		$smarty->assign("dateFormat", $Calendar_daFormat);
		//The id must be assigned in all dashlet options pages
		$smarty->assign('id', $this->id); 
		$smarty->assign('date_from', $options['date_from']);
		$smarty->assign('date_to', $options['date_to']);
		// Sczegółowość wykresu: category, subcategory
		$smarty->assign('detail', $options['detail']);
		// Typ dokumentu: all, normal, correct
		$smarty->assign('type', $options['type']);
		// Pokazywać dane porównawcze? Tak: enabled, nie: disabled
		$smarty->assign('comparativeData', $options['comparativeData']);
		// Typ wykresu
		$smarty->assign('chartType', $options['chartType']);
		// Tytuł
		$smarty->assign('title', $options['title']);
		// Lang
		$smarty->assign('LANG', $mod_strings);		
		// Przekazuję widok opcji do metody displayOptions()
		return parent::displayOptions() . $smarty->fetch('modules/EcmCharts/Dashlets/MyChartsSalesDashlet/MyChartsSalesDashletOptions.tpl');
		
	}
	
	// Zapisywanie opcji dashletu
	function saveOptions($req) {
		$options = array();
		$options["date_from"] = $req["date_from"];
		$options["date_to"] = $req["date_to"];
		$options["detail"] = $req["detail"];
		$options["comparativeData"] = $req["comparativeData"];
		$options["type"] = $req["type"];
		$options["chartType"] = $req["chartType"];
		$options["title"] = $req["title"];

		return $options;
    }
}
?>