/libraries/joomla/form/helper.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 315 lines · 115 code · 30 blank · 170 comment · 12 complexity · 7ce386cedf73faf87a3ea572dbab5eaf MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Form
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 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. $key = md5($type);
  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. {
  102. return $types[$key];
  103. }
  104. $class = self::loadClass($entity, $type);
  105. if ($class !== false)
  106. {
  107. // Instantiate a new type object.
  108. $types[$key] = new $class;
  109. return $types[$key];
  110. }
  111. else
  112. {
  113. return false;
  114. }
  115. }
  116. /**
  117. * Attempt to import the JFormField class file if it isn't already imported.
  118. * You can use this method outside of JForm for loading a field for inheritance or composition.
  119. *
  120. * @param string $type Type of a field whose class should be loaded.
  121. *
  122. * @return mixed Class name on success or false otherwise.
  123. *
  124. * @since 11.1
  125. */
  126. public static function loadFieldClass($type)
  127. {
  128. return self::loadClass('field', $type);
  129. }
  130. /**
  131. * Attempt to import the JFormRule class file if it isn't already imported.
  132. * You can use this method outside of JForm for loading a rule for inheritance or composition.
  133. *
  134. * @param string $type Type of a rule whose class should be loaded.
  135. *
  136. * @return mixed Class name on success or false otherwise.
  137. *
  138. * @since 11.1
  139. */
  140. public static function loadRuleClass($type)
  141. {
  142. return self::loadClass('rule', $type);
  143. }
  144. /**
  145. * Load a class for one of the form's entities of a particular type.
  146. * Currently, it makes sense to use this method for the "field" and "rule" entities
  147. * (but you can support more entities in your subclass).
  148. *
  149. * @param string $entity One of the form entities (field or rule).
  150. * @param string $type Type of an entity.
  151. *
  152. * @return mixed Class name on success or false otherwise.
  153. *
  154. * @since 11.1
  155. */
  156. protected static function loadClass($entity, $type)
  157. {
  158. if (strpos($type, '.'))
  159. {
  160. list($prefix, $type) = explode('.', $type);
  161. }
  162. else
  163. {
  164. $prefix = 'J';
  165. }
  166. $class = JString::ucfirst($prefix, '_') . 'Form' . JString::ucfirst($entity, '_') . JString::ucfirst($type, '_');
  167. if (class_exists($class))
  168. {
  169. return $class;
  170. }
  171. // Get the field search path array.
  172. $paths = self::addPath($entity);
  173. // If the type is complex, add the base type to the paths.
  174. if ($pos = strpos($type, '_'))
  175. {
  176. // Add the complex type prefix to the paths.
  177. for ($i = 0, $n = count($paths); $i < $n; $i++)
  178. {
  179. // Derive the new path.
  180. $path = $paths[$i] . '/' . strtolower(substr($type, 0, $pos));
  181. // If the path does not exist, add it.
  182. if (!in_array($path, $paths))
  183. {
  184. $paths[] = $path;
  185. }
  186. }
  187. // Break off the end of the complex type.
  188. $type = substr($type, $pos + 1);
  189. }
  190. // Try to find the class file.
  191. $type = strtolower($type) . '.php';
  192. foreach ($paths as $path)
  193. {
  194. if ($file = JPath::find($path, $type))
  195. {
  196. require_once $file;
  197. if (class_exists($class))
  198. {
  199. break;
  200. }
  201. }
  202. }
  203. // Check for all if the class exists.
  204. return class_exists($class) ? $class : false;
  205. }
  206. /**
  207. * Method to add a path to the list of field include paths.
  208. *
  209. * @param mixed $new A path or array of paths to add.
  210. *
  211. * @return array The list of paths that have been added.
  212. *
  213. * @since 11.1
  214. */
  215. public static function addFieldPath($new = null)
  216. {
  217. return self::addPath('field', $new);
  218. }
  219. /**
  220. * Method to add a path to the list of form include paths.
  221. *
  222. * @param mixed $new A path or array of paths to add.
  223. *
  224. * @return array The list of paths that have been added.
  225. *
  226. * @since 11.1
  227. */
  228. public static function addFormPath($new = null)
  229. {
  230. return self::addPath('form', $new);
  231. }
  232. /**
  233. * Method to add a path to the list of rule include paths.
  234. *
  235. * @param mixed $new A path or array of paths to add.
  236. *
  237. * @return array The list of paths that have been added.
  238. *
  239. * @since 11.1
  240. */
  241. public static function addRulePath($new = null)
  242. {
  243. return self::addPath('rule', $new);
  244. }
  245. /**
  246. * Method to add a path to the list of include paths for one of the form's entities.
  247. * Currently supported entities: field, rule and form. You are free to support your own in a subclass.
  248. *
  249. * @param string $entity Form's entity name for which paths will be added.
  250. * @param mixed $new A path or array of paths to add.
  251. *
  252. * @return array The list of paths that have been added.
  253. *
  254. * @since 11.1
  255. */
  256. protected static function addPath($entity, $new = null)
  257. {
  258. // Reference to an array with paths for current entity
  259. $paths = &self::$paths[$entity];
  260. // Add the default entity's search path if not set.
  261. if (empty($paths))
  262. {
  263. // While we support limited number of entities (form, field and rule)
  264. // we can do this simple pluralisation:
  265. $entity_plural = $entity . 's';
  266. /*
  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. */
  271. $paths[] = __DIR__ . '/' . $entity_plural;
  272. }
  273. // Force the new path(s) to an array.
  274. settype($new, 'array');
  275. // Add the new paths to the stack if not already there.
  276. foreach ($new as $path)
  277. {
  278. if (!in_array($path, $paths))
  279. {
  280. array_unshift($paths, trim($path));
  281. }
  282. }
  283. return $paths;
  284. }
  285. }