<?php
namespace JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\Html;
/**  
 * @package JREALTIMEANALYTICS::CONFIG::components::com_jrealtimeanalytics
 * @subpackage Field
 * @author Joomla! Extensions Store
 * @copyright (C) 2013 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html   
 */ 
defined( '_JEXEC' ) or die( 'Restricted access' );
use Joomla\CMS\Language\Text;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;

/**
 * Form Field for the extensions list
 * @package JREALTIMEANALYTICS::CONFIG::components::com_jrealtimeanalytics
 * @subpackage Html
 * @since 2.0
 */
class Extensions {
	/**
	 * The form field type.
	 *
	 * @var    string
	 *
	 * @since  11.1
	 */
	public $type = 'extensions';

	/**
	 * Method to get the field options.
	 *
	 * @return  array  The field option objects.
	 *
	 * @since   11.1
	 */
	public static function getOptions() {
		$items = array();
		
		$db = Factory::getContainer()->get('DatabaseDriver');
		$queryComponent = "SELECT DISTINCT " . $db->quoteName('element') . " AS value, SUBSTRING(" . $db->quoteName('element') . ", 5) AS text" .
						  "\n FROM #__extensions" .
						  "\n WHERE (" . $db->quoteName('protected') . " = 0" .
						  "\n OR " . $db->quoteName('element') . " = " . $db->quote('com_content') . // Exception for custom content sources
						  "\n OR " . $db->quoteName('element') . " = " . $db->quote('com_tags') . // Exception for custom tags sources
						  "\n OR " . $db->quoteName('element') . " = " . $db->quote('com_docman') . ")" . // Exception for docman 2 protected
		 				  "\n AND ". $db->quoteName('type') . " = " . $db->quote('component') .
		 				  "\n AND ". $db->quoteName('element') . " != " . $db->quote('com_jrealtimeanalytics') .
		 				  "\n ORDER BY text ASC";
		$db->setQuery($queryComponent);
		
		// Check for a database error.
		try {
			$items = $db->loadObjectList ();
		} catch(\Exception $e) {
			return array();
		}
		
		foreach ( $items as &$item ) {
			$options[] = HTMLHelper::_ ( 'select.option', $item->value, $item->text );
		}
		
		$noActiveOption = HTMLHelper::_('select.option', '0', Text::_('COM_JREALTIME_ALL_COMPONENTS'));
		array_unshift($options, $noActiveOption);
		
		return $options;
	}
}
