/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

  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. defined('JPATH_PLATFORM') or die;
  10. jimport('joomla.filesystem.folder');
  11. jimport('joomla.filesystem.file');
  12. JFormHelper::loadFieldClass('list');
  13. /**
  14. * Supports an HTML select list of files
  15. *
  16. * @package Joomla.Platform
  17. * @subpackage Form
  18. * @since 11.1
  19. */
  20. class JFormFieldFileList extends JFormFieldList
  21. {
  22. /**
  23. * The form field type.
  24. *
  25. * @var string
  26. * @since 11.1
  27. */
  28. public $type = 'FileList';
  29. /**
  30. * Method to get the list of files for the field options.
  31. * Specify the target directory with a directory attribute
  32. * Attributes allow an exclude mask and stripping of extensions from file name.
  33. * Default attribute may optionally be set to null (no file) or -1 (use a default).
  34. *
  35. * @return array The field option objects.
  36. *
  37. * @since 11.1
  38. */
  39. protected function getOptions()
  40. {
  41. $options = array();
  42. // Initialize some field attributes.
  43. $filter = (string) $this->element['filter'];
  44. $exclude = (string) $this->element['exclude'];
  45. $stripExt = (string) $this->element['stripext'];
  46. $hideNone = (string) $this->element['hide_none'];
  47. $hideDefault = (string) $this->element['hide_default'];
  48. // Get the path in which to search for file options.
  49. $path = (string) $this->element['directory'];
  50. if (!is_dir($path))
  51. {
  52. $path = JPATH_ROOT . '/' . $path;
  53. }
  54. // Prepend some default options based on field attributes.
  55. if (!$hideNone)
  56. {
  57. $options[] = JHtml::_('select.option', '-1', JText::alt('JOPTION_DO_NOT_USE', preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)));
  58. }
  59. if (!$hideDefault)
  60. {
  61. $options[] = JHtml::_('select.option', '', JText::alt('JOPTION_USE_DEFAULT', preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)));
  62. }
  63. // Get a list of files in the search path with the given filter.
  64. $files = JFolder::files($path, $filter);
  65. // Build the options list from the list of files.
  66. if (is_array($files))
  67. {
  68. foreach ($files as $file)
  69. {
  70. // Check to see if the file is in the exclude mask.
  71. if ($exclude)
  72. {
  73. if (preg_match(chr(1) . $exclude . chr(1), $file))
  74. {
  75. continue;
  76. }
  77. }
  78. // If the extension is to be stripped, do it.
  79. if ($stripExt)
  80. {
  81. $file = JFile::stripExt($file);
  82. }
  83. $options[] = JHtml::_('select.option', $file, $file);
  84. }
  85. }
  86. // Merge any additional options in the XML definition.
  87. $options = array_merge(parent::getOptions(), $options);
  88. return $options;
  89. }
  90. }