TranslateBehavior.php 21 KB

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