2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-05-29 15:20:28 +00:00
|
|
|
* Translate behavior
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs.model.behaviors
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.4525
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-05-29 15:20:28 +00:00
|
|
|
* Translate behavior
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs.model.behaviors
|
2010-04-05 03:31:50 +00:00
|
|
|
* @link http://book.cakephp.org/view/1328/Translate
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class TranslateBehavior extends ModelBehavior {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used for runtime configuration of model
|
2010-06-10 18:23:49 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $runtime = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Callback
|
|
|
|
*
|
|
|
|
* $config for TranslateBehavior should be
|
|
|
|
* array( 'fields' => array('field_one',
|
|
|
|
* 'field_two' => 'FieldAssoc', 'field_three'))
|
|
|
|
*
|
|
|
|
* With above example only one permanent hasMany will be joined (for field_two
|
|
|
|
* as FieldAssoc)
|
|
|
|
*
|
|
|
|
* $config could be empty - and translations configured dynamically by
|
|
|
|
* bindTranslation() method
|
2008-11-03 23:58:44 +00:00
|
|
|
*
|
2010-06-10 18:23:49 +00:00
|
|
|
* @param Model $model Model the behavior is being attached to.
|
|
|
|
* @param array $config Array of configuration information.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return mixed
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-12 22:38:49 +00:00
|
|
|
public function setup($model, $config = array()) {
|
2010-11-13 04:05:44 +00:00
|
|
|
$db = ConnectionManager::getDataSource($model->useDbConfig);
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$db->connected) {
|
2008-11-03 23:58:44 +00:00
|
|
|
trigger_error(
|
2010-12-05 01:37:13 +00:00
|
|
|
__('Datasource %s for TranslateBehavior of model %s is not connected', $model->useDbConfig, $model->alias),
|
2008-11-03 23:58:44 +00:00
|
|
|
E_USER_ERROR
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->settings[$model->alias] = array();
|
|
|
|
$this->runtime[$model->alias] = array('fields' => array());
|
|
|
|
$this->translateModel($model);
|
|
|
|
return $this->bindTranslation($model, $config, false);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-06-10 18:23:49 +00:00
|
|
|
* Cleanup Callback unbinds bound translations and deletes setting information.
|
2008-11-03 23:58:44 +00:00
|
|
|
*
|
2010-06-10 18:23:49 +00:00
|
|
|
* @param Model $model Model being detached.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-12 22:38:49 +00:00
|
|
|
public function cleanup($model) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->unbindTranslation($model);
|
|
|
|
unset($this->settings[$model->alias]);
|
|
|
|
unset($this->runtime[$model->alias]);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-09-25 16:49:56 +00:00
|
|
|
* beforeFind Callback
|
2008-11-03 23:58:44 +00:00
|
|
|
*
|
2010-06-10 18:23:49 +00:00
|
|
|
* @param Model $model Model find is being run on.
|
|
|
|
* @param array $query Array of Query parameters.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return array Modified query
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-12 22:38:49 +00:00
|
|
|
public function beforeFind($model, $query) {
|
2010-10-26 22:19:43 +00:00
|
|
|
$this->runtime[$model->alias]['virtualFields'] = $model->virtualFields;
|
2008-05-30 11:40:08 +00:00
|
|
|
$locale = $this->_getLocale($model);
|
|
|
|
if (empty($locale)) {
|
|
|
|
return $query;
|
|
|
|
}
|
2010-11-13 04:05:44 +00:00
|
|
|
$db = ConnectionManager::getDataSource($model->useDbConfig);
|
|
|
|
$RuntimeModel = $this->translateModel($model);
|
2010-01-24 22:57:08 +00:00
|
|
|
if (!empty($RuntimeModel->tablePrefix)) {
|
|
|
|
$tablePrefix = $RuntimeModel->tablePrefix;
|
|
|
|
} else {
|
|
|
|
$tablePrefix = $db->config['prefix'];
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (is_string($query['fields']) && 'COUNT(*) AS '.$db->name('count') == $query['fields']) {
|
|
|
|
$query['fields'] = 'COUNT(DISTINCT('.$db->name($model->alias . '.' . $model->primaryKey) . ')) ' . $db->alias . 'count';
|
|
|
|
$query['joins'][] = array(
|
|
|
|
'type' => 'INNER',
|
|
|
|
'alias' => $RuntimeModel->alias,
|
|
|
|
'table' => $db->name($tablePrefix . $RuntimeModel->useTable),
|
|
|
|
'conditions' => array(
|
2008-09-16 00:42:01 +00:00
|
|
|
$model->alias . '.' . $model->primaryKey => $db->identifier($RuntimeModel->alias.'.foreign_key'),
|
2008-05-30 11:40:08 +00:00
|
|
|
$RuntimeModel->alias.'.model' => $model->name,
|
|
|
|
$RuntimeModel->alias.'.locale' => $locale
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields = array_merge($this->settings[$model->alias], $this->runtime[$model->alias]['fields']);
|
|
|
|
$addFields = array();
|
2010-10-26 04:10:24 +00:00
|
|
|
if (empty($query['fields'])) {
|
|
|
|
$addFields = $fields;
|
|
|
|
} else if (is_array($query['fields'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($fields as $key => $value) {
|
2008-07-30 20:34:01 +00:00
|
|
|
$field = (is_numeric($key)) ? $value : $key;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-10-26 04:10:24 +00:00
|
|
|
if (in_array($model->alias.'.*', $query['fields']) || in_array($model->alias.'.'.$field, $query['fields']) || in_array($field, $query['fields'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$addFields[] = $field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-26 22:19:43 +00:00
|
|
|
$this->runtime[$model->alias]['virtualFields'] = $model->virtualFields;
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($addFields) {
|
2010-10-26 22:52:13 +00:00
|
|
|
foreach ($addFields as $_f => $field) {
|
|
|
|
$aliasField = is_numeric($_f) ? $field : $_f;
|
|
|
|
|
|
|
|
foreach (array($aliasField, $model->alias.'.'.$aliasField) as $_field) {
|
2010-10-26 22:20:37 +00:00
|
|
|
$key = array_search($_field, (array)$query['fields']);
|
|
|
|
|
|
|
|
if ($key !== false) {
|
|
|
|
unset($query['fields'][$key]);
|
|
|
|
}
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (is_array($locale)) {
|
|
|
|
foreach ($locale as $_locale) {
|
2010-10-26 22:21:29 +00:00
|
|
|
$model->virtualFields['i18n_'.$field.'_'.$_locale] = 'I18n__'.$field.'__'.$_locale.'.content';
|
|
|
|
if (!empty($query['fields'])) {
|
|
|
|
$query['fields'][] = 'i18n_'.$field.'_'.$_locale;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$query['joins'][] = array(
|
|
|
|
'type' => 'LEFT',
|
|
|
|
'alias' => 'I18n__'.$field.'__'.$_locale,
|
|
|
|
'table' => $db->name($tablePrefix . $RuntimeModel->useTable),
|
|
|
|
'conditions' => array(
|
2008-09-16 00:42:01 +00:00
|
|
|
$model->alias . '.' . $model->primaryKey => $db->identifier("I18n__{$field}__{$_locale}.foreign_key"),
|
2008-05-30 11:40:08 +00:00
|
|
|
'I18n__'.$field.'__'.$_locale.'.model' => $model->name,
|
2010-10-26 22:52:13 +00:00
|
|
|
'I18n__'.$field.'__'.$_locale.'.'.$RuntimeModel->displayField => $aliasField,
|
2008-05-30 11:40:08 +00:00
|
|
|
'I18n__'.$field.'__'.$_locale.'.locale' => $_locale
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2010-10-26 22:21:29 +00:00
|
|
|
$model->virtualFields['i18n_'.$field] = 'I18n__'.$field.'.content';
|
|
|
|
if (!empty($query['fields'])) {
|
|
|
|
$query['fields'][] = 'i18n_'.$field;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$query['joins'][] = array(
|
|
|
|
'type' => 'LEFT',
|
|
|
|
'alias' => 'I18n__'.$field,
|
|
|
|
'table' => $db->name($tablePrefix . $RuntimeModel->useTable),
|
|
|
|
'conditions' => array(
|
2008-09-16 00:42:01 +00:00
|
|
|
$model->alias . '.' . $model->primaryKey => $db->identifier("I18n__{$field}.foreign_key"),
|
2008-05-30 11:40:08 +00:00
|
|
|
'I18n__'.$field.'.model' => $model->name,
|
2010-10-26 22:52:13 +00:00
|
|
|
'I18n__'.$field.'.'.$RuntimeModel->displayField => $aliasField
|
2008-05-30 11:40:08 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (is_string($query['conditions'])) {
|
|
|
|
$query['conditions'] = $db->conditions($query['conditions'], true, false, $model) . ' AND '.$db->name('I18n__'.$field.'.locale').' = \''.$locale.'\'';
|
|
|
|
} else {
|
|
|
|
$query['conditions'][$db->name("I18n__{$field}.locale")] = $locale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->runtime[$model->alias]['beforeFind'] = $addFields;
|
|
|
|
return $query;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-09-25 16:49:56 +00:00
|
|
|
* afterFind Callback
|
2008-11-03 23:58:44 +00:00
|
|
|
*
|
2010-06-10 18:23:49 +00:00
|
|
|
* @param Model $model Model find was run on
|
|
|
|
* @param array $results Array of model results.
|
|
|
|
* @param boolean $primary Did the find originate on $model.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return array Modified results
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-12 22:38:49 +00:00
|
|
|
public function afterFind($model, $results, $primary) {
|
2010-10-26 22:21:29 +00:00
|
|
|
$model->virtualFields = $this->runtime[$model->alias]['virtualFields'];
|
|
|
|
$this->runtime[$model->alias]['virtualFields'] = $this->runtime[$model->alias]['fields'] = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
$locale = $this->_getLocale($model);
|
|
|
|
|
|
|
|
if (empty($locale) || empty($results) || empty($this->runtime[$model->alias]['beforeFind'])) {
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
$beforeFind = $this->runtime[$model->alias]['beforeFind'];
|
|
|
|
|
2010-10-26 04:10:24 +00:00
|
|
|
foreach ($results as $key => &$row) {
|
|
|
|
$results[$key][$model->alias]['locale'] = (is_array($locale)) ? current($locale) : $locale;
|
|
|
|
foreach ($beforeFind as $_f => $field) {
|
2010-10-26 22:22:40 +00:00
|
|
|
$aliasField = is_numeric($_f) ? $field : $_f;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($locale)) {
|
|
|
|
foreach ($locale as $_locale) {
|
2010-10-26 22:22:40 +00:00
|
|
|
if (!isset($row[$model->alias][$aliasField]) && !empty($row[$model->alias]['i18n_'.$field.'_'.$_locale])) {
|
|
|
|
$row[$model->alias][$aliasField] = $row[$model->alias]['i18n_'.$field.'_'.$_locale];
|
|
|
|
$row[$model->alias]['locale'] = $_locale;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-10-26 22:22:40 +00:00
|
|
|
unset($row[$model->alias]['i18n_'.$field.'_'.$_locale]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-10-26 22:22:40 +00:00
|
|
|
if (!isset($row[$model->alias][$aliasField])) {
|
|
|
|
$row[$model->alias][$aliasField] = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2008-07-30 20:34:01 +00:00
|
|
|
$value = '';
|
2010-10-26 22:22:40 +00:00
|
|
|
if (!empty($row[$model->alias]['i18n_' . $field])) {
|
|
|
|
$value = $row[$model->alias]['i18n_' . $field];
|
2008-07-30 20:34:01 +00:00
|
|
|
}
|
2010-10-26 04:10:24 +00:00
|
|
|
$row[$model->alias][$aliasField] = $value;
|
2010-10-26 22:22:40 +00:00
|
|
|
unset($row[$model->alias]['i18n_' . $field]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-09-25 16:49:56 +00:00
|
|
|
* beforeValidate Callback
|
2008-11-03 23:58:44 +00:00
|
|
|
*
|
2010-06-10 18:23:49 +00:00
|
|
|
* @param Model $model Model invalidFields was called on.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return boolean
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-12 22:38:49 +00:00
|
|
|
public function beforeValidate($model) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$locale = $this->_getLocale($model);
|
|
|
|
if (empty($locale)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$fields = array_merge($this->settings[$model->alias], $this->runtime[$model->alias]['fields']);
|
|
|
|
$tempData = array();
|
|
|
|
|
|
|
|
foreach ($fields as $key => $value) {
|
2008-07-30 20:34:01 +00:00
|
|
|
$field = (is_numeric($key)) ? $value : $key;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (isset($model->data[$model->alias][$field])) {
|
|
|
|
$tempData[$field] = $model->data[$model->alias][$field];
|
2008-06-14 23:21:09 +00:00
|
|
|
if (is_array($model->data[$model->alias][$field])) {
|
|
|
|
if (is_string($locale) && !empty($model->data[$model->alias][$field][$locale])) {
|
|
|
|
$model->data[$model->alias][$field] = $model->data[$model->alias][$field][$locale];
|
|
|
|
} else {
|
|
|
|
$values = array_values($model->data[$model->alias][$field]);
|
|
|
|
$model->data[$model->alias][$field] = $values[0];
|
|
|
|
}
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->runtime[$model->alias]['beforeSave'] = $tempData;
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-09-25 16:49:56 +00:00
|
|
|
* afterSave Callback
|
2008-11-03 23:58:44 +00:00
|
|
|
*
|
2010-06-10 18:23:49 +00:00
|
|
|
* @param Model $model Model the callback is called on
|
|
|
|
* @param boolean $created Whether or not the save created a record.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-12 22:38:49 +00:00
|
|
|
public function afterSave($model, $created) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!isset($this->runtime[$model->alias]['beforeSave'])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$locale = $this->_getLocale($model);
|
|
|
|
$tempData = $this->runtime[$model->alias]['beforeSave'];
|
|
|
|
unset($this->runtime[$model->alias]['beforeSave']);
|
|
|
|
$conditions = array('model' => $model->alias, 'foreign_key' => $model->id);
|
2010-12-12 22:38:49 +00:00
|
|
|
$RuntimeModel = $this->translateModel($model);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
foreach ($tempData as $field => $value) {
|
|
|
|
unset($conditions['content']);
|
|
|
|
$conditions['field'] = $field;
|
|
|
|
if (is_array($value)) {
|
|
|
|
$conditions['locale'] = array_keys($value);
|
|
|
|
} else {
|
|
|
|
$conditions['locale'] = $locale;
|
|
|
|
if (is_array($locale)) {
|
|
|
|
$value = array($locale[0] => $value);
|
|
|
|
} else {
|
|
|
|
$value = array($locale => $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$translations = $RuntimeModel->find('list', array('conditions' => $conditions, 'fields' => array($RuntimeModel->alias . '.locale', $RuntimeModel->alias . '.id')));
|
|
|
|
foreach ($value as $_locale => $_value) {
|
|
|
|
$RuntimeModel->create();
|
|
|
|
$conditions['locale'] = $_locale;
|
|
|
|
$conditions['content'] = $_value;
|
|
|
|
if (array_key_exists($_locale, $translations)) {
|
|
|
|
$RuntimeModel->save(array($RuntimeModel->alias => array_merge($conditions, array('id' => $translations[$_locale]))));
|
|
|
|
} else {
|
|
|
|
$RuntimeModel->save(array($RuntimeModel->alias => $conditions));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-09-25 16:49:56 +00:00
|
|
|
* afterDelete Callback
|
2008-11-03 23:58:44 +00:00
|
|
|
*
|
2010-06-10 18:23:49 +00:00
|
|
|
* @param Model $model Model the callback was run on.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-12 22:38:49 +00:00
|
|
|
public function afterDelete($model) {
|
|
|
|
$RuntimeModel = $this->translateModel($model);
|
2008-05-30 11:40:08 +00:00
|
|
|
$conditions = array('model' => $model->alias, 'foreign_key' => $model->id);
|
|
|
|
$RuntimeModel->deleteAll($conditions);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Get selected locale for model
|
|
|
|
*
|
2010-06-10 18:23:49 +00:00
|
|
|
* @param Model $model Model the locale needs to be set/get on.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return mixed string or false
|
|
|
|
*/
|
2010-12-12 22:38:49 +00:00
|
|
|
protected function _getLocale($model) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!isset($model->locale) || is_null($model->locale)) {
|
|
|
|
if (!class_exists('I18n')) {
|
|
|
|
App::import('Core', 'i18n');
|
|
|
|
}
|
2010-12-12 22:38:49 +00:00
|
|
|
$I18n = I18n::getInstance();
|
2008-08-16 16:39:48 +00:00
|
|
|
$I18n->l10n->get(Configure::read('Config.language'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$model->locale = $I18n->l10n->locale;
|
|
|
|
}
|
2008-08-16 16:39:48 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
return $model->locale;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-06-10 18:23:49 +00:00
|
|
|
* Get instance of model for translations.
|
|
|
|
*
|
|
|
|
* If the model has a translateModel property set, this will be used as the class
|
|
|
|
* name to find/use. If no translateModel property is found 'I18nModel' will be used.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-06-10 18:23:49 +00:00
|
|
|
* @param Model $model Model to get a translatemodel for.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return object
|
|
|
|
*/
|
2010-12-12 22:38:49 +00:00
|
|
|
public function translateModel($model) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!isset($this->runtime[$model->alias]['model'])) {
|
|
|
|
if (!isset($model->translateModel) || empty($model->translateModel)) {
|
|
|
|
$className = 'I18nModel';
|
|
|
|
} else {
|
|
|
|
$className = $model->translateModel;
|
|
|
|
}
|
|
|
|
|
2010-07-06 02:19:22 +00:00
|
|
|
$this->runtime[$model->alias]['model'] = ClassRegistry::init($className, 'Model');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-11-03 23:58:44 +00:00
|
|
|
if (!empty($model->translateTable) && $model->translateTable !== $this->runtime[$model->alias]['model']->useTable) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->runtime[$model->alias]['model']->setSource($model->translateTable);
|
2009-02-19 13:45:51 +00:00
|
|
|
} elseif (empty($model->translateTable) && empty($model->translateModel)) {
|
|
|
|
$this->runtime[$model->alias]['model']->setSource('i18n');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-11-30 01:22:32 +00:00
|
|
|
return $this->runtime[$model->alias]['model'];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Bind translation for fields, optionally with hasMany association for
|
|
|
|
* fake field
|
|
|
|
*
|
|
|
|
* @param object instance of model
|
|
|
|
* @param mixed string with field or array(field1, field2=>AssocName, field3)
|
|
|
|
* @param boolean $reset
|
|
|
|
* @return bool
|
|
|
|
*/
|
2010-12-12 22:38:49 +00:00
|
|
|
function bindTranslation($model, $fields, $reset = true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_string($fields)) {
|
|
|
|
$fields = array($fields);
|
|
|
|
}
|
|
|
|
$associations = array();
|
2010-12-12 22:38:49 +00:00
|
|
|
$RuntimeModel = $this->translateModel($model);
|
2008-05-30 11:40:08 +00:00
|
|
|
$default = array('className' => $RuntimeModel->alias, 'foreignKey' => 'foreign_key');
|
|
|
|
|
|
|
|
foreach ($fields as $key => $value) {
|
|
|
|
if (is_numeric($key)) {
|
|
|
|
$field = $value;
|
|
|
|
$association = null;
|
|
|
|
} else {
|
|
|
|
$field = $key;
|
|
|
|
$association = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists($field, $this->settings[$model->alias])) {
|
|
|
|
unset($this->settings[$model->alias][$field]);
|
|
|
|
} elseif (in_array($field, $this->settings[$model->alias])) {
|
|
|
|
$this->settings[$model->alias] = array_merge(array_diff_assoc($this->settings[$model->alias], array($field)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists($field, $this->runtime[$model->alias]['fields'])) {
|
|
|
|
unset($this->runtime[$model->alias]['fields'][$field]);
|
|
|
|
} elseif (in_array($field, $this->runtime[$model->alias]['fields'])) {
|
|
|
|
$this->runtime[$model->alias]['fields'] = array_merge(array_diff_assoc($this->runtime[$model->alias]['fields'], array($field)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($association)) {
|
|
|
|
if ($reset) {
|
|
|
|
$this->runtime[$model->alias]['fields'][] = $field;
|
|
|
|
} else {
|
|
|
|
$this->settings[$model->alias][] = $field;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($reset) {
|
|
|
|
$this->runtime[$model->alias]['fields'][$field] = $association;
|
|
|
|
} else {
|
|
|
|
$this->settings[$model->alias][$field] = $association;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (array('hasOne', 'hasMany', 'belongsTo', 'hasAndBelongsToMany') as $type) {
|
|
|
|
if (isset($model->{$type}[$association]) || isset($model->__backAssociation[$type][$association])) {
|
2008-11-03 23:58:44 +00:00
|
|
|
trigger_error(
|
2010-12-05 01:37:13 +00:00
|
|
|
__('Association %s is already binded to model %s', $association, $model->alias),
|
2008-11-03 23:58:44 +00:00
|
|
|
E_USER_ERROR
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$associations[$association] = array_merge($default, array('conditions' => array(
|
|
|
|
'model' => $model->alias,
|
|
|
|
$RuntimeModel->displayField => $field
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($associations)) {
|
|
|
|
$model->bindModel(array('hasMany' => $associations), $reset);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Unbind translation for fields, optionally unbinds hasMany association for
|
|
|
|
* fake field
|
|
|
|
*
|
2010-06-10 18:23:49 +00:00
|
|
|
* @param object $model instance of model
|
|
|
|
* @param mixed $fields string with field, or array(field1, field2=>AssocName, field3), or null for
|
|
|
|
* unbind all original translations
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2010-12-12 22:38:49 +00:00
|
|
|
function unbindTranslation($model, $fields = null) {
|
2010-02-12 03:03:43 +00:00
|
|
|
if (empty($fields) && empty($this->settings[$model->alias])) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($fields)) {
|
|
|
|
return $this->unbindTranslation($model, $this->settings[$model->alias]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($fields)) {
|
|
|
|
$fields = array($fields);
|
|
|
|
}
|
2010-12-12 22:38:49 +00:00
|
|
|
$RuntimeModel = $this->translateModel($model);
|
2008-05-30 11:40:08 +00:00
|
|
|
$associations = array();
|
|
|
|
|
|
|
|
foreach ($fields as $key => $value) {
|
|
|
|
if (is_numeric($key)) {
|
|
|
|
$field = $value;
|
|
|
|
$association = null;
|
|
|
|
} else {
|
|
|
|
$field = $key;
|
|
|
|
$association = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists($field, $this->settings[$model->alias])) {
|
|
|
|
unset($this->settings[$model->alias][$field]);
|
|
|
|
} elseif (in_array($field, $this->settings[$model->alias])) {
|
|
|
|
$this->settings[$model->alias] = array_merge(array_diff_assoc($this->settings[$model->alias], array($field)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists($field, $this->runtime[$model->alias]['fields'])) {
|
|
|
|
unset($this->runtime[$model->alias]['fields'][$field]);
|
|
|
|
} elseif (in_array($field, $this->runtime[$model->alias]['fields'])) {
|
|
|
|
$this->runtime[$model->alias]['fields'] = array_merge(array_diff_assoc($this->runtime[$model->alias]['fields'], array($field)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_null($association) && (isset($model->hasMany[$association]) || isset($model->__backAssociation['hasMany'][$association]))) {
|
|
|
|
$associations[] = $association;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($associations)) {
|
|
|
|
$model->unbindModel(array('hasMany' => $associations), false);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs.model.behaviors
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-12 22:38:49 +00:00
|
|
|
class I18nModel extends AppModel {
|
|
|
|
public $name = 'I18nModel';
|
|
|
|
public $useTable = 'i18n';
|
|
|
|
public $displayField = 'field';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|