/
Locale.php
761 lines (670 loc) · 21.6 KB
/
Locale.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
<?php
namespace TractorCow\Fluent\Model;
use DateTime;
use DateTimeZone;
use Exception;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\TabSet;
use SilverStripe\Forms\TextField;
use SilverStripe\i18n\i18n;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\HasManyList;
use SilverStripe\ORM\ManyManyThroughList;
use SilverStripe\Security\Member;
use SilverStripe\Security\Permission;
use SilverStripe\Security\PermissionProvider;
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
use TractorCow\Fluent\Control\LocaleAdmin;
use TractorCow\Fluent\Extension\FluentDirectorExtension;
use TractorCow\Fluent\Extension\Traits\FluentObjectTrait;
use TractorCow\Fluent\State\FluentState;
/**
* @property string $Title
* @property string $Locale
* @property string $URLSegment
* @property bool $IsGlobalDefault
* @property int $DomainID
* @property bool $UseDefaultCode
* @property string $Timezone
* @method HasManyList<FallbackLocale> FallbackLocales()
* @method ManyManyThroughList<Locale> Fallbacks()
* @method Domain Domain() Raw SQL Domain (unfiltered by domain mode)
*/
class Locale extends DataObject implements PermissionProvider
{
use CachableModel;
/**
* Code for accessing cross-locale actions
*/
const CMS_ACCESS_MULTI_LOCALE = 'Fluent_Actions_MultiLocale';
/**
* Prefix for per-locale permission code.
*
* Note that this is not a permission code in itself, and must always be
* joined with a locale.
*/
const CMS_ACCESS_FLUENT_LOCALE = 'Fluent_Locale_';
private static $table_name = 'Fluent_Locale';
private static $singular_name = 'Locale';
private static $plural_name = 'Locales';
/**
* hreflang for default landing pages.
* Note: PHP's ext-intl doesn't support this code, so only use it
* in templates.
*/
const X_DEFAULT = 'x-default';
private static $summary_fields = [
'Title' => 'Title',
'Locale' => 'Locale',
'URLSegment' => 'URL',
'IsGlobalDefault' => 'Global Default',
'Domain.Domain' => 'Domain',
];
/**
* @config
* @var array
*/
private static $db = [
'Title' => 'Varchar(100)',
'Locale' => 'Varchar(10)',
'URLSegment' => 'Varchar(100)',
'IsGlobalDefault' => 'Boolean',
'UseDefaultCode' => 'Boolean',
'Sort' => 'Int',
'Timezone' => 'Varchar(100)',
];
private static $default_sort = '"Fluent_Locale"."Sort" ASC, "Fluent_Locale"."Locale" ASC';
public function populateDefaults()
{
parent::populateDefaults();
$this->Timezone = date_default_timezone_get();
}
/**
* @config
* @var array
*/
private static $has_one = [
'Domain' => Domain::class,
];
private static $has_many = [
'FallbackLocales' => FallbackLocale::class . '.Parent',
];
private static $many_many = [
'Fallbacks' => [
'through' => FallbackLocale::class,
'from' => 'Parent',
'to' => 'Locale',
],
];
/**
* @var ArrayList<Locale>
*/
protected $chain = null;
/**
* @var Locale[]
*/
protected static $locales_by_title;
public function requireDefaultRecords(): void
{
parent::requireDefaultRecords();
// Migrate legacy permission codes to new codes
$permissions = Permission::get()->filter('Code:StartsWith', 'CMS_ACCESS_Fluent_');
$count = $permissions->count();
if ($count) {
DB::alteration_message(sprintf('Migrating %d old fluent permissions', $count), 'changed');
}
foreach ($permissions as $permission) {
$permission->Code = str_replace('CMS_ACCESS_Fluent_', 'Fluent_', $permission->Code);
$permission->write();
}
}
/**
* Get internal title for this locale
*
* @return string
*/
public function getTitle()
{
$title = $this->getField('Title');
if ($title) {
return $title;
}
return $this->getDefaultTitle();
}
/**
* Long title (including locale code)
*
* @return string
*/
public function getLongTitle()
{
return "{$this->Title} ({$this->Locale})";
}
/**
* @return string
*/
protected function getDefaultTitle()
{
// Get default name from locale
return i18n::getData()->localeName($this->getLocale());
}
/**
* Locale code for this object
*
* @return string
*/
public function getLocale()
{
$locale = $this->getField('Locale');
if ($locale) {
return $locale;
}
return $this->getDefaultLocale();
}
/**
* Default locale for
*
* @return string
*/
public function getDefaultLocale()
{
return i18n::config()->get('default_locale');
}
/**
* Get the locale's country part
*
* @return string e.g. "NZ" for "en_NZ"
*/
public function getLocaleSuffix()
{
$bits = explode('_', $this->Locale);
return array_pop($bits);
}
/**
* Returns the label to display for Fluent badges in the CMS. By default this is the
* locale's URLSegment as set in /admin/locales, but can be configured with extensions.
*
* For example, you may want to display the full locale badge:
* <code>
* public function updateBadgeLabel(&$badgeLabel)
* {
* $badgeLabel = $this->owner->Locale;
* }
* </code>
*
* @return string
*/
public function getBadgeLabel()
{
$badgeLabel = $this->getURLSegment();
$this->extend('updateBadgeLabel', $badgeLabel);
return (string)$badgeLabel;
}
/**
* RFC 1766 hreflang
*
* @return string
*/
public function getHrefLang()
{
if ($this->UseDefaultCode) {
return self::X_DEFAULT;
}
return strtolower(i18n::convert_rfc1766($this->Locale));
}
/**
* Get URLSegment for this locale
*
* @return string
*/
public function getURLSegment()
{
$segment = $this->getField('URLSegment');
if ($segment) {
return $segment;
}
// Default to locale
return $this->getLocale();
}
public function getCMSFields()
{
$fields = FieldList::create(TabSet::create('Root'));
// Main tab
$fields->addFieldsToTab(
'Root.Main',
[
DropdownField::create(
'Locale',
_t(__CLASS__ . '.LOCALE', 'Locale'),
i18n::getData()->getLocales()
),
TextField::create(
'Title',
_t(__CLASS__ . '.LOCALE_TITLE', 'Title')
)->setAttribute('placeholder', $this->getDefaultTitle()),
TextField::create(
'URLSegment',
_t(__CLASS__ . '.LOCALE_URL', 'URL Segment')
)->setAttribute('placeholder', $this->Locale),
$globalDefault = CheckboxField::create(
'IsGlobalDefault',
_t(__CLASS__ . '.IS_DEFAULT', 'This is the global default locale')
)
->setAttribute('data-hides', 'ParentDefaultID')
->setDescription(_t(
__CLASS__ . '.IS_DEFAULT_DESCRIPTION',
'Note: Per-domain specific locale can be assigned on the Locales tab'
. ' and will override this value for specific domains.'
)),
CheckboxField::create(
'UseDefaultCode',
_t(__CLASS__ . '.USE_X_DEFAULT', 'Use {code} as SEO language code (treat as global)', ['code' => self::X_DEFAULT])
)
->setDescription(_t(
__CLASS__ . '.USE_X_DEFAULT_DESCRIPTION',
'Use of this code indicates to search engines that this is a non-localised global landing page'
)),
DropdownField::create(
'Timezone',
_t(__CLASS__ . '.TIMEZONE', 'Timezone'),
$this->getTimezones()
)->setEmptyString(_t(__CLASS__ . '.DEFAULT_NONE', '(none)')),
DropdownField::create(
'DomainID',
_t(__CLASS__ . '.DOMAIN', 'Domain'),
Domain::get()->map('ID', 'Domain')
)->setEmptyString(_t(__CLASS__ . '.DEFAULT_NONE', '(none)'))
]
);
if ($this->exists()) {
$config = GridFieldConfig::create()
->addComponents(
new GridFieldButtonRow(),
new GridFieldAddNewInlineButton(),
new GridFieldOrderableRows('Sort'),
$editable = new GridFieldEditableColumns(),
new GridFieldDeleteAction()
);
$editable->setDisplayFields([
'LocaleID' => function () {
return DropdownField::create(
'LocaleID',
_t(__CLASS__ . '.LOCALE', 'Locale'),
Locale::getCached()->exclude('Locale', $this->Locale)->map('ID', 'Title')
);
}
]);
// Add default selection
$defaultField = GridField::create(
'FallbackLocales',
_t(__CLASS__ . '.FALLBACKS', 'Fallback Locales'),
$this->FallbackLocales(),
$config
);
$fields->addFieldToTab('Root.Fallbacks', $defaultField);
} else {
$fields->addFieldToTab(
'Root.Fallbacks',
LiteralField::create(
'UnsavedNotice',
'<p>' . _t(__CLASS__ . '.UnsavedNotice', "You can add fallbacks once you've saved the locale.")
)
);
// If this is the first locale, it should be checked by default
if (static::getCached()->count() === 0) {
$globalDefault->setValue(true);
}
}
$this->extend('updateCMSFields', $fields);
return $fields;
}
/**
* Get default locale
*
* @param string|null|true $domain If provided, the default locale for the given domain will be returned.
* If true, then the current state domain will be used (if in domain mode).
* @return Locale
*/
public static function getDefault($domain = null)
{
// Get by domain if it exists and has a default
$domainObject = Domain::getByDomain($domain);
if ($domainObject) {
$default = $domainObject->DefaultLocale();
if ($default) {
return $default;
}
}
// Get explicit or implicit default
$locales = static::getLocales();
return $locales->filter('IsGlobalDefault', 1)->first()
?: $locales->first();
}
/**
* Get current locale object
*
* @return Locale|null
*/
public static function getCurrentLocale(): ?Locale
{
$locale = FluentState::singleton()->getLocale();
return static::getByLocale($locale);
}
/**
* Get object by locale code.
*
* @param string|Locale $locale
* @return Locale|null
*/
public static function getByLocale($locale): ?Locale
{
if (!$locale) {
return null;
}
if ($locale instanceof Locale) {
return $locale;
}
// Get filtered locale
return Locale::getCached()->find('Locale', $locale);
}
/**
* Returns whether the given locale matches the current Locale object
*
* @param string $locale E.g. en_NZ, en-NZ, en-nz-1990
* @return bool
*/
public function isLocale($locale)
{
return stripos(i18n::convert_rfc1766($locale), i18n::convert_rfc1766($this->Locale)) === 0;
}
/**
* Check if this is the default (non-global).
* Use IsGlobalDefault check if global default otherwise.
*
* @return bool
*/
public function getIsDefault()
{
// Get default for own domain
$default = static::getDefault($this->getDomain());
// Compare best default with current locale
return $default && ((int)$default->ID === (int)$this->ID);
}
/**
* Get domain if in domain mode
*
* @return Domain|null Domain found, or null if not in domain mode (or no domain)
*/
public function getDomain()
{
if (FluentState::singleton()->getIsDomainMode() && $this->DomainID) {
return Domain::getCached()->byID($this->DomainID);
}
return null;
}
/**
* Determine if this locale is the sole locale on its domain,
* or globally if domain mode is disabled
*
* @return bool
*/
public function getIsOnlyLocale()
{
// Get locales filtered by same domain (in domain mode)
$locales = $this->getSiblingLocales();
return $locales->count() < 2;
}
/**
* Get available locales
*
* @param string|null|true $domain If provided, locales for the given domain will be returned.
* If true, then the current state domain will be used (if in domain mode).
* @return ArrayList<Locale>
*/
public static function getLocales($domain = null)
{
// Optionally filter by domain
$domainObj = Domain::getByDomain($domain);
if ($domainObj) {
return $domainObj->getLocales();
}
return Locale::getCached();
}
public function onAfterWrite()
{
parent::onAfterWrite();
// If this is the default locale, remove default from other locales
if ($this->IsGlobalDefault) {
$table = $this->baseTable();
DB::prepared_query(
"UPDATE \"{$table}\" SET \"IsGlobalDefault\" = 0 WHERE \"ID\" != ?",
[$this->ID]
);
}
}
/**
* Get chain of all locales that should be preferred when this locale is current
*
* @return ArrayList<Locale>
*/
public function getChain()
{
if ($this->chain) {
return $this->chain;
}
$this->chain = ArrayList::create();
// Push the current locale as the first fallback.
$this->chain->push($this);
// Get the current locale and sort them by "Sort" field.
$fallbacks = $this->FallbackLocales()->sort('Sort');
foreach ($fallbacks as $fallback) {
$this->chain->push($fallback->Locale());
}
return $this->chain;
}
/**
* Fetch a native language string from the {@link i18n} class via the current locale code in the format "XX_xx". In
* the event a match cannot be found in any framework resource, an empty string is returned.
*
* @return string The native language string for the current locale e.g. "português (Brazil)"
*/
public function getNativeName()
{
$locales = i18n::getData();
// Attempts to fetch the native language string via the `i18n::$common_languages` array
if ($native = $locales->languageName($locales->langFromLocale($this->Locale))) {
return $native;
}
return '';
}
/**
* Determine the base URL within the current locale
*
* @return string
*/
public function getBaseURL()
{
$base = Director::baseURL();
// Prepend hostname for domain mode
$domain = $this->getDomain();
if ($domain) {
$base = Controller::join_links($domain->Link(), $base);
}
// Determine if base suffix should be appended
$append = true;
if ($this->getIsDefault()) {
// Apply config
$append = !(bool)Config::inst()->get(FluentDirectorExtension::class, 'disable_default_prefix');
}
if ($append) {
// Append locale url segment
$base = Controller::join_links($base, $this->getURLSegment(), '/');
}
return $base;
}
/**
* Get other locales that appear alongside this (including self)
*
* @return ArrayList<Locale>
*/
public function getSiblingLocales()
{
$domain = $this->getDomain();
$locales = $domain
? $domain->getLocales()
: Locale::getCached();
return $locales;
}
/**
* Get details for the current object in this locale.
*
* @return null|RecordLocale
* @see FluentObjectTrait::LinkedLocales()
*/
public function RecordLocale()
{
$recordID = $this->getSourceQueryParam('FluentObjectID');
$recordClass = $this->getSourceQueryParam('FluentObjectClass');
if (!$recordID || !$recordClass) {
return null;
}
$record = DataObject::get($recordClass)->byID($recordID);
if ($record) {
return RecordLocale::create($record, $this);
}
return null;
}
/**
* Get permission code to enable access in this locale
*
* @return string
*/
public function getLocaleEditPermission()
{
$prefix = self::CMS_ACCESS_FLUENT_LOCALE;
return "{$prefix}{$this->Locale}";
}
public function providePermissions()
{
$category = _t(__CLASS__ . '.PERMISSION', 'Localisation');
$permissions = [
// @todo - Actually implement this check on those actions
self::CMS_ACCESS_MULTI_LOCALE => [
'name' => _t(
__CLASS__ . '.MULTI_LOCALE',
'Access to multi-locale actions (E.g. save in all locales)'
),
'category' => $category,
],
];
foreach (Locale::getCached() as $locale) {
$permissions[$locale->getLocaleEditPermission()] = [
'name' => _t(
__CLASS__ . '.EDIT_LOCALE',
'Access "{title}" ({locale})',
[
'title' => $locale->Title,
'locale' => $locale->Locale,
]
),
'category' => $category,
];
}
return $permissions;
}
/**
* @param Member $member
* @return boolean
*/
public function canView($member = null)
{
$extended = $this->extendedCan(__FUNCTION__, $member);
if ($extended !== null) {
return $extended;
}
return Permission::check('CMS_ACCESS', 'any', $member);
}
/**
* @param Member $member
* @return boolean
*/
public function canEdit($member = null)
{
$extended = $this->extendedCan(__FUNCTION__, $member);
if ($extended !== null) {
return $extended;
}
// Access locale admin permission
return LocaleAdmin::singleton()->canView($member);
}
/**
* @param Member $member
* @return boolean
*/
public function canDelete($member = null)
{
$extended = $this->extendedCan(__FUNCTION__, $member);
if ($extended !== null) {
return $extended;
}
// Access locale admin permission
return LocaleAdmin::singleton()->canView($member);
}
/**
* @param Member $member
* @param array $context Additional context-specific data which might
* affect whether (or where) this object could be created.
* @return boolean
*/
public function canCreate($member = null, $context = [])
{
$extended = $this->extendedCan(__FUNCTION__, $member, $context);
if ($extended !== null) {
return $extended;
}
// Access locale admin permission
return LocaleAdmin::singleton()->canView($member);
}
/**
* Get list of timezones
*
* @return array
* @throws Exception
*/
protected function getTimezones()
{
static $timezones = null;
if ($timezones !== null) {
return $timezones;
}
$timezones = [];
$offsets = [];
$now = new DateTime('now', new DateTimeZone('UTC'));
foreach (DateTimeZone::listIdentifiers() as $timezone) {
$now->setTimezone(new DateTimeZone($timezone));
$offsets[] = $offset = $now->getOffset();
// Format offset
$hours = intval($offset / 3600);
$minutes = abs(intval($offset % 3600 / 60));
$name = str_replace(['/', '_', 'St'], [', ', ' ', 'St. '], $timezone);
$offsetTime = $offset ? sprintf('%+03d:%02d', $hours, $minutes) : '';
$timezones[$timezone] = "(GMT{$offsetTime}) {$name}";
}
array_multisort($offsets, $timezones);
return $timezones;
}
}