<?php
/*
 * Software Name: Eorisis Pixels
 * Software URI: https://eorisis.com/shop/software/joomla/pixels
 * Author: Eorisis
 * Author URI: https://eorisis.com
 * Copyright: Copyright (C) 2011-2025 EORISIS. All Rights Reserved.
 * License: GNU General Public License version 2; See https://eorisis.com/gpl
 * Terms of Service: https://eorisis.com/tos
 */
defined('_JEXEC') or die;

use com_eorisis_pixels\framework\lib\core\cms\language as cms_language;
use com_eorisis_pixels\framework\lib\core\model\form as form_model;
use com_eorisis_pixels\framework\lib\core\model\language as language_model;
use com_eorisis_pixels\framework\lib\core\model\params;
use com_eorisis_pixels\framework\lib\core\model\clean;
use com_eorisis_pixels\framework\lib\core\model\math;
use com_eorisis_pixels\framework\lib\core\view\help;

if (!class_exists('EorisisFormField', false))
{
	if (version_compare(JVERSION, '3.8', '>='))
	{
		JLoader::registerAlias('EorisisFormField', '\\Joomla\\CMS\\Form\\FormField');
	}
	else
	{
		JLoader::registerAlias('EorisisFormField', 'JFormField');
	}
}

class JFormFieldTxtarea extends EorisisFormField
{
	protected $type = 'txtarea';
	protected $form;

	// --------------------------------------------------

	protected function getLabel() : string
	{
		if (!params::field_value('label', $this->element))
		{
			return '';
		}

		return parent::getLabel().help::icon($this->element);
	}

	// --------------------------------------------------

	protected function getInput() : string
	{
		form_model::set($this->form);
		$id = (string)$this->element['name'];

		if ($this->default == '')
		{
			$language_key = params::field_value('language_key', $this->element);
			if ($language_key)
			{
				$language_file = params::field_value('language_file', $this->element);
				if ($language_file)
				{
					language_model::jload($language_file);
				}

				$this->default = cms_language::text($language_key);
			}
		}

		$value = (string)form_model::get($id, $this->default);
		if ($value)
		{
			if (params::field_value('inline', $this->element) == 'true')
			{
				$value = str_replace(["\r\n","\n","\r"], ' ', (string)$value);
			}

			$value = clean::string($value);
		}

		$args = [];

		if ($this->class)
		{
			$args['class'] = $this->class;
		}

		$cols = math::whole_number(params::field_value('cols', $this->element));
		if ($cols)
		{
			$args['cols'] = $cols;
		}

		$rows = math::whole_number(params::field_value('rows', $this->element));
		if ($rows)
		{
			$args['rows'] = $rows;
		}

		return form_model::textarea($id, $value, $args);
	}
}
