PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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