<?php
/**
 * @copyright	Copyright (c) 2015 Skyline Technology Ltd (http://extstore.com). All rights reserved.
 * @license		http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

// No direct access.
defined('_JEXEC') or die;

/**
 * ExtStore Advanced Popup Helper.
 *
 * @package		Joomla.Administrator
 * @subpackage	ExtStore.AdvPopup
 */
class AdvPopupHelper {
	/**
	 * Configure the Linkbar.
	 *
	 * @param	string	The name of the active view.
	 */
	public static function addSubmenu($vName = '') {
		JHtmlSidebar::addEntry(
			JText::_('COM_ADVPOPUP_SUBMENU_POPUPS'),
			'index.php?option=com_advpopup&view=popups',
			$vName == 'popups'
		);


	}

	/**
	 * Gets a list of the actions that can be performed.
	 *
	 * @param	int		The category ID.
	 * @return	JObject
	 */
	public static function getActions($categoryId = 0) {
		$user	= JFactory::getUser();
		$result	= new JObject();

		if (empty($categoryId)) {
			$assetName	= 'com_advpopup';
		} else {
			$assetName	= 'com_advpopup.category.' . (int) $categoryId;
		}

		$actions	= array(
			'core.admin', 'core.manage', 'core.create', 'core.edit', 'core.edit.own', 'core.edit.state', 'core.delete',
		);

		foreach ($actions as $action) {
			$result->set($action, $user->authorise($action, $assetName));
		}

		return $result;
	}

	/**
	 * Method to get plugin results.
	 */
	public static function getPluginResults($array) {
		$results	= array();

		if (is_array($array)) {
			foreach ($array as $result) {
				if (is_array($result)) {
					if (array_key_exists('type', $result)) {
						$results[]	= (object) $result;
					} else {
						foreach ($result as $r) {
							if ($r != null) {
								$results[]	= (object) $r;
							}
						}
					}
				} else if ($result != null) {
					$results[]	= (object) $result;
				}
			}
		}

		return $results;
	}

	/**
	 * Get layer types.
	 */
	public static function getLayerTypes() {
		$layers	= array();
		$config	= JComponentHelper::getParams('com_advpopup');

		if (!$config->get('disable_image_layer')) {
			$layers[]	= (object) array(
				'type'		=> 'image',
				'caption'	=> JText::_('COM_ADVPOPUP_IMAGE_LAYER_CAPTION')
			);
		}

		if (!$config->get('disable_text_layer')) {
			$layers[]	= (object) array(
				'type'		=> 'text',
				'caption'	=> JText::_('COM_ADVPOPUP_TEXT_LAYER_CAPTION')
			);
		}

		JPluginHelper::importPlugin('advpopup');
		$dispatcher		= JEventDispatcher::getInstance();
		$pluginLayers	= self::getPluginResults($dispatcher->trigger('onADVPGetLayerTypes'));

		return array_merge_recursive($layers, $pluginLayers);
	}
}