<?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') && defined('ARI_FRAMEWORK_LOADED')) or die;

AriKernel::import('Application.ARISmartContent.Utils.Helper');

class AriSmartContentApplicationHelper
{
	protected static $instance;
	protected static $plugins = array();
	
	private $_jQueryIsLoaded = false;
	private $_loadedJQueryUIThemes = array();
	
	function getInstance()
	{
		if (is_null(self::$instance))
		{
			$className = __CLASS__;
			self::$instance = new $className();
		}
		
		return self::$instance;
	}

	function getCacheFolder()
	{
		$cacheDir = JPATH_CACHE;
		$extCacheDir = $cacheDir . '/com_arismartcontent';

		return (@file_exists($extCacheDir) && is_dir($extCacheDir))
			? $extCacheDir
			: $cacheDir;
	}

	function loadJQuery()
	{
		if ($this->_jQueryIsLoaded)
			return ;

		$this->_jQueryIsLoaded = true;

		$config = AriSmartContentHelper::getConfig();

		$loadJQuery = (bool)$config->get('loadJQuery');
		if (!$loadJQuery)
			return ;

		$baseUri = JURI::root(true) . '/media/arismartcontent/jquery/';

		$doc = JFactory::getDocument();
		$loadJQueryMethod = $config->get('loadJQueryMethod', 'google_cdn');
		$noConflict = (bool)$config->get('jQueryNoConflict');
		if ($loadJQueryMethod == 'local')
		{
			if (J3_0)
				JHtml::_('jquery.framework', $noConflict);
			else
				$doc->addScript($baseUri . 'jquery.min.js');
		}
		else
		{
			$jQueryVersion = $config->get('jQueryVersion', '1.8.2');

			$doc->addScript('//ajax.googleapis.com/ajax/libs/jquery/' . $jQueryVersion . '/jquery.min.js');
		}

		if ($noConflict)
		{
			if (!J3_0 || $loadJQueryMethod != 'local')
				$doc->addScript($baseUri . 'jquery.noconflict.js');
		}
	}
	
	function loadJQueryLib($lib)
	{
		$baseUri = JURI::root(true) . '/media/arismartcontent/jquery/';
		
		$doc = JFactory::getDocument();
		$doc->addScript($baseUri . 'jquery.' . $lib . '.js');
	}

	function loadJQueryUILib($lib)
	{
		if ($lib !== 'core')
			self::loadJQueryUILib('core');
		
		$baseUri = JURI::root(true) . '/media/arismartcontent/jqueryui/js/';
		
		$doc = JFactory::getDocument();
		$doc->addScript($baseUri . 'jquery.ui.' . $lib . '.min.js');
	}

	function loadJQueryUITheme($theme)
	{
		$theme = (string)preg_replace('/[^A-Z_]/i', '', $theme);
		if (empty($theme))
		{
			$config = AriSmartContentHelper::getConfig();

			$theme = $config->get('jQueryUITheme');
			if (empty($theme))
				return ;
		}

		if (in_array($theme, $this->_loadedJQueryUIThemes))
			return ;

		$doc = JFactory::getDocument();
		$themeUri = JURI::root(true) . '/media/arismartcontent/jqueryui/themes/' . $theme . '/style.css';
		$doc->addStyleSheet($themeUri);

		$this->_loadedJQueryUIThemes[] = $theme;
	}

	function getDefaultJQueryUITheme()
	{
		$config = AriSmartContentHelper::getConfig();

		return $config->get('jQueryUITheme');
	}
	
	function resolveJQueryUITheme($theme)
	{
		if (empty($theme))
			$theme = $this->getDefaultJQueryUITheme();
			
		return $theme;
	}
	
	function needExecutePlugins($option)
	{
		$config = AriSmartContentHelper::getConfig();
		$allPages = (bool)$config->get('allComponents');
		if (!$allPages)
		{
			if (empty($option)) 
				return false;

			$components = trim($config->get('components', ''));
			$components = explode("\n", $components);
			$components = array_map('trim', $components);

			if ($option != 'com_content' && !in_array($option, $components)) 
				return false;
		}

		return true;
	}

	function executePlugins($content)
	{
		jimport('joomla.plugin.helper');
		AriKernel::import('Application.ARISmartContent.Plugin.ContentPlugin');

		JPluginHelper::importPlugin(ARISMARTCONTENT_PLUGIN_TYPE, null, false);

		$dispatcher = JDispatcher::getInstance();
		$plugins = JPluginHelper::getPlugin(ARISMARTCONTENT_PLUGIN_TYPE, null);
		foreach ($plugins as $plugin)
		{
			$className = 'plg' . $plugin->type . $plugin->name;
			if (class_exists($className))
			{
				if (!isset($pluginMetadata->params))
					$plugin = JPluginHelper::getPlugin($plugin->type, $plugin->name);

				$plg = new $className($dispatcher, (array) ($plugin));
				$content = $plg->execute($content);
			}
		}

		return $content;
	}
	
	function getPlugin($type, $name)
	{
		if (isset(self::$plugins[$type][$name]))
			return self::$plugins[$type][$name];

		$pluginInstance = null;
		JPluginHelper::importPlugin($type, $name, false);
		$plugin = JPluginHelper::getPlugin($type, $name);

		if (empty($plugin))
			return null;

		$className = 'plg' . $plugin->type . $plugin->name;
		if (class_exists($className))
		{
			if (!isset($pluginMetadata->params))
				$plugin = JPluginHelper::getPlugin($plugin->type, $plugin->name);

			$dispatcher = JDispatcher::getInstance();

			$pluginInstance = new $className($dispatcher, (array) ($plugin));
		}

		if (is_null($pluginInstance))
			return $pluginInstance;

		self::$plugins[$type][$name] = $pluginInstance;

		return self::$plugins[$type][$name];
	}

	function addTrustedModule($id)
	{
		$app = JFactory::getApplication();
		$trustedModules = $app->getUserState('arismartcontent.trustedmodules');
		if (!is_array($trustedModules))
			$trustedModules = array();
		
		if (!in_array($id, $trustedModules))
			$trustedModules[] = $id;
			
		$app->setUserState('arismartcontent.trustedmodules', $trustedModules);
	}

	function checkTrustedModule($id)
	{
		$app = JFactory::getApplication();
		$trustedModules = $app->getUserState('arismartcontent.trustedmodules');
		
		return ($id && is_array($trustedModules) && in_array($id, $trustedModules));
	}
}