PageRenderTime 91ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/form/helper.php

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