PageRenderTime 84ms CodeModel.GetById 41ms app.highlight 7ms RepoModel.GetById 33ms app.codeStats 0ms

/libraries/joomla/form/helper.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 313 lines | 115 code | 29 blank | 169 comment | 12 complexity | d462f85cf7e2a99eb8f05683e50c15e0 MD5 | raw file
  1<?php
  2/**
  3 * @package     Joomla.Platform
  4 * @subpackage  Form
  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.filesystem.path');
 13
 14/**
 15 * JForm's helper class.
 16 * Provides a storage for filesystem's paths where JForm's entities reside and methods for creating those entities.
 17 * Also stores objects with entities' prototypes for further reusing.
 18 *
 19 * @package     Joomla.Platform
 20 * @subpackage  Form
 21 * @since       11.1
 22 */
 23class JFormHelper
 24{
 25	/**
 26	 * Array with paths where entities(field, rule, form) can be found.
 27	 *
 28	 * Array's structure:
 29	 * <code>
 30	 * paths:
 31	 * {ENTITY_NAME}:
 32	 * - /path/1
 33	 * - /path/2
 34	 * </code>
 35	 *
 36	 * @var    array
 37	 * @since  11.1
 38	 *
 39	 */
 40	protected static $paths;
 41
 42	/**
 43	 * Static array of JForm's entity objects for re-use.
 44	 * Prototypes for all fields and rules are here.
 45	 *
 46	 * Array's structure:
 47	 * <code>
 48	 * entities:
 49	 * {ENTITY_NAME}:
 50	 * {KEY}: {OBJECT}
 51	 * </code>
 52	 *
 53	 * @var    array
 54	 * @since  11.1
 55	 */
 56	protected static $entities = array();
 57
 58	/**
 59	 * Method to load a form field object given a type.
 60	 *
 61	 * @param   string   $type  The field type.
 62	 * @param   boolean  $new   Flag to toggle whether we should get a new instance of the object.
 63	 *
 64	 * @return  mixed  JFormField object on success, false otherwise.
 65	 *
 66	 * @since   11.1
 67	 */
 68	public static function loadFieldType($type, $new = true)
 69	{
 70		return self::loadType('field', $type, $new);
 71	}
 72
 73	/**
 74	 * Method to load a form rule object given a type.
 75	 *
 76	 * @param   string   $type  The rule type.
 77	 * @param   boolean  $new   Flag to toggle whether we should get a new instance of the object.
 78	 *
 79	 * @return  mixed  JFormRule object on success, false otherwise.
 80	 *
 81	 * @since   11.1
 82	 */
 83	public static function loadRuleType($type, $new = true)
 84	{
 85		return self::loadType('rule', $type, $new);
 86	}
 87
 88	/**
 89	 * Method to load a form entity object given a type.
 90	 * Each type is loaded only once and then used as a prototype for other objects of same type.
 91	 * Please, use this method only with those entities which support types (forms don't support them).
 92	 *
 93	 * @param   string   $entity  The entity.
 94	 * @param   string   $type    The entity type.
 95	 * @param   boolean  $new     Flag to toggle whether we should get a new instance of the object.
 96	 *
 97	 * @return  mixed  Entity object on success, false otherwise.
 98	 *
 99	 * @since   11.1
100	 */
101	protected static function loadType($entity, $type, $new = true)
102	{
103		// Reference to an array with current entity's type instances
104		$types = &self::$entities[$entity];
105
106		// Initialize variables.
107		$key = md5($type);
108		$class = '';
109
110		// Return an entity object if it already exists and we don't need a new one.
111		if (isset($types[$key]) && $new === false)
112		{
113			return $types[$key];
114		}
115
116		if (($class = self::loadClass($entity, $type)) !== false)
117		{
118			// Instantiate a new type object.
119			$types[$key] = new $class;
120			return $types[$key];
121		}
122		else
123		{
124			return false;
125		}
126	}
127
128	/**
129	 * Attempt to import the JFormField class file if it isn't already imported.
130	 * You can use this method outside of JForm for loading a field for inheritance or composition.
131	 *
132	 * @param   string  $type  Type of a field whose class should be loaded.
133	 *
134	 * @return  mixed  Class name on success or false otherwise.
135	 *
136	 * @since   11.1
137	 */
138	public static function loadFieldClass($type)
139	{
140		return self::loadClass('field', $type);
141	}
142
143	/**
144	 * Attempt to import the JFormRule class file if it isn't already imported.
145	 * You can use this method outside of JForm for loading a rule for inheritance or composition.
146	 *
147	 * @param   string  $type  Type of a rule whose class should be loaded.
148	 *
149	 * @return  mixed  Class name on success or false otherwise.
150	 *
151	 * @since   11.1
152	 */
153	public static function loadRuleClass($type)
154	{
155		return self::loadClass('rule', $type);
156	}
157
158	/**
159	 * Load a class for one of the form's entities of a particular type.
160	 * Currently, it makes sense to use this method for the "field" and "rule" entities
161	 * (but you can support more entities in your subclass).
162	 *
163	 * @param   string  $entity  One of the form entities (field or rule).
164	 * @param   string  $type    Type of an entity.
165	 *
166	 * @return  mixed  Class name on success or false otherwise.
167	 *
168	 * @since   11.1
169	 */
170	protected static function loadClass($entity, $type)
171	{
172		if (strpos($type, '.'))
173		{
174			list($prefix, $type) = explode('.', $type);
175		}
176		else
177		{
178			$prefix = 'J';
179		}
180
181		$class = JString::ucfirst($prefix, '_') . 'Form' . JString::ucfirst($entity, '_') . JString::ucfirst($type, '_');
182
183		if (class_exists($class))
184		{
185			return $class;
186		}
187
188		// Get the field search path array.
189		$paths = JFormHelper::addPath($entity);
190
191		// If the type is complex, add the base type to the paths.
192		if ($pos = strpos($type, '_'))
193		{
194
195			// Add the complex type prefix to the paths.
196			for ($i = 0, $n = count($paths); $i < $n; $i++)
197			{
198				// Derive the new path.
199				$path = $paths[$i] . '/' . strtolower(substr($type, 0, $pos));
200
201				// If the path does not exist, add it.
202				if (!in_array($path, $paths))
203				{
204					$paths[] = $path;
205				}
206			}
207			// Break off the end of the complex type.
208			$type = substr($type, $pos + 1);
209		}
210
211		// Try to find the class file.
212		$type = strtolower($type) . '.php';
213		foreach ($paths as $path)
214		{
215			if ($file = JPath::find($path, $type))
216			{
217				require_once $file;
218				if (class_exists($class))
219				{
220					break;
221				}
222			}
223		}
224
225		// Check for all if the class exists.
226		return class_exists($class) ? $class : false;
227	}
228
229	/**
230	 * Method to add a path to the list of field include paths.
231	 *
232	 * @param   mixed  $new  A path or array of paths to add.
233	 *
234	 * @return  array  The list of paths that have been added.
235	 *
236	 * @since   11.1
237	 */
238	public static function addFieldPath($new = null)
239	{
240		return self::addPath('field', $new);
241	}
242
243	/**
244	 * Method to add a path to the list of form include paths.
245	 *
246	 * @param   mixed  $new  A path or array of paths to add.
247	 *
248	 * @return  array  The list of paths that have been added.
249	 *
250	 * @since   11.1
251	 */
252	public static function addFormPath($new = null)
253	{
254		return self::addPath('form', $new);
255	}
256
257	/**
258	 * Method to add a path to the list of rule include paths.
259	 *
260	 * @param   mixed  $new  A path or array of paths to add.
261	 *
262	 * @return  array  The list of paths that have been added.
263	 *
264	 * @since   11.1
265	 */
266	public static function addRulePath($new = null)
267	{
268		return self::addPath('rule', $new);
269	}
270
271	/**
272	 * Method to add a path to the list of include paths for one of the form's entities.
273	 * Currently supported entities: field, rule and form. You are free to support your own in a subclass.
274	 *
275	 * @param   string  $entity  Form's entity name for which paths will be added.
276	 * @param   mixed   $new     A path or array of paths to add.
277	 *
278	 * @return  array  The list of paths that have been added.
279	 *
280	 * @since   11.1
281	 */
282	protected static function addPath($entity, $new = null)
283	{
284		// Reference to an array with paths for current entity
285		$paths = &self::$paths[$entity];
286
287		// Add the default entity's search path if not set.
288		if (empty($paths))
289		{
290			// While we support limited number of entities (form, field and rule)
291			// we can do this simple pluralisation:
292			$entity_plural = $entity . 's';
293			// But when someday we would want to support more entities, then we should consider adding
294			// an inflector class to "libraries/joomla/utilities" and use it here (or somebody can use a real inflector in his subclass).
295			// see also: pluralization snippet by Paul Osman in JControllerForm's constructor.
296			$paths[] = dirname(__FILE__) . '/' . $entity_plural;
297		}
298
299		// Force the new path(s) to an array.
300		settype($new, 'array');
301
302		// Add the new paths to the stack if not already there.
303		foreach ($new as $path)
304		{
305			if (!in_array($path, $paths))
306			{
307				array_unshift($paths, trim($path));
308			}
309		}
310
311		return $paths;
312	}
313}