1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Form
5 *
6 * @copyright Copyright (C) 2005 - 2017 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 JFormHelper::loadFieldClass('list');
13
14 /**
15 * Form Field class for the Joomla Platform.
16 * Supports a list of installed application languages
17 *
18 * @see JFormFieldContentLanguage for a select list of content languages.
19 * @since 11.1
20 */
21 class JFormFieldLanguage extends JFormFieldList
22 {
23 /**
24 * The form field type.
25 *
26 * @var string
27 * @since 11.1
28 */
29 protected $type = 'Language';
30
31 /**
32 * Method to get the field options.
33 *
34 * @return array The field option objects.
35 *
36 * @since 11.1
37 */
38 protected function getOptions()
39 {
40 // Initialize some field attributes.
41 $client = (string) $this->element['client'];
42
43 if ($client != 'site' && $client != 'administrator')
44 {
45 $client = 'site';
46 }
47
48 // Make sure the languages are sorted base on locale instead of random sorting
49 $languages = JLanguageHelper::createLanguageList($this->value, constant('JPATH_' . strtoupper($client)), true, true);
50 if (count($languages) > 1)
51 {
52 usort(
53 $languages,
54 function ($a, $b)
55 {
56 return strcmp($a['value'], $b['value']);
57 }
58 );
59 }
60
61 // Merge any additional options in the XML definition.
62 $options = array_merge(
63 parent::getOptions(),
64 $languages
65 );
66
67 // Set the default value active language
68 if ($langParams = JComponentHelper::getParams('com_languages'))
69 {
70 switch ((string) $this->value)
71 {
72 case 'site':
73 case 'frontend':
74 case '0':
75 $this->value = $langParams->get('site', 'en-GB');
76 break;
77 case 'admin':
78 case 'administrator':
79 case 'backend':
80 case '1':
81 $this->value = $langParams->get('administrator', 'en-GB');
82 break;
83 case 'active':
84 case 'auto':
85 $lang = JFactory::getLanguage();
86 $this->value = $lang->getTag();
87 break;
88 default:
89 break;
90 }
91 }
92
93 return $options;
94 }
95 }
96