TranslateBehavior.php 21 KB

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