PageRenderTime 84ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 31ms app.codeStats 1ms

/libraries/gantry/core/config/gantryformhelper.class.php

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