TranslateBehavior.php 21 KB

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