<?php
/**
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @copyright   Copyright (C) 2016-2024 CMExension
 * @license     GNU General Public License version 2 or later
 */

defined('JPATH_BASE') or die();

require_once JPATH_ROOT . '/administrator/components/com_cmgiftcard/models/base_fields/checkbox.php';

use Joomla\CMS\Language\Text;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Form\Field\CheckboxField;

/**
 * Form field for terms and conditions agreement.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class JFormFieldCMGCTOS extends CMGiftCardCheckboxField
{
	/**
	 * The form field type.
	 *
	 * @var    string
	 * @since  1.0.0
	 */
	protected $type = 'CMGCTOS';

	/**
	 * Method to get the field input markup.
	 * The checked element sets the field to selected.
	 *
	 * @return  string  The field input markup.
	 *
	 * @since   1.0.0
	 */
	protected function getInput()
	{
		// Initialize some field attributes.
		$class     = !empty($this->class) ? ' class="' . $this->class . '"' : '';
		$disabled  = $this->disabled ? ' disabled' : '';
		$value     = !empty($this->default) ? $this->default : '1';
		$required  = $this->required ? ' required aria-required="true"' : '';
		$autofocus = $this->autofocus ? ' autofocus' : '';
		$checked   = $this->checked || !empty($this->value) ? ' checked' : '';

		// Initialize JavaScript field attributes.
		$onclick  = !empty($this->onclick) ? ' onclick="' . $this->onclick . '"' : '';
		$onchange = !empty($this->onchange) ? ' onchange="' . $this->onchange . '"' : '';

		// Including fallback code for HTML5 non supported browsers.
		HTMLHelper::_('jquery.framework');
		HTMLHelper::_('script', 'system/html5fallback.js');

		$html = '<label class="checkbox">';
		$html .= '<input type="checkbox" name="' . $this->name . '" id="' . $this->id . '" value="'
			. htmlspecialchars($value, ENT_COMPAT, 'UTF-8') . '"' . $class . $checked . $disabled . $onclick . $onchange
			. $required . $autofocus . ' />';

		$tosMenuItemId = (int) ComponentHelper::getParams('com_cmgiftcard')->get('tos_menu_item');

		if ($tosMenuItemId > 0)
		{
			$tos = '<a href="' . Route::_('index.php?Itemid=' . $tosMenuItemId) . '" target="_blank">' . Text::_('COM_CMGIFTCARD_TOS') . '</a>';
		}
		else
		{
			$tos = Text::_('COM_CMGIFTCARD_TOS');
		}

		$label = str_replace('%s', $tos, Text::_('COM_CMGIFTCARD_TOS_AGREEMENT'));

		$html .= ' ' . $label . '</label>';

		return $html;
	}
}