/plugins/system/t3/includes/depend/t3form.php

https://gitlab.com/lankerd/paGO---Testing-Site · PHP · 162 lines · 89 code · 21 blank · 52 comment · 15 complexity · 392d749a3cf4d1922c1f4ebc4666217b MD5 · raw file

  1. <?php
  2. /**
  3. *------------------------------------------------------------------------------
  4. * @package T3 Framework for Joomla!
  5. *------------------------------------------------------------------------------
  6. * @copyright Copyright (C) 2004-2013 JoomlArt.com. All Rights Reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. * @authors JoomlArt, JoomlaBamboo, (contribute to this project at github
  9. * & Google group to become co-author)
  10. * @Google group: https://groups.google.com/forum/#!forum/t3fw
  11. * @Link: http://t3-framework.org
  12. *------------------------------------------------------------------------------
  13. */
  14. // No direct access
  15. defined('_JEXEC') or die();
  16. /**
  17. * Radio List Element
  18. *
  19. * @package JAT3.Core.Element
  20. */
  21. class T3Form extends JForm
  22. {
  23. public function __construct($name, array $options = array()){
  24. if($name instanceof JForm){
  25. foreach($name as $property => $value) {
  26. $this->$property = $value;
  27. }
  28. } else {
  29. parent::__construct($name, $options);
  30. }
  31. }
  32. /**
  33. * Method to load the form description from an XML string or object.
  34. *
  35. * The replace option works per field. If a field being loaded already exists in the current
  36. * form definition then the behavior or load will vary depending upon the replace flag. If it
  37. * is set to true, then the existing field will be replaced in its exact location by the new
  38. * field being loaded. If it is false, then the new field being loaded will be ignored and the
  39. * method will move on to the next field to load.
  40. *
  41. * @param string $data The name of an XML string or object.
  42. * @param string $replace Flag to toggle whether form fields should be replaced if a field
  43. * already exists with the same group/name.
  44. * @param string $xpath An optional xpath to search for the fields.
  45. *
  46. * @return boolean True on success, false otherwise.
  47. *
  48. * @since 11.1
  49. */
  50. public function load($data, $replace = true, $xpath = false)
  51. {
  52. // If the data to load isn't already an XML element or string return false.
  53. if ((!($data instanceof SimpleXMLElement)) && (!is_string($data)))
  54. {
  55. return false;
  56. }
  57. // Attempt to load the XML if a string.
  58. if (is_string($data))
  59. {
  60. try
  61. {
  62. $data = new SimpleXMLElement($data);
  63. }
  64. catch (Exception $e)
  65. {
  66. return false;
  67. }
  68. // Make sure the XML loaded correctly.
  69. if (!$data)
  70. {
  71. return false;
  72. }
  73. }
  74. // If we have no XML definition at this point let's make sure we get one.
  75. if (empty($this->xml))
  76. {
  77. // If no XPath query is set to search for fields, and we have a <form />, set it and return.
  78. if (!$xpath && ($data->getName() == 'form'))
  79. {
  80. $this->xml = $data;
  81. // Synchronize any paths found in the load.
  82. $this->syncPaths();
  83. return true;
  84. }
  85. // Create a root element for the form.
  86. else
  87. {
  88. $this->xml = new SimpleXMLElement('<form></form>');
  89. }
  90. }
  91. // Get the XML elements to load.
  92. $elements = array();
  93. if ($xpath)
  94. {
  95. $elements = $data->xpath($xpath);
  96. }
  97. elseif ($data->getName() == 'form')
  98. {
  99. $elements = $data->children();
  100. }
  101. // If there is nothing to load return true.
  102. if (empty($elements))
  103. {
  104. return true;
  105. }
  106. // Load the found form elements.
  107. foreach ($elements as $element)
  108. {
  109. // Get an array of fields with the correct name.
  110. $fields = $element->xpath('descendant-or-self::field');
  111. foreach ($fields as $field)
  112. {
  113. // Get the group names as strings for ancestor fields elements.
  114. $attrs = $field->xpath('ancestor::fields[@name]/@name');
  115. $groups = array_map('strval', $attrs ? $attrs : array());
  116. // Check to see if the field exists in the current form.
  117. if ($current = $this->findField((string) $field['name'], implode('.', $groups)))
  118. {
  119. // If set to replace found fields, replace the data and remove the field so we don't add it twice.
  120. if ($replace)
  121. {
  122. $olddom = dom_import_simplexml($current);
  123. $loadeddom = dom_import_simplexml($field);
  124. $addeddom = $olddom->ownerDocument->importNode($loadeddom, true); // Import child nodes too
  125. $olddom->parentNode->replaceChild($addeddom, $olddom);
  126. $loadeddom->parentNode->removeChild($loadeddom);
  127. }
  128. else
  129. {
  130. unset($field);
  131. }
  132. }
  133. }
  134. // Merge the new field data into the existing XML document.
  135. self::addNode($this->xml, $element);
  136. }
  137. // Synchronize any paths found in the load.
  138. $this->syncPaths();
  139. return true;
  140. }
  141. }