Source for file helper.php

Documentation is available at helper.php

  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.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. jimport('joomla.filesystem.path');
  13.  
  14. /**
  15.  * JForm's helper class.
  16.  * Provides a storage for filesystem's paths where JForm's entities reside and methods for creating those entities.
  17.  * Also stores objects with entities' prototypes for further reusing.
  18.  *
  19.  * @package     Joomla.Platform
  20.  * @subpackage  Form
  21.  * @since       11.1
  22.  */
  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  11.1
  37.      *
  38.      */
  39.     protected static $paths;
  40.  
  41.     /**
  42.      * Static array of JForm's entity objects for re-use.
  43.      * Prototypes for all fields and rules are here.
  44.      *
  45.      * Array's structure:
  46.      * <code>
  47.      * entities:
  48.      * {ENTITY_NAME}:
  49.      * {KEY}: {OBJECT}
  50.      * </code>
  51.      *
  52.      * @var    array 
  53.      * @since  11.1
  54.      */
  55.     protected static $entities array();
  56.  
  57.     /**
  58.      * Method to load a form field object given a type.
  59.      *
  60.      * @param   string   $type  The field type.
  61.      * @param   boolean  $new   Flag to toggle whether we should get a new instance of the object.
  62.      *
  63.      * @return  mixed  JFormField object on success, false otherwise.
  64.      *
  65.      * @since   11.1
  66.      */
  67.     public static function loadFieldType($type$new true)
  68.     {
  69.         return self::loadType('field'$type$new);
  70.     }
  71.  
  72.     /**
  73.      * Method to load a form rule object given a type.
  74.      *
  75.      * @param   string   $type  The rule type.
  76.      * @param   boolean  $new   Flag to toggle whether we should get a new instance of the object.
  77.      *
  78.      * @return  mixed  JFormRule object on success, false otherwise.
  79.      *
  80.      * @since   11.1
  81.      */
  82.     public static function loadRuleType($type$new true)
  83.     {
  84.         return self::loadType('rule'$type$new);
  85.     }
  86.  
  87.     /**
  88.      * Method to load a form entity object given a type.
  89.      * Each type is loaded only once and then used as a prototype for other objects of same type.
  90.      * Please, use this method only with those entities which support types (forms don't support them).
  91.      *
  92.      * @param   string   $entity  The entity.
  93.      * @param   string   $type    The entity type.
  94.      * @param   boolean  $new     Flag to toggle whether we should get a new instance of the object.
  95.      *
  96.      * @return  mixed  Entity object on success, false otherwise.
  97.      *
  98.      * @since   11.1
  99.      */
  100.     protected static function loadType($entity$type$new true)
  101.     {
  102.         // Reference to an array with current entity's type instances
  103.         $types &self::$entities[$entity];
  104.  
  105.         $key md5($type);
  106.  
  107.         // Return an entity object if it already exists and we don't need a new one.
  108.         if (isset($types[$key]&& $new === false)
  109.         {
  110.             return $types[$key];
  111.         }
  112.  
  113.         $class self::loadClass($entity$type);
  114.         if ($class !== false)
  115.         {
  116.             // Instantiate a new type object.
  117.             $types[$keynew $class;
  118.             return $types[$key];
  119.         }
  120.         else
  121.         {
  122.             return false;
  123.         }
  124.     }
  125.  
  126.     /**
  127.      * Attempt to import the JFormField class file if it isn't already imported.
  128.      * You can use this method outside of JForm for loading a field for inheritance or composition.
  129.      *
  130.      * @param   string  $type  Type of a field whose class should be loaded.
  131.      *
  132.      * @return  mixed  Class name on success or false otherwise.
  133.      *
  134.      * @since   11.1
  135.      */
  136.     public static function loadFieldClass($type)
  137.     {
  138.         return self::loadClass('field'$type);
  139.     }
  140.  
  141.     /**
  142.      * Attempt to import the JFormRule class file if it isn't already imported.
  143.      * You can use this method outside of JForm for loading a rule for inheritance or composition.
  144.      *
  145.      * @param   string  $type  Type of a rule whose class should be loaded.
  146.      *
  147.      * @return  mixed  Class name on success or false otherwise.
  148.      *
  149.      * @since   11.1
  150.      */
  151.     public static function loadRuleClass($type)
  152.     {
  153.         return self::loadClass('rule'$type);
  154.     }
  155.  
  156.     /**
  157.      * Load a class for one of the form's entities of a particular type.
  158.      * Currently, it makes sense to use this method for the "field" and "rule" entities
  159.      * (but you can support more entities in your subclass).
  160.      *
  161.      * @param   string  $entity  One of the form entities (field or rule).
  162.      * @param   string  $type    Type of an entity.
  163.      *
  164.      * @return  mixed  Class name on success or false otherwise.
  165.      *
  166.      * @since   11.1
  167.      */
  168.     protected static function loadClass($entity$type)
  169.     {
  170.         if (strpos($type'.'))
  171.         {
  172.             list($prefix$typeexplode('.'$type);
  173.         }
  174.         else
  175.         {
  176.             $prefix 'J';
  177.         }
  178.  
  179.         $class JString::ucfirst($prefix'_''Form' JString::ucfirst($entity'_'JString::ucfirst($type'_');
  180.  
  181.         if (class_exists($class))
  182.         {
  183.             return $class;
  184.         }
  185.  
  186.         // Get the field search path array.
  187.         $paths self::addPath($entity);
  188.  
  189.         // If the type is complex, add the base type to the paths.
  190.         if ($pos strpos($type'_'))
  191.         {
  192.  
  193.             // Add the complex type prefix to the paths.
  194.             for ($i 0$n count($paths)$i $n$i++)
  195.             {
  196.                 // Derive the new path.
  197.                 $path $paths[$i'/' strtolower(substr($type0$pos));
  198.  
  199.                 // If the path does not exist, add it.
  200.                 if (!in_array($path$paths))
  201.                 {
  202.                     $paths[$path;
  203.                 }
  204.             }
  205.             // Break off the end of the complex type.
  206.             $type substr($type$pos 1);
  207.         }
  208.  
  209.         // Try to find the class file.
  210.         $type strtolower($type'.php';
  211.         foreach ($paths as $path)
  212.         {
  213.             if ($file JPath::find($path$type))
  214.             {
  215.                 require_once $file;
  216.                 if (class_exists($class))
  217.                 {
  218.                     break;
  219.                 }
  220.             }
  221.         }
  222.  
  223.         // Check for all if the class exists.
  224.         return class_exists($class$class false;
  225.     }
  226.  
  227.     /**
  228.      * Method to add a path to the list of field include paths.
  229.      *
  230.      * @param   mixed  $new  A path or array of paths to add.
  231.      *
  232.      * @return  array  The list of paths that have been added.
  233.      *
  234.      * @since   11.1
  235.      */
  236.     public static function addFieldPath($new null)
  237.     {
  238.         return self::addPath('field'$new);
  239.     }
  240.  
  241.     /**
  242.      * Method to add a path to the list of form include paths.
  243.      *
  244.      * @param   mixed  $new  A path or array of paths to add.
  245.      *
  246.      * @return  array  The list of paths that have been added.
  247.      *
  248.      * @since   11.1
  249.      */
  250.     public static function addFormPath($new null)
  251.     {
  252.         return self::addPath('form'$new);
  253.     }
  254.  
  255.     /**
  256.      * Method to add a path to the list of rule include paths.
  257.      *
  258.      * @param   mixed  $new  A path or array of paths to add.
  259.      *
  260.      * @return  array  The list of paths that have been added.
  261.      *
  262.      * @since   11.1
  263.      */
  264.     public static function addRulePath($new null)
  265.     {
  266.         return self::addPath('rule'$new);
  267.     }
  268.  
  269.     /**
  270.      * Method to add a path to the list of include paths for one of the form's entities.
  271.      * Currently supported entities: field, rule and form. You are free to support your own in a subclass.
  272.      *
  273.      * @param   string  $entity  Form's entity name for which paths will be added.
  274.      * @param   mixed   $new     A path or array of paths to add.
  275.      *
  276.      * @return  array  The list of paths that have been added.
  277.      *
  278.      * @since   11.1
  279.      */
  280.     protected static function addPath($entity$new null)
  281.     {
  282.         // Reference to an array with paths for current entity
  283.         $paths &self::$paths[$entity];
  284.  
  285.         // Add the default entity's search path if not set.
  286.         if (empty($paths))
  287.         {
  288.             // While we support limited number of entities (form, field and rule)
  289.             // we can do this simple pluralisation:
  290.             $entity_plural $entity 's';
  291.  
  292.             /*
  293.              * But when someday we would want to support more entities, then we should consider adding
  294.              * an inflector class to "libraries/joomla/utilities" and use it here (or somebody can use a real inflector in his subclass).
  295.              * See also: pluralization snippet by Paul Osman in JControllerForm's constructor.
  296.              */
  297.             $paths[= __DIR__ . '/' $entity_plural;
  298.         }
  299.  
  300.         // Force the new path(s) to an array.
  301.         settype($new'array');
  302.  
  303.         // Add the new paths to the stack if not already there.
  304.         foreach ($new as $path)
  305.         {
  306.             if (!in_array($path$paths))
  307.             {
  308.                 array_unshift($pathstrim($path));
  309.             }
  310.         }
  311.  
  312.         return $paths;
  313.     }
  314. }

Documentation generated on Tue, 19 Nov 2013 15:04:29 +0100 by phpDocumentor 1.4.3