<?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('_JEXEC') or die;

jimport('joomla.form.formfield');
JFormHelper::loadFieldClass('groupedlist');

class JFormFieldVpctbutton extends JFormFieldGroupedList
{
	public $media_url = 'media/plg_vpconversiontracking';
	
	public $_version = '1.2';
	
	public $type = 'Vpctbutton';
	
	protected $blank_order = null;
	
	protected $bt_fields = null;
	
	protected $st_fields = null;

	
	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') || VM_VERSION < 3)
		{
			return;
		}
		
		$doc = JFactory::getDocument();
		
		$this->media_url = JUri::root(true) . '/' . $this->media_url;
		
		if(version_compare(JVERSION, '3.0.0', 'ge'))
		{
			$doc->addStyleSheet($this->media_url . '/admin/css/j3/button.css?ver=' . $this->_version);
			$doc->addScript($this->media_url . '/admin/js/j3/button.js?ver=' . $this->_version);
		}
		else
		{
			$doc->addStyleSheet($this->media_url . '/admin/css/j2.5/button.css?ver=' . $this->_version);
			$doc->addScript($this->media_url . '/admin/js/j2.5/button.js?ver=' . $this->_version);
		}
		
		$this->class = 'vpct-button-options';
		$this->name = '__none';
		
		$html = array();
		
		$html[] = '<div class="vpct-button-container clearfix">';
		$html[] = '<span class="vpct-button-label">' . JText::_('PLG_SYSTEM_VPCONVERSIONTRACKING_ORDER_INFO') . '</span>';
		$html[] = parent::getInput();
		$html[] = '<button type="button" class="btn vpct-button" onclick="VPCT.addSnippet(this);"><i class="icon-star"></i>' . JText::_('PLG_SYSTEM_VPCONVERSIONTRACKING_INSERT') . '</button>';
		
		$html[] = '</div>';
		
		
		return implode($html);
	}
	
	protected function getGroups()
	{
		include_once JPATH_SITE . '/plugins/system/vpconversiontracking/includes.php';
		
		if(!class_exists('VPCTHelperOrder'))
		{
			require VPCT_PATH . '/helpers/order.php';
		}
		
		if($this->blank_order === null)
		{
			VmConfig::loadJLang('com_virtuemart', true);
			VmConfig::loadJLang('com_virtuemart_shoppers', true);
			VmConfig::loadJLang('com_virtuemart_orders', true);
			
			$this->blank_order = new VPCTHelperOrder();
		}
		
		$skip_properties = array('bill_to', 'ship_to', 'details', 'items', 'history', 'vendor', 'currency');
		
		$groups = array();
		
		$order_label    = JText::_('PLG_SYSTEM_VPCONVERSIONTRACKING_ORDER_GROUP');
		$bt_label       = JText::_('PLG_SYSTEM_VPCONVERSIONTRACKING_BT_GROUP');
		$st_label       = JText::_('PLG_SYSTEM_VPCONVERSIONTRACKING_ST_GROUP');
		$products_label = JText::_('PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCTS_GROUP');
		
		$groups[$order_label]    = array();
		$groups[$bt_label]       = array();
		$groups[$st_label]       = array();
		$groups[$products_label] = array();
		
		foreach($this->blank_order as $property => $value)
		{
			if(in_array($property, $skip_properties)) continue;
			
			$name = JText::_('PLG_SYSTEM_VPCONVERSIONTRACKING_' . strtoupper($property));
			
			$groups[$order_label][] = JHtml::_('select.option', '{{ ' . $property . ' }}', $name);
		}
		
		if (!class_exists('VirtueMartModelUserfields'))
		{
			require(VMPATH_ADMIN . '/models/userfields.php');
		}
		
		$userFieldsModel = VmModel::getModel('userfields');
		
		if($this->bt_fields === null)
		{
			$this->bt_fields = $userFieldsModel->getUserFields(
				'account',
				array('captcha' => true, 'delimiters' => true), // Ignore these types
				array('delimiter_userinfo', 'user_is_vendor', 'username', 'name', 'password', 'password2', 'agreed', 'address_type') // Skips
			);
		}

		if($this->st_fields === null)
		{
			$this->st_fields = $userFieldsModel->getUserFields(
				'shipment',
				array(), // Default switches
				array('delimiter_userinfo', 'username', 'email', 'password', 'password2', 'agreed', 'address_type') // Skips
			);
		}
		
		foreach($this->bt_fields as $field)
		{
			$groups[$bt_label][] = JHtml::_('select.option', '{{ details.BT.' . $field->name . ' }}', JText::_($field->title));
		}
		
		foreach($this->st_fields as $field)
		{
			$groups[$st_label][] = JHtml::_('select.option', '{{ details.ST.' . $field->name . ' }}', JText::_($field->title));
		}
		
		$product_vars = array(
			'virtuemart_product_id' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_VIRTUEMART_PRODUCT_ID',
			'virtuemart_vendor_id' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_VIRTUEMART_VENDOR_ID',
			'product_available_date' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_AVAILABLE_DATE',
			'product_basePriceWithTax' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_BASEPRICEWITHTAX',
			'product_desc' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_DESC',
			'product_discountedPriceWithoutTax' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_DISCOUNTEDPRICEWITHOUTTAX',
			'product_final_price' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_FINAL_PRICE',
			'product_in_stock' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_IN_STOCK',
			'product_item_price' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_ITEM_PRICE',
			'product_lwh_uom' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_LWH_UOM',
			'product_mpn' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_MPN',
			'product_name' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_NAME',
			'product_variant' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_VARIANT',
			'product_ordered' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_ORDERED',
			'product_parent_id' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_PARENT_ID',
			'product_priceWithoutTax' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_PRICEWITHOUTTAX',
			'product_quantity' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_QUANTITY',
			'product_s_desc' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_S_DESC',
			'product_sales' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_SALES',
			'product_sku' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_SKU',
			'category_name' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_CATEGORY_NAME',
			'product_special' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_SPECIAL',
			'product_subtotal_discount' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_SUBTOTAL_DISCOUNT',
			'product_subtotal_with_tax' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_SUBTOTAL_WITH_TAX',
			'product_tax' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_TAX',
			'product_unit' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_UNIT',
			'product_url' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_URL',
			'product_weight' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_WEIGHT',
			'product_weight_uom' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_PRODUCT_WEIGHT_UOM',
			'low_stock_notification' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_LOW_STOCK_NOTIFICATION',
			'slug' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_SLUG',
			'virtuemart_category_id' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_VIRTUEMART_CATEGORY_ID',
			'virtuemart_manufacturercategories_id' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_VIRTUEMART_MANUFACTURERCATEGORIES_ID',
			'mf_desc' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_MF_DESC',
			'mf_email' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_MF_EMAIL',
			'mf_name' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_MF_NAME',
			'mf_url' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_MF_URL',
			'thumbnail_image' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_THUMBNAIL_IMAGE',
			'list_thumbnail_images' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_LIST_THUMBNAIL_IMAGES',
			'image' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_IMAGE',
			'list_images' => 'PLG_SYSTEM_VPCONVERSIONTRACKING_LIST_IMAGES'
		);
		
		foreach($product_vars as $property => $name)
		{
			$property = '{{# items }} ' . PHP_EOL . ' {{ ' . $property . ' }} ' . PHP_EOL . ' {{/ items }}';

			$groups[$products_label][] = JHtml::_('select.option', htmlspecialchars($property), JText::_($name));
		}
		
		// Merge any additional groups in the XML definition.
		$groups = array_merge(parent::getGroups(), $groups);
		
		return $groups;
	}
}