PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/fof/form/helper.php

https://gitlab.com/lankerd/paGO---Testing-Site
PHP | 232 lines | 110 code | 23 blank | 99 comment | 11 complexity | 3a51dc3c78a63975d6f3ec1ffd426ff0 MD5 | raw file
  1. <?php
  2. /**
  3. * @package FrameworkOnFramework
  4. * @subpackage form
  5. * @copyright Copyright (C) 2010 - 2015 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE.txt
  7. */
  8. // Protect from unauthorized access
  9. defined('FOF_INCLUDED') or die;
  10. JLoader::import('joomla.form.helper');
  11. /**
  12. * FOFForm's helper class.
  13. * Provides a storage for filesystem's paths where FOFForm's entities reside and
  14. * methods for creating those entities. Also stores objects with entities'
  15. * prototypes for further reusing.
  16. *
  17. * @package FrameworkOnFramework
  18. * @since 2.0
  19. */
  20. class FOFFormHelper extends JFormHelper
  21. {
  22. /**
  23. * Method to load a form field object given a type.
  24. *
  25. * @param string $type The field type.
  26. * @param boolean $new Flag to toggle whether we should get a new instance of the object.
  27. *
  28. * @return mixed JFormField object on success, false otherwise.
  29. *
  30. * @since 11.1
  31. */
  32. public static function loadFieldType($type, $new = true)
  33. {
  34. return self::loadType('field', $type, $new);
  35. }
  36. /**
  37. * Method to load a form field object given a type.
  38. *
  39. * @param string $type The field type.
  40. * @param boolean $new Flag to toggle whether we should get a new instance of the object.
  41. *
  42. * @return mixed JFormField object on success, false otherwise.
  43. *
  44. * @since 11.1
  45. */
  46. public static function loadHeaderType($type, $new = true)
  47. {
  48. return self::loadType('header', $type, $new);
  49. }
  50. /**
  51. * Method to load a form entity object given a type.
  52. * Each type is loaded only once and then used as a prototype for other objects of same type.
  53. * Please, use this method only with those entities which support types (forms don't support them).
  54. *
  55. * @param string $entity The entity.
  56. * @param string $type The entity type.
  57. * @param boolean $new Flag to toggle whether we should get a new instance of the object.
  58. *
  59. * @return mixed Entity object on success, false otherwise.
  60. *
  61. * @since 11.1
  62. */
  63. protected static function loadType($entity, $type, $new = true)
  64. {
  65. // Reference to an array with current entity's type instances
  66. $types = &self::$entities[$entity];
  67. $key = md5($type);
  68. // Return an entity object if it already exists and we don't need a new one.
  69. if (isset($types[$key]) && $new === false)
  70. {
  71. return $types[$key];
  72. }
  73. $class = self::loadClass($entity, $type);
  74. if ($class !== false)
  75. {
  76. // Instantiate a new type object.
  77. $types[$key] = new $class;
  78. return $types[$key];
  79. }
  80. else
  81. {
  82. return false;
  83. }
  84. }
  85. /**
  86. * Attempt to import the JFormField class file if it isn't already imported.
  87. * You can use this method outside of JForm for loading a field for inheritance or composition.
  88. *
  89. * @param string $type Type of a field whose class should be loaded.
  90. *
  91. * @return mixed Class name on success or false otherwise.
  92. *
  93. * @since 11.1
  94. */
  95. public static function loadFieldClass($type)
  96. {
  97. return self::loadClass('field', $type);
  98. }
  99. /**
  100. * Attempt to import the FOFFormHeader class file if it isn't already imported.
  101. * You can use this method outside of JForm for loading a field for inheritance or composition.
  102. *
  103. * @param string $type Type of a field whose class should be loaded.
  104. *
  105. * @return mixed Class name on success or false otherwise.
  106. *
  107. * @since 11.1
  108. */
  109. public static function loadHeaderClass($type)
  110. {
  111. return self::loadClass('header', $type);
  112. }
  113. /**
  114. * Load a class for one of the form's entities of a particular type.
  115. * Currently, it makes sense to use this method for the "field" and "rule" entities
  116. * (but you can support more entities in your subclass).
  117. *
  118. * @param string $entity One of the form entities (field or rule).
  119. * @param string $type Type of an entity.
  120. *
  121. * @return mixed Class name on success or false otherwise.
  122. *
  123. * @since 2.0
  124. */
  125. public static function loadClass($entity, $type)
  126. {
  127. if (strpos($type, '.'))
  128. {
  129. list($prefix, $type) = explode('.', $type);
  130. $altPrefix = $prefix;
  131. }
  132. else
  133. {
  134. $prefix = 'FOF';
  135. $altPrefix = 'J';
  136. }
  137. $class = JString::ucfirst($prefix, '_') . 'Form' . JString::ucfirst($entity, '_') . JString::ucfirst($type, '_');
  138. $altClass = JString::ucfirst($altPrefix, '_') . 'Form' . JString::ucfirst($entity, '_') . JString::ucfirst($type, '_');
  139. if (class_exists($class))
  140. {
  141. return $class;
  142. }
  143. elseif (class_exists($altClass))
  144. {
  145. return $altClass;
  146. }
  147. // Get the field search path array.
  148. $paths = self::addPath($entity);
  149. // If the type is complex, add the base type to the paths.
  150. if ($pos = strpos($type, '_'))
  151. {
  152. // Add the complex type prefix to the paths.
  153. for ($i = 0, $n = count($paths); $i < $n; $i++)
  154. {
  155. // Derive the new path.
  156. $path = $paths[$i] . '/' . strtolower(substr($type, 0, $pos));
  157. // If the path does not exist, add it.
  158. if (!in_array($path, $paths))
  159. {
  160. $paths[] = $path;
  161. }
  162. }
  163. // Break off the end of the complex type.
  164. $type = substr($type, $pos + 1);
  165. }
  166. // Try to find the class file.
  167. $type = strtolower($type) . '.php';
  168. $filesystem = FOFPlatform::getInstance()->getIntegrationObject('filesystem');
  169. foreach ($paths as $path)
  170. {
  171. if ($file = $filesystem->pathFind($path, $type))
  172. {
  173. require_once $file;
  174. if (class_exists($class))
  175. {
  176. break;
  177. }
  178. elseif (class_exists($altClass))
  179. {
  180. break;
  181. }
  182. }
  183. }
  184. // Check for all if the class exists.
  185. if (class_exists($class))
  186. {
  187. return $class;
  188. }
  189. elseif (class_exists($altClass))
  190. {
  191. return $altClass;
  192. }
  193. else
  194. {
  195. return false;
  196. }
  197. }
  198. /**
  199. * Method to add a path to the list of header include paths.
  200. *
  201. * @param mixed $new A path or array of paths to add.
  202. *
  203. * @return array The list of paths that have been added.
  204. */
  205. public static function addHeaderPath($new = null)
  206. {
  207. return self::addPath('header', $new);
  208. }
  209. }