<?php
/*
 *
 * @package		ARI Smart Content
 * @author		ARI Soft
 * @copyright	Copyright (c) 2011 www.ari-soft.com. All rights reserved
 * @license		GNU/GPL (http://www.gnu.org/copyleft/gpl.html)
 * 
 */

defined('_JEXEC') or die;

require_once JPATH_ADMINISTRATOR . '/components/com_arismartcontent/kernel/class.AriKernel.php';

AriKernel::import('Parameters.ParametersHelper');
AriKernel::import('Application.ARISmartContent.Utils.Widget');
AriKernel::import('Web.JSON.JSON');
AriKernel::import('Document.DocumentHelper');

class AriSmartContentWidgetOrgGChart extends AriSmartContentWidget
{
	var $_params;
	var $_id;

	function __construct($id, $params)
	{
		$this->_id = $id;
		$this->_params = AriParametersHelper::flatParametersToArray($params);
	}
	
	function getId()
	{
		return $this->_id;
	}
	
	function loadAssets()
	{
		static $loaded;

		if ($loaded)
			return ;
		
		$baseUri = JURI::root(true) . '/media/arismartcontent/orggchart/';
		$jsUri = $baseUri . 'js/';
		$cssUri = $baseUri . 'css/';
		$params = $this->_params;
		
		$doc = JFactory::getDocument();

		AriDocumentHelper::includeJsFile('//www.google.com/jsapi');
		AriDocumentHelper::includeCssFile($cssUri . 'style.css');
		AriDocumentHelper::includeJsFile($jsUri . 'orggchart.js');
		$doc->addScriptDeclaration(';google.load("visualization", "1", {packages: ["orgchart"]});');

		$loaded = true;
	}

	function registerClientInstance($data)
	{
		$params = $this->_params;
		$mainParams = AriUtils::getParam($params, '_default');

		$jsOptions = $this->getJsOptions();
		$jsCode = sprintf('ARIOrgGChart("%1$s_chart", %2$s, %3$s);if (typeof(SexyLightbox) != "undefined") SexyLightbox.refresh();',
			$this->getId(),
			json_encode($data),
			!empty($jsOptions) ? json_encode($jsOptions) : '{}'
		);

		$document = JFactory::getDocument();
		$document->addScriptDeclaration(
			';google.setOnLoadCallback(function(){' . $jsCode . '});'
		);
	}

	function getJsOptions()
	{
		$params = $this->_params;
		$id = $this->getId();
		$mainParams = AriUtils::getParam($params, '_default');
		$options = AriUtils::getParam($params, 'chart');

		$defOptions = array(
			'allowCollapse' => false,
			'allowHtml' => false,
			'nodeClass' => '',
			'selectedNodeClass' => '',
			'size' => 'medium'
		);

		$jsOptions = AriParametersHelper::getUniqueOverrideParameters($defOptions, $options, true);

		return $jsOptions;
	}

	function renderItems($data)
	{
		$data = $this->prepareData($data);
		$mainParams = AriUtils::getParam($this->_params, '_default');

		$this->loadAssets();
		$this->registerClientInstance($data);

		echo '<div id="' . $this->getId() . '_chart" class="orggchart"></div>';
	}
	
	function prepareData($data)
	{
		$preparedData = array();

		if (!is_array($data) || count($data) == 0)
			return $preparedData;

		$stack = array();
		foreach ($data as $dataItem)
			$stack[] = &$dataItem;

		while (count($stack) > 0)
		{
			$currentItem =& array_pop($stack);
			
			$title = AriUtils::getParam($currentItem['params'], 'title', $currentItem['content']);

			$item = array(
				'id' => $currentItem['nodeId'],
				'parent' => $currentItem['parentId'],
				'title' => $title,
				'tooltip' => AriUtils::getParam($currentItem['params'], 'tooltip', '')
			);

            $collapsed = (bool)AriUtils::getParam($currentItem['params'], 'collapsed');
            if ($collapsed)
                $item['collapsed'] = true;

            $preparedData[] = $item;

			foreach ($currentItem['children'] as &$childItem)
				$stack[] =& $childItem;
		}
		
		$preparedData = array_reverse($preparedData);

		return $preparedData;
	}
}