PageRenderTime 35ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/form/helper.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 313 lines | 115 code | 29 blank | 169 comment | 12 complexity | d462f85cf7e2a99eb8f05683e50c15e0 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  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. 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. *
  75. * @since 11.1
  76. */
  77. public static function loadRuleType($type, $new = true)
  78. {
  79. return self::loadType('rule', $type, $new);
  80. }
  81. /**
  82. * Method to load a form entity object given a type.
  83. * Each type is loaded only once and then used as a prototype for other objects of same type.
  84. * Please, use this method only with those entities which support types (forms don't support them).
  85. *
  86. * @param string $entity The entity.
  87. * @param string $type The entity type.
  88. * @param boolean $new Flag to toggle whether we should get a new instance of the object.
  89. *
  90. * @return mixed Entity object on success, false otherwise.
  91. *
  92. * @since 11.1
  93. */
  94. protected static function loadType($entity, $type, $new = true)
  95. {
  96. // Reference to an array with current entity's type instances
  97. $types = &self::$entities[$entity];
  98. // Initialize variables.
  99. $key = md5($type);
  100. $class = '';
  101. // Return an entity object if it already exists and we don't need a new one.
  102. if (isset($types[$key]) && $new === false)
  103. {
  104. return $types[$key];
  105. }
  106. if (($class = self::loadClass($entity, $type)) !== false)
  107. {
  108. // Instantiate a new type object.
  109. $types[$key] = new $class;
  110. return $types[$key];
  111. }
  112. else
  113. {
  114. return false;
  115. }
  116. }
  117. /**
  118. * Attempt to import the JFormField class file if it isn't already imported.
  119. * You can use this method outside of JForm for loading a field for inheritance or composition.
  120. *
  121. * @param string $type Type of a field whose class should be loaded.
  122. *
  123. * @return mixed Class name on success or false otherwise.
  124. *
  125. * @since 11.1
  126. */
  127. public static function loadFieldClass($type)
  128. {
  129. return self::loadClass('field', $type);
  130. }
  131. /**
  132. * Attempt to import the JFormRule class file if it isn't already imported.
  133. * You can use this method outside of JForm for loading a rule for inheritance or composition.
  134. *
  135. * @param string $type Type of a rule whose class should be loaded.
  136. *
  137. * @return mixed Class name on success or false otherwise.
  138. *
  139. * @since 11.1
  140. */
  141. public static function loadRuleClass($type)
  142. {
  143. return self::loadClass('rule', $type);
  144. }
  145. /**
  146. * Load a class for one of the form's entities of a particular type.
  147. * Currently, it makes sense to use this method for the "field" and "rule" entities
  148. * (but you can support more entities in your subclass).
  149. *
  150. * @param string $entity One of the form entities (field or rule).
  151. * @param string $type Type of an entity.
  152. *
  153. * @return mixed Class name on success or false otherwise.
  154. *
  155. * @since 11.1
  156. */
  157. protected static function loadClass($entity, $type)
  158. {
  159. if (strpos($type, '.'))
  160. {
  161. list($prefix, $type) = explode('.', $type);
  162. }
  163. else
  164. {
  165. $prefix = 'J';
  166. }
  167. $class = JString::ucfirst($prefix, '_') . 'Form' . JString::ucfirst($entity, '_') . JString::ucfirst($type, '_');
  168. if (class_exists($class))
  169. {
  170. return $class;
  171. }
  172. // Get the field search path array.
  173. $paths = JFormHelper::addPath($entity);
  174. // If the type is complex, add the base type to the paths.
  175. if ($pos = strpos($type, '_'))
  176. {
  177. // Add the complex type prefix to the paths.
  178. for ($i = 0, $n = count($paths); $i < $n; $i++)
  179. {
  180. // Derive the new path.
  181. $path = $paths[$i] . '/' . strtolower(substr($type, 0, $pos));
  182. // If the path does not exist, add it.
  183. if (!in_array($path, $paths))
  184. {
  185. $paths[] = $path;
  186. }
  187. }
  188. // Break off the end of the complex type.
  189. $type = substr($type, $pos + 1);
  190. }
  191. // Try to find the class file.
  192. $type = strtolower($type) . '.php';
  193. foreach ($paths as $path)
  194. {
  195. if ($file = JPath::find($path, $type))
  196. {
  197. require_once $file;
  198. if (class_exists($class))
  199. {
  200. break;
  201. }
  202. }
  203. }
  204. // Check for all if the class exists.
  205. return class_exists($class) ? $class : false;
  206. }
  207. /**
  208. * Method to add a path to the list of field include paths.
  209. *
  210. * @param mixed $new A path or array of paths to add.
  211. *
  212. * @return array The list of paths that have been added.
  213. *
  214. * @since 11.1
  215. */
  216. public static function addFieldPath($new = null)
  217. {
  218. return self::addPath('field', $new);
  219. }
  220. /**
  221. * Method to add a path to the list of form include paths.
  222. *
  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. *
  227. * @since 11.1
  228. */
  229. public static function addFormPath($new = null)
  230. {
  231. return self::addPath('form', $new);
  232. }
  233. /**
  234. * Method to add a path to the list of rule include paths.
  235. *
  236. * @param mixed $new A path or array of paths to add.
  237. *
  238. * @return array The list of paths that have been added.
  239. *
  240. * @since 11.1
  241. */
  242. public static function addRulePath($new = null)
  243. {
  244. return self::addPath('rule', $new);
  245. }
  246. /**
  247. * Method to add a path to the list of include paths for one of the form's entities.
  248. * Currently supported entities: field, rule and form. You are free to support your own in a subclass.
  249. *
  250. * @param string $entity Form's entity name for which paths will be added.
  251. * @param mixed $new A path or array of paths to add.
  252. *
  253. * @return array The list of paths that have been added.
  254. *
  255. * @since 11.1
  256. */
  257. protected static function addPath($entity, $new = null)
  258. {
  259. // Reference to an array with paths for current entity
  260. $paths = &self::$paths[$entity];
  261. // Add the default entity's search path if not set.
  262. if (empty($paths))
  263. {
  264. // While we support limited number of entities (form, field and rule)
  265. // we can do this simple pluralisation:
  266. $entity_plural = $entity . 's';
  267. // But when someday we would want to support more entities, then we should consider adding
  268. // an inflector class to "libraries/joomla/utilities" and use it here (or somebody can use a real inflector in his subclass).
  269. // see also: pluralization snippet by Paul Osman in JControllerForm's constructor.
  270. $paths[] = dirname(__FILE__) . '/' . $entity_plural;
  271. }
  272. // Force the new path(s) to an array.
  273. settype($new, 'array');
  274. // Add the new paths to the stack if not already there.
  275. foreach ($new as $path)
  276. {
  277. if (!in_array($path, $paths))
  278. {
  279. array_unshift($paths, trim($path));
  280. }
  281. }
  282. return $paths;
  283. }
  284. }