Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11
 * @link          http://cakephp.org CakePHP(tm) Project
12
 * @package       Cake.Model.Behavior
13
 * @since         CakePHP(tm) v 1.2.0.4525
14
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
15
 */
16
 
17
App::uses('ModelBehavior', 'Model');
18
App::uses('I18n', 'I18n');
19
App::uses('I18nModel', 'Model');
20
 
21
/**
22
 * Translate behavior
23
 *
24
 * @package       Cake.Model.Behavior
25
 * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/translate.html
26
 */
27
class TranslateBehavior extends ModelBehavior {
28
 
29
/**
30
 * Used for runtime configuration of model
31
 *
32
 * @var array
33
 */
34
	public $runtime = array();
35
 
36
/**
37
 * Stores the joinTable object for generating joins.
38
 *
39
 * @var object
40
 */
41
	protected $_joinTable;
42
 
43
/**
44
 * Stores the runtime model for generating joins.
45
 *
46
 * @var Model
47
 */
48
	protected $_runtimeModel;
49
 
50
/**
51
 * Callback
52
 *
53
 * $config for TranslateBehavior should be
54
 * array('fields' => array('field_one',
55
 * 'field_two' => 'FieldAssoc', 'field_three'))
56
 *
57
 * With above example only one permanent hasMany will be joined (for field_two
58
 * as FieldAssoc)
59
 *
60
 * $config could be empty - and translations configured dynamically by
61
 * bindTranslation() method
62
 *
63
 * @param Model $Model Model the behavior is being attached to.
64
 * @param array $config Array of configuration information.
65
 * @return mixed
66
 */
67
	public function setup(Model $Model, $config = array()) {
68
		$db = ConnectionManager::getDataSource($Model->useDbConfig);
69
		if (!$db->connected) {
70
			trigger_error(
71
				__d('cake_dev', 'Datasource %s for TranslateBehavior of model %s is not connected', $Model->useDbConfig, $Model->alias),
72
				E_USER_ERROR
73
			);
74
			return false;
75
		}
76
 
77
		$this->settings[$Model->alias] = array();
78
		$this->runtime[$Model->alias] = array('fields' => array());
79
		$this->translateModel($Model);
80
		return $this->bindTranslation($Model, $config, false);
81
	}
82
 
83
/**
84
 * Cleanup Callback unbinds bound translations and deletes setting information.
85
 *
86
 * @param Model $Model Model being detached.
87
 * @return void
88
 */
89
	public function cleanup(Model $Model) {
90
		$this->unbindTranslation($Model);
91
		unset($this->settings[$Model->alias]);
92
		unset($this->runtime[$Model->alias]);
93
	}
94
 
95
/**
96
 * beforeFind Callback
97
 *
98
 * @param Model $Model Model find is being run on.
99
 * @param array $query Array of Query parameters.
100
 * @return array Modified query
101
 */
102
	public function beforeFind(Model $Model, $query) {
103
		$this->runtime[$Model->alias]['virtualFields'] = $Model->virtualFields;
104
		$locale = $this->_getLocale($Model);
105
		if (empty($locale)) {
106
			return $query;
107
		}
108
		$db = $Model->getDataSource();
109
		$RuntimeModel = $this->translateModel($Model);
110
 
111
		if (!empty($RuntimeModel->tablePrefix)) {
112
			$tablePrefix = $RuntimeModel->tablePrefix;
113
		} else {
114
			$tablePrefix = $db->config['prefix'];
115
		}
116
		$joinTable = new StdClass();
117
		$joinTable->tablePrefix = $tablePrefix;
118
		$joinTable->table = $RuntimeModel->table;
119
		$joinTable->schemaName = $RuntimeModel->getDataSource()->getSchemaName();
120
 
121
		$this->_joinTable = $joinTable;
122
		$this->_runtimeModel = $RuntimeModel;
123
 
124
		if (is_string($query['fields']) && "COUNT(*) AS {$db->name('count')}" == $query['fields']) {
125
			$query['fields'] = "COUNT(DISTINCT({$db->name($Model->escapeField())})) {$db->alias}count";
126
			$query['joins'][] = array(
127
				'type' => 'INNER',
128
				'alias' => $RuntimeModel->alias,
129
				'table' => $joinTable,
130
				'conditions' => array(
131
					$Model->escapeField() => $db->identifier($RuntimeModel->escapeField('foreign_key')),
132
					$RuntimeModel->escapeField('model') => $Model->name,
133
					$RuntimeModel->escapeField('locale') => $locale
134
				)
135
			);
136
			$conditionFields = $this->_checkConditions($Model, $query);
137
			foreach ($conditionFields as $field) {
138
				$query = $this->_addJoin($Model, $query, $field, $field, $locale);
139
			}
140
			unset($this->_joinTable, $this->_runtimeModel);
141
			return $query;
142
		}
143
 
144
		$fields = array_merge(
145
			$this->settings[$Model->alias],
146
			$this->runtime[$Model->alias]['fields']
147
		);
148
		$addFields = array();
149
		if (empty($query['fields'])) {
150
			$addFields = $fields;
151
		} elseif (is_array($query['fields'])) {
152
			$isAllFields = (
153
				in_array($Model->alias . '.' . '*', $query['fields']) ||
154
				in_array($Model->escapeField('*'), $query['fields'])
155
			);
156
			foreach ($fields as $key => $value) {
157
				$field = (is_numeric($key)) ? $value : $key;
158
				if (
159
					$isAllFields ||
160
					in_array($Model->alias . '.' . $field, $query['fields']) ||
161
					in_array($field, $query['fields'])
162
				) {
163
					$addFields[] = $field;
164
				}
165
			}
166
		}
167
 
168
		$this->runtime[$Model->alias]['virtualFields'] = $Model->virtualFields;
169
		if ($addFields) {
170
			foreach ($addFields as $_f => $field) {
171
				$aliasField = is_numeric($_f) ? $field : $_f;
172
 
173
				foreach (array($aliasField, $Model->alias . '.' . $aliasField) as $_field) {
174
					$key = array_search($_field, (array)$query['fields']);
175
 
176
					if ($key !== false) {
177
						unset($query['fields'][$key]);
178
					}
179
				}
180
				$query = $this->_addJoin($Model, $query, $field, $aliasField, $locale);
181
			}
182
		}
183
		$this->runtime[$Model->alias]['beforeFind'] = $addFields;
184
		unset($this->_joinTable, $this->_runtimeModel);
185
		return $query;
186
	}
187
 
188
/**
189
 * Check a query's conditions for translated fields.
190
 * Return an array of translated fields found in the conditions.
191
 *
192
 * @param Model $Model The model being read.
193
 * @param array $query The query array.
194
 * @return array The list of translated fields that are in the conditions.
195
 */
196
	protected function _checkConditions(Model $Model, $query) {
197
		$conditionFields = array();
198
		if (empty($query['conditions']) || (!empty($query['conditions']) && !is_array($query['conditions']))) {
199
			return $conditionFields;
200
		}
201
		foreach ($query['conditions'] as $col => $val) {
202
			foreach ($this->settings[$Model->alias] as $field => $assoc) {
203
				if (is_numeric($field)) {
204
					$field = $assoc;
205
				}
206
				if (strpos($col, $field) !== false) {
207
					$conditionFields[] = $field;
208
				}
209
			}
210
		}
211
		return $conditionFields;
212
	}
213
 
214
/**
215
 * Appends a join for translated fields.
216
 *
217
 * @param Model $Model The model being worked on.
218
 * @param array $query The query array to append a join to.
219
 * @param string $field The field name being joined.
220
 * @param string $aliasField The aliased field name being joined.
221
 * @param string|array $locale The locale(s) having joins added.
222
 * @return array The modified query
223
 */
224
	protected function _addJoin(Model $Model, $query, $field, $aliasField, $locale) {
225
		$db = ConnectionManager::getDataSource($Model->useDbConfig);
226
		$RuntimeModel = $this->_runtimeModel;
227
		$joinTable = $this->_joinTable;
228
		$aliasVirtual = "i18n_{$field}";
229
		$alias = "I18n__{$field}";
230
		if (is_array($locale)) {
231
			foreach ($locale as $_locale) {
232
				$aliasVirtualLocale = "{$aliasVirtual}_{$_locale}";
233
				$aliasLocale = "{$alias}__{$_locale}";
234
				$Model->virtualFields[$aliasVirtualLocale] = "{$aliasLocale}.content";
235
				if (!empty($query['fields']) && is_array($query['fields'])) {
236
					$query['fields'][] = $aliasVirtualLocale;
237
				}
238
				$query['joins'][] = array(
239
					'type' => 'LEFT',
240
					'alias' => $aliasLocale,
241
					'table' => $joinTable,
242
					'conditions' => array(
243
						$Model->escapeField() => $db->identifier("{$aliasLocale}.foreign_key"),
244
						"{$aliasLocale}.model" => $Model->name,
245
						"{$aliasLocale}.{$RuntimeModel->displayField}" => $aliasField,
246
						"{$aliasLocale}.locale" => $_locale
247
					)
248
				);
249
			}
250
		} else {
251
			$Model->virtualFields[$aliasVirtual] = "{$alias}.content";
252
			if (!empty($query['fields']) && is_array($query['fields'])) {
253
				$query['fields'][] = $aliasVirtual;
254
			}
255
			$query['joins'][] = array(
256
				'type' => 'INNER',
257
				'alias' => $alias,
258
				'table' => $joinTable,
259
				'conditions' => array(
260
					"{$Model->alias}.{$Model->primaryKey}" => $db->identifier("{$alias}.foreign_key"),
261
					"{$alias}.model" => $Model->name,
262
					"{$alias}.{$RuntimeModel->displayField}" => $aliasField,
263
					"{$alias}.locale" => $locale
264
				)
265
			);
266
		}
267
		return $query;
268
	}
269
 
270
/**
271
 * afterFind Callback
272
 *
273
 * @param Model $Model Model find was run on
274
 * @param array $results Array of model results.
275
 * @param boolean $primary Did the find originate on $model.
276
 * @return array Modified results
277
 */
278
	public function afterFind(Model $Model, $results, $primary = false) {
279
		$Model->virtualFields = $this->runtime[$Model->alias]['virtualFields'];
280
		$this->runtime[$Model->alias]['virtualFields'] = $this->runtime[$Model->alias]['fields'] = array();
281
		$locale = $this->_getLocale($Model);
282
 
283
		if (empty($locale) || empty($results) || empty($this->runtime[$Model->alias]['beforeFind'])) {
284
			return $results;
285
		}
286
		$beforeFind = $this->runtime[$Model->alias]['beforeFind'];
287
 
288
		foreach ($results as $key => &$row) {
289
			$results[$key][$Model->alias]['locale'] = (is_array($locale)) ? current($locale) : $locale;
290
			foreach ($beforeFind as $_f => $field) {
291
				$aliasField = is_numeric($_f) ? $field : $_f;
292
				$aliasVirtual = "i18n_{$field}";
293
				if (is_array($locale)) {
294
					foreach ($locale as $_locale) {
295
						$aliasVirtualLocale = "{$aliasVirtual}_{$_locale}";
296
						if (!isset($row[$Model->alias][$aliasField]) && !empty($row[$Model->alias][$aliasVirtualLocale])) {
297
							$row[$Model->alias][$aliasField] = $row[$Model->alias][$aliasVirtualLocale];
298
							$row[$Model->alias]['locale'] = $_locale;
299
						}
300
						unset($row[$Model->alias][$aliasVirtualLocale]);
301
					}
302
 
303
					if (!isset($row[$Model->alias][$aliasField])) {
304
						$row[$Model->alias][$aliasField] = '';
305
					}
306
				} else {
307
					$value = '';
308
					if (!empty($row[$Model->alias][$aliasVirtual])) {
309
						$value = $row[$Model->alias][$aliasVirtual];
310
					}
311
					$row[$Model->alias][$aliasField] = $value;
312
					unset($row[$Model->alias][$aliasVirtual]);
313
				}
314
			}
315
		}
316
		return $results;
317
	}
318
 
319
/**
320
 * beforeValidate Callback
321
 *
322
 * @param Model $Model Model invalidFields was called on.
323
 * @param array $options Options passed from Model::save().
324
 * @return boolean
325
 * @see Model::save()
326
 */
327
	public function beforeValidate(Model $Model, $options = array()) {
328
		unset($this->runtime[$Model->alias]['beforeSave']);
329
		$this->_setRuntimeData($Model);
330
		return true;
331
	}
332
 
333
/**
334
 * beforeSave callback.
335
 *
336
 * Copies data into the runtime property when `$options['validate']` is
337
 * disabled. Or the runtime data hasn't been set yet.
338
 *
339
 * @param Model $Model Model save was called on.
340
 * @param array $options Options passed from Model::save().
341
 * @return boolean true.
342
 * @see Model::save()
343
 */
344
	public function beforeSave(Model $Model, $options = array()) {
345
		if (isset($options['validate']) && !$options['validate']) {
346
			unset($this->runtime[$Model->alias]['beforeSave']);
347
		}
348
		if (isset($this->runtime[$Model->alias]['beforeSave'])) {
349
			return true;
350
		}
351
		$this->_setRuntimeData($Model);
352
		return true;
353
	}
354
 
355
/**
356
 * Sets the runtime data.
357
 *
358
 * Used from beforeValidate() and beforeSave() for compatibility issues,
359
 * and to allow translations to be persisted even when validation
360
 * is disabled.
361
 *
362
 * @param Model $Model
363
 * @return void
364
 */
365
	protected function _setRuntimeData(Model $Model) {
366
		$locale = $this->_getLocale($Model);
367
		if (empty($locale)) {
368
			return true;
369
		}
370
		$fields = array_merge($this->settings[$Model->alias], $this->runtime[$Model->alias]['fields']);
371
		$tempData = array();
372
 
373
		foreach ($fields as $key => $value) {
374
			$field = (is_numeric($key)) ? $value : $key;
375
 
376
			if (isset($Model->data[$Model->alias][$field])) {
377
				$tempData[$field] = $Model->data[$Model->alias][$field];
378
				if (is_array($Model->data[$Model->alias][$field])) {
379
					if (is_string($locale) && !empty($Model->data[$Model->alias][$field][$locale])) {
380
						$Model->data[$Model->alias][$field] = $Model->data[$Model->alias][$field][$locale];
381
					} else {
382
						$values = array_values($Model->data[$Model->alias][$field]);
383
						$Model->data[$Model->alias][$field] = $values[0];
384
					}
385
				}
386
			}
387
		}
388
		$this->runtime[$Model->alias]['beforeSave'] = $tempData;
389
	}
390
 
391
/**
392
 * Restores model data to the original data.
393
 * This solves issues with saveAssociated and validate = first.
394
 *
395
 * @param Model $model
396
 * @return void
397
 */
398
	public function afterValidate(Model $Model) {
399
		$Model->data[$Model->alias] = array_merge(
400
			$Model->data[$Model->alias],
401
			$this->runtime[$Model->alias]['beforeSave']
402
		);
403
		return true;
404
	}
405
 
406
/**
407
 * afterSave Callback
408
 *
409
 * @param Model $Model Model the callback is called on
410
 * @param boolean $created Whether or not the save created a record.
411
 * @param array $options Options passed from Model::save().
412
 * @return void
413
 */
414
	public function afterSave(Model $Model, $created, $options = array()) {
415
		if (!isset($this->runtime[$Model->alias]['beforeValidate']) && !isset($this->runtime[$Model->alias]['beforeSave'])) {
416
			return true;
417
		}
418
		if (isset($this->runtime[$Model->alias]['beforeValidate'])) {
419
			$tempData = $this->runtime[$Model->alias]['beforeValidate'];
420
		} else {
421
			$tempData = $this->runtime[$Model->alias]['beforeSave'];
422
		}
423
 
424
		unset($this->runtime[$Model->alias]['beforeValidate'], $this->runtime[$Model->alias]['beforeSave']);
425
		$conditions = array('model' => $Model->name, 'foreign_key' => $Model->id);
426
		$RuntimeModel = $this->translateModel($Model);
427
 
428
		if ($created) {
429
			$tempData = $this->_prepareTranslations($Model, $tempData);
430
		}
431
		$locale = $this->_getLocale($Model);
432
 
433
		foreach ($tempData as $field => $value) {
434
			unset($conditions['content']);
435
			$conditions['field'] = $field;
436
			if (is_array($value)) {
437
				$conditions['locale'] = array_keys($value);
438
			} else {
439
				$conditions['locale'] = $locale;
440
				if (is_array($locale)) {
441
					$value = array($locale[0] => $value);
442
				} else {
443
					$value = array($locale => $value);
444
				}
445
			}
446
			$translations = $RuntimeModel->find('list', array(
447
				'conditions' => $conditions,
448
				'fields' => array(
449
					$RuntimeModel->alias . '.locale',
450
					$RuntimeModel->alias . '.id'
451
				)
452
			));
453
			foreach ($value as $_locale => $_value) {
454
				$RuntimeModel->create();
455
				$conditions['locale'] = $_locale;
456
				$conditions['content'] = $_value;
457
				if (array_key_exists($_locale, $translations)) {
458
					$RuntimeModel->save(array(
459
						$RuntimeModel->alias => array_merge(
460
							$conditions, array('id' => $translations[$_locale])
461
						)
462
					));
463
				} else {
464
					$RuntimeModel->save(array($RuntimeModel->alias => $conditions));
465
				}
466
			}
467
		}
468
	}
469
 
470
/**
471
 * Prepares the data to be saved for translated records.
472
 * Add blank fields, and populates data for multi-locale saves.
473
 *
474
 * @param Model $Model Model instance
475
 * @param array $data The sparse data that was provided.
476
 * @return array The fully populated data to save.
477
 */
478
	protected function _prepareTranslations(Model $Model, $data) {
479
		$fields = array_merge($this->settings[$Model->alias], $this->runtime[$Model->alias]['fields']);
480
		$locales = array();
481
		foreach ($data as $key => $value) {
482
			if (is_array($value)) {
483
				$locales = array_merge($locales, array_keys($value));
484
			}
485
		}
486
		$locales = array_unique($locales);
487
		$hasLocales = count($locales) > 0;
488
 
489
		foreach ($fields as $key => $field) {
490
			if (!is_numeric($key)) {
491
				$field = $key;
492
			}
493
			if ($hasLocales && !isset($data[$field])) {
494
				$data[$field] = array_fill_keys($locales, '');
495
			} elseif (!isset($data[$field])) {
496
				$data[$field] = '';
497
			}
498
		}
499
		return $data;
500
	}
501
 
502
/**
503
 * afterDelete Callback
504
 *
505
 * @param Model $Model Model the callback was run on.
506
 * @return void
507
 */
508
	public function afterDelete(Model $Model) {
509
		$RuntimeModel = $this->translateModel($Model);
510
		$conditions = array('model' => $Model->name, 'foreign_key' => $Model->id);
511
		$RuntimeModel->deleteAll($conditions);
512
	}
513
 
514
/**
515
 * Get selected locale for model
516
 *
517
 * @param Model $Model Model the locale needs to be set/get on.
518
 * @return mixed string or false
519
 */
520
	protected function _getLocale(Model $Model) {
521
		if (!isset($Model->locale) || $Model->locale === null) {
522
			$I18n = I18n::getInstance();
523
			$I18n->l10n->get(Configure::read('Config.language'));
524
			$Model->locale = $I18n->l10n->locale;
525
		}
526
 
527
		return $Model->locale;
528
	}
529
 
530
/**
531
 * Get instance of model for translations.
532
 *
533
 * If the model has a translateModel property set, this will be used as the class
534
 * name to find/use. If no translateModel property is found 'I18nModel' will be used.
535
 *
536
 * @param Model $Model Model to get a translatemodel for.
537
 * @return Model
538
 */
539
	public function translateModel(Model $Model) {
540
		if (!isset($this->runtime[$Model->alias]['model'])) {
541
			if (!isset($Model->translateModel) || empty($Model->translateModel)) {
542
				$className = 'I18nModel';
543
			} else {
544
				$className = $Model->translateModel;
545
			}
546
 
547
			$this->runtime[$Model->alias]['model'] = ClassRegistry::init($className);
548
		}
549
		if (!empty($Model->translateTable) && $Model->translateTable !== $this->runtime[$Model->alias]['model']->useTable) {
550
			$this->runtime[$Model->alias]['model']->setSource($Model->translateTable);
551
		} elseif (empty($Model->translateTable) && empty($Model->translateModel)) {
552
			$this->runtime[$Model->alias]['model']->setSource('i18n');
553
		}
554
		return $this->runtime[$Model->alias]['model'];
555
	}
556
 
557
/**
558
 * Bind translation for fields, optionally with hasMany association for
559
 * fake field.
560
 *
561
 * *Note* You should avoid binding translations that overlap existing model properties.
562
 * This can cause un-expected and un-desirable behavior.
563
 *
564
 * @param Model $Model instance of model
565
 * @param string|array $fields string with field or array(field1, field2=>AssocName, field3)
566
 * @param boolean $reset Leave true to have the fields only modified for the next operation.
567
 *   if false the field will be added for all future queries.
568
 * @return boolean
569
 * @throws CakeException when attempting to bind a translating called name. This is not allowed
570
 *   as it shadows Model::$name.
571
 */
572
	public function bindTranslation(Model $Model, $fields, $reset = true) {
573
		if (is_string($fields)) {
574
			$fields = array($fields);
575
		}
576
		$associations = array();
577
		$RuntimeModel = $this->translateModel($Model);
578
		$default = array('className' => $RuntimeModel->alias, 'foreignKey' => 'foreign_key');
579
 
580
		foreach ($fields as $key => $value) {
581
			if (is_numeric($key)) {
582
				$field = $value;
583
				$association = null;
584
			} else {
585
				$field = $key;
586
				$association = $value;
587
			}
588
			if ($association === 'name') {
589
				throw new CakeException(
590
					__d('cake_dev', 'You cannot bind a translation named "name".')
591
				);
592
			}
593
 
594
			$this->_removeField($Model, $field);
595
 
596
			if ($association === null) {
597
				if ($reset) {
598
					$this->runtime[$Model->alias]['fields'][] = $field;
599
				} else {
600
					$this->settings[$Model->alias][] = $field;
601
				}
602
			} else {
603
				if ($reset) {
604
					$this->runtime[$Model->alias]['fields'][$field] = $association;
605
				} else {
606
					$this->settings[$Model->alias][$field] = $association;
607
				}
608
 
609
				foreach (array('hasOne', 'hasMany', 'belongsTo', 'hasAndBelongsToMany') as $type) {
610
					if (isset($Model->{$type}[$association]) || isset($Model->__backAssociation[$type][$association])) {
611
						trigger_error(
612
							__d('cake_dev', 'Association %s is already bound to model %s', $association, $Model->alias),
613
							E_USER_ERROR
614
						);
615
						return false;
616
					}
617
				}
618
				$associations[$association] = array_merge($default, array('conditions' => array(
619
					'model' => $Model->name,
620
					$RuntimeModel->displayField => $field
621
				)));
622
			}
623
		}
624
 
625
		if (!empty($associations)) {
626
			$Model->bindModel(array('hasMany' => $associations), $reset);
627
		}
628
		return true;
629
	}
630
 
631
/**
632
 * Update runtime setting for a given field.
633
 *
634
 * @param Model $Model Model instance
635
 * @param string $field The field to update.
636
 * @return void
637
 */
638
	protected function _removeField(Model $Model, $field) {
639
		if (array_key_exists($field, $this->settings[$Model->alias])) {
640
			unset($this->settings[$Model->alias][$field]);
641
		} elseif (in_array($field, $this->settings[$Model->alias])) {
642
			$this->settings[$Model->alias] = array_merge(array_diff($this->settings[$Model->alias], array($field)));
643
		}
644
 
645
		if (array_key_exists($field, $this->runtime[$Model->alias]['fields'])) {
646
			unset($this->runtime[$Model->alias]['fields'][$field]);
647
		} elseif (in_array($field, $this->runtime[$Model->alias]['fields'])) {
648
			$this->runtime[$Model->alias]['fields'] = array_merge(array_diff($this->runtime[$Model->alias]['fields'], array($field)));
649
		}
650
	}
651
 
652
/**
653
 * Unbind translation for fields, optionally unbinds hasMany association for
654
 * fake field
655
 *
656
 * @param Model $Model instance of model
657
 * @param string|array $fields string with field, or array(field1, field2=>AssocName, field3), or null for
658
 *    unbind all original translations
659
 * @return boolean
660
 */
661
	public function unbindTranslation(Model $Model, $fields = null) {
662
		if (empty($fields) && empty($this->settings[$Model->alias])) {
663
			return false;
664
		}
665
		if (empty($fields)) {
666
			return $this->unbindTranslation($Model, $this->settings[$Model->alias]);
667
		}
668
 
669
		if (is_string($fields)) {
670
			$fields = array($fields);
671
		}
672
		$associations = array();
673
 
674
		foreach ($fields as $key => $value) {
675
			if (is_numeric($key)) {
676
				$field = $value;
677
				$association = null;
678
			} else {
679
				$field = $key;
680
				$association = $value;
681
			}
682
 
683
			$this->_removeField($Model, $field);
684
 
685
			if ($association !== null && (isset($Model->hasMany[$association]) || isset($Model->__backAssociation['hasMany'][$association]))) {
686
				$associations[] = $association;
687
			}
688
		}
689
 
690
		if (!empty($associations)) {
691
			$Model->unbindModel(array('hasMany' => $associations), false);
692
		}
693
		return true;
694
	}
695
 
696
}