5
5
use Cake \Database \Expression \FieldInterface ;
6
6
use Cake \Datasource \EntityInterface ;
7
7
use Cake \Event \Event ;
8
+ use Cake \I18n \I18n ;
8
9
use Cake \ORM \Behavior \TranslateBehavior ;
9
10
use Cake \ORM \Query ;
10
11
use Cake \ORM \Table ;
@@ -23,8 +24,13 @@ class ShadowTranslateBehavior extends TranslateBehavior
23
24
*/
24
25
public function __construct (Table $ table , array $ config = [])
25
26
{
26
- $ tableAlias = $ table ->alias ();
27
- list ($ plugin ) = pluginSplit ($ table ->registryAlias (), true );
27
+ $ this ->_defaultConfig ['implementedMethods ' ] += [
28
+ 'setLocale ' => 'setLocale ' ,
29
+ 'getLocale ' => 'getLocale ' ,
30
+ ];
31
+
32
+ $ tableAlias = $ table ->getAlias ();
33
+ list ($ plugin ) = pluginSplit ($ table ->getRegistryAlias (), true );
28
34
29
35
if (isset ($ config ['referenceName ' ])) {
30
36
$ tableReferenceName = $ config ['referenceName ' ];
@@ -41,6 +47,46 @@ public function __construct(Table $table, array $config = [])
41
47
parent ::__construct ($ table , $ config );
42
48
}
43
49
50
+ /**
51
+ * Sets the locale that should be used for all future find and save operations on
52
+ * the table where this behavior is attached to.
53
+ *
54
+ * When fetching records, the behavior will include the content for the locale set
55
+ * via this method, and likewise when saving data, it will save the data in that
56
+ * locale.
57
+ *
58
+ * Note that in case an entity has a `_locale` property set, that locale will win
59
+ * over the locale set via this method (and over the globally configured one for
60
+ * that matter)!
61
+ *
62
+ * @param string|null $locale The locale to use for fetching and saving records. Pass `null`
63
+ * in order to unset the current locale, and to make the behavior fall back to using the
64
+ * globally configured locale.
65
+ * @return $this
66
+ * @see \ShadowTranslate\ORM\Behavior\ShadowTranslateBehavior::getLocale()
67
+ */
68
+ public function setLocale ($ locale )
69
+ {
70
+ $ this ->_locale = $ locale ;
71
+
72
+ return $ this ;
73
+ }
74
+
75
+ /**
76
+ * Returns the current locale.
77
+ *
78
+ * If no locale has been explicitly set via `setLocale()`, this method will return
79
+ * the currently configured global locale.
80
+ *
81
+ * @return string
82
+ * @see \Cake\I18n\I18n::getLocale()
83
+ * @see \ShadowTranslate\ORM\Behavior\ShadowTranslateBehavior::setLocale()
84
+ */
85
+ public function getLocale ()
86
+ {
87
+ return $ this ->_locale ?: I18n::getLocale ();
88
+ }
89
+
44
90
/**
45
91
* Create a hasMany association for all records
46
92
*
@@ -56,14 +102,14 @@ public function __construct(Table $table, array $config = [])
56
102
*/
57
103
public function setupFieldAssociations ($ fields , $ table , $ fieldConditions , $ strategy )
58
104
{
59
- $ config = $ this ->config ();
105
+ $ config = $ this ->getConfig ();
60
106
61
107
$ this ->_table ->hasMany ($ config ['translationTable ' ], [
62
108
'className ' => $ config ['translationTable ' ],
63
109
'foreignKey ' => 'id ' ,
64
110
'strategy ' => $ strategy ,
65
111
'propertyName ' => '_i18n ' ,
66
- 'dependent ' => true
112
+ 'dependent ' => true ,
67
113
]);
68
114
}
69
115
@@ -79,13 +125,13 @@ public function setupFieldAssociations($fields, $table, $fieldConditions, $strat
79
125
*/
80
126
public function beforeFind (Event $ event , Query $ query , $ options )
81
127
{
82
- $ locale = $ this ->locale ();
128
+ $ locale = $ this ->getLocale ();
83
129
84
- if ($ locale === $ this ->config ('defaultLocale ' )) {
130
+ if ($ locale === $ this ->getConfig ('defaultLocale ' )) {
85
131
return ;
86
132
}
87
133
88
- $ config = $ this ->config ();
134
+ $ config = $ this ->getConfig ();
89
135
90
136
if (isset ($ options ['filterByCurrentLocale ' ])) {
91
137
$ joinType = $ options ['filterByCurrentLocale ' ] ? 'INNER ' : 'LEFT ' ;
@@ -133,7 +179,7 @@ public function beforeFind(Event $event, Query $query, $options)
133
179
*/
134
180
protected function _addFieldsToQuery (Query $ query , array $ config )
135
181
{
136
- if ($ query ->autoFields ()) {
182
+ if ($ query ->isAutoFieldsEnabled ()) {
137
183
return true ;
138
184
}
139
185
@@ -264,8 +310,8 @@ protected function _traverseClause(Query $query, $name = '', $config = [])
264
310
*/
265
311
public function beforeSave (Event $ event , EntityInterface $ entity , ArrayObject $ options )
266
312
{
267
- $ locale = $ entity ->get ('_locale ' ) ?: $ this ->locale ();
268
- $ newOptions = [$ this ->_translationTable ->alias () => ['validate ' => false ]];
313
+ $ locale = $ entity ->get ('_locale ' ) ?: $ this ->getLocale ();
314
+ $ newOptions = [$ this ->_translationTable ->getAlias () => ['validate ' => false ]];
269
315
$ options ['associated ' ] = $ newOptions + $ options ['associated ' ];
270
316
271
317
// Check early if empty translations are present in the entity.
@@ -281,7 +327,7 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o
281
327
282
328
// No additional translation records need to be saved,
283
329
// as the entity is in the default locale.
284
- if ($ noBundled && $ locale === $ this ->config ('defaultLocale ' )) {
330
+ if ($ noBundled && $ locale === $ this ->getConfig ('defaultLocale ' )) {
285
331
return ;
286
332
}
287
333
@@ -296,15 +342,15 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o
296
342
return ;
297
343
}
298
344
299
- $ primaryKey = (array )$ this ->_table ->primaryKey ();
345
+ $ primaryKey = (array )$ this ->_table ->getPrimaryKey ();
300
346
$ id = $ entity ->get (current ($ primaryKey ));
301
347
302
348
// When we have no key and bundled translations, we
303
349
// need to mark the entity dirty so the root
304
350
// entity persists.
305
351
if ($ noFields && $ bundled && !$ id ) {
306
352
foreach ($ this ->_translationFields () as $ field ) {
307
- $ entity ->dirty ($ field , true );
353
+ $ entity ->setDirty ($ field , true );
308
354
}
309
355
310
356
return ;
@@ -319,7 +365,7 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o
319
365
$ translation = $ this ->_translationTable ()->find ()
320
366
->select (array_merge (['id ' , 'locale ' ], $ fields ))
321
367
->where ($ where )
322
- ->bufferResults (false )
368
+ ->enableBufferedResults (false )
323
369
->first ();
324
370
325
371
if ($ translation ) {
@@ -331,17 +377,17 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o
331
377
$ where + $ values ,
332
378
[
333
379
'useSetters ' => false ,
334
- 'markNew ' => true
380
+ 'markNew ' => true ,
335
381
]
336
382
);
337
383
}
338
384
339
385
$ entity ->set ('_i18n ' , array_merge ($ bundled , [$ translation ]));
340
386
$ entity ->set ('_locale ' , $ locale , ['setter ' => false ]);
341
- $ entity ->dirty ('_locale ' , false );
387
+ $ entity ->setDirty ('_locale ' , false );
342
388
343
389
foreach ($ fields as $ field ) {
344
- $ entity ->dirty ($ field , false );
390
+ $ entity ->setDirty ($ field , false );
345
391
}
346
392
}
347
393
@@ -367,13 +413,13 @@ public function buildMarshalMap($marshaller, $map, $options)
367
413
*/
368
414
public function translationField ($ field )
369
415
{
370
- if ($ this ->locale () === $ this ->getConfig ('defaultLocale ' )) {
416
+ if ($ this ->getLocale () === $ this ->getConfig ('defaultLocale ' )) {
371
417
return $ this ->_table ->aliasField ($ field );
372
418
}
373
419
374
420
$ translatedFields = $ this ->_translationFields ();
375
421
if (in_array ($ field , $ translatedFields )) {
376
- return $ this ->config ('hasOneAlias ' ) . '. ' . $ field ;
422
+ return $ this ->getConfig ('hasOneAlias ' ) . '. ' . $ field ;
377
423
}
378
424
379
425
return $ this ->_table ->aliasField ($ field );
@@ -399,7 +445,7 @@ protected function _rowMapper($results, $locale)
399
445
$ hydrated = !is_array ($ row );
400
446
401
447
if (empty ($ row ['translation ' ])) {
402
- $ row ['_locale ' ] = $ this ->locale ();
448
+ $ row ['_locale ' ] = $ this ->getLocale ();
403
449
unset($ row ['translation ' ]);
404
450
405
451
if ($ hydrated ) {
@@ -459,7 +505,7 @@ public function groupTranslations($results)
459
505
460
506
$ row ['_translations ' ] = $ result ;
461
507
unset($ row ['_i18n ' ]);
462
- if (is_object ( $ row) ) {
508
+ if ($ row instanceof EntityInterface ) {
463
509
$ row ->clean ();
464
510
}
465
511
@@ -479,11 +525,11 @@ protected function _bundleTranslatedFields($entity)
479
525
{
480
526
$ translations = (array )$ entity ->get ('_translations ' );
481
527
482
- if (empty ($ translations ) && !$ entity ->dirty ('_translations ' )) {
528
+ if (empty ($ translations ) && !$ entity ->isDirty ('_translations ' )) {
483
529
return ;
484
530
}
485
531
486
- $ primaryKey = (array )$ this ->_table ->primaryKey ();
532
+ $ primaryKey = (array )$ this ->_table ->getPrimaryKey ();
487
533
$ key = $ entity ->get (current ($ primaryKey ));
488
534
489
535
foreach ($ translations as $ lang => $ translation ) {
@@ -512,7 +558,7 @@ protected function _bundleTranslatedFields($entity)
512
558
protected function _translationTable ($ config = [])
513
559
{
514
560
if (!$ config ) {
515
- $ config = $ this ->config ();
561
+ $ config = $ this ->getConfig ();
516
562
}
517
563
518
564
return TableRegistry::get ($ config ['translationTable ' ]);
@@ -525,16 +571,16 @@ protected function _translationTable($config = [])
525
571
*/
526
572
protected function _mainFields ()
527
573
{
528
- $ fields = $ this ->config ('mainTableFields ' );
574
+ $ fields = $ this ->getConfig ('mainTableFields ' );
529
575
530
576
if ($ fields ) {
531
577
return $ fields ;
532
578
}
533
579
534
580
$ table = $ this ->_table ;
535
- $ fields = $ table ->schema ()->columns ();
581
+ $ fields = $ table ->getSchema ()->columns ();
536
582
537
- $ this ->config ('mainTableFields ' , $ fields );
583
+ $ this ->setConfig ('mainTableFields ' , $ fields );
538
584
539
585
return $ fields ;
540
586
}
@@ -546,17 +592,17 @@ protected function _mainFields()
546
592
*/
547
593
protected function _translationFields ()
548
594
{
549
- $ fields = $ this ->config ('fields ' );
595
+ $ fields = $ this ->getConfig ('fields ' );
550
596
551
597
if ($ fields ) {
552
598
return $ fields ;
553
599
}
554
600
555
601
$ table = $ this ->_translationTable ();
556
- $ fields = $ table ->schema ()->columns ();
602
+ $ fields = $ table ->getSchema ()->columns ();
557
603
$ fields = array_values (array_diff ($ fields , ['id ' , 'locale ' ]));
558
604
559
- $ this ->config ('fields ' , $ fields );
605
+ $ this ->setConfig ('fields ' , $ fields );
560
606
561
607
return $ fields ;
562
608
}
0 commit comments