PageRenderTime 224ms CodeModel.GetById 141ms app.highlight 44ms RepoModel.GetById 36ms app.codeStats 1ms

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

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