1 <?php
2 3 4 5 6 7
8
9 defined('FOF_INCLUDED') or die;
10
11 JFormHelper::loadFieldClass('list');
12
13 14 15 16 17 18 19
20 class FOFFormFieldList extends JFormFieldList implements FOFFormField
21 {
22 protected $static;
23
24 protected $repeatable;
25
26
27 public $item;
28
29
30 public $rowid;
31
32 33 34 35 36 37 38 39 40
41 public function __get($name)
42 {
43 switch ($name)
44 {
45 case 'static':
46 if (empty($this->static))
47 {
48 $this->static = $this->getStatic();
49 }
50
51 return $this->static;
52 break;
53
54 case 'repeatable':
55 if (empty($this->repeatable))
56 {
57 $this->repeatable = $this->getRepeatable();
58 }
59
60 return $this->repeatable;
61 break;
62
63 default:
64 return parent::__get($name);
65 }
66 }
67
68 69 70 71 72 73 74 75
76 public function getStatic()
77 {
78 $class = $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
79
80 return '<span id="' . $this->id . '" ' . $class . '>' .
81 htmlspecialchars(self::getOptionName($this->getOptions(), $this->value), ENT_COMPAT, 'UTF-8') .
82 '</span>';
83 }
84
85 86 87 88 89 90 91 92
93 public function getRepeatable()
94 {
95 $show_link = false;
96 $link_url = '';
97
98 $class = $this->element['class'] ? (string) $this->element['class'] : '';
99
100 if ($this->element['show_link'] == 'true')
101 {
102 $show_link = true;
103 }
104
105 if ($this->element['url'])
106 {
107 $link_url = $this->element['url'];
108 }
109 else
110 {
111 $show_link = false;
112 }
113
114 if ($show_link && ($this->item instanceof FOFTable))
115 {
116 $link_url = $this->parseFieldTags($link_url);
117 }
118 else
119 {
120 $show_link = false;
121 }
122
123 $html = '<span class="' . $this->id . ' ' . $class . '">';
124
125 if ($show_link)
126 {
127 $html .= '<a href="' . $link_url . '">';
128 }
129
130 $html .= htmlspecialchars(self::getOptionName($this->getOptions(), $this->value), ENT_COMPAT, 'UTF-8');
131
132 if ($show_link)
133 {
134 $html .= '</a>';
135 }
136
137 $html .= '</span>';
138
139 return $html;
140 }
141
142 143 144 145 146 147 148 149 150 151
152 public static function getOptionName($data, $selected = null, $optKey = 'value', $optText = 'text')
153 {
154 $ret = null;
155
156 foreach ($data as $elementKey => &$element)
157 {
158 if (is_array($element))
159 {
160 $key = $optKey === null ? $elementKey : $element[$optKey];
161 $text = $element[$optText];
162 }
163 elseif (is_object($element))
164 {
165 $key = $optKey === null ? $elementKey : $element->$optKey;
166 $text = $element->$optText;
167 }
168 else
169 {
170
171 $key = $elementKey;
172 $text = $element;
173 }
174
175 if (is_null($ret))
176 {
177 $ret = $text;
178 }
179 elseif ($selected == $key)
180 {
181 $ret = $text;
182 }
183 }
184
185 return $ret;
186 }
187
188 189 190 191 192 193 194 195 196 197 198 199 200 201
202 protected function getOptions()
203 {
204
205 $order = false;
206
207
208 $order_dir = 'asc';
209
210
211 $order_case_sensitive = false;
212
213 if ($this->element['order'] && $this->element['order'] !== 'false')
214 {
215 $order = $this->element['order'];
216 }
217
218 if ($this->element['order_dir'])
219 {
220 $order_dir = $this->element['order_dir'];
221 }
222
223 if ($this->element['order_case_sensitive'])
224 {
225
226 if ($this->element['order_case_sensitive'] == 'true')
227 {
228 $order_case_sensitive = true;
229 }
230 }
231
232
233 $i = 0;
234 $sortOptions = array();
235
236 foreach ($this->element->children() as $option)
237 {
238 $name = JText::alt(trim((string) $option), preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname));
239
240 $sortOptions[$i] = new stdClass;
241 $sortOptions[$i]->option = $option;
242 $sortOptions[$i]->value = $option['value'];
243 $sortOptions[$i]->name = $name;
244 $i++;
245 }
246
247
248 if ($order)
249 {
250 jimport('joomla.utilities.arrayhelper');
251 FOFUtilsArray::sortObjects($sortOptions, $order, $order_dir == 'asc' ? 1 : -1, $order_case_sensitive, false);
252 }
253
254
255 $options = array();
256
257
258 foreach ($sortOptions as $sortOption)
259 {
260 $option = $sortOption->option;
261 $name = $sortOption->name;
262
263
264 if ($option->getName() != 'option')
265 {
266 continue;
267 }
268
269 $tmp = JHtml::_('select.option', (string) $option['value'], $name, 'value', 'text', ((string) $option['disabled'] == 'true'));
270
271
272 $tmp->class = (string) $option['class'];
273
274
275 $tmp->onclick = (string) $option['onclick'];
276
277
278 $options[] = $tmp;
279 }
280
281
282 $source_file = empty($this->element['source_file']) ? '' : (string) $this->element['source_file'];
283 $source_class = empty($this->element['source_class']) ? '' : (string) $this->element['source_class'];
284 $source_method = empty($this->element['source_method']) ? '' : (string) $this->element['source_method'];
285 $source_key = empty($this->element['source_key']) ? '*' : (string) $this->element['source_key'];
286 $source_value = empty($this->element['source_value']) ? '*' : (string) $this->element['source_value'];
287 $source_translate = empty($this->element['source_translate']) ? 'true' : (string) $this->element['source_translate'];
288 $source_translate = in_array(strtolower($source_translate), array('true','yes','1','on')) ? true : false;
289 $source_format = empty($this->element['source_format']) ? '' : (string) $this->element['source_format'];
290
291 if ($source_class && $source_method)
292 {
293
294 if (!empty($source_file))
295 {
296 $source_file = FOFTemplateUtils::parsePath($source_file, true);
297
298 if (FOFPlatform::getInstance()->getIntegrationObject('filesystem')->fileExists($source_file))
299 {
300 include_once $source_file;
301 }
302 }
303
304
305 if (class_exists($source_class, true))
306 {
307
308 if (in_array($source_method, get_class_methods($source_class)))
309 {
310
311 if ($source_format == 'optionsobject')
312 {
313 $options = array_merge($options, $source_class::$source_method());
314 }
315 else
316 {
317
318 $source_data = $source_class::$source_method();
319
320
321 foreach ($source_data as $k => $v)
322 {
323 $key = (empty($source_key) || ($source_key == '*')) ? $k : $v[$source_key];
324 $value = (empty($source_value) || ($source_value == '*')) ? $v : $v[$source_value];
325
326 if ($source_translate)
327 {
328 $value = JText::_($value);
329 }
330
331 $options[] = JHtml::_('select.option', $key, $value, 'value', 'text');
332 }
333 }
334 }
335 }
336 }
337
338 reset($options);
339
340 return $options;
341 }
342
343 344 345 346 347 348 349
350 protected function parseFieldTags($text)
351 {
352 $ret = $text;
353
354
355
356 $keyfield = $this->item->getKeyName();
357 $replace = $this->item->$keyfield;
358 $ret = str_replace('[ITEM:ID]', $replace, $ret);
359
360
361 $ret = str_replace('[ITEMID]', JFactory::getApplication()->input->getInt('Itemid', 0), $ret);
362
363
364 $fields = $this->item->getTableFields();
365
366 foreach ($fields as $fielddata)
367 {
368 $fieldname = $fielddata->Field;
369
370 if (empty($fieldname))
371 {
372 $fieldname = $fielddata->column_name;
373 }
374
375 $search = '[ITEM:' . strtoupper($fieldname) . ']';
376 $replace = $this->item->$fieldname;
377 $ret = str_replace($search, $replace, $ret);
378 }
379
380 return $ret;
381 }
382 }
383