1<?php
2/**
3 * @package     Joomla.Libraries
4 * @subpackage  Form
5 *
6 * @copyright   Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
7 * @license     GNU General Public License version 2 or later; see LICENSE
8 */
9
10defined('JPATH_PLATFORM') or die;
11
12JFormHelper::loadFieldClass('list');
13
14/**
15 * Form Field to load a list of predefined values
16 *
17 * @since  3.2
18 */
19abstract class JFormFieldPredefinedList extends JFormFieldList
20{
21	/**
22	 * The form field type.
23	 *
24	 * @var    string
25	 * @since  3.2
26	 */
27	protected $type = 'PredefinedList';
28
29	/**
30	 * Cached array of the category items.
31	 *
32	 * @var    array
33	 * @since  3.2
34	 */
35	protected static $options = array();
36
37	/**
38	 * Available predefined options
39	 *
40	 * @var  array
41	 * @since  3.2
42	 */
43	protected $predefinedOptions = array();
44
45	/**
46	 * Translate options labels ?
47	 *
48	 * @var  boolean
49	 * @since  3.2
50	 */
51	protected $translate = true;
52
53	/**
54	 * Method to get the options to populate list
55	 *
56	 * @return  array  The field option objects.
57	 *
58	 * @since   3.2
59	 */
60	protected function getOptions()
61	{
62		// Hash for caching
63		$hash = md5($this->element);
64		$type = strtolower($this->type);
65
66		if (!isset(static::$options[$type][$hash]) && !empty($this->predefinedOptions))
67		{
68			static::$options[$type][$hash] = parent::getOptions();
69
70			$options = array();
71
72			// Allow to only use specific values of the predefined list
73			$filter = isset($this->element['filter']) ? explode(',', $this->element['filter']) : array();
74
75			foreach ($this->predefinedOptions as $value => $text)
76			{
77				$val = (string) $value;
78
79				if (empty($filter) || in_array($val, $filter, true))
80				{
81					$text = $this->translate ? JText::_($text) : $text;
82
83					$options[] = (object) array(
84						'value' => $value,
85						'text'  => $text,
86					);
87				}
88			}
89
90			static::$options[$type][$hash] = array_merge(static::$options[$type][$hash], $options);
91		}
92
93		return static::$options[$type][$hash];
94	}
95}
96