PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/libs/model/behaviors/translate.php

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