PageRenderTime 9ms CodeModel.GetById 5ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/form/fields/filelist.php

https://bitbucket.org/eternaware/joomus
PHP | 105 lines | 52 code | 15 blank | 38 comment | 7 complexity | cba8048d6b3826d5ef5fc8775ddb6db8 MD5 | raw file
Possible License(s): LGPL-2.1
  1<?php
  2/**
  3 * @package     Joomla.Platform
  4 * @subpackage  Form
  5 *
  6 * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7 * @license     GNU General Public License version 2 or later; see LICENSE
  8 */
  9
 10defined('JPATH_PLATFORM') or die;
 11
 12jimport('joomla.filesystem.folder');
 13jimport('joomla.filesystem.file');
 14JFormHelper::loadFieldClass('list');
 15
 16/**
 17 * Supports an HTML select list of files
 18 *
 19 * @package     Joomla.Platform
 20 * @subpackage  Form
 21 * @since       11.1
 22 */
 23class JFormFieldFileList extends JFormFieldList
 24{
 25
 26	/**
 27	 * The form field type.
 28	 *
 29	 * @var    string
 30	 * @since  11.1
 31	 */
 32	public $type = 'FileList';
 33
 34	/**
 35	 * Method to get the list of files for the field options.
 36	 * Specify the target directory with a directory attribute
 37	 * Attributes allow an exclude mask and stripping of extensions from file name.
 38	 * Default attribute may optionally be set to null (no file) or -1 (use a default).
 39	 *
 40	 * @return  array  The field option objects.
 41	 *
 42	 * @since   11.1
 43	 */
 44	protected function getOptions()
 45	{
 46		$options = array();
 47
 48		// Initialize some field attributes.
 49		$filter = (string) $this->element['filter'];
 50		$exclude = (string) $this->element['exclude'];
 51		$stripExt = (string) $this->element['stripext'];
 52		$hideNone = (string) $this->element['hide_none'];
 53		$hideDefault = (string) $this->element['hide_default'];
 54
 55		// Get the path in which to search for file options.
 56		$path = (string) $this->element['directory'];
 57		if (!is_dir($path))
 58		{
 59			$path = JPATH_ROOT . '/' . $path;
 60		}
 61
 62		// Prepend some default options based on field attributes.
 63		if (!$hideNone)
 64		{
 65			$options[] = JHtml::_('select.option', '-1', JText::alt('JOPTION_DO_NOT_USE', preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)));
 66		}
 67		if (!$hideDefault)
 68		{
 69			$options[] = JHtml::_('select.option', '', JText::alt('JOPTION_USE_DEFAULT', preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)));
 70		}
 71
 72		// Get a list of files in the search path with the given filter.
 73		$files = JFolder::files($path, $filter);
 74
 75		// Build the options list from the list of files.
 76		if (is_array($files))
 77		{
 78			foreach ($files as $file)
 79			{
 80
 81				// Check to see if the file is in the exclude mask.
 82				if ($exclude)
 83				{
 84					if (preg_match(chr(1) . $exclude . chr(1), $file))
 85					{
 86						continue;
 87					}
 88				}
 89
 90				// If the extension is to be stripped, do it.
 91				if ($stripExt)
 92				{
 93					$file = JFile::stripExt($file);
 94				}
 95
 96				$options[] = JHtml::_('select.option', $file, $file);
 97			}
 98		}
 99
100		// Merge any additional options in the XML definition.
101		$options = array_merge(parent::getOptions(), $options);
102
103		return $options;
104	}
105}