2006-06-14 19:06:14 +00:00
|
|
|
<?php
|
2006-11-27 18:15:36 +00:00
|
|
|
/* SVN FILE: $Id$ */
|
2006-06-14 19:06:14 +00:00
|
|
|
/**
|
|
|
|
* Short description for file.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2007-02-02 10:39:45 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
2006-11-27 18:15:36 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2006-06-14 19:06:14 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2007-02-02 10:39:45 +00:00
|
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
2006-11-27 18:15:36 +00:00
|
|
|
* @package cake
|
2007-02-18 13:10:11 +00:00
|
|
|
* @subpackage cake.cake.libs.model.behaviors
|
|
|
|
* @since CakePHP(tm) v 1.2.0.4525
|
2006-11-27 18:15:36 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2006-06-14 19:06:14 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Short description for file.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
2006-11-27 18:15:36 +00:00
|
|
|
* @package cake
|
2007-02-18 13:10:11 +00:00
|
|
|
* @subpackage cake.cake.libs.model.behaviors
|
2006-06-14 19:06:14 +00:00
|
|
|
*/
|
2007-02-18 13:10:11 +00:00
|
|
|
class TranslateBehavior extends ModelBehavior {
|
2007-04-25 00:24:19 +00:00
|
|
|
/**
|
|
|
|
* Used for runtime configuration of model
|
|
|
|
*/
|
|
|
|
var $runtime = array();
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2007-02-18 13:10:11 +00:00
|
|
|
function setup(&$model, $config = array()) {
|
2007-04-25 00:24:19 +00:00
|
|
|
$db =& ConnectionManager::getDataSource($model->useDbConfig);
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!$db->connected) {
|
2007-09-18 04:16:04 +00:00
|
|
|
trigger_error('Datasource '.$model->useDbConfig.' for TranslateBehavior of model '.$model->name.' is not connected', E_USER_ERROR);
|
2007-04-25 00:24:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-09-18 04:16:04 +00:00
|
|
|
|
|
|
|
$this->settings[$model->name] = array();
|
|
|
|
$this->runtime[$model->name] = array('fields' => array());
|
|
|
|
$this->translateModel($model);
|
|
|
|
return $this->bindTranslation($model, null, false);
|
2007-02-18 13:10:11 +00:00
|
|
|
}
|
2007-04-25 00:24:19 +00:00
|
|
|
/**
|
|
|
|
* Callback
|
|
|
|
*/
|
2007-02-18 13:10:11 +00:00
|
|
|
function beforeFind(&$model, $query) {
|
2007-05-02 18:50:16 +00:00
|
|
|
$locale = $this->_getLocale($model);
|
2007-09-18 04:16:04 +00:00
|
|
|
$db =& ConnectionManager::getDataSource($model->useDbConfig);
|
|
|
|
$tablePrefix = $db->config['prefix'];
|
|
|
|
$RuntimeModel =& $this->translateModel($model);
|
2007-05-02 18:50:16 +00:00
|
|
|
|
2007-09-18 04:16:04 +00:00
|
|
|
if (is_string($query['fields']) && 'COUNT(*) AS '.$db->name('count') == $query['fields']) {
|
2007-04-25 00:24:19 +00:00
|
|
|
$this->runtime[$model->name]['count'] = true;
|
2007-05-02 18:50:16 +00:00
|
|
|
|
2007-09-18 04:16:04 +00:00
|
|
|
if (empty($locale)) {
|
|
|
|
return $query;
|
|
|
|
}
|
2007-05-02 18:50:16 +00:00
|
|
|
$query['fields'] = 'COUNT(DISTINCT('.$db->name($model->name).'.'.$db->name($model->primaryKey).')) ' . $db->alias . 'count';
|
|
|
|
$query['joins'][] = array(
|
2007-09-18 04:16:04 +00:00
|
|
|
'type' => 'INNER',
|
|
|
|
'alias' => $RuntimeModel->name,
|
|
|
|
'table' => $db->name($tablePrefix . 'i18n'),
|
|
|
|
'conditions' => array(
|
|
|
|
$model->name.'.id' => '{$__cakeIdentifier['.$RuntimeModel->name.'.foreign_key]__$}',
|
|
|
|
$RuntimeModel->name.'.model' => $model->name,
|
|
|
|
$RuntimeModel->name.'.locale' => $locale));
|
2007-04-25 00:24:19 +00:00
|
|
|
return $query;
|
|
|
|
}
|
2007-02-18 13:10:11 +00:00
|
|
|
|
2007-09-18 04:16:04 +00:00
|
|
|
if (empty($locale)) {
|
2007-04-25 00:24:19 +00:00
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
$autoFields = false;
|
2006-06-14 19:06:14 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (empty($query['fields'])) {
|
2007-04-25 00:24:19 +00:00
|
|
|
$query['fields'] = array($model->name.'.*');
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach (array('hasOne', 'belongsTo') as $type) {
|
|
|
|
foreach ($model->{$type} as $key => $value) {
|
2007-04-25 00:24:19 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (empty($value['fields'])) {
|
2007-04-25 00:24:19 +00:00
|
|
|
$query['fields'][] = $key.'.*';
|
|
|
|
} else {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($value['fields'] as $field) {
|
2007-04-25 00:24:19 +00:00
|
|
|
$query['fields'][] = $key.'.'.$field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$autoFields = true;
|
|
|
|
}
|
|
|
|
$fields = am($this->settings[$model->name], $this->runtime[$model->name]['fields']);
|
|
|
|
$addFields = array();
|
2007-06-20 06:15:35 +00:00
|
|
|
if (is_array($query['fields'])) {
|
2007-09-18 04:16:04 +00:00
|
|
|
foreach ($fields as $key => $value) {
|
|
|
|
$field = ife(is_numeric($key), $value, $key);
|
|
|
|
|
|
|
|
if (in_array($model->name.'.*', $query['fields']) || $autoFields || in_array($model->name.'.'.$field, $query['fields']) || in_array($field, $query['fields'])) {
|
|
|
|
$addFields[] = $field;
|
2007-04-25 00:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($addFields) {
|
|
|
|
foreach ($addFields as $field) {
|
2007-09-18 04:16:04 +00:00
|
|
|
foreach (array($field, $model->name.'.'.$field) as $_field) {
|
|
|
|
$key = array_search($_field, $query['fields']);
|
2007-04-25 00:24:19 +00:00
|
|
|
|
2007-09-18 04:16:04 +00:00
|
|
|
if ($key !== false) {
|
|
|
|
unset($query['fields'][$key]);
|
|
|
|
}
|
|
|
|
}
|
2007-04-25 00:24:19 +00:00
|
|
|
|
2007-09-18 04:16:04 +00:00
|
|
|
if (is_array($locale)) {
|
|
|
|
foreach ($locale as $_locale) {
|
|
|
|
$query['fields'][] = 'I18n__'.$field.'__'.$_locale.'.content';
|
|
|
|
$query['joins'][] = array(
|
|
|
|
'type' => 'LEFT',
|
|
|
|
'alias' => 'I18n__'.$field.'__'.$_locale,
|
|
|
|
'table' => $db->name($tablePrefix . 'i18n'),
|
|
|
|
'conditions' => array(
|
|
|
|
$model->name.'.id' => '{$__cakeIdentifier[I18n__'.$field.'__'.$_locale.'.foreign_key]__$}',
|
|
|
|
'I18n__'.$field.'__'.$_locale.'.model' => $model->name,
|
|
|
|
'I18n__'.$field.'__'.$_locale.'.'.$RuntimeModel->displayField => $field,
|
|
|
|
'I18n__'.$field.'__'.$_locale.'.locale' => $_locale));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$query['fields'][] = 'I18n__'.$field.'.content';
|
|
|
|
$query['joins'][] = array(
|
|
|
|
'type' => 'LEFT',
|
|
|
|
'alias' => 'I18n__'.$field,
|
|
|
|
'table' => $db->name($tablePrefix . 'i18n'),
|
|
|
|
'conditions' => array(
|
|
|
|
$model->name.'.id' => '{$__cakeIdentifier[I18n__'.$field.'.foreign_key]__$}',
|
|
|
|
'I18n__'.$field.'.model' => $model->name,
|
|
|
|
'I18n__'.$field.'.'.$RuntimeModel->displayField => $field));
|
|
|
|
$query['conditions'][$db->name('I18n__'.$field.'.locale')] = $locale;
|
|
|
|
}
|
2007-04-25 00:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$query['fields'] = am($query['fields']);
|
|
|
|
$this->runtime[$model->name]['beforeFind'] = $addFields;
|
|
|
|
return $query;
|
2006-11-27 18:15:36 +00:00
|
|
|
}
|
2007-04-25 00:24:19 +00:00
|
|
|
/**
|
|
|
|
* Callback
|
|
|
|
*/
|
|
|
|
function afterFind(&$model, $results, $primary) {
|
|
|
|
$this->runtime[$model->name]['fields'] = array();
|
|
|
|
$locale = $this->_getLocale($model);
|
2006-06-14 19:06:14 +00:00
|
|
|
|
2007-09-18 04:16:04 +00:00
|
|
|
if (empty($locale) || empty($results) || empty($this->runtime[$model->name]['beforeFind'])) {
|
2007-04-25 00:24:19 +00:00
|
|
|
return $results;
|
|
|
|
}
|
2007-09-18 04:16:04 +00:00
|
|
|
$beforeFind = $this->runtime[$model->name]['beforeFind'];
|
2007-02-18 13:10:11 +00:00
|
|
|
|
2007-09-18 04:16:04 +00:00
|
|
|
foreach ($results as $key => $row) {
|
|
|
|
$results[$key][$model->name]['locale'] = ife(is_array($locale), @$locale[0], $locale);
|
2007-04-25 00:24:19 +00:00
|
|
|
|
2007-09-18 04:16:04 +00:00
|
|
|
foreach ($beforeFind as $field) {
|
|
|
|
if (is_array($locale)) {
|
|
|
|
foreach ($locale as $_locale) {
|
|
|
|
if (!isset($results[$key][$model->name][$field]) && !empty($results[$key]['I18n__'.$field.'__'.$_locale]['content'])) {
|
|
|
|
$results[$key][$model->name][$field] = $results[$key]['I18n__'.$field.'__'.$_locale]['content'];
|
|
|
|
}
|
|
|
|
unset($results[$key]['I18n__'.$field.'__'.$_locale]);
|
|
|
|
}
|
2007-04-25 00:24:19 +00:00
|
|
|
|
2007-09-18 04:16:04 +00:00
|
|
|
if (!isset($results[$key][$model->name][$field])) {
|
|
|
|
$results[$key][$model->name][$field] = '';
|
|
|
|
}
|
|
|
|
} else {
|
2007-04-25 00:24:19 +00:00
|
|
|
$value = ife(empty($results[$key]['I18n__'.$field]['content']), '', $results[$key]['I18n__'.$field]['content']);
|
|
|
|
$results[$key][$model->name][$field] = $value;
|
|
|
|
unset($results[$key]['I18n__'.$field]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $results;
|
2007-09-18 04:16:04 +00:00
|
|
|
}
|
2007-04-25 00:24:19 +00:00
|
|
|
/**
|
|
|
|
* Callback
|
|
|
|
*/
|
|
|
|
function beforeSave(&$model) {
|
|
|
|
$locale = $this->_getLocale($model);
|
2006-06-14 19:06:14 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (empty($locale) || is_array($locale)) {
|
2007-04-25 00:24:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$fields = am($this->settings[$model->name], $this->runtime[$model->name]['fields']);
|
|
|
|
$tempData = array();
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($fields as $key => $value) {
|
2007-04-25 00:24:19 +00:00
|
|
|
$field = ife(is_numeric($key), $value, $key);
|
2007-02-18 13:10:11 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (isset($model->data[$model->name][$field])) {
|
2007-04-25 00:24:19 +00:00
|
|
|
$tempData[$field] = $model->data[$model->name][$field];
|
|
|
|
unset($model->data[$model->name][$field]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->runtime[$model->name]['beforeSave'] = $tempData;
|
|
|
|
return true;
|
2006-11-27 18:15:36 +00:00
|
|
|
}
|
2007-04-25 00:24:19 +00:00
|
|
|
/**
|
|
|
|
* Callback
|
|
|
|
*/
|
|
|
|
function afterSave(&$model, $created) {
|
2007-09-18 04:16:04 +00:00
|
|
|
if (!isset($this->runtime[$model->name]['beforeSave'])) {
|
2007-04-25 00:24:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-09-18 04:16:04 +00:00
|
|
|
$locale = $this->_getLocale($model);
|
2007-04-25 00:24:19 +00:00
|
|
|
$tempData = $this->runtime[$model->name]['beforeSave'];
|
|
|
|
unset($this->runtime[$model->name]['beforeSave']);
|
2007-09-18 04:16:04 +00:00
|
|
|
$conditions = array('locale' => $locale, 'model' => $model->name, 'foreign_key' => $model->id);
|
|
|
|
$RuntimeModel =& $this->translateModel($model);
|
2007-04-25 00:24:19 +00:00
|
|
|
|
2007-09-18 04:16:04 +00:00
|
|
|
if (empty($created)) {
|
|
|
|
$translations = $RuntimeModel->generateList(am($conditions, array($RuntimeModel->displayField => array_keys($tempData))));
|
2007-04-25 00:24:19 +00:00
|
|
|
|
2007-09-18 04:16:04 +00:00
|
|
|
if ($translations) {
|
|
|
|
foreach ($translations as $id => $field) {
|
|
|
|
$RuntimeModel->create();
|
|
|
|
$RuntimeModel->save(array($RuntimeModel->name => array('id' => $id, 'content' => $tempData[$field])));
|
|
|
|
unset($tempData[$field]);
|
2007-04-25 00:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-18 04:16:04 +00:00
|
|
|
|
|
|
|
if (!empty($tempData)) {
|
|
|
|
foreach ($tempData as $field => $value) {
|
|
|
|
$RuntimeModel->create(am($conditions, array($RuntimeModel->displayField => $field, 'content' => $value)));
|
|
|
|
$RuntimeModel->save();
|
|
|
|
}
|
|
|
|
}
|
2007-04-25 00:24:19 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Callback
|
|
|
|
*/
|
|
|
|
function afterDelete(&$model) {
|
2007-09-18 04:16:04 +00:00
|
|
|
$RuntimeModel =& $this->translateModel($model);
|
|
|
|
$conditions = array('model' => $model->name, 'foreign_key' => $model->id);
|
|
|
|
$RuntimeModel->deleteAll($conditions);
|
2007-04-25 00:24:19 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get selected locale for model
|
|
|
|
*
|
|
|
|
* @return mixed string or false
|
|
|
|
*/
|
|
|
|
function _getLocale(&$model) {
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!isset($model->locale) || is_null($model->locale)) {
|
2007-10-20 05:55:37 +00:00
|
|
|
if (!class_exists('I18n')) {
|
|
|
|
uses('i18n');
|
|
|
|
}
|
2007-09-18 04:16:04 +00:00
|
|
|
$I18n =& I18n::getInstance();
|
|
|
|
$model->locale = $I18n->l10n->locale;
|
2007-04-25 00:24:19 +00:00
|
|
|
}
|
|
|
|
return $model->locale;
|
2006-11-27 18:15:36 +00:00
|
|
|
}
|
2007-09-18 04:16:04 +00:00
|
|
|
/**
|
|
|
|
* Get instance of model for translations
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
function &translateModel(&$model) {
|
|
|
|
if (!isset($this->runtime[$model->name]['model'])) {
|
|
|
|
if (!isset($model->translateModel) || empty($model->translateModel)) {
|
|
|
|
$className = 'I18nModel';
|
|
|
|
} else {
|
|
|
|
$className = $model->translateModel;
|
|
|
|
if (!class_exists($className) && !loadModel($className)) {
|
|
|
|
return $this->cakeError('missingModel', array(array('className' => $className)));
|
|
|
|
}
|
|
|
|
}
|
2006-06-14 19:06:14 +00:00
|
|
|
|
2007-09-18 04:16:04 +00:00
|
|
|
if (ClassRegistry::isKeySet($className)) {
|
|
|
|
if (PHP5) {
|
|
|
|
$this->runtime[$model->name]['model'] = ClassRegistry::getObject($className);
|
|
|
|
} else {
|
|
|
|
$this->runtime[$model->name]['model'] =& ClassRegistry::getObject($className);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (PHP5) {
|
|
|
|
$this->runtime[$model->name]['model'] = new $className();
|
|
|
|
} else {
|
|
|
|
$this->runtime[$model->name]['model'] =& new $className();
|
|
|
|
}
|
|
|
|
ClassRegistry::addObject($className, $this->runtime[$model->name]['model']);
|
|
|
|
ClassRegistry::map($className, $className);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->runtime[$model->name]['model'];
|
|
|
|
}
|
2007-04-25 00:24:19 +00:00
|
|
|
/**
|
|
|
|
* Bind translation for fields, optionally with hasMany association for
|
|
|
|
* fake field
|
|
|
|
*
|
|
|
|
* @param object instance of model
|
2007-09-18 04:16:04 +00:00
|
|
|
* @param mixed string with field, or array(field1, field2=>AssocName, field3), or null for bind all original translations
|
2007-04-25 00:24:19 +00:00
|
|
|
* @param boolead $reset
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2007-09-18 04:16:04 +00:00
|
|
|
function bindTranslation(&$model, $fields = null, $reset = true) {
|
2007-06-20 06:15:35 +00:00
|
|
|
if (empty($fields)) {
|
2007-09-18 04:16:04 +00:00
|
|
|
return $this->bindTranslation($model, $model->actsAs['Translate'], $reset);
|
2007-04-25 00:24:19 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (is_string($fields)) {
|
2007-04-25 00:24:19 +00:00
|
|
|
$fields = array($fields);
|
|
|
|
}
|
|
|
|
$associations = array();
|
2007-09-18 04:16:04 +00:00
|
|
|
$RuntimeModel =& $this->translateModel($model);
|
|
|
|
$default = array('className' => $RuntimeModel->name, 'foreignKey' => 'foreign_key');
|
2007-04-25 00:24:19 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($fields as $key => $value) {
|
|
|
|
if (is_numeric($key)) {
|
2007-04-25 00:24:19 +00:00
|
|
|
$field = $value;
|
|
|
|
$association = null;
|
|
|
|
} else {
|
|
|
|
$field = $key;
|
|
|
|
$association = $value;
|
|
|
|
}
|
|
|
|
|
2007-09-18 04:16:04 +00:00
|
|
|
if (array_key_exists($field, $this->settings[$model->name])) {
|
|
|
|
unset($this->settings[$model->name][$field]);
|
|
|
|
|
|
|
|
} elseif (in_array($field, $this->settings[$model->name])) {
|
|
|
|
$this->settings[$model->name] = am(array_diff_assoc($this->settings[$model->name], array($field)));
|
2007-04-25 00:24:19 +00:00
|
|
|
}
|
|
|
|
|
2007-09-18 04:16:04 +00:00
|
|
|
if (array_key_exists($field, $this->runtime[$model->name]['fields'])) {
|
|
|
|
unset($this->runtime[$model->name]['fields'][$field]);
|
|
|
|
|
|
|
|
} elseif (in_array($field, $this->runtime[$model->name]['fields'])) {
|
|
|
|
$this->runtime[$model->name]['fields'] = am(array_diff_assoc($this->runtime[$model->name]['fields'], array($field)));
|
2007-04-25 00:24:19 +00:00
|
|
|
}
|
2006-06-14 19:06:14 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (is_null($association)) {
|
|
|
|
if ($reset) {
|
2007-09-18 04:16:04 +00:00
|
|
|
$this->runtime[$model->name]['fields'][] = $field;
|
2007-04-25 00:24:19 +00:00
|
|
|
} else {
|
2007-09-18 04:16:04 +00:00
|
|
|
$this->settings[$model->name][] = $field;
|
2007-04-25 00:24:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2007-09-18 04:16:04 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($reset) {
|
2007-09-18 04:16:04 +00:00
|
|
|
$this->runtime[$model->name]['fields'][$field] = $association;
|
2007-04-25 00:24:19 +00:00
|
|
|
} else {
|
2007-09-18 04:16:04 +00:00
|
|
|
$this->settings[$model->name][$field] = $association;
|
2007-04-25 00:24:19 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach (array('hasOne', 'hasMany', 'belongsTo', 'hasAndBelongsToMany') as $type) {
|
|
|
|
if (isset($model->{$type}[$association]) || isset($model->__backAssociation[$type][$association])) {
|
2007-04-25 00:24:19 +00:00
|
|
|
trigger_error('Association '.$association.' is already binded to model '.$model->name, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$associations[$association] = am($default, array('conditions' => array(
|
2007-09-18 04:16:04 +00:00
|
|
|
'model' => $model->name,
|
|
|
|
$RuntimeModel->displayField => $field)));
|
2007-04-25 00:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!empty($associations)) {
|
2007-04-25 00:24:19 +00:00
|
|
|
$model->bindModel(array('hasMany' => $associations), $reset);
|
|
|
|
}
|
|
|
|
return true;
|
2007-02-18 13:10:11 +00:00
|
|
|
}
|
2007-04-25 00:24:19 +00:00
|
|
|
/**
|
|
|
|
* Unbind translation for fields, optionally unbinds hasMany association for
|
|
|
|
* fake field
|
|
|
|
*
|
|
|
|
* @param object instance of model
|
2007-09-18 04:16:04 +00:00
|
|
|
* @param mixed string with field, or array(field1, field2=>AssocName, field3), or null for unbind all original translations
|
2007-04-25 00:24:19 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2007-09-18 04:16:04 +00:00
|
|
|
function unbindTranslation(&$model, $fields = null) {
|
2007-06-20 06:15:35 +00:00
|
|
|
if (empty($fields)) {
|
2007-09-18 04:16:04 +00:00
|
|
|
return $this->unbindTranslation($model, $model->actsAs['Translate']);
|
2007-04-25 00:24:19 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (is_string($fields)) {
|
2007-04-25 00:24:19 +00:00
|
|
|
$fields = array($fields);
|
|
|
|
}
|
2007-09-18 04:16:04 +00:00
|
|
|
$RuntimeModel =& $this->translateModel($model);
|
|
|
|
$default = array('className' => $RuntimeModel->name, 'foreignKey' => 'foreign_key');
|
2007-04-25 00:24:19 +00:00
|
|
|
$associations = array();
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($fields as $key => $value) {
|
|
|
|
if (is_numeric($key)) {
|
2007-04-25 00:24:19 +00:00
|
|
|
$field = $value;
|
|
|
|
$association = null;
|
|
|
|
} else {
|
|
|
|
$field = $key;
|
|
|
|
$association = $value;
|
|
|
|
}
|
|
|
|
|
2007-09-18 04:16:04 +00:00
|
|
|
if (array_key_exists($field, $this->settings[$model->name])) {
|
|
|
|
unset($this->settings[$model->name][$field]);
|
|
|
|
|
|
|
|
} elseif (in_array($field, $this->settings[$model->name])) {
|
|
|
|
$this->settings[$model->name] = am(array_diff_assoc($this->settings[$model->name], array($field)));
|
2007-04-25 00:24:19 +00:00
|
|
|
}
|
|
|
|
|
2007-09-18 04:16:04 +00:00
|
|
|
if (array_key_exists($field, $this->runtime[$model->name]['fields'])) {
|
|
|
|
unset($this->runtime[$model->name]['fields'][$field]);
|
|
|
|
|
|
|
|
} elseif (in_array($field, $this->runtime[$model->name]['fields'])) {
|
|
|
|
$this->runtime[$model->name]['fields'] = am(array_diff_assoc($this->runtime[$model->name]['fields'], array($field)));
|
2007-04-25 00:24:19 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!is_null($association) && (isset($model->hasMany[$association]) || isset($model->__backAssociation['hasMany'][$association]))) {
|
2007-04-25 00:24:19 +00:00
|
|
|
$associations[] = $association;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!empty($associations)) {
|
2007-04-25 00:24:19 +00:00
|
|
|
$model->unbindModel(array('hasMany' => $associations), false);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2007-09-18 04:16:04 +00:00
|
|
|
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
|
2007-04-25 00:24:19 +00:00
|
|
|
/**
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model.behaviors
|
|
|
|
*/
|
2007-09-18 04:16:04 +00:00
|
|
|
class I18nModel extends AppModel {
|
|
|
|
var $name = 'I18nModel';
|
|
|
|
var $useTable = 'i18n';
|
|
|
|
var $displayField = 'field';
|
|
|
|
}
|
2006-06-14 19:06:14 +00:00
|
|
|
}
|
2007-07-25 04:38:28 +00:00
|
|
|
?>
|