[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> FolderlistField.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2010 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Form\Field;
  11  
  12  use Joomla\CMS\HTML\HTMLHelper;
  13  use Joomla\CMS\Language\Text;
  14  use Joomla\Filesystem\Folder;
  15  use Joomla\Filesystem\Path;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('JPATH_PLATFORM') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Supports an HTML select list of folder
  23   *
  24   * @since  1.7.0
  25   */
  26  class FolderlistField extends ListField
  27  {
  28      /**
  29       * The form field type.
  30       *
  31       * @var    string
  32       * @since  1.7.0
  33       */
  34      protected $type = 'Folderlist';
  35  
  36      /**
  37       * The folder name filter.
  38       *
  39       * @var    string
  40       * @since  4.0.0
  41       */
  42      protected $folderFilter;
  43  
  44      /**
  45       * The exclude.
  46       *
  47       * @var    string
  48       * @since  3.2
  49       */
  50      protected $exclude;
  51  
  52      /**
  53       * The recursive.
  54       *
  55       * @var    string
  56       * @since  3.6
  57       */
  58      protected $recursive;
  59  
  60      /**
  61       * The hideNone.
  62       *
  63       * @var    boolean
  64       * @since  3.2
  65       */
  66      protected $hideNone = false;
  67  
  68      /**
  69       * The hideDefault.
  70       *
  71       * @var    boolean
  72       * @since  3.2
  73       */
  74      protected $hideDefault = false;
  75  
  76      /**
  77       * The directory.
  78       *
  79       * @var    string
  80       * @since  3.2
  81       */
  82      protected $directory;
  83  
  84      /**
  85       * Method to get certain otherwise inaccessible properties from the form field object.
  86       *
  87       * @param   string  $name  The property name for which to get the value.
  88       *
  89       * @return  mixed  The property value or null.
  90       *
  91       * @since   3.2
  92       */
  93      public function __get($name)
  94      {
  95          switch ($name) {
  96              case 'folderFilter':
  97              case 'exclude':
  98              case 'recursive':
  99              case 'hideNone':
 100              case 'hideDefault':
 101              case 'directory':
 102                  return $this->$name;
 103          }
 104  
 105          return parent::__get($name);
 106      }
 107  
 108      /**
 109       * Method to set certain otherwise inaccessible properties of the form field object.
 110       *
 111       * @param   string  $name   The property name for which to set the value.
 112       * @param   mixed   $value  The value of the property.
 113       *
 114       * @return  void
 115       *
 116       * @since   3.2
 117       */
 118      public function __set($name, $value)
 119      {
 120          switch ($name) {
 121              case 'folderFilter':
 122              case 'directory':
 123              case 'exclude':
 124              case 'recursive':
 125                  $this->$name = (string) $value;
 126                  break;
 127  
 128              case 'hideNone':
 129              case 'hideDefault':
 130                  $value = (string) $value;
 131                  $this->$name = ($value === 'true' || $value === $name || $value === '1');
 132                  break;
 133  
 134              default:
 135                  parent::__set($name, $value);
 136          }
 137      }
 138  
 139      /**
 140       * Method to attach a Form object to the field.
 141       *
 142       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 143       * @param   mixed              $value    The form field value to validate.
 144       * @param   string             $group    The field name group control value. This acts as an array container for the field.
 145       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
 146       *                                       full field name would end up being "bar[foo]".
 147       *
 148       * @return  boolean  True on success.
 149       *
 150       * @see     FormField::setup()
 151       * @since   3.2
 152       */
 153      public function setup(\SimpleXMLElement $element, $value, $group = null)
 154      {
 155          $return = parent::setup($element, $value, $group);
 156  
 157          if ($return) {
 158              $this->folderFilter = (string) $this->element['folderFilter'];
 159              $this->exclude      = (string) $this->element['exclude'];
 160  
 161              $recursive       = (string) $this->element['recursive'];
 162              $this->recursive = ($recursive === 'true' || $recursive === 'recursive' || $recursive === '1');
 163  
 164              $hideNone       = (string) $this->element['hide_none'];
 165              $this->hideNone = ($hideNone === 'true' || $hideNone === 'hideNone' || $hideNone === '1');
 166  
 167              $hideDefault       = (string) $this->element['hide_default'];
 168              $this->hideDefault = ($hideDefault === 'true' || $hideDefault === 'hideDefault' || $hideDefault === '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 field options.
 179       *
 180       * @return  array  The field option objects.
 181       *
 182       * @since   1.7.0
 183       */
 184      protected function getOptions()
 185      {
 186          $options = array();
 187  
 188          $path = $this->directory;
 189  
 190          if (!is_dir($path)) {
 191              if (is_dir(JPATH_ROOT . '/' . $path)) {
 192                  $path = JPATH_ROOT . '/' . $path;
 193              } else {
 194                  return [];
 195              }
 196          }
 197  
 198          $path = Path::clean($path);
 199  
 200          // Prepend some default options based on field attributes.
 201          if (!$this->hideNone) {
 202              $options[] = HTMLHelper::_('select.option', '-1', Text::alt('JOPTION_DO_NOT_USE', preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)));
 203          }
 204  
 205          if (!$this->hideDefault) {
 206              $options[] = HTMLHelper::_('select.option', '', Text::alt('JOPTION_USE_DEFAULT', preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)));
 207          }
 208  
 209          // Get a list of folders in the search path with the given filter.
 210          $folders = Folder::folders($path, $this->folderFilter, $this->recursive, true);
 211  
 212          // Build the options list from the list of folders.
 213          if (\is_array($folders)) {
 214              foreach ($folders as $folder) {
 215                  // Remove the root part and the leading /
 216                  $folder = trim(str_replace($path, '', $folder), DIRECTORY_SEPARATOR);
 217  
 218                  // Check to see if the file is in the exclude mask.
 219                  if ($this->exclude) {
 220                      if (preg_match(\chr(1) . $this->exclude . \chr(1), $folder)) {
 221                          continue;
 222                      }
 223                  }
 224  
 225                  $options[] = HTMLHelper::_('select.option', $folder, $folder);
 226              }
 227          }
 228  
 229          // Merge any additional options in the XML definition.
 230          $options = array_merge(parent::getOptions(), $options);
 231  
 232          return $options;
 233      }
 234  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer