<?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\Language\Text;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Form\Field\TextField;

/**
 * Form Field for ip address
 * @package JREALTIMEANALYTICS::CONFIG::components::com_jrealtimeanalytics
 * @subpackage Field
 * @since 2.0
 */
class IpaddressField extends TextField {
	/**
	 * The form field type.
	 *
	 * @var    string
	 *
	 * @since  11.1
	 */
	protected $type = 'ipaddress';
	
	/**
	 * Method to get the field input markup for a generic list.
	 * Use the multiple attribute to enable multiselect.
	 *
	 * @return string The field input markup.
	 *
	 * @since 11.1
	 */
	protected function getInput() {
		// Translate placeholder text
		$hint = $this->translateHint ? Text::_ ( $this->hint ) : $this->hint;
		
		// Initialize some field attributes.
		$size = ! empty ( $this->size ) ? ' size="' . $this->size . '"' : '';
		$maxLength = ! empty ( $this->maxLength ) ? ' maxlength="' . $this->maxLength . '"' : '';
		$class = ! empty ( $this->class ) ? ' class="form-control ' . $this->class . '"' : ' class="form-control"';
		$readonly = $this->readonly ? ' readonly' : '';
		$disabled = $this->disabled ? ' disabled' : '';
		$required = $this->required ? ' required aria-required="true"' : '';
		$hint = $hint ? ' placeholder="' . $hint . '"' : '';
		$autocomplete = ! $this->autocomplete ? ' autocomplete="off"' : ' autocomplete="' . $this->autocomplete . '"';
		$autocomplete = $autocomplete == ' autocomplete="on"' ? '' : $autocomplete;
		$autofocus = $this->autofocus ? ' autofocus' : '';
		$spellcheck = $this->spellcheck ? '' : ' spellcheck="false"';
		$pattern = ! empty ( $this->pattern ) ? ' pattern="' . $this->pattern . '"' : '';
		$inputmode = ! empty ( $this->inputmode ) ? ' inputmode="' . $this->inputmode . '"' : '';
		$dirname = ! empty ( $this->dirname ) ? ' dirname="' . $this->dirname . '"' : '';
		$list = '';
		
		// Initialize JavaScript field attributes.
		$onchange = ! empty ( $this->onchange ) ? ' onchange="' . $this->onchange . '"' : '';
		
		// Including fallback code for HTML5 non supported browsers.
		$document = Factory::getApplication()->getDocument();
		$document->getWebAssetManager()->useScript('jquery');
		$document->getWebAssetManager()->useScript('jquery-noconflict');
		
		// Get the field suggestions.
		if(method_exists($this, 'getSuggestions')) {
			$options = ( array ) $this->getSuggestions ();
			if (! empty ( $options )) {
				$html [] = HTMLHelper::_ ( 'select.suggestionlist', $options, 'value', 'text', $this->id . '_datalist"' );
				$list = ' list="' . $this->id . '_datalist"';
			}
		}
		
		$html [] = '<input type="text" data-validation="ipaddress" name="' . $this->name . '" id="' . $this->id . '"' . $dirname . ' value="' . htmlspecialchars ( $this->value, ENT_COMPAT, 'UTF-8' ) . '"' . $class . $size . $disabled . $readonly . $list . $hint . $onchange . $maxLength . $required . $autocomplete . $autofocus . $spellcheck . $inputmode . $pattern . ' />';
		
		return implode ( $html );
	}
}
