Browse Source

Merge pull request #5822 from patrickconroy/3.0-translate-fieldconditions

3.0 translate fieldconditions
Mark Story 11 years ago
parent
commit
e8d88a7efa

+ 26 - 13
src/ORM/Behavior/TranslateBehavior.php

@@ -77,7 +77,8 @@ class TranslateBehavior extends Behavior
         'defaultLocale' => '',
         'model' => '',
         'onlyTranslated' => false,
-        'strategy' => 'subquery'
+        'strategy' => 'subquery',
+        'conditions' => ['model' => '']
     ];
 
     /**
@@ -109,11 +110,20 @@ class TranslateBehavior extends Behavior
         } else {
             $this->_translationTable = TableRegistry::get($translationAlias);
         }
+        
+        if ($this->config('model')) {
+            $model = $this->config('model');
+        } elseif ($this->config('conditions.model')) {
+            $model = $this->config('conditions.model');
+        } else {
+            $model = $this->_table->alias();
+        }
+        $this->config('conditions.model', $model);
 
         $this->setupFieldAssociations(
             $this->_config['fields'],
             $this->_config['translationTable'],
-            $this->_config['model'] ? $this->_config['model'] : $this->_table->alias(),
+            $this->_config['conditions'],
             $this->_config['strategy']
         );
     }
@@ -127,12 +137,12 @@ class TranslateBehavior extends Behavior
      *
      * @param array $fields list of fields to create associations for
      * @param string $table the table name to use for storing each field translation
-     * @param string $model the model field value
+     * @param array $fieldConditions conditions for finding fields
      * @param string $strategy the strategy used in the _i18n association
      *
      * @return void
      */
-    public function setupFieldAssociations($fields, $table, $model, $strategy)
+    public function setupFieldAssociations($fields, $table, $fieldConditions, $strategy)
     {
         $targetAlias = $this->_translationTable->alias();
         $alias = $this->_table->alias();
@@ -140,7 +150,13 @@ class TranslateBehavior extends Behavior
 
         foreach ($fields as $field) {
             $name = $alias . '_' . $field . '_translation';
-
+            $conditions = [
+                $name . '.model' => $fieldConditions['model'],
+                $name . '.field' => $field,
+            ];
+            foreach ($fieldConditions as $fieldName => $fieldValue) {
+                $conditions[$name . '.' . $fieldName] = $fieldValue;
+            }
             if (!TableRegistry::exists($name)) {
                 $fieldTable = TableRegistry::get($name, [
                     'className' => $table,
@@ -155,10 +171,7 @@ class TranslateBehavior extends Behavior
                 'targetTable' => $fieldTable,
                 'foreignKey' => 'foreign_key',
                 'joinType' => $filter ? 'INNER' : 'LEFT',
-                'conditions' => [
-                    $name . '.model' => $model,
-                    $name . '.field' => $field,
-                ],
+                'conditions' => $conditions,
                 'propertyName' => $field . '_translation'
             ]);
         }
@@ -167,7 +180,7 @@ class TranslateBehavior extends Behavior
             'className' => $table,
             'foreignKey' => 'foreign_key',
             'strategy' => $strategy,
-            'conditions' => ["$targetAlias.model" => $model],
+            'conditions' => ["$targetAlias.model" => $fieldConditions['model']],
             'propertyName' => '_i18n',
             'dependent' => true
         ]);
@@ -263,7 +276,7 @@ class TranslateBehavior extends Behavior
         $fields = array_keys($values);
         $primaryKey = (array)$this->_table->primaryKey();
         $key = $entity->get(current($primaryKey));
-        $model = $this->_table->alias();
+        $model = $this->config('conditions.model');
 
         $preexistent = $this->_translationTable->find()
             ->select(['id', 'field'])
@@ -469,14 +482,14 @@ class TranslateBehavior extends Behavior
         }
 
         $results = $this->_findExistingTranslations($find);
-        $alias = $this->_table->alias();
+        $model = $this->config('conditions.model');
 
         foreach ($find as $i => $translation) {
             if (!empty($results[$i])) {
                 $contents[$i]->set('id', $results[$i], ['setter' => false]);
                 $contents[$i]->isNew(false);
             } else {
-                $translation['model'] = $alias;
+                $translation['model'] = $model;
                 $contents[$i]->set($translation, ['setter' => false, 'guard' => false]);
                 $contents[$i]->isNew(true);
             }

+ 3 - 0
tests/Fixture/TranslatesFixture.php

@@ -53,6 +53,9 @@ class TranslatesFixture extends TestFixture
     public $records = [
         ['locale' => 'eng', 'model' => 'Articles', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Title #1'],
         ['locale' => 'eng', 'model' => 'Articles', 'foreign_key' => 1, 'field' => 'body', 'content' => 'Content #1'],
+        ['locale' => 'eng', 'model' => 'Articles', 'foreign_key' => 1, 'field' => 'description', 'content' => 'Description #1'],
+        ['locale' => 'spa', 'model' => 'Articles', 'foreign_key' => 1, 'field' => 'body', 'content' => 'Contenido #1'],
+        ['locale' => 'spa', 'model' => 'Articles', 'foreign_key' => 1, 'field' => 'description', 'content' => ''],
         ['locale' => 'deu', 'model' => 'Articles', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titel #1'],
         ['locale' => 'deu', 'model' => 'Articles', 'foreign_key' => 1, 'field' => 'body', 'content' => 'Inhalt #1'],
         ['locale' => 'cze', 'model' => 'Articles', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titulek #1'],

+ 21 - 3
tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php

@@ -209,7 +209,7 @@ class TranslateBehaviorTest extends TestCase
             [
                 'id' => 1,
                 'title' => 'First Article',
-                'body' => 'First Article Body',
+                'body' => 'Contenido #1',
                 'comments' => [
                     ['article_id' => 1, 'comment' => 'First Comment for First Article', '_locale' => 'spa'],
                     ['article_id' => 1, 'comment' => 'Second Comment for First Article', '_locale' => 'spa'],
@@ -328,9 +328,10 @@ class TranslateBehaviorTest extends TestCase
         $results = $table->find('translations');
         $expected = [
             [
-                'eng' => ['title' => 'Title #1', 'body' => 'Content #1', 'locale' => 'eng'],
+                'eng' => ['title' => 'Title #1', 'body' => 'Content #1', 'description' => 'Description #1', 'locale' => 'eng'],
                 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
-                'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze']
+                'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze'],
+                'spa' => ['body' => 'Contenido #1', 'locale' => 'spa', 'description' => '']
             ],
             [
                 'eng' => ['title' => 'Title #2', 'body' => 'Content #2', 'locale' => 'eng'],
@@ -968,4 +969,21 @@ class TranslateBehaviorTest extends TestCase
         $results = $table->find('translations')->all();
         $this->assertCount(1, $results);
     }
+
+    /**
+     * Tests that conditions set when defining the behavior are applied correctly
+     *
+     * @return void
+     */
+    public function testConditions()
+    {
+        $table = TableRegistry::get('Articles');
+        $table->addBehavior('Translate', [
+            'fields' => ['title', 'body', 'description'],
+            'conditions' => ['content <>' => '']
+        ]);
+        $table->locale('spa');
+        $result = $table->find()->first();
+        $this->assertNull($result->description);
+    }
 }