Skip to content

Commit

Permalink
Implemented filterUntranslated for TranslateBehavior, fixes #4652
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 25, 2014
1 parent 58b9851 commit e6c1731
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/Model/Behavior/TranslateBehavior.php
Expand Up @@ -66,7 +66,8 @@ class TranslateBehavior extends Behavior {
'fields' => [],
'translationTable' => 'i18n',
'defaultLocale' => '',
'model' => ''
'model' => '',
'filterUntranslated' => false
];

/**
Expand Down Expand Up @@ -107,6 +108,7 @@ public function initialize(array $config) {
* @return void
*/
public function setupFieldAssociations($fields, $table, $model) {
$filter = $this->_config['filterUntranslated'];
foreach ($fields as $field) {
$name = $this->_table->alias() . '_' . $field . '_translation';
$target = TableRegistry::get($name);
Expand All @@ -115,7 +117,7 @@ public function setupFieldAssociations($fields, $table, $model) {
$this->_table->hasOne($name, [
'targetTable' => $target,
'foreignKey' => 'foreign_key',
'joinType' => 'LEFT',
'joinType' => $filter? 'INNER' : 'LEFT',
'conditions' => [
$name . '.model' => $model,
$name . '.field' => $field,
Expand All @@ -140,9 +142,10 @@ public function setupFieldAssociations($fields, $table, $model) {
*
* @param \Cake\Event\Event $event The beforeFind event that was fired.
* @param \Cake\ORM\Query $query Query
* @param \ArrayObject $options The options for the query
* @return void
*/
public function beforeFind(Event $event, Query $query) {
public function beforeFind(Event $event, Query $query, $options) {
$locale = $this->locale();

if ($locale === $this->config('defaultLocale')) {
Expand All @@ -169,14 +172,21 @@ public function beforeFind(Event $event, Query $query) {
$fields = $this->_config['fields'];
$alias = $this->_table->alias();
$select = $query->clause('select');
$changeFilter = isset($options['filterUntranslated']) &&
$options['filterUntranslated'] !== $this->_config['filterUntranslated'];

foreach ($fields as $field) {
$contain[$alias . '_' . $field . '_translation'] = $conditions(
$contain[$alias . '_' . $field . '_translation']['queryBuilder'] = $conditions(
$field,
$locale,
$query,
$select
);

if ($changeFilter) {
$filter = $options['filterUntranslated'] ? 'INNER' : 'LEFT';
$contain[$alias . '_' . $field . '_translation']['joinType'] = $filter;
}
}

$query->contain($contain);
Expand Down
52 changes: 52 additions & 0 deletions tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
Expand Up @@ -845,4 +845,56 @@ public function testChangingModelFieldValue() {

$this->assertTrue($found, '`model` field condition on a Translation association was not found');
}

/**
* Tests that filterUntranslated will remove records from the result set
* if they are not fully translated
*
* @return void
*/
public function testFilterUntranslated() {
$table = TableRegistry::get('Articles');
$table->addBehavior('Translate', [
'fields' => ['title', 'body'],
'filterUntranslated' => true
]);
$table->locale('eng');
$results = $table->find()->where(['Articles.id' => 1])->all();
$this->assertCount(1, $results);

$table->locale('fr');
$results = $table->find()->where(['Articles.id' => 1])->all();
$this->assertCount(0, $results);
}

/**
* Tests that records not translated in the current locale will not be
* present in the results for the translations finder, and also proves
* that this can be overridden.
*
* @return void
*/
public function testFilterUntranslatedWithFinder() {
$table = TableRegistry::get('Comments');
$table->addBehavior('Translate', [
'fields' => ['comment'],
'filterUntranslated' => true
]);
$table->locale('eng');
$results = $table->find('translations')->all();
$this->assertCount(4, $results);

$table->locale('spa');
$results = $table->find('translations')->all();
$this->assertCount(1, $results);

$table->locale('spa');
$results = $table->find('translations', ['filterUntranslated' => false])->all();
$this->assertCount(6, $results);

$table->locale('spa');
$results = $table->find('translations')->all();
$this->assertCount(1, $results);
}

}

0 comments on commit e6c1731

Please sign in to comment.