<?php
/**
 *---------------------------------------------------------------------------------------
 * @package      VP Conversion Tracking - Joomla! System Plugin
 * @subpackage   For VirtueMart 3+
 *---------------------------------------------------------------------------------------
 * @copyright    Copyright (C) 2012-2021 VirtuePlanet Services LLP. All rights reserved.
 * @license      GNU General Public License version 2 or later; see LICENSE.txt
 * @authors      Abhishek Das
 * @email        info@virtueplanet.com
 * @link         https://www.virtueplanet.com
 *---------------------------------------------------------------------------------------
*/
defined('JPATH_BASE') or die;

jimport('joomla.form.formfield');
jimport('joomla.registry.registry');

class JFormFieldVPAdminHelper extends JFormField
{
	public $extension_name = 'VP Conversion Tracking';
	
	public $media_url = 'media/plg_vpconversiontracking';
	
	public $extension_version = '2.0';
	
	public $type = 'VPAdminHelper';
	
	
	protected function getInput()
	{
		$app = JFactory::getApplication();
		
		if(!class_exists('VmConfig'))
		{
			$file = JPath::clean(JPATH_ADMINISTRATOR . '/components/com_virtuemart/helpers/config.php');
			
			if(is_file($file)) require($file);
		}
		
		if(!defined('VM_VERSION') || !class_exists('VmConfig'))
		{
			$app->enqueueMessage('It appears VirtueMart Component is not installed. ' . $this->extension_name . ' is an extension of VirtueMart Component.', 'error');
			return;
		}
		
		if(VM_VERSION < 3)
		{
			$app->enqueueMessage($this->extension_name . ' is compatible to VirtueMart 3 and above.', 'error');
		}
		
		$doc  = JFactory::getDocument();
		$root = JUri::root(true);
		
		if(version_compare(JVERSION, '3.0.0', 'ge'))
		{
			$staticJS  = $this->media_url . '/admin/js/j3/admin.js';
			$staticCSS = $this->media_url . '/admin/css/j3/admin.css';
		}
		else
		{
			$staticJS = array();
			$staticJS[] = $this->media_url . '/admin/js/jquery.min.js';
			$staticJS[] = $this->media_url . '/admin/js/jquery-noconflict.js';
			$staticJS[] = $this->media_url . '/admin/js/jquery-migrate.min.js';
			$staticJS[] = $this->media_url . '/admin/js/j2.5/admin.js';
			
			$staticCSS = $this->media_url . '/admin/css/j2.5/admin.css';
		}
		
		$scripts     = !empty($this->element['scripts']) ? $this->element['scripts'] : $staticJS;
		$styleSheets = !empty($this->element['styleSheets']) ? $this->element['styleSheets'] : $staticCSS;
		
		if(!empty($scripts))
		{
			if(is_string($scripts))
			{
				if(strpos($scripts, ',') !== false)
				{
					$scripts = explode(',', $scripts);
				}
				else
				{
					$scripts = (array) $scripts;
				}
			}
			
			foreach($scripts as $script)
			{
				$path = JPath::clean(JPATH_ROOT . '/' . $script);
				
				if(is_file($path))
				{
					$version = strpos($script, 'j2.5') || strpos($script, 'j3') ? '?ver=' . $this->extension_version : '';
					$doc->addScript($root . '/' . trim($script) . $version);
				}
			}
		}

		if(!empty($styleSheets))
		{
			if(is_string($styleSheets))
			{
				if(strpos($styleSheets, ',') !== false)
				{
					$styleSheets = explode(',', $styleSheets);
				}
				else
				{
					$styleSheets = array($styleSheets);
				}
			}
			
			foreach($styleSheets as $styleSheet)
			{
				$path = JPath::clean(JPATH_ROOT . '/' . $styleSheet);

				if(is_file($path))
				{
					$version = strpos($styleSheet, 'j2.5') || strpos($styleSheet, 'j3') ? '?ver=' . $this->extension_version : '';
					$doc->addStyleSheet($root . '/' . trim($styleSheet) . $version);
				}
			}
		}
		
		return null;
	}
	
	public function getLabel() 
	{
		return null;
	}
}