PageRenderTime 65ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Model/Behavior/TranslateBehavior.php

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