/
SiteLocaleSelector.php
198 lines (172 loc) · 7.98 KB
/
SiteLocaleSelector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace Concrete\Core\Form\Service\Widget;
use Concrete\Core\Entity\Site\Locale;
use Concrete\Core\Entity\Site\Site;
use Concrete\Core\Multilingual\Service\UserInterface\Flag;
use Concrete\Core\Utility\Service\Identifier;
class SiteLocaleSelector
{
/**
* Creates form fields and JavaScript page chooser for choosing a locale.
*
* @param string $fieldName
* @param \Concrete\Core\Entity\Site\Site $site
* @param \Concrete\Core\Entity\Site\Locale|null $selectedLocale
* @param array $options Supported options are:
* bool $allowNull Set to a non falsy value to allow users to choose "no" locale [default: false]
* string $noLocaleText The string to represent "no locale" [default: t('No Locale')]
* bool|string $displayLocaleCode Set to 'auto' to automatically determine it; set to a non falsy value to display the locale ID [default: 'auto']
*
* @return string
*/
public function selectLocale($fieldName, Site $site, ?Locale $selectedLocale = null, array $options = [])
{
$siteLocales = $site->getLocales()->toArray();
$allowNull = !empty($options['allowNull']);
$nullText = $options['noLocaleText'] ?? t('No Locale');
$displayLocaleCode = $this->shouldDisplayLocaleCode($options, $siteLocales);
if ($selectedLocale === null && !$allowNull) {
$selectedLocale = $site->getDefaultLocale();
}
$localeID = $selectedLocale ? $selectedLocale->getLocaleID() : '';
$identifier = (new Identifier())->getString(32);
$flag = $selectedLocale ? Flag::getLocaleFlagIcon($selectedLocale) : '';
if ($selectedLocale) {
$label = h($selectedLocale->getLanguageText());
if ($displayLocaleCode) {
$label .= ' <span class="text-muted small">' . h($selectedLocale->getLocale()) . '</span>';
}
} else {
$label = $nullText;
}
$localeHTML = '';
if ($allowNull) {
$localeHTML .= '<li><a href="#"' . ($selectedLocale === null ? ' data-locale="default"' : '') . ' data-select-locale="">' . $nullText . '</li>';
}
foreach ($siteLocales as $locale) {
$localeHTML .= '<li><a href="#" ';
if ($selectedLocale && $selectedLocale->getLocaleID() == $locale->getLocaleID()) {
$localeHTML .= 'data-locale="default"';
}
$localeHTML .= 'data-select-locale="' . $locale->getLocaleID() . '">';
$localeHTML .= Flag::getLocaleFlagIcon($locale) . ' ' . $locale->getLanguageText();
if ($displayLocaleCode) {
$localeHTML .= ' <span class="text-muted small">' . h($locale->getLocale()) . '</span>';
}
$localeHTML .= '</a></li>';
}
return <<<EOL
<input type="hidden" name="{$fieldName}" value="{$localeID}">
<div class="btn-group" data-locale-selector="{$identifier}">
<button type="button" class="btn btn-secondary" data-bs-toggle="dropdown">
{$flag} {$label}
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
{$localeHTML}
</ul>
</div>
<script type="text/javascript">
$(function() {
$('[data-bs-toggle=dropdown]').dropdown();
$('div[data-locale-selector={$identifier}]').on('click', 'a[data-select-locale]', function(e) {
e.preventDefault();
var localeID = $(this).attr('data-select-locale'),
html = $(this).html() + ' <span class="caret"></span>',
form = $(this).closest('form');
form.find('input[name={$fieldName}]').val(localeID);
form.find('div[data-locale-selector={$identifier}] > button').html(html);
});
});
</script>
EOL;
}
/**
* Creates form fields and JavaScript page chooser for choosing a locale.
*
* @param string $fieldName
* @param \Concrete\Core\Entity\Site\Site $site
* @param \Concrete\Core\Entity\Site\Locale[]|string[]|int[] $selectedLocales
* @param array $options Supported options are:
* string $noLocaleText The string to represent "no locale" [default: t('No Locale')]
* bool|string $displayLocaleCode Set to 'auto' to automatically determine it; set to a non falsy value to display the locale ID [default: 'auto']
*
* @return string
*/
public function selectLocaleMultiple($fieldName, Site $site, array $selectedLocales = [], array $options = [])
{
$siteLocales = $site->getLocales()->toArray();
$displayLocaleCode = $this->shouldDisplayLocaleCode($options, $siteLocales);
$actuallySelectedLocales = [];
foreach ($selectedLocales as $selectedLocale) {
$localeID = null;
$localeCode = null;
if ($selectedLocale instanceof Locale) {
$localeID = $selectedLocale->getLocaleID();
} elseif (is_numeric($selectedLocale)) {
$localeID = (int) $selectedLocale;
} else {
$localeCode = (string) $localeCode;
}
foreach ($siteLocales as $siteLocale) {
if ($localeID === $siteLocale->getLocaleID() || $localeCode === $siteLocale->getLocale()) {
$actuallySelectedLocales[] = $siteLocale;
}
}
}
$htmlOptions = '';
foreach ($siteLocales as $siteLocale) {
$optionContent = '<div>' . (string) Flag::getLocaleFlagIcon($siteLocale) . ' ' . htmlspecialchars($siteLocale->getLanguageText());
if ($displayLocaleCode) {
$optionContent .= ' <span class="text-muted small">' + htmlspecialchars($siteLocale->getLocale()) + '</span>';
}
$optionContent .= '</div>';
$optionContent = str_replace('"', "'", $optionContent);
$htmlOptions .= '<option';
$htmlOptions .= ' data-content="' . $optionContent . '"';
$searchToken = $displayLocaleCode ? htmlspecialchars($siteLocale->getLanguageText()) . ' ' . htmlspecialchars($siteLocale->getLocale()) : htmlspecialchars($siteLocale->getLanguageText());
$htmlOptions .= ' data-tokens="' . $searchToken . '"';
$htmlOptions .= ' value="' . $siteLocale->getLocaleID() . '"';
if (in_array($siteLocale, $actuallySelectedLocales, true)) {
$htmlOptions .= ' selected="selected"';
}
$htmlOptions .= '>' . h($siteLocale->getLanguageText()) . '</option>';
}
$fieldNameHtml = h($fieldName . (substr($fieldName, -2) === '[]' ? '' : '[]'));
$identifier = 'ccm-sitelocaleselector-' . trim(preg_replace('/\W+/', '_', $fieldName), '_') . '-' . (new Identifier())->getString(32);
$identifierHtml = h($identifier);
$identifierJS = json_encode($identifier);
return <<<EOL
<select id="{$identifierHtml}" name="{$fieldNameHtml}" multiple="multiple" class="ccm-sitelocaleselector">
{$htmlOptions}
</select>
<script type="text/javascript">
$(document).ready(function() {
$('#' + {$identifierJS}).selectpicker({
liveSearch: true,
width: '100%'
});
});
</script>
EOL;
}
/**
* @param array $options
* @param \Concrete\Core\Entity\Site\Locale[] $locales
*
* @return bool
*/
private function shouldDisplayLocaleCode(array $options, array $locales)
{
if (isset($options['displayLocaleCode']) && $options['displayLocaleCode'] !== 'auto') {
return $options['displayLocaleCode'] ? true : false;
}
$names = array_map(
function (Locale $locale) {
return $locale->getLanguageText();
},
$locales
);
return count($names) > count(array_unique($names));
}
}