PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/administrator/components/com_advancedmodules/helpers/formhelper.php

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