<?php
/**
 * @package    VP Email Manager
 * @copyright  Copyright (C) 2012-2021 VirtuePlanet Services LLP. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 * @author     Abhishek Das <info@virtueplanet.com>
 * @link       https://www.virtueplanet.com
 */

defined('_JEXEC') or die;

jimport('joomla.application.component.modeladmin');

use \Joomla\CMS\Table\Table;
use \Joomla\CMS\Factory;
use \Joomla\CMS\Language\Text;
use \Joomla\CMS\Plugin\PluginHelper;

/**
 * Vpem model.
 */
class VpemModelTemplate extends \Joomla\CMS\MVC\Model\AdminModel
{
	/**
	 * @var   string   The prefix to use with controller messages.
	 */
	protected $text_prefix = 'COM_VPEM';

	/**
	 * @var   string  Alias to manage history control
	 */
	public $typeAlias = 'com_vpem.template';

	/**
	 * @var null  Item data
	 */
	protected $item = null;

	/**
	 * Returns a reference to the a Table object, always creating it.
	 *
	 * @param   string  $type    The table type to instantiate
	 * @param   string  $prefix  A prefix for the table class name. Optional.
	 * @param   array   $config  Configuration array for model. Optional.
	 *
	 * @return    JTable    A database object
	 */
	public function getTable($type = 'Template', $prefix = 'VpemTable', $config = [])
	{
		return Table::getInstance($type, $prefix, $config);
	}

	/**
	 * Method to get the record form.
	 *
	 * @param   array    $data      An optional array of data for the form to interogate.
	 * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
	 *
	 * @return  JForm  A JForm object on success, false on failure
	 */
	public function getForm($data = [], $loadData = true)
	{
		// Initialise variables.
		$app = Factory::getApplication();

		// Get the form.
		$form = $this->loadForm('com_vpem.template', 'template', ['control' => 'jform', 'load_data' => $loadData]);

		if (empty($form))
		{
			return false;
		}

		return $form;
	}
	
	/**
	 * Method to allow derived classes to preprocess the form.
	 *
	 * @param   JForm   $form   A JForm object.
	 * @param   mixed   $data   The data expected for the form.
	 * @param   string  $group  The name of the plugin group to import (defaults to "content").
	 *
	 * @return  void
	 *
	 * @see     JFormField
	 * @throws  Exception if there is an error in the form event.
	 */
	protected function preprocessForm(JForm $form, $data, $group = 'content')
	{
		if (!empty($form))
		{
			$dataObject = (object) $data;
			
			if ($dataObject->type)
			{
				$form->setFieldAttribute('type', 'readonly', true);
			}
			
			$default_css_style = JPATH_SITE . '/media/com_vpem/css/default.mail.css';
			
			if (is_file($default_css_style))
			{
				$css_style = file_get_contents($default_css_style);
				
				$form->setFieldAttribute('css_style', 'default', $css_style);
			}
		}
		
		parent::preprocessForm($form, $data, $group);
	}

	/**
	 * Method to get the data that should be injected in the form.
	 *
	 * @return   mixed  The data for the form.
	 */
	protected function loadFormData()
	{
		// Check the session for previously entered form data.
		$data = Factory::getApplication()->getUserState('com_vpem.edit.template.data', []);

		if (empty($data))
		{
			if ($this->item === null)
			{
				$this->item = $this->getItem();
			}

			$data = $this->item;
		}

		return $data;
	}

	/**
	 * Method to get a single record.
	 *
	 * @param   integer  $pk  The id of the primary key.
	 *
	 * @return  mixed    Object on success, false on failure.
	 */
	public function getItem($pk = null)
	{
		if ($item = parent::getItem($pk))
		{
			// Do something if needed
		}
		
		return $item;
	}

	/**
	 * Method to duplicate an Template
	 *
	 * @param   array  &$pks  An array of primary key IDs.
	 *
	 * @return  boolean  True if successful.
	 *
	 * @throws  Exception
	 */
	public function duplicate(&$pks)
	{
		$user = Factory::getUser();

		// Access checks.
		if (!$user->authorise('core.create', 'com_vpem'))
		{
			throw new Exception(Text::_('JERROR_CORE_CREATE_NOT_PERMITTED'));
		}

		$dispatcher = JEventDispatcher::getInstance();
		$context    = $this->option . '.' . $this->name;

		// Include the plugins for the save events.
		PluginHelper::importPlugin($this->events_map['save']);

		$table = $this->getTable();

		foreach ($pks as $pk)
		{
			if ($table->load($pk, true))
			{
				// Reset the id to create a new record.
				$table->id = 0;

				if (!$table->check())
				{
					throw new Exception($table->getError());
				}

				// Trigger the before save event.
				$result = $dispatcher->trigger($this->event_before_save, array($context, &$table, true));

				if (in_array(false, $result, true) || !$table->store())
				{
					throw new Exception($table->getError());
				}

				// Trigger the after save event.
				$dispatcher->trigger($this->event_after_save, array($context, &$table, true));
			}
			else
			{
				throw new Exception($table->getError());
			}
		}

		// Clean cache
		$this->cleanCache();

		return true;
	}

	/**
	 * Prepare and sanitise the table prior to saving.
	 *
	 * @param   JTable  $table  Table Object
	 *
	 * @return void
	 */
	protected function prepareTable($table)
	{
		jimport('joomla.filter.output');
		
		if (!empty($table->body) && strpos($table->body, '</body>') !== false)
		{
			$dom  = new DOMDocument;
			$mock = new DOMDocument;
			
			$dom->loadHTML('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' . $table->body);
			
			$body = $dom->getElementsByTagName('body')->item(0);

			foreach ($body->childNodes as $child)
			{
				$mock->appendChild($mock->importNode($child, true));
			}

			$table->body = $mock->saveHTML();
		}

		if (empty($table->id))
		{
			// Set ordering to the last item if not set
			if (@$table->ordering === '')
			{
				$db = Factory::getDbo();
				$db->setQuery('SELECT MAX(ordering) FROM #__vpem_templates');
				
				$max = $db->loadResult();
				$table->ordering = $max + 1;
			}
		}
	}
	
	protected function getVendorVariables()
	{
		$vendorModel  = VmModel::getModel('vendor');
		$vendor       = $vendorModel->getVendor();
		$vendorFields = $vendorModel->getVendorAddressFields();
		$groups       = [];
		$key          = JText::_('COM_VPEM_VENDOR_GROUP');
		$groups[$key] = [];
		
		$static_fields = [
			'vendor_name'             => 'COM_VIRTUEMART_STORE_FORM_COMPANY_NAME',
			'vendor_store_name'       => 'COM_VIRTUEMART_STORE_FORM_STORE_NAME',
			'vendor_url'              => 'COM_VIRTUEMART_PRODUCT_FORM_URL',
			'vendor_store_desc'       => 'COM_VIRTUEMART_STORE_FORM_DESCRIPTION',
			'vendor_terms_of_service' => 'COM_VIRTUEMART_STORE_FORM_TOS',
			'vendor_legal_info'       => 'COM_VIRTUEMART_STORE_FORM_LEGAL',
			'vendor_image'            => 'COM_VPEM_VENDOR_IMAGE'
		];
		
		foreach ($static_fields as $name => $field)
		{
			$groups[$key][] = JHtml::_('select.option', '{{ vendor.' . $name . ' }}', JText::_($field));
		}

		foreach ($vendorFields['fields'] as $field)
		{
			if ($field['type'] == 'delimiter')
			{
				continue;
			}
			
			if ($field['name'] == 'virtuemart_country_id')
			{
				$groups[$key][] = JHtml::_('select.option', '{{ vendor.virtuemart_country_name }}', JText::_($field['title']));
			}
			elseif ($field['name'] == 'virtuemart_state_id')
			{
				$groups[$key][] = JHtml::_('select.option', '{{ vendor.virtuemart_state_name }}', JText::_($field['title']));
			}
			else
			{
				$groups[$key][] = JHtml::_('select.option', '{{ vendor.' . $field['name'] . ' }}', JText::_($field['title']));
			}
		}
		
		return $groups;
	}
	
	public function getFunctions($add_order_functions = false)
	{
		$groups       = [];
		$key          = JText::_('COM_VPEM_FUNCTIONS_GROUP');
		$groups[$key] = [];
		
		$groups[$key][] = JHtml::_('select.option', '{{# JText._ }}LANGUAGE_STRING{{/ JText._ }}', JText::_('COM_VPEM_FUNCTION_JTEXT'));
		$groups[$key][] = JHtml::_('select.option', '{{# JText.sprintf }}LANGUAGE_STRING, VAR_1{{/ JText.sprintf }}', JText::_('COM_VPEM_FUNCTION_JTEXT_SPRINTF'));
		$groups[$key][] = JHtml::_('select.option', '{{# do.link }}URL, TEXT, ATTRIBUTES{{/ do.link }}', JText::_('COM_VPEM_FUNCTION_DO_LINK'));
		$groups[$key][] = JHtml::_('select.option', '{{# round }}NUMERICAL_VALUE, vendor/user{{/ round }}', JText::_('COM_VPEM_FUNCTION_ROUND'));
		$groups[$key][] = JHtml::_('select.option', '{{# math }}(1+2)*3{{/ math }}', JText::_('COM_VPEM_FUNCTION_MATH'));
		$groups[$key][] = JHtml::_('select.option', '{{# strip_tags }}HTML_STRING{{/ strip_tags }}', JText::_('COM_VPEM_FUNCTION_STRIP_TAGS'));
		$groups[$key][] = JHtml::_('select.option', '{{# htmlspecialchars }}STRING, VAR_1, VAR_2{{/ htmlspecialchars }}', JText::_('COM_VPEM_FUNCTION_HTMLSPECIALCHARS'));
		
		if ($add_order_functions)
		{
			$groups[$key][] = JHtml::_('select.option', '{{# in_user_currency }}NUMERICAL_VALUE{{/ in_user_currency }}', JText::_('COM_VPEM_FUNCTION_IN_USER_CURRENCY'));
			$groups[$key][] = JHtml::_('select.option', '{{# in_shop_currency }}NUMERICAL_VALUE{{/ in_shop_currency }}', JText::_('COM_VPEM_FUNCTION_IN_SHOP_CURRENCY'));
		}
		
		return $groups;
	}
	
	public function getOrderVariables()
	{
		JLoader::register('VpemHelperOrder', JPATH_COMPONENT_ADMINISTRATOR . '/helpers/order.php');
		
		VmConfig::loadJLang('com_virtuemart', true);
		VmConfig::loadJLang('com_virtuemart_shoppers', true);
		VmConfig::loadJLang('com_virtuemart_orders', true);
		
		$blank_order     = new VpemHelperOrder();
		$skip_properties = ['bill_to', 'ship_to', 'details', 'items', 'history', 'vendor', 'currency', '_errors'];
		$order_label     = JText::_('COM_VPEM_ORDER_GROUP');
		$bt_label        = JText::_('COM_VPEM_BT_GROUP');
		$st_label        = JText::_('COM_VPEM_ST_GROUP');
		$products_label  = JText::_('COM_VPEM_PRODUCTS_GROUP');
		$groups          = [];
		$vendorGroup     = $this->getVendorVariables();
		$groups          = array_merge($vendorGroup, $groups);
		$functions       = $this->getFunctions(true);
		
		$groups[$order_label]    = [];
		$groups[$bt_label]       = [];
		$groups[$st_label]       = [];
		$groups[$products_label] = [];
		
		$groups = array_merge($groups, $functions);
		
		foreach ($blank_order as $property => $value)
		{
			if (in_array($property, $skip_properties)) continue;
			
			$name = JText::_('COM_VPEM_' . strtoupper($property));
			
			$groups[$order_label][] = JHtml::_('select.option', '{{ ' . $property . ' }}', $name);
		}
		
		$groups[$order_label][] = JHtml::_('select.option', '{{ comments }}', JText::_('COM_VIRTUEMART_ORDER_HISTORY_COMMENT'));
		
		JLoader::register('VirtueMartModelUserfields', VMPATH_ADMIN . '/models/userfields.php');
		
		$userFieldsModel = VmModel::getModel('userfields');
		
		$bt_fields = $userFieldsModel->getUserFields(
			'account',
			['captcha' => true, 'delimiters' => true], // Ignore these types
			['delimiter_userinfo', 'user_is_vendor', 'username', 'name', 'password', 'password2', 'agreed', 'address_type'] // Skips
		);

		$st_fields = $userFieldsModel->getUserFields(
			'shipment',
			['delimiters' => true], // Default switches
			['delimiter_userinfo', 'username', 'email', 'password', 'password2', 'agreed', 'address_type'] // Skips
		);
		
		$cart_fields = $userFieldsModel->getUserFields(
			'cart',
			['delimiters' => true], // Default switches
			['customer_note', 'tos'] // Skips
		);
		
		foreach ($bt_fields as $field)
		{
			$groups[$bt_label][] = JHtml::_('select.option', '{{ details.BT.' . $field->name . ' }}', JText::_($field->title));
			
			if ($field->name == 'virtuemart_country_id')
			{
				$groups[$bt_label][] = JHtml::_('select.option', '{{ details.BT.virtuemart_country_name }}', JText::_($field->title));
			}
			
			if ($field->name == 'virtuemart_state_id')
			{
				$groups[$bt_label][] = JHtml::_('select.option', '{{ details.BT.virtuemart_state_name }}', JText::_($field->title));
			}
		}
		
		foreach ($st_fields as $field)
		{
			$groups[$st_label][] = JHtml::_('select.option', '{{ details.ST.' . $field->name . ' }}', JText::_($field->title));
			
			if ($field->name == 'virtuemart_country_id')
			{
				$groups[$st_label][] = JHtml::_('select.option', '{{ details.ST.virtuemart_country_name }}', JText::_($field->title));
			}
			
			if ($field->name == 'virtuemart_state_id')
			{
				$groups[$st_label][] = JHtml::_('select.option', '{{ details.ST.virtuemart_state_name }}', JText::_($field->title));
			}
		}
		
		foreach ($cart_fields as $field)
		{
			$groups[$bt_label][] = JHtml::_('select.option', '{{ details.BT.' . $field->name . ' }}', JText::_($field->title));
		}
		
		$product_vars = [
			'virtuemart_product_id',
			'virtuemart_vendor_id',
			'product_available_date',
			'product_basePriceWithTax',
			'product_desc',
			'product_discountedPriceWithoutTax',
			'product_final_price',
			'product_in_stock',
			'product_item_price',
			'product_lwh_uom',
			'product_mpn',
			'product_gtin',
			'product_name',
			'product_variant',
			'product_ordered',
			'product_parent_id',
			'product_priceWithoutTax',
			'product_quantity',
			'product_s_desc',
			'product_sales',
			'product_sku',
			'category_name',
			'product_special',
			'product_subtotal_discount',
			'product_subtotal_with_tax',
			'product_tax',
			'product_unit',
			'product_url',
			'product_weight',
			'product_weight_uom',
			'low_stock_notification',
			'slug',
			'virtuemart_category_id',
			'virtuemart_manufacturercategories_id',
			'mf_desc',
			'mf_email',
			'mf_name',
			'mf_url',
			'thumbnail_image',
			'image_url',
			'list_images'
		];
		
		$groups[$products_label][] = JHtml::_('select.option', '{{ order_items_table }}', JText::_('COM_VPEM_ORDER_ITEMS_TABLE'));
		
		foreach ($product_vars as $property)
		{
			$name  = 'COM_VPEM_' . strtoupper($property);
			$value = '{{# items }} {{ ' . $property . ' }} {{/ items }}';

			$groups[$products_label][] = JHtml::_('select.option', htmlspecialchars($value), JText::_($name));
		}
		
		return $groups;
	}
	
	public function getProductVariables()
	{
		VmConfig::loadJLang('com_virtuemart');
		VmConfig::loadJLang('com_virtuemart', true);
		VmConfig::loadJLang('com_virtuemart_shoppers', true);
		VmConfig::loadJLang('com_virtuemart_orders', true);
		
		$user_label      = JText::_('COM_VPEM_USER_GROUP');
		$comment_label   = JText::_('COM_VPEM_COMMENT_GROUP');
		$products_label  = JText::_('COM_VPEM_PRODUCTS_GROUP');
		$groups          = [];
		$vendorGroup     = $this->getVendorVariables();
		$groups          = array_merge($vendorGroup, $groups);

		$groups[$products_label] = [];
		$groups[$user_label]     = [];
		$groups[$comment_label]  = [];
		
		$functions = $this->getFunctions();
		$groups    = array_merge($groups, $functions);
		
		$groups[$user_label][] = JHtml::_('select.option', '{{ user.name }}', JText::_('JFIELD_NAME_LABEL'));
		$groups[$user_label][] = JHtml::_('select.option', '{{ user.email }}', JText::_('JGLOBAL_EMAIL'));
		
		$groups[$comment_label][] = JHtml::_('select.option', '{{ comment }}', JText::_('COM_VPEM_LBL_COMMENT'));
		
		$product_vars = [
			'virtuemart_product_id',
			'virtuemart_vendor_id',
			'product_available_date',
			'product_basePriceWithTax',
			'product_desc',
			'product_discountedPriceWithoutTax',
			'product_final_price',
			'product_in_stock',
			'product_item_price',
			'product_lwh_uom',
			'product_mpn',
			'product_gtin',
			'product_name',
			'product_variant',
			'product_ordered',
			'product_parent_id',
			'product_priceWithoutTax',
			'product_quantity',
			'product_s_desc',
			'product_sales',
			'product_sku',
			'category_name',
			'product_special',
			'product_subtotal_discount',
			'product_subtotal_with_tax',
			'product_tax',
			'product_unit',
			'product_url',
			'product_weight',
			'product_weight_uom',
			'low_stock_notification',
			'slug',
			'virtuemart_category_id',
			'virtuemart_manufacturercategories_id',
			'mf_desc',
			'mf_email',
			'mf_name',
			'mf_url',
		];
		
		foreach ($product_vars as $property)
		{
			$name  = 'COM_VPEM_' . strtoupper($property);
			$value = '{{ product.' . $property . ' }}';

			$groups[$products_label][] = JHtml::_('select.option', htmlspecialchars($value), JText::_($name));
		}
		
		$groups[$products_label][] = JHtml::_('select.option', '{{ product_thumbnail }}', JText::_('COM_VPEM_PRODUCT_THUMBNAIL'));
		
		return $groups;
	}
	
	public function getRecommendProductVariables()
	{
		VmConfig::loadJLang('com_virtuemart');
		VmConfig::loadJLang('com_virtuemart', true);
		VmConfig::loadJLang('com_virtuemart_shoppers', true);
		VmConfig::loadJLang('com_virtuemart_orders', true);
		
		$user_label      = JText::_('COM_VPEM_USER_GROUP');
		$friend_label    = JText::_('COM_VPEM_FRIEND_GROUP');
		$comment_label   = JText::_('COM_VPEM_COMMENT_GROUP');
		$products_label  = JText::_('COM_VPEM_PRODUCTS_GROUP');
		$groups          = [];
		$vendorGroup     = $this->getVendorVariables();
		$groups          = array_merge($vendorGroup, $groups);

		$groups[$products_label] = [];
		$groups[$user_label]     = [];
		$groups[$friend_label]   = [];
		$groups[$comment_label]  = [];
		
		$functions = $this->getFunctions();
		$groups    = array_merge($groups, $functions);
		
		$groups[$user_label][] = JHtml::_('select.option', '{{ user.name }}', JText::_('JFIELD_NAME_LABEL'));
		$groups[$user_label][] = JHtml::_('select.option', '{{ user.email }}', JText::_('JGLOBAL_EMAIL'));
		
		$groups[$friend_label][] = JHtml::_('select.option', '{{ friend.name }}', JText::_('JFIELD_NAME_LABEL'));
		$groups[$friend_label][] = JHtml::_('select.option', '{{ friend.email }}', JText::_('JGLOBAL_EMAIL'));
		
		$groups[$comment_label][] = JHtml::_('select.option', '{{ comment }}', JText::_('COM_VPEM_LBL_COMMENT'));
		
		$product_vars = [
			'virtuemart_product_id',
			'virtuemart_vendor_id',
			'product_available_date',
			'product_basePriceWithTax',
			'product_desc',
			'product_discountedPriceWithoutTax',
			'product_final_price',
			'product_in_stock',
			'product_item_price',
			'product_lwh_uom',
			'product_mpn',
			'product_gtin',
			'product_name',
			'product_variant',
			'product_ordered',
			'product_parent_id',
			'product_priceWithoutTax',
			'product_quantity',
			'product_s_desc',
			'product_sales',
			'product_sku',
			'category_name',
			'product_special',
			'product_subtotal_discount',
			'product_subtotal_with_tax',
			'product_tax',
			'product_unit',
			'product_url',
			'product_weight',
			'product_weight_uom',
			'low_stock_notification',
			'slug',
			'virtuemart_category_id',
			'virtuemart_manufacturercategories_id',
			'mf_desc',
			'mf_email',
			'mf_name',
			'mf_url',
		];
		
		foreach ($product_vars as $property)
		{
			$name  = 'COM_VPEM_' . strtoupper($property);
			$value = '{{ product.' . $property . ' }}';

			$groups[$products_label][] = JHtml::_('select.option', htmlspecialchars($value), JText::_($name));
		}
		
		$groups[$products_label][] = JHtml::_('select.option', '{{ product_thumbnail }}', JText::_('COM_VPEM_PRODUCT_THUMBNAIL'));
		
		return $groups;
	}
	
	
	public function getProductNotifyVariables()
	{
		VmConfig::loadJLang('com_virtuemart');
		VmConfig::loadJLang('com_virtuemart', true);
		VmConfig::loadJLang('com_virtuemart_shoppers', true);
		VmConfig::loadJLang('com_virtuemart_orders', true);
		
		$products_label  = JText::_('COM_VPEM_PRODUCTS_GROUP');
		$groups          = [];
		$vendorGroup     = $this->getVendorVariables();
		$groups          = array_merge($vendorGroup, $groups);

		$groups[$products_label] = [];
		
		$functions = $this->getFunctions();
		$groups    = array_merge($groups, $functions);
		
		$groups[$products_label][] = JHtml::_('select.option', '{{ product_name }}', JText::_('COM_VPEM_PRODUCT_NAME'));
		$groups[$products_label][] = JHtml::_('select.option', '{{ product_link }}', JText::_('COM_VPEM_PRODUCT_LINK'));
		$groups[$products_label][] = JHtml::_('select.option', '{{ mail_body }}', JText::_('COM_VPEM_MAIL_BODY'));
		
		return $groups;
	}
	
	public function getUserVariables()
	{
		VmConfig::loadJLang('com_virtuemart');
		VmConfig::loadJLang('com_virtuemart', true);
		VmConfig::loadJLang('com_virtuemart_shoppers', true);
		VmConfig::loadJLang('com_virtuemart_orders', true);
		
		$user_label  = JText::_('COM_VPEM_USER_GROUP');
		$groups      = [];
		$vendorGroup = $this->getVendorVariables();
		$groups      = array_merge($vendorGroup, $groups);
		
		$groups[$user_label] = [];
		
		$functions = $this->getFunctions();
		$groups    = array_merge($groups, $functions);
		
		JLoader::register('VirtueMartModelUserfields', VMPATH_ADMIN . '/models/userfields.php');
		
		$userFieldsModel = VmModel::getModel('userfields');
		
		$bt_fields = $userFieldsModel->getUserFields(
			'account',
			['captcha' => true, 'delimiters' => true, 'hidden' => true], // Ignore these types
			['delimiter_userinfo', 'user_is_vendor', 'password2', 'agreed', 'address_type'] // Skips
		);
		
		foreach ($bt_fields as $field)
		{
			if ($field->name == 'virtuemart_country_id')
			{
				$groups[$user_label][] = JHtml::_('select.option', '{{ user.title.virtuemart_country_name }}', JText::_('COM_VIRTUEMART_TITLE') . ': ' . JText::_($field->title));
				$groups[$user_label][] = JHtml::_('select.option', '{{ user.virtuemart_country_name }}', JText::_($field->title));
			}
			elseif ($field->name == 'virtuemart_state_id')
			{
				$groups[$user_label][] = JHtml::_('select.option', '{{ user.title.virtuemart_state_name }}', JText::_('COM_VIRTUEMART_TITLE') . ': ' . JText::_($field->title));
				$groups[$user_label][] = JHtml::_('select.option', '{{ user.virtuemart_state_name }}', JText::_($field->title));
			}
			else
			{
				$groups[$user_label][] = JHtml::_('select.option', '{{ user.title.' . $field->name . ' }}', JText::_('COM_VIRTUEMART_TITLE') . ': ' . JText::_($field->title));
				$groups[$user_label][] = JHtml::_('select.option', '{{ user.' . $field->name . ' }}', JText::_($field->title));
			}
		}
		
		$groups[$user_label][] = JHtml::_('select.option', '{{ user.activationLink }}', JText::_('COM_VPEM_USER_ACTIVATION_LINK'));
		$groups[$user_label][] = JHtml::_('select.option', '{{# userFields }} {{ title }} {{/ userFields }}', JText::_('COM_VPEM_USER_ALL_USER_FIELDS_TITLE'));
		$groups[$user_label][] = JHtml::_('select.option', '{{# userFields }} {{ value }} {{/ userFields }}', JText::_('COM_VPEM_USER_ALL_USER_FIELDS_VALUE'));
		
		return $groups;
	}
}
