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