[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
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\File; 15 use Joomla\Filesystem\Folder; 16 use Joomla\Filesystem\Path; 17 18 // phpcs:disable PSR1.Files.SideEffects 19 \defined('JPATH_PLATFORM') or die; 20 // phpcs:enable PSR1.Files.SideEffects 21 22 /** 23 * Supports an HTML select list of files 24 * 25 * @since 1.7.0 26 */ 27 class FilelistField extends ListField 28 { 29 /** 30 * The form field type. 31 * 32 * @var string 33 * @since 1.7.0 34 */ 35 protected $type = 'Filelist'; 36 37 /** 38 * The filename filter. 39 * 40 * @var string 41 * @since 4.0.0 42 */ 43 protected $fileFilter; 44 45 /** 46 * The exclude. 47 * 48 * @var string 49 * @since 3.2 50 */ 51 protected $exclude; 52 53 /** 54 * The hideNone. 55 * 56 * @var boolean 57 * @since 3.2 58 */ 59 protected $hideNone = false; 60 61 /** 62 * The hideDefault. 63 * 64 * @var boolean 65 * @since 3.2 66 */ 67 protected $hideDefault = false; 68 69 /** 70 * The stripExt. 71 * 72 * @var boolean 73 * @since 3.2 74 */ 75 protected $stripExt = false; 76 77 /** 78 * The directory. 79 * 80 * @var string 81 * @since 3.2 82 */ 83 protected $directory; 84 85 /** 86 * Method to get certain otherwise inaccessible properties from the form field object. 87 * 88 * @param string $name The property name for which to get the value. 89 * 90 * @return mixed The property value or null. 91 * 92 * @since 3.2 93 */ 94 public function __get($name) 95 { 96 switch ($name) { 97 case 'fileFilter': 98 case 'exclude': 99 case 'hideNone': 100 case 'hideDefault': 101 case 'stripExt': 102 case 'directory': 103 return $this->$name; 104 } 105 106 return parent::__get($name); 107 } 108 109 /** 110 * Method to set certain otherwise inaccessible properties of the form field object. 111 * 112 * @param string $name The property name for which to set the value. 113 * @param mixed $value The value of the property. 114 * 115 * @return void 116 * 117 * @since 3.2 118 */ 119 public function __set($name, $value) 120 { 121 switch ($name) { 122 case 'fileFilter': 123 case 'directory': 124 case 'exclude': 125 $this->$name = (string) $value; 126 break; 127 128 case 'hideNone': 129 case 'hideDefault': 130 case 'stripExt': 131 $value = (string) $value; 132 $this->$name = ($value === 'true' || $value === $name || $value === '1'); 133 break; 134 135 default: 136 parent::__set($name, $value); 137 } 138 } 139 140 /** 141 * Method to attach a Form object to the field. 142 * 143 * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object. 144 * @param mixed $value The form field value to validate. 145 * @param string $group The field name group control value. This acts as an array container for the field. 146 * For example if the field has name="foo" and the group value is set to "bar" then the 147 * full field name would end up being "bar[foo]". 148 * 149 * @return boolean True on success. 150 * 151 * @see FormField::setup() 152 * @since 3.2 153 */ 154 public function setup(\SimpleXMLElement $element, $value, $group = null) 155 { 156 $return = parent::setup($element, $value, $group); 157 158 if ($return) { 159 $this->fileFilter = (string) $this->element['fileFilter']; 160 $this->exclude = (string) $this->element['exclude']; 161 162 $hideNone = (string) $this->element['hide_none']; 163 $this->hideNone = ($hideNone === 'true' || $hideNone === 'hideNone' || $hideNone === '1'); 164 165 $hideDefault = (string) $this->element['hide_default']; 166 $this->hideDefault = ($hideDefault === 'true' || $hideDefault === 'hideDefault' || $hideDefault === '1'); 167 168 $stripExt = (string) $this->element['stripext']; 169 $this->stripExt = ($stripExt === 'true' || $stripExt === 'stripExt' || $stripExt === '1'); 170 171 // Get the path in which to search for file options. 172 $this->directory = (string) $this->element['directory']; 173 } 174 175 return $return; 176 } 177 178 /** 179 * Method to get the list of files for the field options. 180 * Specify the target directory with a directory attribute 181 * Attributes allow an exclude mask and stripping of extensions from file name. 182 * Default attribute may optionally be set to null (no file) or -1 (use a default). 183 * 184 * @return array The field option objects. 185 * 186 * @since 1.7.0 187 */ 188 protected function getOptions() 189 { 190 $options = array(); 191 192 $path = $this->directory; 193 194 if (!is_dir($path)) { 195 $path = JPATH_ROOT . '/' . $path; 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 files in the search path with the given filter. 210 $files = Folder::files($path, $this->fileFilter); 211 212 // Build the options list from the list of files. 213 if (\is_array($files)) { 214 foreach ($files as $file) { 215 // Check to see if the file is in the exclude mask. 216 if ($this->exclude) { 217 if (preg_match(\chr(1) . $this->exclude . \chr(1), $file)) { 218 continue; 219 } 220 } 221 222 // If the extension is to be stripped, do it. 223 if ($this->stripExt) { 224 $file = File::stripExt($file); 225 } 226 227 $options[] = HTMLHelper::_('select.option', $file, $file); 228 } 229 } 230 231 // Merge any additional options in the XML definition. 232 $options = array_merge(parent::getOptions(), $options); 233 234 return $options; 235 } 236 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |