Source for file filelist.php

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

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