/
TranslateBehavior.php
320 lines (286 loc) · 9.1 KB
/
TranslateBehavior.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
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Model\Behavior;
use ArrayObject;
use Cake\Collection\Collection;
use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
/**
* This behavior provides a way to translate dynamic data by keeping translations
* in a separate table linked to the original record from another one. Translated
* fields can be configured to override those in the main table when fetched or
* put aside into another property for the same entity.
*
* If you wish to override fields, you need to call the `locale` method in this
* behavior for setting the language you want to fetch from the translations table.
*
* If you want to bring all or certain languages for each of the fetched records,
* you can use the custom `translations` finders that is exposed to the table.
*/
class TranslateBehavior extends Behavior {
/**
* Table instance
*
* @var \Cake\ORM\Table
*/
protected $_table;
/**
* The locale name that will be used to override fields in the bound table
* from the translations table
*
* @var string
*/
protected $_locale;
/**
* Default config
*
* These are merged with user-provided configuration when the behavior is used.
*
* @var array
*/
protected static $_defaultConfig = [
'implementedFinders' => ['translations' => 'findTranslations'],
'implementedMethods' => ['locale' => 'locale'],
'fields' => []
];
/**
* Constructor
*
* @param Table $table The table this behavior is attached to.
* @param array $config The config for this behavior.
*/
public function __construct(Table $table, array $config = []) {
parent::__construct($table, $config);
$this->_table = $table;
$fields = $this->config()['fields'];
$this->setupFieldAssociations($fields);
}
/**
* Creates the associations between the bound table and every field passed to
* this method.
*
* Additionally it creates a `I18n` HasMany association that will be
* used for fetching all translations for each record in the bound table
*
* @param array $fields list of fields to create associations for
* @return void
*/
public function setupFieldAssociations($fields) {
$alias = $this->_table->alias();
foreach ($fields as $field) {
$name = $this->_table->alias() . '_' . $field . '_translation';
$target = TableRegistry::get($name);
$target->table('i18n');
$this->_table->hasOne($name, [
'targetTable' => $target,
'foreignKey' => 'foreign_key',
'joinType' => 'LEFT',
'conditions' => [
$name . '.model' => $alias,
$name . '.field' => $field,
],
'propertyName' => $field . '_translation'
]);
}
$this->_table->hasMany('I18n', [
'foreignKey' => 'foreign_key',
'strategy' => 'subquery',
'conditions' => ['I18n.model' => $alias],
'propertyName' => '_i18n'
]);
}
/**
* Callback method that listens to the `beforeFind` event in the bound
* table. It modifies the passed query by eager loading the translated fields
* and adding a formatter to copy the values into the main table records.
*
* @param \Cake\Event\Event $event
* @param \Cake\ORM\Query $query
* @return void
*/
public function beforeFind(Event $event, $query) {
$locale = $this->locale();
if (empty($locale)) {
return;
}
$conditions = function($q) use ($locale) {
return $q
->select(['id', 'content'])
->where([$q->repository()->alias() . '.locale' => $locale]);
};
$contain = [];
$fields = $this->config()['fields'];
$alias = $this->_table->alias();
foreach ($fields as $field) {
$contain[$alias . '_' . $field . '_translation'] = $conditions;
}
$query->contain($contain);
$query->formatResults(function($results) use ($locale) {
return $this->_rowMapper($results, $locale);
}, $query::PREPEND);
}
/**
* Modifies the entity before it is saved so that translated fields are persisted
* in the database too.
*
* @param \Cake\Event\Event the beforeSave event that was fired
* @param \Cake\ORM\Entity the entity that is going to be saved
* @param \ArrayObject $options the options passed to the save method
* @return void
*/
public function beforeSave(Event $event, Entity $entity, ArrayObject $options) {
$locale = $entity->get('_locale') ?: $this->locale();
if (!$locale) {
return;
}
$newOptions = ['I18n' => ['validate' => false]];
$options['associated'] = $newOptions + $options['associated'];
$values = $entity->extract($this->config()['fields'], true);
$fields = array_keys($values);
$primaryKey = (array)$this->_table->primaryKey();
$key = $entity->get(current($primaryKey));
$preexistent = TableRegistry::get('I18n')->find()
->select(['id', 'field'])
->where(['field IN' => $fields, 'locale' => $locale, 'foreign_key' => $key])
->bufferResults(false)
->indexBy('field');
$modified = [];
foreach ($preexistent as $field => $translation) {
$translation->set('content', $values[$field]);
$modified[$field] = $translation;
}
$new = array_diff_key($values, $modified);
$model = $this->_table->alias();
foreach ($new as $field => $content) {
$new[$field] = new Entity(compact('locale', 'field', 'content', 'model'), [
'useSetters' => false,
'markNew' => true
]);
}
$entity->set('_i18n', array_values($modified + $new));
$entity->set('_locale', $locale, ['setter' => false]);
$entity->dirty('_locale', false);
foreach ($fields as $field) {
$entity->dirty($field, false);
}
}
/**
* Sets all future finds for the bound table to also fetch translated fields for
* the passed locale. If no value is passed, it returns the currently configured
* locale
*
* @param string $locale The locale to use for fetching translated records
* @return string
*/
public function locale($locale = null) {
if ($locale === null) {
return $this->_locale;
}
return $this->_locale = (string)$locale;
}
/**
* Custom finder method used to retrieve all translations for the found records.
* Fetched translations can be filtered by locale by passing the `locales` key
* in the options array.
*
* Translated values will be found for each entity under the property `_translations`,
* containing an array indexed by locale name.
*
* ### Example:
*
* {{{
* $article = $articles->find('translations', ['locales' => ['eng', 'deu'])->first();
* $englishTranslatedFields = $article->get('_translations')['eng'];
* }}}
*
* If the `locales` array is not passed, it will bring all translations found
* for each record.
*
* @param \Cake\ORM\Query $query the original query to modify
* @param array $options
* @return \Cake\ORM\Query
*/
public function findTranslations($query, $options) {
$locales = isset($options['locales']) ? $options['locales'] : [];
return $query
->contain(['I18n' => function($q) use ($locales) {
if ($locales) {
$q->where(['I18n.locale IN' => $locales]);
}
return $q;
}])
->formatResults(function($results) {
return $this->_groupTranslations($results);
}, $query::PREPEND);
}
/**
* Modifies the results from a table find in order to merge the translated fields
* into each entity for a given locale.
*
* @param \Cake\ORM\ResultSetDecorator $results
* @param string $locale
* @return \Cake\Collection\Collection
*/
protected function _rowMapper($results, $locale) {
return $results->map(function($row) use ($locale) {
$options = ['setter' => false, 'guard' => false];
foreach ($this->config()['fields'] as $field) {
$name = $field . '_translation';
$translation = $row->get($name);
if (!$translation) {
continue;
}
$content = $translation->get('content');
if ($content !== null) {
$row->set($field, $content, $options);
}
unset($row[$name]);
}
$row->set('_locale', $locale, $options);
$row->clean();
return $row;
});
}
/**
* Modifies the results from a table find in order to merge full translation records
* into each entity under the `_translations` key
*
* @param \Cake\ORM\ResultSetDecorator $results
* @return \Cake\Collection\Collection
*/
protected function _groupTranslations($results) {
return $results->map(function($row) {
$translations = (array)$row->get('_i18n');
$grouped = new Collection($translations);
$result = [];
foreach ($grouped->combine('field', 'content', 'locale') as $locale => $keys) {
$translation = new Entity($keys + ['locale' => $locale], [
'markNew' => false,
'useSetters' => false,
'markClean' => true
]);
$result[$locale] = $translation;
}
$options = ['setter' => false, 'guard' => false];
$row->set('_translations', $result, $options);
unset($row['_i18n']);
$row->clean();
return $row;
});
}
}