1<?php
2/**
3 * @package     Joomla.Platform
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
12use Joomla\Utilities\ArrayHelper;
13
14JFormHelper::loadFieldClass('list');
15
16/**
17 * Form Field class for the Joomla Framework.
18 *
19 * @since  3.7.0
20 */
21class JFormFieldComponents extends JFormFieldList
22{
23	/**
24	 * The form field type.
25	 *
26	 * @var     string
27	 * @since   3.7.0
28	 */
29	protected $type = 'Components';
30
31	/**
32	 * Method to get a list of options for a list input.
33	 *
34	 * @return	array  An array of JHtml options.
35	 *
36	 * @since   2.5.0
37	 */
38	protected function getOptions()
39	{
40		$db    = JFactory::getDbo();
41		$query = $db->getQuery(true)
42			->select('name AS text, element AS value')
43			->from('#__extensions')
44			->where('enabled >= 1')
45			->where('type =' . $db->quote('component'));
46
47		$items = $db->setQuery($query)->loadObjectList();
48
49		if ($items)
50		{
51			$lang = JFactory::getLanguage();
52
53			foreach ($items as &$item)
54			{
55				// Load language
56				$extension = $item->value;
57
58				$lang->load("$extension.sys", JPATH_ADMINISTRATOR, null, false, true)
59					|| $lang->load("$extension.sys", JPATH_ADMINISTRATOR . '/components/' . $extension, null, false, true);
60
61				// Translate component name
62				$item->text = JText::_($item->text);
63			}
64
65			// Sort by component name
66			$items = ArrayHelper::sortObjects($items, 'text', 1, true, true);
67		}
68
69		// Merge any additional options in the XML definition.
70		$options = array_merge(parent::getOptions(), $items);
71
72		return $options;
73	}
74}
75