PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/application/component/modelform.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 285 lines | 125 code | 36 blank | 124 comment | 22 complexity | 5945d2c11d200cd0da1cd6bc24f48e53 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Application
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. jimport('joomla.application.component.model');
  11. /**
  12. * Prototype form model.
  13. *
  14. * @package Joomla.Platform
  15. * @subpackage Application
  16. * @see JForm
  17. * @see JFormField
  18. * @see JformRule
  19. * @since 11.1
  20. */
  21. abstract class JModelForm extends JModel
  22. {
  23. /**
  24. * Array of form objects.
  25. *
  26. * @var array
  27. * @since 11.1
  28. */
  29. protected $_forms = array();
  30. /**
  31. * Method to checkin a row.
  32. *
  33. * @param integer $pk The numeric id of the primary key.
  34. *
  35. * @return boolean False on failure or error, true otherwise.
  36. *
  37. * @since 11.1
  38. */
  39. public function checkin($pk = null)
  40. {
  41. // Only attempt to check the row in if it exists.
  42. if ($pk)
  43. {
  44. $user = JFactory::getUser();
  45. // Get an instance of the row to checkin.
  46. $table = $this->getTable();
  47. if (!$table->load($pk))
  48. {
  49. $this->setError($table->getError());
  50. return false;
  51. }
  52. // Check if this is the user having previously checked out the row.
  53. if ($table->checked_out > 0 && $table->checked_out != $user->get('id') && !$user->authorise('core.admin', 'com_checkin'))
  54. {
  55. $this->setError(JText::_('JLIB_APPLICATION_ERROR_CHECKIN_USER_MISMATCH'));
  56. return false;
  57. }
  58. // Attempt to check the row in.
  59. if (!$table->checkin($pk))
  60. {
  61. $this->setError($table->getError());
  62. return false;
  63. }
  64. }
  65. return true;
  66. }
  67. /**
  68. * Method to check-out a row for editing.
  69. *
  70. * @param integer $pk The numeric id of the primary key.
  71. *
  72. * @return boolean False on failure or error, true otherwise.
  73. *
  74. * @since 11.1
  75. */
  76. public function checkout($pk = null)
  77. {
  78. // Only attempt to check the row in if it exists.
  79. if ($pk)
  80. {
  81. $user = JFactory::getUser();
  82. // Get an instance of the row to checkout.
  83. $table = $this->getTable();
  84. if (!$table->load($pk))
  85. {
  86. $this->setError($table->getError());
  87. return false;
  88. }
  89. // Check if this is the user having previously checked out the row.
  90. if ($table->checked_out > 0 && $table->checked_out != $user->get('id'))
  91. {
  92. $this->setError(JText::_('JLIB_APPLICATION_ERROR_CHECKOUT_USER_MISMATCH'));
  93. return false;
  94. }
  95. // Attempt to check the row out.
  96. if (!$table->checkout($user->get('id'), $pk))
  97. {
  98. $this->setError($table->getError());
  99. return false;
  100. }
  101. }
  102. return true;
  103. }
  104. /**
  105. * Abstract method for getting the form from the model.
  106. *
  107. * @param array $data Data for the form.
  108. * @param boolean $loadData True if the form is to load its own data (default case), false if not.
  109. *
  110. * @return mixed A JForm object on success, false on failure
  111. *
  112. * @since 11.1
  113. */
  114. abstract public function getForm($data = array(), $loadData = true);
  115. /**
  116. * Method to get a form object.
  117. *
  118. * @param string $name The name of the form.
  119. * @param string $source The form source. Can be XML string if file flag is set to false.
  120. * @param array $options Optional array of options for the form creation.
  121. * @param boolean $clear Optional argument to force load a new form.
  122. * @param string $xpath An optional xpath to search for the fields.
  123. *
  124. * @return mixed JForm object on success, False on error.
  125. *
  126. * @see JForm
  127. * @since 11.1
  128. */
  129. protected function loadForm($name, $source = null, $options = array(), $clear = false, $xpath = false)
  130. {
  131. // Handle the optional arguments.
  132. $options['control'] = JArrayHelper::getValue($options, 'control', false);
  133. // Create a signature hash.
  134. $hash = md5($source . serialize($options));
  135. // Check if we can use a previously loaded form.
  136. if (isset($this->_forms[$hash]) && !$clear)
  137. {
  138. return $this->_forms[$hash];
  139. }
  140. // Get the form.
  141. JForm::addFormPath(JPATH_COMPONENT . '/models/forms');
  142. JForm::addFieldPath(JPATH_COMPONENT . '/models/fields');
  143. try
  144. {
  145. $form = JForm::getInstance($name, $source, $options, false, $xpath);
  146. if (isset($options['load_data']) && $options['load_data'])
  147. {
  148. // Get the data for the form.
  149. $data = $this->loadFormData();
  150. }
  151. else
  152. {
  153. $data = array();
  154. }
  155. // Allow for additional modification of the form, and events to be triggered.
  156. // We pass the data because plugins may require it.
  157. $this->preprocessForm($form, $data);
  158. // Load the data into the form after the plugins have operated.
  159. $form->bind($data);
  160. }
  161. catch (Exception $e)
  162. {
  163. $this->setError($e->getMessage());
  164. return false;
  165. }
  166. // Store the form for later.
  167. $this->_forms[$hash] = $form;
  168. return $form;
  169. }
  170. /**
  171. * Method to get the data that should be injected in the form.
  172. *
  173. * @return array The default data is an empty array.
  174. *
  175. * @since 11.1
  176. */
  177. protected function loadFormData()
  178. {
  179. return array();
  180. }
  181. /**
  182. * Method to allow derived classes to preprocess the form.
  183. *
  184. * @param JForm $form A JForm object.
  185. * @param mixed $data The data expected for the form.
  186. * @param string $group The name of the plugin group to import (defaults to "content").
  187. *
  188. * @return void
  189. *
  190. * @see JFormField
  191. * @since 11.1
  192. * @throws Exception if there is an error in the form event.
  193. */
  194. protected function preprocessForm(JForm $form, $data, $group = 'content')
  195. {
  196. // Import the appropriate plugin group.
  197. JPluginHelper::importPlugin($group);
  198. // Get the dispatcher.
  199. $dispatcher = JDispatcher::getInstance();
  200. // Trigger the form preparation event.
  201. $results = $dispatcher->trigger('onContentPrepareForm', array($form, $data));
  202. // Check for errors encountered while preparing the form.
  203. if (count($results) && in_array(false, $results, true))
  204. {
  205. // Get the last error.
  206. $error = $dispatcher->getError();
  207. if (!($error instanceof Exception))
  208. {
  209. throw new Exception($error);
  210. }
  211. }
  212. }
  213. /**
  214. * Method to validate the form data.
  215. *
  216. * @param JForm $form The form to validate against.
  217. * @param array $data The data to validate.
  218. * @param string $group The name of the field group to validate.
  219. *
  220. * @return mixed Array of filtered data if valid, false otherwise.
  221. *
  222. * @see JFormRule
  223. * @see JFilterInput
  224. * @since 11.1
  225. */
  226. public function validate($form, $data, $group = null)
  227. {
  228. // Filter and validate the form data.
  229. $data = $form->filter($data);
  230. $return = $form->validate($data, $group);
  231. // Check for an error.
  232. if ($return instanceof Exception)
  233. {
  234. $this->setError($return->getMessage());
  235. return false;
  236. }
  237. // Check the validation results.
  238. if ($return === false)
  239. {
  240. // Get the validation messages from the form.
  241. foreach ($form->getErrors() as $message)
  242. {
  243. $this->setError(JText::_($message));
  244. }
  245. return false;
  246. }
  247. return $data;
  248. }
  249. }