Source for file folderlist.php

Documentation is available at folderlist.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.folder');
  13.  
  14. /**
  15.  * Supports an HTML select list of folder
  16.  *
  17.  * @package     Joomla.Platform
  18.  * @subpackage  Form
  19.  * @since       11.1
  20.  */
  21. {
  22.     /**
  23.      * The form field type.
  24.      *
  25.      * @var    string 
  26.      * @since  11.1
  27.      */
  28.     protected $type = 'FolderList';
  29.  
  30.     /**
  31.      * The filter.
  32.      *
  33.      * @var    string 
  34.      * @since  3.2
  35.      */
  36.     protected $filter;
  37.  
  38.     /**
  39.      * The exclude.
  40.      *
  41.      * @var    string 
  42.      * @since  3.2
  43.      */
  44.     protected $exclude;
  45.  
  46.     /**
  47.      * The hideNone.
  48.      *
  49.      * @var    boolean 
  50.      * @since  3.2
  51.      */
  52.     protected $hideNone = false;
  53.  
  54.     /**
  55.      * The hideDefault.
  56.      *
  57.      * @var    boolean 
  58.      * @since  3.2
  59.      */
  60.     protected $hideDefault = false;
  61.  
  62.     /**
  63.      * The directory.
  64.      *
  65.      * @var    string 
  66.      * @since  3.2
  67.      */
  68.     protected $directory;
  69.  
  70.     /**
  71.      * Method to get certain otherwise inaccessible properties from the form field object.
  72.      *
  73.      * @param   string  $name  The property name for which to the the value.
  74.      *
  75.      * @return  mixed  The property value or null.
  76.      *
  77.      * @since   3.2
  78.      */
  79.     public function __get($name)
  80.     {
  81.         switch ($name)
  82.         {
  83.             case 'filter':
  84.             case 'exclude':
  85.             case 'hideNone':
  86.             case 'hideDefault':
  87.             case 'directory':
  88.                 return $this->$name;
  89.         }
  90.  
  91.         return parent::__get($name);
  92.     }
  93.  
  94.     /**
  95.      * Method to set certain otherwise inaccessible properties of the form field object.
  96.      *
  97.      * @param   string  $name   The property name for which to the the value.
  98.      * @param   mixed   $value  The value of the property.
  99.      *
  100.      * @return  void 
  101.      *
  102.      * @since   3.2
  103.      */
  104.     public function __set($name$value)
  105.     {
  106.         switch ($name)
  107.         {
  108.             case 'filter':
  109.             case 'directory':
  110.             case 'exclude':
  111.                 $this->$name = (string) $value;
  112.                 break;
  113.  
  114.             case 'hideNone':
  115.             case 'hideDefault':
  116.                 $value = (string) $value;
  117.                 $this->$name ($value === 'true' || $value === $name || $value === '1');
  118.                 break;
  119.  
  120.             default:
  121.                 parent::__set($name$value);
  122.         }
  123.     }
  124.  
  125.     /**
  126.      * Method to attach a JForm object to the field.
  127.      *
  128.      * @param   SimpleXMLElement  $element  The SimpleXMLElement object representing the <field /> tag for the form field object.
  129.      * @param   mixed             $value    The form field value to validate.
  130.      * @param   string            $group    The field name group control value. This acts as as an array container for the field.
  131.      *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  132.      *                                       full field name would end up being "bar[foo]".
  133.      *
  134.      * @return  boolean  True on success.
  135.      *
  136.      * @see     JFormField::setup()
  137.      * @since   3.2
  138.      */
  139.     public function setup(SimpleXMLElement $element$value$group null)
  140.     {
  141.         $return parent::setup($element$value$group);
  142.  
  143.         if ($return)
  144.         {
  145.             $this->filter  = (string) $this->element['filter'];
  146.             $this->exclude = (string) $this->element['exclude'];
  147.  
  148.             $hideNone       = (string) $this->element['hide_none'];
  149.             $this->hideNone = ($hideNone == 'true' || $hideNone == 'hideNone' || $hideNone == '1');
  150.  
  151.             $hideDefault       = (string) $this->element['hide_default'];
  152.             $this->hideDefault = ($hideDefault == 'true' || $hideDefault == 'hideDefault' || $hideDefault == '1');
  153.  
  154.             // Get the path in which to search for file options.
  155.             $this->directory = (string) $this->element['directory'];
  156.         }
  157.  
  158.         return $return;
  159.     }
  160.  
  161.     /**
  162.      * Method to get the field options.
  163.      *
  164.      * @return  array  The field option objects.
  165.      *
  166.      * @since   11.1
  167.      */
  168.     protected function getOptions()
  169.     {
  170.         $options array();
  171.  
  172.         $path $this->directory;
  173.  
  174.         if (!is_dir($path))
  175.         {
  176.             $path JPATH_ROOT '/' $path;
  177.         }
  178.  
  179.         // Prepend some default options based on field attributes.
  180.         if (!$this->hideNone)
  181.         {
  182.             $options[JHtml::_('select.option''-1'JText::alt('JOPTION_DO_NOT_USE'preg_replace('/[^a-zA-Z0-9_\-]/''_'$this->fieldname)));
  183.         }
  184.  
  185.         if (!$this->hideDefault)
  186.         {
  187.             $options[JHtml::_('select.option'''JText::alt('JOPTION_USE_DEFAULT'preg_replace('/[^a-zA-Z0-9_\-]/''_'$this->fieldname)));
  188.         }
  189.  
  190.         // Get a list of folders in the search path with the given filter.
  191.         $folders JFolder::folders($path$this->filter);
  192.  
  193.         // Build the options list from the list of folders.
  194.         if (is_array($folders))
  195.         {
  196.             foreach ($folders as $folder)
  197.             {
  198.                 // Check to see if the file is in the exclude mask.
  199.                 if ($this->exclude)
  200.                 {
  201.                     if (preg_match(chr(1$this->exclude . chr(1)$folder))
  202.                     {
  203.                         continue;
  204.                     }
  205.                 }
  206.  
  207.                 $options[JHtml::_('select.option'$folder$folder);
  208.             }
  209.         }
  210.  
  211.         // Merge any additional options in the XML definition.
  212.         $options array_merge(parent::getOptions()$options);
  213.  
  214.         return $options;
  215.     }
  216. }

Documentation generated on Tue, 19 Nov 2013 15:03:28 +0100 by phpDocumentor 1.4.3