<?php
namespace JExtstore\Component\JRealtimeAnalytics\Administrator\Field;
/**  
 * @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\Form\FormField;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;

/**
 * Form Field for the module status
 * @package JREALTIMEANALYTICS::components::com_jrealtimeanalytics
 * @subpackage framework 
 * @subpackage html
 * @since 2.0
 */
class ModulestatusField extends FormField {
	/**
	 * The form field type.
	 *
	 * @var string
	 * @since 11.1
	 */
	protected $type = 'modulestatus';
	
	/**
	 * Method to get the radio button field input markup.
	 *
	 * @return string The field input markup.
	 *        
	 * @since 11.1
	 */
	protected function getInput() {
		// Initialize variables.
		$html = null;
		$isModulePublished = null;
		
		// Retrieve status informations about the chat module
		$db = Factory::getContainer()->get('DatabaseDriver');
		$queryModuleStatus = "SELECT id, published, position" .
							 "\n FROM #__modules" .
							 "\n WHERE " . $db->quoteName('module') . "=" . $db->quote('mod_jrealtimeanalytics') .
							 "\n AND " . $db->quoteName('published') . ">= 0" ;
		$db->setQuery($queryModuleStatus);
		$publishedModule = $db->loadObject();
		if (is_object($publishedModule)) {
			$isModulePublished = $publishedModule->published && ($publishedModule->position != '');
		}
		
		// Initialize some field attributes.
		
		if ($isModulePublished) {
			$html =	'<a target="_blank" href="index.php?option=com_modules&amp;task=module.edit&amp;id=' . $publishedModule->id . '">' .
					'<span data-bs-content="' . Text::sprintf ( 'COM_JREALTIME_MODULE_ENABLED_DESC', $publishedModule->position) . 
					'" class="badge bg-success label-large hasPopover">' . '<span class="icon-checkmark" aria-hidden="true"></span>' . 
					Text::sprintf ( 'COM_JREALTIME_MODULE_ENABLED' ) . '</span></a>';
		} elseif(is_object($publishedModule)) {
			$html = '<a target="_blank" href="index.php?option=com_modules&amp;task=module.edit&amp;id=' . $publishedModule->id . '">' .
					'<span data-bs-content="' . Text::_ ( 'COM_JREALTIME_MODULE_DISABLED_DESC' ) . 
					'" class="badge bg-danger label-large hasPopover">' . '<span class="icon-remove" aria-hidden="true"></span>' . 
					Text::sprintf ( 'COM_JREALTIME_MODULE_DISABLED' ) . '</span></a>';
		} else {
			$html = '<span data-bs-content="' . Text::_ ( 'COM_JREALTIME_MODULE_NOTFOUND_DESC' ) .
					'" class="badge bg-danger label-large hasPopover">' . '<span class="icon-remove" aria-hidden="true"></span>' .
					Text::sprintf ( 'COM_JREALTIME_MODULE_NOTFOUND' ) . '</span>';
		}
		
		return $html;
	}
}
