<?php
/**
 * @package     Reporter 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
 * @author      Abhishek Das <info@virtueplanet.com> - https://www.virtueplanet.com
 */

defined('_JEXEC') or die;

use Joomla\Registry\Registry;

JFormHelper::loadFieldClass('list');

JLoader::register('VmConfig', JPATH_ADMINISTRATOR . '/components/com_virtuemart/helpers/config.php');

class JFormFieldVMShoppergroups extends JFormFieldList
{
	public $type = 'VMShoppergroups';
	
	protected static $groups = null;
	
	protected function getOptions()
	{
		$options = array();
		
		VmConfig::loadConfig();
		VmConfig::loadJLang('com_virtuemart');
		VmConfig::loadJLang('com_virtuemart_shoppers', true);
		
		if (self::$groups === null)
		{
			$db    = JFactory::getDBO();
			$query = $db->getQuery(true);
			
			$query->select($db->quoteName('virtuemart_shoppergroup_id') . ' AS value')
				->select($db->quoteName('shopper_group_name') . ' AS text')
				->from($db->quoteName('#__virtuemart_shoppergroups'))
				->where($db->quoteName('published') . ' = 1')
				->order($db->quoteName('virtuemart_shoppergroup_id') . ' ASC');
			
			try
			{
				$db->setQuery($query);
				self::$groups = $db->loadObjectList();
			}
			catch (Exception $e)
			{
				JError::raiseWarning(500, $e->getMessage());
				return false;
			}
		}

		foreach (self::$groups as $group)
		{
			$options[] = JHtml::_('select.option', $group->value, JText::_($group->text));
		}
		
		return array_merge(parent::getOptions(), $options);
	}
}