<?php
/**
 *-------------------------------------------------------------------------------------
 * @package    VP Payment by Shipment Plugin for VirtueMart
 *-------------------------------------------------------------------------------------
 * @copyright  Copyright (C) 2012-2019 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('_JEXEC') or die;

require_once __DIR__ . '/include.php';

/**
* VP Payment by Shipment Plugin Helper class
* 
* @since 1.0.0
*/
class vmPSPluginHelper
{
	protected static $cache = array();

	/**
	* Method to check if we are in the VirtueMart cart page 
	* 
	* @return boolean Returns false if not cart page
	*/
	public static function isCart()
	{
		if(!class_exists('VmConfig'))
		{
			// VirtueMart is not installed in this site.
			return false;
		}

		$app  = JFactory::getApplication();
		$doc  = JFactory::getDocument();
		$type = strtolower($doc->getType());

		if(version_compare(JVERSION, '3.0.0', 'ge'))
		{
			$input  = $app->input;
			$option = strtolower($input->getCmd('option', ''));
			$view   = strtolower($input->getCmd('view', ''));
			$task   = strtolower($input->getCmd('task', ''));
			$task   = $input->post->getCmd('task', '') ?
			          strtolower($input->post->getCmd('task', '')) :
			          $task;
			$layout = strtolower($input->getCmd('layout', ''));
		}
		else
		{
			$option = strtolower(JRequest::getCmd('option', ''));
			$view   = strtolower(JRequest::getCmd('view', ''));
			$task   = strtolower(JRequest::getCmd('task', ''));
			$layout = strtolower(JRequest::getCmd('layout', ''));
		}
		
		$isCart = ($option == 'com_virtuemart' && $view == 'cart' && $type == 'html') ||
		          ($option == 'com_virtuemart' && $view == 'vmplg' && $task == 'pluginuserpaymentcancel' && $type == 'html') ||
		          ($option == 'com_virtuemart' && $view == 'pluginresponse' && $task == 'pluginuserpaymentcancel' && $type == 'html');
		
		if($isCart)
		{
			VmConfig::loadConfig();
			
			$cart   = self::getCart();
			$isCart = $isCart && ($layout != 'cart' && $cart->layout != 'cart');
		}
		
		return $isCart;
	}
	
	public static function getCart()
	{
		JLoader::register('VirtueMartCart', JPATH_SITE . '/components/com_virtuemart/helpers/cart.php');
		
		return VirtueMartCart::getCart();
	}
	
	/**
	* Method to check if the the plugin is compatible to installed VirtueMart version
	* 
	* @return boolean Returns false if not compatible
	*/
	public static function isCompatible()
	{
		$app = JFactory::getApplication();
		
		if(class_exists('VmConfig'))
		{
			VmConfig::loadConfig();
		}
		
		if(!class_exists('VmConfig') || !defined('VM_VERSION'))
		{
			$app->enqueueMessage('It appears VirtueMart Component is not installed. Disable all VirtueMart related plugin to stop seeing this message.', 'notice');
			return false;
		}
		
		if(VM_VERSION < 3)
		{
			$app->enqueueMessage('VirtuePlanet\'s vmPSPluginHelper is compatible to VirtueMart 3 and above.', 'warning');
			return false;
		}
		
		return true;
	}
	
	/**
	* Method to check if override is properly implemented
	* 
	* @return boolean
	*/
	public static function checkOverride()
	{
		$app = JFactory::getApplication();
		
		if(!self::isCompatible())
		{
			return false;
		}
		
		VmConfig::loadConfig();
		
		$vmPSPlugin     = JPath::clean(__DIR__ . '/plugins/vmpsplugin.php');
		$vmPSPluginTest = JPath::clean(__DIR__ . '/plugins/vmpsplugintest.php');
		
		if(class_exists('vmPSPlugin'))
		{
			if(!class_exists('vmPSPluginTest'))
			{
				if(is_file($vmPSPluginTest))
				{
					require $vmPSPluginTest;
				}
				else
				{
					$file = str_replace(JPATH_SITE, '', $vmPSPluginTest);
					$app->enqueueMessage('VP Payment by Shipment plugin failed to load ' . $file . '.', 'warning');
					return false;
				}
			}
			
			if(!vmPSPluginTest::isOverride())
			{
				$app->enqueueMessage('VP Payment by Shipment plugin failed to initialize. <b>vmPSPlugin</b> class is already overridden by some other 3rd party extension.', 'warning');
				return false;
			}
		}
		elseif(!is_file($vmPSPlugin))
		{
			$file = str_replace(JPATH_SITE, '', $vmPSPlugin);
			
			$app->enqueueMessage('VP Payment by Shipment plugin failed to load ' . $file . '.', 'warning');
			return false;
		}
		else
		{
			require $vmPSPlugin;
		}
		
		return true;
	}

	/**
	* Method to get published shipment method ids
	* 
	* @return array
	*/
	public static function getShipmentMethods()
	{
		if(!isset(self::$cache['shipment_methods']))
		{
			$db    = JFactory::getDbo();
			$query = $db->getQuery(true);
			
			$query->select('a.' . $db->quoteName('virtuemart_shipmentmethod_id'))
				->from($db->quoteName('#__virtuemart_shipmentmethods') . ' AS a')
				->where('a.' . $db->quoteName('published') . ' = 1');
			
			$db->setQuery($query);
			self::$cache['shipment_methods'] = $db->loadColumn();
		}
		
		return (array) self::$cache['shipment_methods'];
	}

	/**
	* Method to get published payment method ids
	* 
	* @return array
	*/
	public static function getPaymentMethods()
	{
		if(!isset(self::$cache['payment_methods']))
		{
			$db    = JFactory::getDbo();
			$query = $db->getQuery(true);
			
			$query->select('a.' . $db->quoteName('virtuemart_paymentmethod_id'))
				->from($db->quoteName('#__virtuemart_paymentmethods') . ' AS a')
				->where('a.' . $db->quoteName('published') . ' = 1');
			
			$db->setQuery($query);
			self::$cache['payment_methods'] = $db->loadColumn();
		}

		return (array) self::$cache['payment_methods'];
	}
}