-
Notifications
You must be signed in to change notification settings - Fork 108
/
RecordLocale.php
385 lines (333 loc) · 9.96 KB
/
RecordLocale.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
<?php
namespace TractorCow\Fluent\Model;
use SilverStripe\Control\Director;
use SilverStripe\i18n\i18n;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\ViewableData;
use TractorCow\Fluent\Extension\FluentExtension;
use TractorCow\Fluent\Extension\FluentFilteredExtension;
use TractorCow\Fluent\Extension\FluentVersionedExtension;
use TractorCow\Fluent\State\FluentState;
/**
* Object that represents details of a specific dataobject in a specific locale
*/
class RecordLocale extends ViewableData
{
/**
* @var Locale
*/
protected $localeObject;
/**
* Object with possibly either fluent or filtered extension
*
* @var DataObject
*/
protected $originalRecord;
/**
* Object in the appropriate locale (cached)
*
* @var DataObject
*/
protected $record = null;
/**
* FluentLocale constructor.
*
* @param DataObject $record
* @param Locale $locale
*/
public function __construct(DataObject $record, Locale $locale)
{
$this->originalRecord = $record;
$this->localeObject = $locale;
}
/**
* Get the locale object
*
* @return Locale
*/
public function getLocaleObject()
{
return $this->localeObject;
}
/**
* Get record (note: May not be localised in the correct locale)
*
* @return DataObject
*/
protected function getOriginalRecord()
{
return $this->originalRecord;
}
/**
* Get localised record
*
* @return DataObject
*/
public function getRecord()
{
// Save cached result
if (isset($this->record)) {
return $this->record ?: null;
}
// If record isn't saved, use draft record
if (!$this->originalRecord->isInDB()) {
return $this->originalRecord;
}
// Reload localised record in the corret locale
$record = FluentState::singleton()->withState(function (FluentState $newState) {
$newState->setLocale($this->getLocale());
$originalRecord = $this->getOriginalRecord();
return $originalRecord->get()->byID($originalRecord->ID);
});
// Reload this record in the correct locale
$this->record = $record ?: false;
return $record;
}
/**
* Locale code
*
* @return string
*/
public function getLocale()
{
return $this->getLocaleObject()->Locale;
}
/**
* RFC1766 version of this locale
*
* @return string
*/
public function getLocaleRFC1766()
{
return $this->getHrefLang();
}
/**
* Get hreflang field
*
* @return string
*/
public function getHrefLang()
{
$locale = $this->getLocaleObject();
return $locale->getHrefLang();
}
/**
* URLSegment of this language (not the record)
*
* @return string
*/
public function getURLSegment()
{
return $this->getLocaleObject()->URLSegment;
}
/**
* Title of this language
*
* @return string
*/
public function getTitle()
{
return $this->getLocaleObject()->getTitle();
}
/**
* @return string
*/
public function getLanguageNative()
{
return $this->getLocaleObject()->getNativeName();
}
/**
* @return string
*/
public function getLanguage()
{
return i18n::getData()->langFromLocale($this->getLocale());
}
/**
* Check if this object can be viewed in this locale
*
* @return bool
*/
public function canViewInLocale()
{
$locale = $this->getLocale();
$results = $this->originalRecord->invokeWithExtensions('canViewInLocale', $locale);
$results = array_filter($results, function ($v) {
return !is_null($v);
});
if ($results) {
return min($results);
}
return true;
}
/**
* Get link to this object in the given locale
*
* @return string|null
*/
public function getLink()
{
// Check if object is linkable
if (!$this->getOriginalRecord()->hasMethod('Link')) {
return null;
}
// Check if object is viewable in this locale
// Note that this object could be filtered, or unpublished,
// in which case fail over to locale base.
$record = $this->getRecord();
if (!$record || !$this->canViewInLocale()) {
return $this->getLocaleObject()->getBaseURL();
}
// If original record implements LocaleLink, respect this
// note: hasMethod() will infinite loop, so don't use this.
if (method_exists($record, 'LocaleLink')) {
return $record->LocaleLink($this->getLocale());
}
// Get link from localised record
return $record->Link();
}
/**
* @return string|null
*/
public function getAbsoluteLink()
{
$link = $this->getLink();
return $link ? Director::absoluteURL($link) : null;
}
/**
* @return string
*/
public function getLinkingMode()
{
if (!$this->canViewInLocale()) {
return 'invalid';
}
if ($this->getLocale() === FluentState::singleton()->getLocale()) {
return 'current';
}
return 'link';
}
/**
* Check if object is visible (ignore published status)
*
* @return bool
*/
public function IsVisible()
{
$locale = $this->getLocale();
$results = $this->originalRecord->invokeWithExtensions('isAvailableInLocale', $locale);
$results = array_filter($results, function ($v) {
return !is_null($v);
});
if ($results) {
return min($results);
}
return true;
}
/**
* Check if record is visible on live
* Set $inLocale to true if this record must be published in the specified locale.
* If frontend_publish_required is false (and $inLocale is false) then this page is considered live.
*
* @param bool $inLocale
* @return bool
*/
public function IsPublished($inLocale = false): bool
{
// If object is filtered, object is not available (regardless of published status)
if (!$inLocale && !$this->IsVisible()) {
return false;
}
/** @var DataObject|FluentExtension|FluentVersionedExtension|FluentFilteredExtension|Versioned $record */
$record = $this->getOriginalRecord();
// If frontend publishing is not required for localisation,
// we need to check if record is published in the source locale
if (!$inLocale && $record->config()->get('frontend_publish_required') !== FluentExtension::INHERITANCE_MODE_EXACT) {
$sourceLocale = $this->getSourceLocale();
if (!$sourceLocale) {
// No source locale available
return false;
}
// Record needs to be published in the source locale
$locale = $sourceLocale->Locale;
} else {
// Record needs to be published in the current locale
$locale = $this->getLocale();
}
// Check if versioned item is published
if ($record->hasExtension(FluentVersionedExtension::class)) {
return $record->isPublishedInLocale($locale);
}
// Check if un-versioned item is saved
if ($record->hasExtension(FluentExtension::class)) {
return $record->existsInLocale($locale);
}
return true;
}
/**
* Check if record is visible on draft
*
* @return bool
*/
public function IsDraft()
{
/** @var DataObject|FluentExtension|FluentVersionedExtension|FluentFilteredExtension $record */
$record = $this->getOriginalRecord();
// Check if versioned item is saved in draft mode
if ($record->hasExtension(FluentVersionedExtension::class)) {
return $record->isDraftedInLocale($this->getLocale());
}
// Check if un-versioned item is saved
if ($record->hasExtension(FluentExtension::class)) {
return $record->existsInLocale($this->getLocale());
}
return true;
}
/**
* Check if a record is published, but modified on draft
* useful to indicate that record has changes that need publishing
*
* @return bool
*/
public function getStagesDiffer(): bool
{
if (!$this->IsPublished(true)) {
return false;
}
/** @var DataObject|FluentVersionedExtension $record */
$record = $this->getOriginalRecord();
// Check if item is versioned
if (!$record->hasExtension(FluentVersionedExtension::class)) {
return false;
}
$locale = $this->getLocale();
$stagesDiffer = $record->stagesDifferInLocale($locale);
// Allow customisation of stages differ
// For example, this is useful for advanced stages comparison which cover nested models like blocks
// Note that this is intentionally separate from FluentVersionedExtension::updateStagesDiffer()
// so we can have more flexibility on where this is applied
$record->extend('updateRecordLocaleStagesDiffer', $stagesDiffer, $locale);
return $stagesDiffer;
}
/**
* Get the locale which is the source of content for this record
*
* @return Locale|null
*/
public function getSourceLocale(): ?Locale
{
/** @var DataObject|FluentExtension $record */
$record = $this->getOriginalRecord();
if ($record->existsInLocale($this->getLocale())) {
return $this->getLocaleObject();
}
foreach ($this->getLocaleObject()->Fallbacks() as $fallback) {
if (!$record->existsInLocale($fallback->Locale)) {
continue;
}
return $fallback;
}
return null;
}
}