<?php
namespace T4Admin;
use Joomla\CMS\Form\Form as JForm;
/**
 * Radio List Element
 *
 * @package  JAT4.Core.Element
 */
class T4Form extends JForm
{

	public function __construct($name, array $options = array()){

		if($name instanceof JForm){
			
			foreach($name as $property => $value) {
				$this->$property = $value;
			}

		} else {
			parent::__construct($name, $options);
		}
	}


	/**
	 * Method to load the form description from an XML string or object.
	 *
	 * The replace option works per field.  If a field being loaded already exists in the current
	 * form definition then the behavior or load will vary depending upon the replace flag.  If it
	 * is set to true, then the existing field will be replaced in its exact location by the new
	 * field being loaded.  If it is false, then the new field being loaded will be ignored and the
	 * method will move on to the next field to load.
	 *
	 * @param   string  $data     The name of an XML string or object.
	 * @param   string  $replace  Flag to toggle whether form fields should be replaced if a field
	 *                            already exists with the same group/name.
	 * @param   string  $xpath    An optional xpath to search for the fields.
	 *
	 * @return  boolean  True on success, false otherwise.
	 *
	 * @since   11.1
	 */
	public function load($data, $replace = true, $xpath = false)
	{
		// If the data to load isn't already an XML element or string return false.
		if ((!($data instanceof \SimpleXMLElement)) && (!is_string($data)))
		{
			return false;
		}
		// Attempt to load the XML if a string.
		if (is_string($data))
		{
			try
			{
				$data = new \SimpleXMLElement($data);
			}
			catch (\Exception $e)
			{
				return false;
			}

			// Make sure the XML loaded correctly.
			if (!$data)
			{
				return false;
			}
		}

		// If we have no XML definition at this point let's make sure we get one.
		if (empty($this->xml))
		{
			// If no XPath query is set to search for fields, and we have a <form />, set it and return.
			if (!$xpath && ($data->getName() == 'form'))
			{
				$this->xml = $data;

				// Synchronize any paths found in the load.
				$this->syncPaths();

				return true;
			}
			// Create a root element for the form.
			else
			{
				$this->xml = new \SimpleXMLElement('<form></form>');
			}
		}

		// Get the XML elements to load.
		$elements = array();
		if ($xpath)
		{
			$elements = $data->xpath($xpath);
		}
		elseif ($data->getName() == 'form')
		{
			$elements = $data->children();
		}

		// If there is nothing to load return true.
		if (empty($elements))
		{
			return true;
		}

		// Load the found form elements.
		foreach ($elements as $element)
		{
			// Get an array of fields with the correct name.
			$fields = $element->xpath('descendant-or-self::field');
			foreach ($fields as $field)
			{
				// Get the group names as strings for ancestor fields elements.
				$attrs = $field->xpath('ancestor::fields[@name]/@name');
				$groups = array_map('strval', $attrs ? $attrs : array());

				// Check to see if the field exists in the current form.
				if ($current = $this->findField((string) $field['name'], implode('.', $groups)))
				{

					// If set to replace found fields, replace the data and remove the field so we don't add it twice.
					if ($replace)
					{
						$olddom = dom_import_simplexml($current);
						$loadeddom = dom_import_simplexml($field);
						$addeddom = $olddom->ownerDocument->importNode($loadeddom, true); // Import child nodes too
						$olddom->parentNode->replaceChild($addeddom, $olddom);
						$loadeddom->parentNode->removeChild($loadeddom);
					}
					else
					{
						unset($field);
					}
				}
				//check to see if new field
				if((string)$field['before'] || (string)$field['after']){
					if($insertBefore = $this->findField((string)$field['before'], implode('.', $groups))){
						$field['class'] = $field['class'] . " before-".$field['before'];
						$dom = dom_import_simplexml($insertBefore);
						$loadeddom = dom_import_simplexml($field);
						// Get the parent element, this should be the fieldset
						$addeddom = $dom->ownerDocument->importNode($loadeddom, true);
						$dom->parentNode->appendChild($addeddom);
					}elseif($insertAfter = $this->findField((string)$field['after'], implode('.', $groups))){
						$field['class'] = $field['class'] . " after-".$field['after'];
						$dom = dom_import_simplexml($insertAfter);
						$loadeddom = dom_import_simplexml($field);
						// Get the parent element, this should be the fieldset
						$addeddom = $dom->ownerDocument->importNode($loadeddom, true);
						$dom->parentNode->appendChild($addeddom);
					}else{
						unset($field);
					}
				}
			}

			// Merge the new field data into the existing XML document.
			self::addNode($this->xml, $element);
		}

		// Synchronize any paths found in the load.
		$this->syncPaths();

		return true;
	}
}