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