TranslateBehavior.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. namespace Cake\Model\Behavior;
  17. use Cake\Core\Configure;
  18. use Cake\Database\ConnectionManager;
  19. use Cake\Error;
  20. use Cake\I18n\I18n;
  21. use Cake\Model\Model;
  22. use Cake\Model\ModelBehavior;
  23. use Cake\Utility\ClassRegistry;
  24. /**
  25. * Translate behavior
  26. *
  27. * @package Cake.Model.Behavior
  28. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/translate.html
  29. */
  30. class TranslateBehavior extends ModelBehavior {
  31. /**
  32. * Used for runtime configuration of model
  33. *
  34. * @var array
  35. */
  36. public $runtime = array();
  37. /**
  38. * Stores the joinTable object for generating joins.
  39. *
  40. * @var object
  41. */
  42. protected $_joinTable;
  43. /**
  44. * Stores the runtime model for generating joins.
  45. *
  46. * @var Model
  47. */
  48. protected $_runtimeModel;
  49. /**
  50. * Callback
  51. *
  52. * $config for TranslateBehavior should be
  53. * array('fields' => array('field_one',
  54. * 'field_two' => 'FieldAssoc', 'field_three'))
  55. *
  56. * With above example only one permanent hasMany will be joined (for field_two
  57. * as FieldAssoc)
  58. *
  59. * $config could be empty - and translations configured dynamically by
  60. * bindTranslation() method
  61. *
  62. * @param Model $Model Model the behavior is being attached to.
  63. * @param array $config Array of configuration information.
  64. * @return mixed
  65. */
  66. public function setup(Model $Model, $config = array()) {
  67. $db = ConnectionManager::getDataSource($Model->useDbConfig);
  68. if (!$db->connected) {
  69. trigger_error(
  70. __d('cake_dev', 'Datasource %s for TranslateBehavior of model %s is not connected', $Model->useDbConfig, $Model->alias),
  71. E_USER_ERROR
  72. );
  73. return false;
  74. }
  75. $this->settings[$Model->alias] = array();
  76. $this->runtime[$Model->alias] = array('fields' => array());
  77. $this->translateModel($Model);
  78. return $this->bindTranslation($Model, $config, false);
  79. }
  80. /**
  81. * Cleanup Callback unbinds bound translations and deletes setting information.
  82. *
  83. * @param Model $Model Model being detached.
  84. * @return void
  85. */
  86. public function cleanup(Model $Model) {
  87. $this->unbindTranslation($Model);
  88. unset($this->settings[$Model->alias]);
  89. unset($this->runtime[$Model->alias]);
  90. }
  91. /**
  92. * beforeFind Callback
  93. *
  94. * @param Model $Model Model find is being run on.
  95. * @param array $query Array of Query parameters.
  96. * @return array Modified query
  97. */
  98. public function beforeFind(Model $Model, $query) {
  99. $this->runtime[$Model->alias]['virtualFields'] = $Model->virtualFields;
  100. $locale = $this->_getLocale($Model);
  101. if (empty($locale)) {
  102. return $query;
  103. }
  104. $db = $Model->getDataSource();
  105. $RuntimeModel = $this->translateModel($Model);
  106. if (!empty($RuntimeModel->tablePrefix)) {
  107. $tablePrefix = $RuntimeModel->tablePrefix;
  108. } else {
  109. $tablePrefix = $db->config['prefix'];
  110. }
  111. $joinTable = new \StdClass();
  112. $joinTable->tablePrefix = $tablePrefix;
  113. $joinTable->table = $RuntimeModel->table;
  114. $joinTable->schemaName = $RuntimeModel->getDataSource()->getSchemaName();
  115. $this->_joinTable = $joinTable;
  116. $this->_runtimeModel = $RuntimeModel;
  117. if (is_string($query['fields']) && "COUNT(*) AS {$db->name('count')}" == $query['fields']) {
  118. $query['fields'] = "COUNT(DISTINCT({$db->name($Model->escapeField())})) {$db->alias}count";
  119. $query['joins'][] = array(
  120. 'type' => 'INNER',
  121. 'alias' => $RuntimeModel->alias,
  122. 'table' => $joinTable,
  123. 'conditions' => array(
  124. $Model->escapeField() => $db->identifier($RuntimeModel->escapeField('foreign_key')),
  125. $RuntimeModel->escapeField('model') => $Model->name,
  126. $RuntimeModel->escapeField('locale') => $locale
  127. )
  128. );
  129. $conditionFields = $this->_checkConditions($Model, $query);
  130. foreach ($conditionFields as $field) {
  131. $query = $this->_addJoin($Model, $query, $field, $field, $locale);
  132. }
  133. unset($this->_joinTable, $this->_runtimeModel);
  134. return $query;
  135. }
  136. $fields = array_merge(
  137. $this->settings[$Model->alias],
  138. $this->runtime[$Model->alias]['fields']
  139. );
  140. $addFields = array();
  141. if (empty($query['fields'])) {
  142. $addFields = $fields;
  143. } elseif (is_array($query['fields'])) {
  144. $isAllFields = (
  145. in_array($Model->alias . '.' . '*', $query['fields']) ||
  146. in_array($Model->escapeField('*'), $query['fields'])
  147. );
  148. foreach ($fields as $key => $value) {
  149. $field = (is_numeric($key)) ? $value : $key;
  150. if (
  151. $isAllFields ||
  152. in_array($Model->alias . '.' . $field, $query['fields']) ||
  153. in_array($field, $query['fields'])
  154. ) {
  155. $addFields[] = $field;
  156. }
  157. }
  158. }
  159. $this->runtime[$Model->alias]['virtualFields'] = $Model->virtualFields;
  160. if ($addFields) {
  161. foreach ($addFields as $_f => $field) {
  162. $aliasField = is_numeric($_f) ? $field : $_f;
  163. foreach (array($aliasField, $Model->alias . '.' . $aliasField) as $_field) {
  164. $key = array_search($_field, (array)$query['fields']);
  165. if ($key !== false) {
  166. unset($query['fields'][$key]);
  167. }
  168. }
  169. $query = $this->_addJoin($Model, $query, $field, $aliasField, $locale);
  170. }
  171. }
  172. $this->runtime[$Model->alias]['beforeFind'] = $addFields;
  173. unset($this->_joinTable, $this->_runtimeModel);
  174. return $query;
  175. }
  176. /**
  177. * Check a query's conditions for translated fields.
  178. * Return an array of translated fields found in the conditions.
  179. *
  180. * @param Model $Model The model being read.
  181. * @param array $query The query array.
  182. * @return array The list of translated fields that are in the conditions.
  183. */
  184. protected function _checkConditions(Model $Model, $query) {
  185. $conditionFields = array();
  186. if (empty($query['conditions']) || (!empty($query['conditions']) && !is_array($query['conditions']))) {
  187. return $conditionFields;
  188. }
  189. foreach ($query['conditions'] as $col => $val) {
  190. foreach ($this->settings[$Model->alias] as $field => $assoc) {
  191. if (is_numeric($field)) {
  192. $field = $assoc;
  193. }
  194. if (strpos($col, $field) !== false) {
  195. $conditionFields[] = $field;
  196. }
  197. }
  198. }
  199. return $conditionFields;
  200. }
  201. /**
  202. * Appends a join for translated fields.
  203. *
  204. * @param Model $Model The model being worked on.
  205. * @param array $query The query array to append a join to.
  206. * @param string $field The field name being joined.
  207. * @param string $aliasField The aliased field name being joined.
  208. * @param string|array $locale The locale(s) having joins added.
  209. * @return array The modified query
  210. */
  211. protected function _addJoin(Model $Model, $query, $field, $aliasField, $locale) {
  212. $db = ConnectionManager::getDataSource($Model->useDbConfig);
  213. $RuntimeModel = $this->_runtimeModel;
  214. $joinTable = $this->_joinTable;
  215. $aliasVirtual = "i18n_{$field}";
  216. $alias = "I18n__{$field}";
  217. if (is_array($locale)) {
  218. foreach ($locale as $_locale) {
  219. $aliasVirtualLocale = "{$aliasVirtual}_{$_locale}";
  220. $aliasLocale = "{$alias}__{$_locale}";
  221. $Model->virtualFields[$aliasVirtualLocale] = "{$aliasLocale}.content";
  222. if (!empty($query['fields']) && is_array($query['fields'])) {
  223. $query['fields'][] = $aliasVirtualLocale;
  224. }
  225. $query['joins'][] = array(
  226. 'type' => 'LEFT',
  227. 'alias' => $aliasLocale,
  228. 'table' => $joinTable,
  229. 'conditions' => array(
  230. $Model->escapeField() => $db->identifier("{$aliasLocale}.foreign_key"),
  231. "{$aliasLocale}.model" => $Model->name,
  232. "{$aliasLocale}.{$RuntimeModel->displayField}" => $aliasField,
  233. "{$aliasLocale}.locale" => $_locale
  234. )
  235. );
  236. }
  237. } else {
  238. $Model->virtualFields[$aliasVirtual] = "{$alias}.content";
  239. if (!empty($query['fields']) && is_array($query['fields'])) {
  240. $query['fields'][] = $aliasVirtual;
  241. }
  242. $query['joins'][] = array(
  243. 'type' => 'INNER',
  244. 'alias' => $alias,
  245. 'table' => $joinTable,
  246. 'conditions' => array(
  247. "{$Model->alias}.{$Model->primaryKey}" => $db->identifier("{$alias}.foreign_key"),
  248. "{$alias}.model" => $Model->name,
  249. "{$alias}.{$RuntimeModel->displayField}" => $aliasField,
  250. "{$alias}.locale" => $locale
  251. )
  252. );
  253. }
  254. return $query;
  255. }
  256. /**
  257. * afterFind Callback
  258. *
  259. * @param Model $Model Model find was run on
  260. * @param array $results Array of model results.
  261. * @param boolean $primary Did the find originate on $model.
  262. * @return array Modified results
  263. */
  264. public function afterFind(Model $Model, $results, $primary) {
  265. $Model->virtualFields = $this->runtime[$Model->alias]['virtualFields'];
  266. $this->runtime[$Model->alias]['virtualFields'] = $this->runtime[$Model->alias]['fields'] = array();
  267. $locale = $this->_getLocale($Model);
  268. if (empty($locale) || empty($results) || empty($this->runtime[$Model->alias]['beforeFind'])) {
  269. return $results;
  270. }
  271. $beforeFind = $this->runtime[$Model->alias]['beforeFind'];
  272. foreach ($results as $key => &$row) {
  273. $results[$key][$Model->alias]['locale'] = (is_array($locale)) ? current($locale) : $locale;
  274. foreach ($beforeFind as $_f => $field) {
  275. $aliasField = is_numeric($_f) ? $field : $_f;
  276. $aliasVirtual = "i18n_{$field}";
  277. if (is_array($locale)) {
  278. foreach ($locale as $_locale) {
  279. $aliasVirtualLocale = "{$aliasVirtual}_{$_locale}";
  280. if (!isset($row[$Model->alias][$aliasField]) && !empty($row[$Model->alias][$aliasVirtualLocale])) {
  281. $row[$Model->alias][$aliasField] = $row[$Model->alias][$aliasVirtualLocale];
  282. $row[$Model->alias]['locale'] = $_locale;
  283. }
  284. unset($row[$Model->alias][$aliasVirtualLocale]);
  285. }
  286. if (!isset($row[$Model->alias][$aliasField])) {
  287. $row[$Model->alias][$aliasField] = '';
  288. }
  289. } else {
  290. $value = '';
  291. if (!empty($row[$Model->alias][$aliasVirtual])) {
  292. $value = $row[$Model->alias][$aliasVirtual];
  293. }
  294. $row[$Model->alias][$aliasField] = $value;
  295. unset($row[$Model->alias][$aliasVirtual]);
  296. }
  297. }
  298. }
  299. return $results;
  300. }
  301. /**
  302. * beforeValidate Callback
  303. *
  304. * @param Model $Model Model invalidFields was called on.
  305. * @return boolean
  306. */
  307. public function beforeValidate(Model $Model) {
  308. unset($this->runtime[$Model->alias]['beforeSave']);
  309. $this->_setRuntimeData($Model);
  310. return true;
  311. }
  312. /**
  313. * beforeSave callback.
  314. *
  315. * Copies data into the runtime property when `$options['validate']` is
  316. * disabled. Or the runtime data hasn't been set yet.
  317. *
  318. * @param Model $Model Model save was called on.
  319. * @return boolean true.
  320. */
  321. public function beforeSave(Model $Model, $options = array()) {
  322. if (isset($options['validate']) && !$options['validate']) {
  323. unset($this->runtime[$Model->alias]['beforeSave']);
  324. }
  325. if (isset($this->runtime[$Model->alias]['beforeSave'])) {
  326. return true;
  327. }
  328. $this->_setRuntimeData($Model);
  329. return true;
  330. }
  331. /**
  332. * Sets the runtime data.
  333. *
  334. * Used from beforeValidate() and beforeSave() for compatibility issues,
  335. * and to allow translations to be persisted even when validation
  336. * is disabled.
  337. *
  338. * @param Model $Model
  339. * @return void
  340. */
  341. protected function _setRuntimeData(Model $Model) {
  342. $locale = $this->_getLocale($Model);
  343. if (empty($locale)) {
  344. return true;
  345. }
  346. $fields = array_merge($this->settings[$Model->alias], $this->runtime[$Model->alias]['fields']);
  347. $tempData = array();
  348. foreach ($fields as $key => $value) {
  349. $field = (is_numeric($key)) ? $value : $key;
  350. if (isset($Model->data[$Model->alias][$field])) {
  351. $tempData[$field] = $Model->data[$Model->alias][$field];
  352. if (is_array($Model->data[$Model->alias][$field])) {
  353. if (is_string($locale) && !empty($Model->data[$Model->alias][$field][$locale])) {
  354. $Model->data[$Model->alias][$field] = $Model->data[$Model->alias][$field][$locale];
  355. } else {
  356. $values = array_values($Model->data[$Model->alias][$field]);
  357. $Model->data[$Model->alias][$field] = $values[0];
  358. }
  359. }
  360. }
  361. }
  362. $this->runtime[$Model->alias]['beforeSave'] = $tempData;
  363. }
  364. /**
  365. * Restores model data to the original data.
  366. * This solves issues with saveAssociated and validate = first.
  367. *
  368. * @param Model $model
  369. * @return void
  370. */
  371. public function afterValidate(Model $Model) {
  372. $Model->data[$Model->alias] = array_merge(
  373. $Model->data[$Model->alias],
  374. $this->runtime[$Model->alias]['beforeSave']
  375. );
  376. return true;
  377. }
  378. /**
  379. * afterSave Callback
  380. *
  381. * @param Model $Model Model the callback is called on
  382. * @param boolean $created Whether or not the save created a record.
  383. * @return void
  384. */
  385. public function afterSave(Model $Model, $created) {
  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 Cake\Error\Exception 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 Error\Exception(
  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. }