Skip to content
This repository
Browse code

Fix saving empty values for translated fields with multiple locales

  • Loading branch information...
commit 788cf5257211495bf77329a07ed915142e103c33 1 parent bbd0b9d
Mark Story authored
52  lib/Cake/Model/Behavior/TranslateBehavior.php
@@ -385,7 +385,6 @@ public function afterSave(Model $Model, $created) {
385 385
 		if (!isset($this->runtime[$Model->alias]['beforeValidate']) && !isset($this->runtime[$Model->alias]['beforeSave'])) {
386 386
 			return true;
387 387
 		}
388  
-		$locale = $this->_getLocale($Model);
389 388
 		if (isset($this->runtime[$Model->alias]['beforeValidate'])) {
390 389
 			$tempData = $this->runtime[$Model->alias]['beforeValidate'];
391 390
 		} else {
@@ -396,18 +395,10 @@ public function afterSave(Model $Model, $created) {
396 395
 		$conditions = array('model' => $Model->alias, 'foreign_key' => $Model->id);
397 396
 		$RuntimeModel = $this->translateModel($Model);
398 397
 
399  
-		$fields = array_merge($this->settings[$Model->alias], $this->runtime[$Model->alias]['fields']);
400 398
 		if ($created) {
401  
-			// set each field value to an empty string
402  
-			foreach ($fields as $key => $field) {
403  
-				if (!is_numeric($key)) {
404  
-					$field = $key;
405  
-				}
406  
-				if (!isset($tempData[$field])) {
407  
-					$tempData[$field] = '';
408  
-				}
409  
-			}
  399
+			$tempData = $this->_prepareTranslations($Model, $tempData);
410 400
 		}
  401
+		$locale = $this->_getLocale($Model);
411 402
 
412 403
 		foreach ($tempData as $field => $value) {
413 404
 			unset($conditions['content']);
@@ -422,7 +413,13 @@ public function afterSave(Model $Model, $created) {
422 413
 					$value = array($locale => $value);
423 414
 				}
424 415
 			}
425  
-			$translations = $RuntimeModel->find('list', array('conditions' => $conditions, 'fields' => array($RuntimeModel->alias . '.locale', $RuntimeModel->alias . '.id')));
  416
+			$translations = $RuntimeModel->find('list', array(
  417
+				'conditions' => $conditions,
  418
+				'fields' => array(
  419
+					$RuntimeModel->alias . '.locale',
  420
+					$RuntimeModel->alias . '.id'
  421
+				)
  422
+			));
426 423
 			foreach ($value as $_locale => $_value) {
427 424
 				$RuntimeModel->create();
428 425
 				$conditions['locale'] = $_locale;
@@ -437,6 +434,37 @@ public function afterSave(Model $Model, $created) {
437 434
 	}
438 435
 
439 436
 /**
  437
+ * Prepares the data to be saved for translated records.
  438
+ * Add blank fields, and populates data for multi-locale saves.
  439
+ *
  440
+ * @param array $data The sparse data that was provided.
  441
+ * @return array The fully populated data to save.
  442
+ */
  443
+	protected function _prepareTranslations(Model $Model, $data) {
  444
+		$fields = array_merge($this->settings[$Model->alias], $this->runtime[$Model->alias]['fields']);
  445
+		$locales = array();
  446
+		foreach ($data as $key => $value) {
  447
+			if (is_array($value)) {
  448
+				$locales = array_merge($locales, array_keys($value));
  449
+			}
  450
+		}
  451
+		$locales = array_unique($locales);
  452
+		$hasLocales = count($locales) > 0;
  453
+
  454
+		foreach ($fields as $key => $field) {
  455
+			if (!is_numeric($key)) {
  456
+				$field = $key;
  457
+			}
  458
+			if ($hasLocales && !isset($data[$field])) {
  459
+				$data[$field] = array_fill_keys($locales, '');
  460
+			} elseif (!isset($data[$field])) {
  461
+				$data[$field] = '';
  462
+			}
  463
+		}
  464
+		return $data;
  465
+	}
  466
+
  467
+/**
440 468
  * afterDelete Callback
441 469
  *
442 470
  * @param Model $Model Model the callback was run on.
39  lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php
@@ -552,7 +552,17 @@ public function testSavePartialFields() {
552 552
 			) + $data
553 553
 		);
554 554
 		$this->assertEquals($expected, $result);
555  
-		
  555
+	}
  556
+
  557
+/**
  558
+ * Test that all fields are create with partial data + multiple locales.
  559
+ *
  560
+ * @return void
  561
+ */
  562
+	public function testSavePartialFieldMultipleLocales() {
  563
+		$this->loadFixtures('Translate', 'TranslatedItem');
  564
+
  565
+		$TestModel = new TranslatedItem();
556 566
 		$TestModel->locale = 'eng';
557 567
 		$data = array(
558 568
 			'slug' => 'fifth_translated',
@@ -561,60 +571,57 @@ public function testSavePartialFields() {
561 571
 		$TestModel->create($data);
562 572
 		$TestModel->save();
563 573
 		$TestModel->unbindTranslation();
  574
+
564 575
 		$translations = array('title' => 'Title', 'content' => 'Content');
565 576
 		$TestModel->bindTranslation($translations, false);
566 577
 		$result = $TestModel->read(null, $TestModel->id);
567 578
 		$expected = array(
568 579
 			'TranslatedItem' => array(
569  
-				'id' => '5',
  580
+				'id' => '4',
570 581
 				'translated_article_id' => null,
571 582
 				'slug' => 'fifth_translated',
572 583
 				'locale' => 'eng',
573  
-				'title' => '',
  584
+				'title' => 'Title #5',
574 585
 				'content' => ''
575 586
 			),
576  
-			0 => array(
577  
-				'TranslatedItem__i18n_Title' => 'Title #5',
578  
-				'TranslatedItem__i18n_Content' => ''
579  
-			),
580 587
 			'Title' => array(
581 588
 				0 => array(
582  
-					'id' => '21',
  589
+					'id' => '19',
583 590
 					'locale' => 'eng',
584 591
 					'model' => 'TranslatedItem',
585  
-					'foreign_key' => '5',
  592
+					'foreign_key' => '4',
586 593
 					'field' => 'title',
587 594
 					'content' => 'Title #5'
588 595
 				),
589 596
 				1 => array(
590  
-					'id' => '22',
  597
+					'id' => '20',
591 598
 					'locale' => 'spa',
592 599
 					'model' => 'TranslatedItem',
593  
-					'foreign_key' => '5',
  600
+					'foreign_key' => '4',
594 601
 					'field' => 'title',
595 602
 					'content' => 'Leyenda #5'
596 603
 				)
597 604
 			),
598 605
 			'Content' => array(
599 606
 				0 => array(
600  
-					'id' => '23',
  607
+					'id' => '21',
601 608
 					'locale' => 'eng',
602 609
 					'model' => 'TranslatedItem',
603  
-					'foreign_key' => '5',
  610
+					'foreign_key' => '4',
604 611
 					'field' => 'content',
605 612
 					'content' => ''
606 613
 				),
607 614
 				1 => array(
608  
-					'id' => '24',
  615
+					'id' => '22',
609 616
 					'locale' => 'spa',
610 617
 					'model' => 'TranslatedItem',
611  
-					'foreign_key' => '5',
  618
+					'foreign_key' => '4',
612 619
 					'field' => 'content',
613 620
 					'content' => ''
614 621
 				)
615 622
 			)
616 623
 		);
617  
-		$this->assertEquals($expected, $result);		
  624
+		$this->assertEquals($expected, $result);
618 625
 	}
619 626
 
620 627
 /**

0 notes on commit 788cf52

Please sign in to comment.
Something went wrong with that request. Please try again.