TranslateBehavior.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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) {
  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 (!empty($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. * @return boolean
  301. */
  302. public function beforeValidate(Model $Model) {
  303. unset($this->runtime[$Model->alias]['beforeSave']);
  304. $this->_setRuntimeData($Model);
  305. return true;
  306. }
  307. /**
  308. * beforeSave callback.
  309. *
  310. * Copies data into the runtime property when `$options['validate']` is
  311. * disabled. Or the runtime data hasn't been set yet.
  312. *
  313. * @param Model $Model Model save was called on.
  314. * @return boolean true.
  315. */
  316. public function beforeSave(Model $Model, $options = array()) {
  317. if (isset($options['validate']) && !$options['validate']) {
  318. unset($this->runtime[$Model->alias]['beforeSave']);
  319. }
  320. if (isset($this->runtime[$Model->alias]['beforeSave'])) {
  321. return true;
  322. }
  323. $this->_setRuntimeData($Model);
  324. return true;
  325. }
  326. /**
  327. * Sets the runtime data.
  328. *
  329. * Used from beforeValidate() and beforeSave() for compatibility issues,
  330. * and to allow translations to be persisted even when validation
  331. * is disabled.
  332. *
  333. * @param Model $Model
  334. * @return void
  335. */
  336. protected function _setRuntimeData(Model $Model) {
  337. $locale = $this->_getLocale($Model);
  338. if (empty($locale)) {
  339. return true;
  340. }
  341. $fields = array_merge($this->settings[$Model->alias], $this->runtime[$Model->alias]['fields']);
  342. $tempData = array();
  343. foreach ($fields as $key => $value) {
  344. $field = (is_numeric($key)) ? $value : $key;
  345. if (isset($Model->data[$Model->alias][$field])) {
  346. $tempData[$field] = $Model->data[$Model->alias][$field];
  347. if (is_array($Model->data[$Model->alias][$field])) {
  348. if (is_string($locale) && !empty($Model->data[$Model->alias][$field][$locale])) {
  349. $Model->data[$Model->alias][$field] = $Model->data[$Model->alias][$field][$locale];
  350. } else {
  351. $values = array_values($Model->data[$Model->alias][$field]);
  352. $Model->data[$Model->alias][$field] = $values[0];
  353. }
  354. }
  355. }
  356. }
  357. $this->runtime[$Model->alias]['beforeSave'] = $tempData;
  358. }
  359. /**
  360. * Restores model data to the original data.
  361. * This solves issues with saveAssociated and validate = first.
  362. *
  363. * @param Model $model
  364. * @return void
  365. */
  366. public function afterValidate(Model $Model) {
  367. $Model->data[$Model->alias] = array_merge(
  368. $Model->data[$Model->alias],
  369. $this->runtime[$Model->alias]['beforeSave']
  370. );
  371. return true;
  372. }
  373. /**
  374. * afterSave Callback
  375. *
  376. * @param Model $Model Model the callback is called on
  377. * @param boolean $created Whether or not the save created a record.
  378. * @return void
  379. */
  380. public function afterSave(Model $Model, $created) {
  381. if (!isset($this->runtime[$Model->alias]['beforeValidate']) && !isset($this->runtime[$Model->alias]['beforeSave'])) {
  382. return true;
  383. }
  384. if (isset($this->runtime[$Model->alias]['beforeValidate'])) {
  385. $tempData = $this->runtime[$Model->alias]['beforeValidate'];
  386. } else {
  387. $tempData = $this->runtime[$Model->alias]['beforeSave'];
  388. }
  389. unset($this->runtime[$Model->alias]['beforeValidate'], $this->runtime[$Model->alias]['beforeSave']);
  390. $conditions = array('model' => $Model->name, 'foreign_key' => $Model->id);
  391. $RuntimeModel = $this->translateModel($Model);
  392. if ($created) {
  393. $tempData = $this->_prepareTranslations($Model, $tempData);
  394. }
  395. $locale = $this->_getLocale($Model);
  396. foreach ($tempData as $field => $value) {
  397. unset($conditions['content']);
  398. $conditions['field'] = $field;
  399. if (is_array($value)) {
  400. $conditions['locale'] = array_keys($value);
  401. } else {
  402. $conditions['locale'] = $locale;
  403. if (is_array($locale)) {
  404. $value = array($locale[0] => $value);
  405. } else {
  406. $value = array($locale => $value);
  407. }
  408. }
  409. $translations = $RuntimeModel->find('list', array(
  410. 'conditions' => $conditions,
  411. 'fields' => array(
  412. $RuntimeModel->alias . '.locale',
  413. $RuntimeModel->alias . '.id'
  414. )
  415. ));
  416. foreach ($value as $_locale => $_value) {
  417. $RuntimeModel->create();
  418. $conditions['locale'] = $_locale;
  419. $conditions['content'] = $_value;
  420. if (array_key_exists($_locale, $translations)) {
  421. $RuntimeModel->save(array(
  422. $RuntimeModel->alias => array_merge(
  423. $conditions, array('id' => $translations[$_locale])
  424. )
  425. ));
  426. } else {
  427. $RuntimeModel->save(array($RuntimeModel->alias => $conditions));
  428. }
  429. }
  430. }
  431. }
  432. /**
  433. * Prepares the data to be saved for translated records.
  434. * Add blank fields, and populates data for multi-locale saves.
  435. *
  436. * @param Model $Model Model instance
  437. * @param array $data The sparse data that was provided.
  438. * @return array The fully populated data to save.
  439. */
  440. protected function _prepareTranslations(Model $Model, $data) {
  441. $fields = array_merge($this->settings[$Model->alias], $this->runtime[$Model->alias]['fields']);
  442. $locales = array();
  443. foreach ($data as $key => $value) {
  444. if (is_array($value)) {
  445. $locales = array_merge($locales, array_keys($value));
  446. }
  447. }
  448. $locales = array_unique($locales);
  449. $hasLocales = count($locales) > 0;
  450. foreach ($fields as $key => $field) {
  451. if (!is_numeric($key)) {
  452. $field = $key;
  453. }
  454. if ($hasLocales && !isset($data[$field])) {
  455. $data[$field] = array_fill_keys($locales, '');
  456. } elseif (!isset($data[$field])) {
  457. $data[$field] = '';
  458. }
  459. }
  460. return $data;
  461. }
  462. /**
  463. * afterDelete Callback
  464. *
  465. * @param Model $Model Model the callback was run on.
  466. * @return void
  467. */
  468. public function afterDelete(Model $Model) {
  469. $RuntimeModel = $this->translateModel($Model);
  470. $conditions = array('model' => $Model->name, 'foreign_key' => $Model->id);
  471. $RuntimeModel->deleteAll($conditions);
  472. }
  473. /**
  474. * Get selected locale for model
  475. *
  476. * @param Model $Model Model the locale needs to be set/get on.
  477. * @return mixed string or false
  478. */
  479. protected function _getLocale(Model $Model) {
  480. if (!isset($Model->locale) || is_null($Model->locale)) {
  481. $I18n = I18n::getInstance();
  482. $I18n->l10n->get(Configure::read('Config.language'));
  483. $Model->locale = $I18n->l10n->locale;
  484. }
  485. return $Model->locale;
  486. }
  487. /**
  488. * Get instance of model for translations.
  489. *
  490. * If the model has a translateModel property set, this will be used as the class
  491. * name to find/use. If no translateModel property is found 'I18nModel' will be used.
  492. *
  493. * @param Model $Model Model to get a translatemodel for.
  494. * @return Model
  495. */
  496. public function translateModel(Model $Model) {
  497. if (!isset($this->runtime[$Model->alias]['model'])) {
  498. if (!isset($Model->translateModel) || empty($Model->translateModel)) {
  499. $className = 'I18nModel';
  500. } else {
  501. $className = $Model->translateModel;
  502. }
  503. $this->runtime[$Model->alias]['model'] = ClassRegistry::init($className);
  504. }
  505. if (!empty($Model->translateTable) && $Model->translateTable !== $this->runtime[$Model->alias]['model']->useTable) {
  506. $this->runtime[$Model->alias]['model']->setSource($Model->translateTable);
  507. } elseif (empty($Model->translateTable) && empty($Model->translateModel)) {
  508. $this->runtime[$Model->alias]['model']->setSource('i18n');
  509. }
  510. return $this->runtime[$Model->alias]['model'];
  511. }
  512. /**
  513. * Bind translation for fields, optionally with hasMany association for
  514. * fake field.
  515. *
  516. * *Note* You should avoid binding translations that overlap existing model properties.
  517. * This can cause un-expected and un-desirable behavior.
  518. *
  519. * @param Model $Model instance of model
  520. * @param string|array $fields string with field or array(field1, field2=>AssocName, field3)
  521. * @param boolean $reset Leave true to have the fields only modified for the next operation.
  522. * if false the field will be added for all future queries.
  523. * @return boolean
  524. * @throws CakeException when attempting to bind a translating called name. This is not allowed
  525. * as it shadows Model::$name.
  526. */
  527. public function bindTranslation(Model $Model, $fields, $reset = true) {
  528. if (is_string($fields)) {
  529. $fields = array($fields);
  530. }
  531. $associations = array();
  532. $RuntimeModel = $this->translateModel($Model);
  533. $default = array('className' => $RuntimeModel->alias, 'foreignKey' => 'foreign_key');
  534. foreach ($fields as $key => $value) {
  535. if (is_numeric($key)) {
  536. $field = $value;
  537. $association = null;
  538. } else {
  539. $field = $key;
  540. $association = $value;
  541. }
  542. if ($association === 'name') {
  543. throw new CakeException(
  544. __d('cake_dev', 'You cannot bind a translation named "name".')
  545. );
  546. }
  547. $this->_removeField($Model, $field);
  548. if (is_null($association)) {
  549. if ($reset) {
  550. $this->runtime[$Model->alias]['fields'][] = $field;
  551. } else {
  552. $this->settings[$Model->alias][] = $field;
  553. }
  554. } else {
  555. if ($reset) {
  556. $this->runtime[$Model->alias]['fields'][$field] = $association;
  557. } else {
  558. $this->settings[$Model->alias][$field] = $association;
  559. }
  560. foreach (array('hasOne', 'hasMany', 'belongsTo', 'hasAndBelongsToMany') as $type) {
  561. if (isset($Model->{$type}[$association]) || isset($Model->__backAssociation[$type][$association])) {
  562. trigger_error(
  563. __d('cake_dev', 'Association %s is already bound to model %s', $association, $Model->alias),
  564. E_USER_ERROR
  565. );
  566. return false;
  567. }
  568. }
  569. $associations[$association] = array_merge($default, array('conditions' => array(
  570. 'model' => $Model->name,
  571. $RuntimeModel->displayField => $field
  572. )));
  573. }
  574. }
  575. if (!empty($associations)) {
  576. $Model->bindModel(array('hasMany' => $associations), $reset);
  577. }
  578. return true;
  579. }
  580. /**
  581. * Update runtime setting for a given field.
  582. *
  583. * @param Model $Model Model instance
  584. * @param string $field The field to update.
  585. * @return void
  586. */
  587. protected function _removeField(Model $Model, $field) {
  588. if (array_key_exists($field, $this->settings[$Model->alias])) {
  589. unset($this->settings[$Model->alias][$field]);
  590. } elseif (in_array($field, $this->settings[$Model->alias])) {
  591. $this->settings[$Model->alias] = array_merge(array_diff($this->settings[$Model->alias], array($field)));
  592. }
  593. if (array_key_exists($field, $this->runtime[$Model->alias]['fields'])) {
  594. unset($this->runtime[$Model->alias]['fields'][$field]);
  595. } elseif (in_array($field, $this->runtime[$Model->alias]['fields'])) {
  596. $this->runtime[$Model->alias]['fields'] = array_merge(array_diff($this->runtime[$Model->alias]['fields'], array($field)));
  597. }
  598. }
  599. /**
  600. * Unbind translation for fields, optionally unbinds hasMany association for
  601. * fake field
  602. *
  603. * @param Model $Model instance of model
  604. * @param string|array $fields string with field, or array(field1, field2=>AssocName, field3), or null for
  605. * unbind all original translations
  606. * @return boolean
  607. */
  608. public function unbindTranslation(Model $Model, $fields = null) {
  609. if (empty($fields) && empty($this->settings[$Model->alias])) {
  610. return false;
  611. }
  612. if (empty($fields)) {
  613. return $this->unbindTranslation($Model, $this->settings[$Model->alias]);
  614. }
  615. if (is_string($fields)) {
  616. $fields = array($fields);
  617. }
  618. $associations = array();
  619. foreach ($fields as $key => $value) {
  620. if (is_numeric($key)) {
  621. $field = $value;
  622. $association = null;
  623. } else {
  624. $field = $key;
  625. $association = $value;
  626. }
  627. $this->_removeField($Model, $field);
  628. if (!is_null($association) && (isset($Model->hasMany[$association]) || isset($Model->__backAssociation['hasMany'][$association]))) {
  629. $associations[] = $association;
  630. }
  631. }
  632. if (!empty($associations)) {
  633. $Model->unbindModel(array('hasMany' => $associations), false);
  634. }
  635. return true;
  636. }
  637. }