<?php

/*------------------------------------------------------------------------
# textmenufx.php - Text Menu FX
# ------------------------------------------------------------------------
# author    FlashXML.net
# copyright Copyright (C) 2011 flashxml.net. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.flashxml.net
# Technical Support:  Comments - http://www.flashxml.net/text-menu.html#comments
-------------------------------------------------------------------------*/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.plugin.plugin');

class plgcontenttextmenufx extends JPlugin {
	var $textmenufx_embed_codes_count;
	var $textmenufx_swfobject_embedded;
	var	$textmenufx_wmode_values;
	var $textmenufx_regexp_match_keys;

	public function __construct(&$subject, $config = array()) {
		parent::__construct($subject, $config);
		$this->textmenufx_embed_codes_count = 0;
		$this->textmenufx_swfobject_embedded = false;
		$this->textmenufx_wmode_values = array(
			'allowed' => array('transparent', 'window'),
			'default' => 'transparent',
		);
		$this->textmenufx_regexp_match_keys = array(
			'settings' => 2,
			'width' => 4,
			'height' => 6,
			'wmode' => 8,
			'alternative_text' => 9,
		);
	}

	public function onContentPrepare($context, &$row, &$params, $page = 0) {
		if (is_object($row)) {
			return $this->_plgtextmenufx($row->text, $params);
		}

		return $this->_plgtextmenufx($row, $params);
	}

	protected function _plgtextmenufx(&$text, &$params) {
		if (JString::strpos($text, 'textmenufx') === false) {
			return true;
		}

		$text = preg_replace_callback('|{textmenufx(\s+settings=([^ ]+))?(\s+width=([0-9]+))?(\s+height=([0-9]+))?(\s+wmode=([a-z]+))?\s*}(.*){/textmenufx}|i', array($this, '_plgtextmenufxembedcode'), $text);
		return true;
	}

	protected function _plgtextmenufxembedcode($params) {
		$textmenu_path = $this->params->get('textmenufx_path');

		if (strpos($textmenu_path, '/') !== 0) {
			$textmenu_path = '/' . $textmenu_path;
			$this->params->def('textmenufx_path', $textmenu_path);
		}

		$settings = !empty($params[$this->textmenufx_regexp_match_keys['settings']]) ? $params[$this->textmenufx_regexp_match_keys['settings']] : 'settings.xml';

		if (empty($this->textmenufx_swfobject_embedded)) {
			$doc =& JFactory::getDocument();
			$doc->addScript('http'.(!empty($_SERVER['HTTPS']) ? 's' : '').'://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js');
			$this->textmenufx_swfobject_embedded = true;
		}

		$textmenufx_width = $textmenufx_height = 0;

		switch (true) {
			case function_exists('simplexml_load_file') && file_exists(JPATH_SITE.$textmenu_path.$settings):
				$xml = simplexml_load_file(JPATH_SITE.$textmenu_path.$settings);
				if ($xml) {
					$textmenufx_width_attributes_array = $xml->General_Properties->componentWidth->attributes();
				$textmenufx_width = !empty($textmenufx_width_attributes_array) ? (int)$textmenufx_width_attributes_array['value'] : 0;
				$textmenufx_height_attributes_array = $xml->General_Properties->componentHeight->attributes();
				$textmenufx_height = !empty($textmenufx_height_attributes_array) ? (int)$textmenufx_height_attributes_array['value'] : 0;
				}
			break;

			case (int)$params[$this->textmenufx_regexp_match_keys['width']] > 0 && (int)$params[$this->textmenufx_regexp_match_keys['height']] > 0:
				$textmenufx_width = (int)$params[$this->textmenufx_regexp_match_keys['width']];
				$textmenufx_height = (int)$params[$this->textmenufx_regexp_match_keys['height']];
			break;

			default:
				return '<!-- invalid path to the settings XML file, please use valid plugin parameter values -->';
			break;
		}

		if (!($textmenufx_width > 0 && $textmenufx_height > 0)) {
			return '<!-- invalid Text Menu FX width and / or height -->';
		}

		if (empty($params[$this->textmenufx_regexp_match_keys['wmode']])) {
			$textmenu_wmode = $this->params->get('textmenufx_wmode');
			if (empty($textmenu_wmode)) {
				$textmenu_wmode = $this->textmenufx_wmode_values['default'];
			}
		} else {
			$textmenu_wmode = in_array($params[$this->textmenufx_regexp_match_keys['wmode']], $this->textmenufx_wmode_values['allowed']) ? $params[$this->textmenufx_regexp_match_keys['wmode']] : $this->textmenufx_wmode_values['default'];
		}

		$this->textmenufx_embed_codes_count++;

		$joomla_install_dir_in_url = rtrim(JURI::root(true), '/');
		if (!empty($joomla_install_dir_in_url) && strpos($joomla_install_dir_in_url, '/') !== 0) {
			$joomla_install_dir_in_url = '/' . $joomla_install_dir_in_url;
		}

		return '<div id="flashxmltextmenu'.$this->textmenufx_embed_codes_count.'">'.$params[$this->textmenufx_regexp_match_keys['alternative_text']].'</div><script type="text/javascript">'."swfobject.embedSWF('{$joomla_install_dir_in_url}{$textmenu_path}TextMenuFX.swf', 'flashxmltextmenu{$this->textmenufx_embed_codes_count}', '{$textmenufx_width}', '{$textmenufx_height}', '9.0.0.0', '', { folderPath: '{$joomla_install_dir_in_url}{$textmenu_path}'".($settings != 'settings.xml' ? ", settingsXML: '{$settings}'" : '')." }, { scale: 'noscale', salign: 'tl', wmode: '{$textmenu_wmode}', allowScriptAccess: 'sameDomain', allowFullScreen: true }, {});</script>";
	}
}
?>