-
Notifications
You must be signed in to change notification settings - Fork 452
/
SiteLocaleSelector.php
136 lines (120 loc) · 5.47 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
<?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 = json_encode($site->getLocales()->toArray());
$selectedLocaleString = '';
if ($selectedLocale) {
$selectedLocaleString = ":selected-locale='{$selectedLocale->getLocaleID()}'";
}
return <<<EOL
<div class="mb-3" data-vue="cms">
<concrete-locale-select
:locales='{$siteLocales}'
name="{$fieldName}"
{$selectedLocaleString}
>
</concrete-locale-select>
</div>
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="form-select ccm-sitelocaleselector">
{$htmlOptions}
</select>
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));
}
}