PageRenderTime 111ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 1ms

/contentmanager/code/trunk/administrator/components/com_contentmanager/libraries/jxtended/form/model.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 265 lines | 129 code | 26 blank | 110 comment | 17 complexity | 9cb88ae7c46a1a872aa478b07745c996 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: model.php 160 2009-07-09 00:06:09Z eddieajau $
  4. * @package JXtended.Libraries
  5. * @subpackage Form
  6. * @copyright Copyright (C) 2008 - 2009 JXtended, LLC. All rights reserved.
  7. * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
  8. * @link http://jxtended.com
  9. */
  10. defined('JPATH_BASE') or die;
  11. /**
  12. * Form model object
  13. *
  14. * @package JXtended.Libraries
  15. * @subpackage Forms
  16. */
  17. class JXFormModel extends JObject
  18. {
  19. /**
  20. * Form name
  21. * @access private
  22. * @var string
  23. */
  24. var $_id = null;
  25. /**
  26. * Form Descriptor
  27. * @access private
  28. * @var object
  29. */
  30. var $_descriptor = null;
  31. /**
  32. * Internal values
  33. */
  34. var $_values = null;
  35. /**
  36. * Constructor
  37. *
  38. * @access protected
  39. * @param string $descriptor XML Form descriptor string
  40. * @param string $data INI Form value data string
  41. * @return void
  42. * @since 1.5
  43. */
  44. function __construct($descriptor = null)
  45. {
  46. $this->_load($descriptor, false);
  47. $this->_values = array();
  48. }
  49. /**
  50. * Loads a form model xml file and sets the form descriptor object
  51. *
  52. * @access public
  53. * @param string $path Path to the form model xml file
  54. * @return boolean True if successful
  55. * @since 1.5
  56. */
  57. function loadFormFile($path)
  58. {
  59. return $this->_load($path, true);
  60. }
  61. /**
  62. * Loads a form model descriptor from an xml string and sets the form descriptor object
  63. *
  64. * @access public
  65. * @param string $descriptor XML Form descriptor string
  66. * @return boolean True if successful
  67. * @since 1.5
  68. */
  69. function loadFormString($descriptor)
  70. {
  71. return $this->_load($descriptor, false);
  72. }
  73. /**
  74. * Get the form id
  75. *
  76. * @access public
  77. * @param string $default Default name if internal id is not set
  78. * @return string Form name
  79. * @since 1.5
  80. */
  81. function getId($default = null)
  82. {
  83. return ($this->_id) ? $this->_id : $default;
  84. }
  85. /**
  86. * Set the form id
  87. *
  88. * @access public
  89. * @param string $value Form id to set
  90. * @return void
  91. * @since 1.5
  92. */
  93. function setId($value)
  94. {
  95. $this->_id = $value;
  96. }
  97. /**
  98. * Gets an internal value
  99. * @param string The key of the value
  100. * @param mixed An optional default if the key is not found
  101. * @return mixed
  102. */
  103. function getValue($key, $def = null)
  104. {
  105. return isset($this->_values[$key]) ? $this->_values[$key] : $def;
  106. }
  107. /**
  108. * Gets the associate array of internal values
  109. * @return array
  110. */
  111. function getValues()
  112. {
  113. return $this->_values;
  114. }
  115. /**
  116. * Sets an internal value
  117. * @param string The key of the value
  118. * @param mixed The value
  119. */
  120. function setValue($key, $value)
  121. {
  122. $this->_values[$key] = $value;
  123. }
  124. /**
  125. * Filter the input array based on the XML specification
  126. *
  127. * @param array
  128. */
  129. function filter(&$input)
  130. {
  131. // Static input filters for specific settings
  132. static $noHtmlFilter = null;
  133. static $safeHtmlFilter = null;
  134. if (is_null($safeHtmlFilter)) {
  135. $safeHtmlFilter = & JFilterInput::getInstance(null, null, 1, 1);
  136. }
  137. if (is_null($noHtmlFilter)) {
  138. $noHtmlFilter = & JFilterInput::getInstance(/* $tags, $attr, $tag_method, $attr_method, $xss_auto */);
  139. }
  140. if (isset($this->_descriptor->fields))
  141. {
  142. $n = count($this->_descriptor->fields);
  143. for ($i = 0; $i < $n; $i++)
  144. {
  145. $fields = $this->_descriptor->fields[$i]->children();
  146. foreach ($fields as $field)
  147. {
  148. $name = $field->attributes('name');
  149. $filter = $field->attributes('filter');
  150. if (isset($input[$name]))
  151. {
  152. $value = &$input[$name];
  153. switch (strtoupper($filter))
  154. {
  155. case 'RAW':
  156. // do nothing
  157. break;
  158. case 'SAFEHTML':
  159. $value = $safeHtmlFilter->clean($value, $filter);
  160. break;
  161. default:
  162. if (function_exists($filter)) {
  163. $value = call_user_func($filter, $value);
  164. }
  165. else {
  166. $value = $noHtmlFilter->clean($value, $filter);
  167. }
  168. break;
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. /**
  176. * Validate the data against the XML specification
  177. *
  178. * @param array
  179. *
  180. * @return boolean|JException
  181. */
  182. function validate($input)
  183. {
  184. // We must have a <fields> tag
  185. if (!isset($this->_descriptor->fields[0])) {
  186. return JError::raiseWarning(500, 'Fields element not found');
  187. }
  188. // make a JObject for easier handling
  189. if (is_array($input))
  190. {
  191. jximport2('joomla.utilities.array');
  192. $input = JArrayHelper::toObject($input, 'JObject');
  193. }
  194. require_once(JPATH_JXPRO_FORMS.DS.'validate.php');
  195. // Validate the input
  196. $validator = new JXFormValidator($this->_descriptor->fields[0], $this);
  197. $result = $validator->validate($input);
  198. return $result;
  199. }
  200. /**
  201. * @param string String data, or a file name
  202. * @param boolean
  203. */
  204. function _load($data, $fromFile = false)
  205. {
  206. // Initialize variables
  207. $result = false;
  208. if ($data) {
  209. $xml = & JFactory::getXMLParser('Simple');
  210. $loaded = $fromFile ? $xml->loadFile($data) : $xml->loadString($data);
  211. if ($loaded) {
  212. $this->_descriptor =& $xml->document;
  213. $result = true;
  214. }
  215. }
  216. return $result;
  217. }
  218. /**
  219. *
  220. */
  221. function _store()
  222. {
  223. $result = true;
  224. // Create the table object
  225. if ($table = &$this->getDataSource())
  226. {
  227. // Store the bound request data set to the data source
  228. if (isset($table->id)) {
  229. $table->id = (int) $table->id;
  230. }
  231. if (!$table->check()) {
  232. $result = JError::raiseWarning(500, $table->getError());
  233. } else if (!$table->store()) {
  234. $result = JError::raiseWarning(500, $table->getError());
  235. }
  236. }
  237. return $result;
  238. }
  239. }