<?php
/**
 * @package DJ-Catalog2
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://dj-extensions.com
 * @author email contact@dj-extensions.com
 */

use Joomla\Registry\Registry;

defined('_JEXEC') or die();
defined('JPATH_BASE') or die;

jimport('joomla.html.html');
jimport('joomla.form.formfield');


class JFormFieldDJEventLocation extends JFormField {
	
	protected $type = 'djeventlocation';
	
	protected function getInput()
	{
		$lang = JFactory::getLanguage();
		$lang->load('com_djevents', JPATH_ADMINISTRATOR, 'en-GB', false, false);
		$lang->load('com_djevents', JPATH_COMPONENT_ADMINISTRATOR, 'en-GB', false, false);
		$lang->load('com_djevents', JPATH_ADMINISTRATOR, null, true, false);
		$lang->load('com_djevents', JPATH_COMPONENT_ADMINISTRATOR, null, true, false);

		$app = JFactory::getApplication();
		$params = JComponentHelper::getParams('com_djevents');
		
		$id    = isset($this->element['id']) ? $this->element['id'] : null;
		$cssId = '#' . $this->getId($id, $this->element['name']);
		
		$limit = $this->multiple ? 0 : 1;
		$context = (isset($this->element['context'])) ? $this->element['context'] : null;
		
		$urlVars = 'index.php?option=com_djevents&task=eventform.searchLocationAjax' . (($context != null) ? '&context=' . trim($context) : '');
		$url = $app->isClient('site') ? JUri::root() . $urlVars : JUri::base() . $urlVars;
		
		$chosenAjaxSettings = new Registry(
			array(
				'selector'      => $cssId,
				'type'          => 'GET',
				'url'           => $url,
				'dataType'      => 'json',
				'jsonTermKey'   => 'like',
				'minTermLength' => 2,
				'disable_search_threshold' => 20
			)
		);
		
		$chosenSettings = array();
		if ($limit > 0) {
			$chosenSettings['max_selected_options'] = (int)$limit;
		}
		$chosenSettings['disable_search_threshold'] = 0;
		$chosenSettings['placeholder_text_multiple'] = JText::_('COM_DJEVENTS_CONFIG_AJAX_USER_PLACEHOLDER');
		$chosenSettings['placeholder_text_single'] = JText::_('COM_DJEVENTS_CONFIG_AJAX_USER_PLACEHOLDER');
		$chosenSettings = new Registry($chosenSettings);


		if(!isset($this->element['chosen'])) {
			JHtml::_('formbehavior.chosen', $cssId, null, $chosenSettings);
			JHtml::_('formbehavior.ajaxchosen', $chosenAjaxSettings);
		}
		
		// Initialize some field attributes.
		$size		= $this->element['size'] ? ' size="'.(int) $this->element['size'].'"' : '';
		$maxLength	= $this->element['maxlength'] ? ' maxlength="'.(int) $this->element['maxlength'].'"' : '';
		$attr = '';
		
		$class = $this->element['class'] ? (string) $this->element['class'] : '';
		$class .= $this->element['required']=='true' ? ' required' : '';
		$class .= $this->element['readonly']=='true' ? ' readonly' : '';
		
		$attr .= 'class="'.$class.'"';
		$name = $this->name;
		if (isset($this->element['multiple']) && $this->element['multiple'] == 'true') {
			$attr .= ' multiple="true" size="10"';
		} else {
			// such situations should not happen, but if they do (it's possible) we need to assign at least one group of fields
			if (is_array($this->value)) {
				$newValue = 0;
				foreach ($this->value as $v) {
					if ($v > 0) {
						$newValue = $v;
						break;
					}
				}
				$this->value = $newValue;
			}
		}
		
		if ($this->onchange != '') {
			$attr .= ' onchange="'.trim($this->onchange).'"';
		}
		
		if ($this->readonly) {
			$attr .= ' readonly="readonly"';
		}
		
		if ($this->disabled) {
			$attr .= ' disabled="disabled"';
		}
		
		$db = JFactory::getDbo();
		$db->setQuery('SELECT id AS value, name AS text FROM #__djev_locations ORDER BY name');
		$partners = $db->loadObjectList();
		$options = array();
		
		$isMultiple = (bool)(isset($this->element['multiple']) && ($this->element['multiple'] != 'true' || $this->element['multiple'] != 'multiple'));
		$isRequired = (bool)(isset($this->element['required']) && ($this->element['required'] != 'true' || $this->element['required'] != 'required'));
		
		if (!$isRequired) {
			if (!$isMultiple) {
				$options[] = JHTML::_('select.option', '' , JText::_('COM_DJEVENTS_LIST_PLEASE_SELECT'));
			} else {
				$options[] = JHTML::_('select.option',0 , JText::_('COM_DJEVENTS_LOCATION_NONE'));
			}
		} else {
			$options[] = JHTML::_('select.option', '' , JText::_('COM_DJEVENTS_LIST_PLEASE_SELECT'));
		}
		
		foreach ($partners as $partner) {
			$options[] = JHtml::_('select.option', $partner->value, $partner->text);
		}
		
		$out = JHtml::_('select.genericlist', $options, $name, trim($attr), 'value', 'text', $this->value, $this->id);
		
		return ($out);
	}
}
?>
