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

/joomla/templates/gantry/core/config/gantryformhelper.class.php

https://gitlab.com/ricardosanchez/prueba
PHP | 309 lines | 99 code | 31 blank | 179 comment | 15 complexity | c5863cb31e6662dda8cc00f7b97309ac MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: gantryformhelper.class.php 2468 2012-08-17 06:16:57Z btowles $
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - 2016 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. } else {
  95. return false;
  96. }
  97. }
  98. /**
  99. * Attempt to import the JFormField class file if it isn't already imported.
  100. * You can use this method outside of JForm for loading a field for inheritance or composition.
  101. *
  102. * @param string Type of a field whose class should be loaded.
  103. *
  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__) . '/' . $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. *
  212. * @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.
  213. * @since 1.5
  214. */
  215. public static function find($paths, $file)
  216. {
  217. settype($paths, 'array'); //force to array
  218. // start looping through the path set
  219. foreach ($paths as $path) {
  220. // get the path to the file
  221. $fullname = $path . '/' . $file;
  222. // is the path based on a stream?
  223. if (strpos($path, '://') === false) {
  224. // not a stream, so do a realpath() to avoid directory
  225. // traversal attempts on the local file system.
  226. $path = realpath($path); // needed for substr() later
  227. $fullname = realpath($fullname);
  228. }
  229. // the substr() check added to make sure that the realpath()
  230. // results in a directory registered so that
  231. // non-registered directores are not accessible via directory
  232. // traversal attempts.
  233. if (file_exists($fullname) && substr($fullname, 0, strlen($path)) == $path) {
  234. return $fullname;
  235. }
  236. }
  237. // could not find the file in the set of paths
  238. return false;
  239. }
  240. /**
  241. * Method to load a form field object given a type.
  242. *
  243. * @param string $type The field type.
  244. * @param boolean $new Flag to toggle whether we should get a new instance of the object.
  245. *
  246. * @return mixed JFormField object on success, false otherwise.
  247. * @since 1.6
  248. */
  249. public static function loadGroupType($type, $new = true)
  250. {
  251. return self::loadType('group', $type, $new);
  252. }
  253. /**
  254. * Attempt to import the JFormRule class file if it isn't already imported.
  255. * You can use this method outside of JForm for loading a rule for inheritance or composition.
  256. *
  257. * @param string Type of a rule whose class should be loaded.
  258. *
  259. * @return mixed Class name on success or false otherwise.
  260. * @since 1.6
  261. */
  262. public static function loadGroupClass($type)
  263. {
  264. return self::loadClass('group', $type);
  265. }
  266. /**
  267. * Method to add a path to the list of field include paths.
  268. *
  269. * @param mixed $new A path or array of paths to add.
  270. *
  271. * @return array The list of paths that have been added.
  272. * @since 1.6
  273. */
  274. public static function addGroupPath($new = null)
  275. {
  276. return self::addPath('group', $new);
  277. }
  278. }