2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model.Behavior
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.4525
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-07-21 18:29:49 +00:00
|
|
|
App::uses('ModelBehavior', 'Model');
|
2010-12-15 05:43:05 +00:00
|
|
|
App::uses('I18n', 'I18n');
|
2012-01-29 18:27:05 +00:00
|
|
|
App::uses('I18nModel', 'Model');
|
2010-12-03 23:07:21 +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
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model.Behavior
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-libraries/behaviors/translate.html
|
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
|
2011-05-28 20:38:46 +00:00
|
|
|
*
|
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
|
|
|
|
2012-01-08 17:28:22 +00:00
|
|
|
/**
|
|
|
|
* Stores the joinTable object for generating joins.
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
2012-02-23 13:45:15 +00:00
|
|
|
protected $_joinTable;
|
2012-01-08 17:28:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores the runtime model for generating joins.
|
|
|
|
*
|
|
|
|
* @var Model
|
|
|
|
*/
|
2012-02-23 13:45:15 +00:00
|
|
|
protected $_runtimeModel;
|
2012-01-08 17:28:22 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Callback
|
|
|
|
*
|
|
|
|
* $config for TranslateBehavior should be
|
2011-12-01 07:21:31 +00:00
|
|
|
* array('fields' => array('field_one',
|
2008-05-30 11:40:08 +00:00
|
|
|
* '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
|
|
|
*
|
2017-06-12 19:41:35 +00:00
|
|
|
* By default INNER joins are used to fetch translations. In order to use
|
2017-06-26 21:02:06 +00:00
|
|
|
* other join types $config should contain 'joinType' key:
|
2017-06-12 19:41:35 +00:00
|
|
|
* ```
|
|
|
|
* array(
|
|
|
|
* 'fields' => array('field_one', 'field_two' => 'FieldAssoc', 'field_three'),
|
2017-06-26 21:02:06 +00:00
|
|
|
* 'joinType' => 'LEFT',
|
2017-06-12 19:41:35 +00:00
|
|
|
* )
|
|
|
|
* ```
|
|
|
|
* In a model it may be configured this way:
|
|
|
|
* ```
|
|
|
|
* public $actsAs = array(
|
|
|
|
* 'Translate' => array(
|
|
|
|
* 'content',
|
|
|
|
* 'title',
|
2017-06-26 21:02:06 +00:00
|
|
|
* 'joinType' => 'LEFT',
|
2017-06-12 19:41:35 +00:00
|
|
|
* ),
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*
|
2012-07-21 20:37:14 +00:00
|
|
|
* @param Model $Model Model the behavior is being attached to.
|
2010-06-10 18:23:49 +00:00
|
|
|
* @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
|
|
|
*/
|
2012-07-21 20:37:14 +00:00
|
|
|
public function setup(Model $Model, $config = array()) {
|
|
|
|
$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(
|
2012-07-21 20:37:14 +00:00
|
|
|
__d('cake_dev', '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;
|
|
|
|
}
|
|
|
|
|
2012-07-21 20:37:14 +00:00
|
|
|
$this->settings[$Model->alias] = array();
|
2017-06-26 21:02:06 +00:00
|
|
|
$this->runtime[$Model->alias] = array(
|
2017-06-12 19:41:35 +00:00
|
|
|
'fields' => array(),
|
2017-06-26 21:02:06 +00:00
|
|
|
'joinType' => 'INNER',
|
2017-06-12 19:41:35 +00:00
|
|
|
);
|
2017-06-26 21:02:06 +00:00
|
|
|
if (isset($config['joinType'])) {
|
|
|
|
$this->runtime[$Model->alias]['joinType'] = $config['joinType'];
|
|
|
|
unset($config['joinType']);
|
2017-06-12 19:41:35 +00:00
|
|
|
}
|
2012-07-21 20:37:14 +00:00
|
|
|
$this->translateModel($Model);
|
|
|
|
return $this->bindTranslation($Model, $config, false);
|
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-06-10 18:23:49 +00:00
|
|
|
* Cleanup Callback unbinds bound translations and deletes setting information.
|
2008-11-03 23:58:44 +00:00
|
|
|
*
|
2012-07-21 20:37:14 +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
|
|
|
*/
|
2012-07-21 20:37:14 +00:00
|
|
|
public function cleanup(Model $Model) {
|
|
|
|
$this->unbindTranslation($Model);
|
|
|
|
unset($this->settings[$Model->alias]);
|
|
|
|
unset($this->runtime[$Model->alias]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
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
|
|
|
*
|
2012-07-21 20:37:14 +00:00
|
|
|
* @param Model $Model Model find is being run on.
|
2010-06-10 18:23:49 +00:00
|
|
|
* @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
|
|
|
*/
|
2012-07-21 20:37:14 +00:00
|
|
|
public function beforeFind(Model $Model, $query) {
|
|
|
|
$this->runtime[$Model->alias]['virtualFields'] = $Model->virtualFields;
|
|
|
|
$locale = $this->_getLocale($Model);
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($locale)) {
|
|
|
|
return $query;
|
|
|
|
}
|
2012-07-21 20:37:14 +00:00
|
|
|
$db = $Model->getDataSource();
|
|
|
|
$RuntimeModel = $this->translateModel($Model);
|
2012-01-08 17:28:22 +00:00
|
|
|
|
2010-01-24 22:57:08 +00:00
|
|
|
if (!empty($RuntimeModel->tablePrefix)) {
|
|
|
|
$tablePrefix = $RuntimeModel->tablePrefix;
|
|
|
|
} else {
|
|
|
|
$tablePrefix = $db->config['prefix'];
|
|
|
|
}
|
2011-10-07 00:34:23 +00:00
|
|
|
$joinTable = new StdClass();
|
|
|
|
$joinTable->tablePrefix = $tablePrefix;
|
|
|
|
$joinTable->table = $RuntimeModel->table;
|
2011-11-07 09:12:57 +00:00
|
|
|
$joinTable->schemaName = $RuntimeModel->getDataSource()->getSchemaName();
|
2011-02-24 06:01:03 +00:00
|
|
|
|
2012-01-08 17:28:22 +00:00
|
|
|
$this->_joinTable = $joinTable;
|
|
|
|
$this->_runtimeModel = $RuntimeModel;
|
|
|
|
|
2017-07-06 23:20:09 +00:00
|
|
|
if (is_string($query['fields'])) {
|
|
|
|
if ($query['fields'] === "COUNT(*) AS {$db->name('count')}") {
|
|
|
|
$query['fields'] = "COUNT(DISTINCT({$db->name($Model->escapeField())})) {$db->alias}count";
|
|
|
|
$query['joins'][] = array(
|
|
|
|
'type' => $this->runtime[$Model->alias]['joinType'],
|
|
|
|
'alias' => $RuntimeModel->alias,
|
|
|
|
'table' => $joinTable,
|
|
|
|
'conditions' => array(
|
|
|
|
$Model->escapeField() => $db->identifier($RuntimeModel->escapeField('foreign_key')),
|
|
|
|
$RuntimeModel->escapeField('model') => $Model->name,
|
|
|
|
$RuntimeModel->escapeField('locale') => $locale
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$conditionFields = $this->_checkConditions($Model, $query);
|
|
|
|
foreach ($conditionFields as $field) {
|
|
|
|
$query = $this->_addJoin($Model, $query, $field, $field, $locale);
|
|
|
|
}
|
|
|
|
unset($this->_joinTable, $this->_runtimeModel);
|
|
|
|
return $query;
|
|
|
|
} else {
|
|
|
|
$query['fields'] = CakeText::tokenize($query['fields']);
|
2012-01-08 17:28:22 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2017-07-06 23:20:09 +00:00
|
|
|
$addFields = $this->_getFields($Model, $query);
|
|
|
|
$this->runtime[$Model->alias]['virtualFields'] = $Model->virtualFields;
|
|
|
|
$query = $this->_addAllJoins($Model, $query, $addFields);
|
|
|
|
$this->runtime[$Model->alias]['beforeFind'] = $addFields;
|
|
|
|
unset($this->_joinTable, $this->_runtimeModel);
|
|
|
|
return $query;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2017-07-06 23:20:09 +00:00
|
|
|
/**
|
|
|
|
* Gets fields to be retrieved.
|
|
|
|
*
|
|
|
|
* @param Model $Model The model being worked on.
|
|
|
|
* @param array $query The query array to take fields from.
|
|
|
|
* @return array The fields.
|
|
|
|
*/
|
|
|
|
protected function _getFields(Model $Model, $query) {
|
2012-09-15 02:06:41 +00:00
|
|
|
$fields = array_merge(
|
|
|
|
$this->settings[$Model->alias],
|
|
|
|
$this->runtime[$Model->alias]['fields']
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
$addFields = array();
|
2010-10-26 04:10:24 +00:00
|
|
|
if (empty($query['fields'])) {
|
|
|
|
$addFields = $fields;
|
2012-02-23 13:38:02 +00:00
|
|
|
} elseif (is_array($query['fields'])) {
|
2012-09-26 01:01:19 +00:00
|
|
|
$isAllFields = (
|
|
|
|
in_array($Model->alias . '.' . '*', $query['fields']) ||
|
|
|
|
in_array($Model->escapeField('*'), $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;
|
2014-12-09 02:17:35 +00:00
|
|
|
if ($isAllFields ||
|
2012-09-15 02:06:41 +00:00
|
|
|
in_array($Model->alias . '.' . $field, $query['fields']) ||
|
|
|
|
in_array($field, $query['fields'])
|
|
|
|
) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$addFields[] = $field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-06 23:20:09 +00:00
|
|
|
return $addFields;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2017-07-06 23:20:09 +00:00
|
|
|
/**
|
|
|
|
* Appends all necessary joins for translated fields.
|
|
|
|
*
|
|
|
|
* @param Model $Model The model being worked on.
|
|
|
|
* @param array $query The query array to append joins to.
|
2017-07-09 17:24:51 +00:00
|
|
|
* @param array $addFields The fields being joined.
|
2017-07-06 23:20:09 +00:00
|
|
|
* @return array The modified query
|
|
|
|
*/
|
|
|
|
protected function _addAllJoins(Model $Model, $query, $addFields) {
|
|
|
|
$locale = $this->_getLocale($Model);
|
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;
|
2012-07-21 20:37:14 +00:00
|
|
|
foreach (array($aliasField, $Model->alias . '.' . $aliasField) as $_field) {
|
2010-10-26 22:20:37 +00:00
|
|
|
$key = array_search($_field, (array)$query['fields']);
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($key !== false) {
|
|
|
|
unset($query['fields'][$key]);
|
|
|
|
}
|
|
|
|
}
|
2012-07-21 20:37:14 +00:00
|
|
|
$query = $this->_addJoin($Model, $query, $field, $aliasField, $locale);
|
2012-01-08 17:28:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $query;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-01-08 17:28:22 +00:00
|
|
|
/**
|
|
|
|
* Check a query's conditions for translated fields.
|
|
|
|
* Return an array of translated fields found in the conditions.
|
|
|
|
*
|
2012-07-21 20:37:14 +00:00
|
|
|
* @param Model $Model The model being read.
|
2012-01-08 17:28:22 +00:00
|
|
|
* @param array $query The query array.
|
|
|
|
* @return array The list of translated fields that are in the conditions.
|
|
|
|
*/
|
2012-07-21 20:37:14 +00:00
|
|
|
protected function _checkConditions(Model $Model, $query) {
|
2012-09-19 23:50:15 +00:00
|
|
|
if (empty($query['conditions']) || (!empty($query['conditions']) && !is_array($query['conditions']))) {
|
2017-07-08 14:51:32 +00:00
|
|
|
return array();
|
2012-01-08 17:28:22 +00:00
|
|
|
}
|
2017-07-08 14:51:32 +00:00
|
|
|
return $this->_getConditionFields($Model, $query['conditions']);
|
|
|
|
}
|
|
|
|
|
2017-07-09 18:05:53 +00:00
|
|
|
/**
|
|
|
|
* Extracts condition field names recursively.
|
|
|
|
*
|
|
|
|
* @param Model $Model The model being read.
|
|
|
|
* @param array $conditions The conditions array.
|
|
|
|
* @return array The list of condition fields.
|
|
|
|
*/
|
2017-07-08 14:51:32 +00:00
|
|
|
protected function _getConditionFields(Model $Model, $conditions) {
|
|
|
|
$conditionFields = array();
|
|
|
|
foreach ($conditions as $col => $val) {
|
|
|
|
if (is_array($val)) {
|
|
|
|
$subConditionFields = $this->_getConditionFields($Model, $val);
|
|
|
|
$conditionFields = array_merge($conditionFields, $subConditionFields);
|
|
|
|
}
|
2012-07-21 20:37:14 +00:00
|
|
|
foreach ($this->settings[$Model->alias] as $field => $assoc) {
|
2012-01-08 17:28:22 +00:00
|
|
|
if (is_numeric($field)) {
|
|
|
|
$field = $assoc;
|
|
|
|
}
|
|
|
|
if (strpos($col, $field) !== false) {
|
|
|
|
$conditionFields[] = $field;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-08 17:28:22 +00:00
|
|
|
return $conditionFields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-21 20:37:14 +00:00
|
|
|
* Appends a join for translated fields.
|
2012-01-08 17:28:22 +00:00
|
|
|
*
|
2012-07-21 20:37:14 +00:00
|
|
|
* @param Model $Model The model being worked on.
|
2012-01-08 17:28:22 +00:00
|
|
|
* @param array $query The query array to append a join to.
|
|
|
|
* @param string $field The field name being joined.
|
2012-01-09 02:40:12 +00:00
|
|
|
* @param string $aliasField The aliased field name being joined.
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $locale The locale(s) having joins added.
|
2013-03-05 07:05:14 +00:00
|
|
|
* @return array The modified query
|
2012-01-08 17:28:22 +00:00
|
|
|
*/
|
2012-07-21 20:37:14 +00:00
|
|
|
protected function _addJoin(Model $Model, $query, $field, $aliasField, $locale) {
|
|
|
|
$db = ConnectionManager::getDataSource($Model->useDbConfig);
|
2012-01-08 17:28:22 +00:00
|
|
|
$RuntimeModel = $this->_runtimeModel;
|
|
|
|
$joinTable = $this->_joinTable;
|
2012-07-21 20:37:14 +00:00
|
|
|
$aliasVirtual = "i18n_{$field}";
|
|
|
|
$alias = "I18n__{$field}";
|
2012-01-08 17:28:22 +00:00
|
|
|
if (is_array($locale)) {
|
|
|
|
foreach ($locale as $_locale) {
|
2012-07-21 20:37:14 +00:00
|
|
|
$aliasVirtualLocale = "{$aliasVirtual}_{$_locale}";
|
|
|
|
$aliasLocale = "{$alias}__{$_locale}";
|
|
|
|
$Model->virtualFields[$aliasVirtualLocale] = "{$aliasLocale}.content";
|
2012-01-08 17:28:22 +00:00
|
|
|
if (!empty($query['fields']) && is_array($query['fields'])) {
|
2012-07-21 20:37:14 +00:00
|
|
|
$query['fields'][] = $aliasVirtualLocale;
|
2012-01-08 17:28:22 +00:00
|
|
|
}
|
|
|
|
$query['joins'][] = array(
|
|
|
|
'type' => 'LEFT',
|
2012-07-21 20:37:14 +00:00
|
|
|
'alias' => $aliasLocale,
|
2012-01-08 17:28:22 +00:00
|
|
|
'table' => $joinTable,
|
|
|
|
'conditions' => array(
|
2012-07-21 20:37:14 +00:00
|
|
|
$Model->escapeField() => $db->identifier("{$aliasLocale}.foreign_key"),
|
|
|
|
"{$aliasLocale}.model" => $Model->name,
|
|
|
|
"{$aliasLocale}.{$RuntimeModel->displayField}" => $aliasField,
|
|
|
|
"{$aliasLocale}.locale" => $_locale
|
2012-01-08 17:28:22 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2012-07-21 20:37:14 +00:00
|
|
|
$Model->virtualFields[$aliasVirtual] = "{$alias}.content";
|
2012-01-08 17:28:22 +00:00
|
|
|
if (!empty($query['fields']) && is_array($query['fields'])) {
|
2012-07-21 20:37:14 +00:00
|
|
|
$query['fields'][] = $aliasVirtual;
|
2012-01-08 17:28:22 +00:00
|
|
|
}
|
|
|
|
$query['joins'][] = array(
|
2017-06-26 21:02:06 +00:00
|
|
|
'type' => $this->runtime[$Model->alias]['joinType'],
|
2012-07-21 20:37:14 +00:00
|
|
|
'alias' => $alias,
|
2012-01-08 17:28:22 +00:00
|
|
|
'table' => $joinTable,
|
|
|
|
'conditions' => array(
|
2012-07-21 20:37:14 +00:00
|
|
|
"{$Model->alias}.{$Model->primaryKey}" => $db->identifier("{$alias}.foreign_key"),
|
|
|
|
"{$alias}.model" => $Model->name,
|
|
|
|
"{$alias}.{$RuntimeModel->displayField}" => $aliasField,
|
|
|
|
"{$alias}.locale" => $locale
|
2012-01-08 17:28:22 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
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
|
|
|
*
|
2012-07-21 20:37:14 +00:00
|
|
|
* @param Model $Model Model find was run on
|
2010-06-10 18:23:49 +00:00
|
|
|
* @param array $results Array of model results.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $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
|
|
|
*/
|
2013-09-10 12:33:11 +00:00
|
|
|
public function afterFind(Model $Model, $results, $primary = false) {
|
2012-07-21 20:37:14 +00:00
|
|
|
$Model->virtualFields = $this->runtime[$Model->alias]['virtualFields'];
|
2014-02-12 02:56:05 +00:00
|
|
|
|
2012-07-21 20:37:14 +00:00
|
|
|
$this->runtime[$Model->alias]['virtualFields'] = $this->runtime[$Model->alias]['fields'] = array();
|
2014-02-12 02:56:05 +00:00
|
|
|
if (!empty($this->runtime[$Model->alias]['restoreFields'])) {
|
|
|
|
$this->runtime[$Model->alias]['fields'] = $this->runtime[$Model->alias]['restoreFields'];
|
|
|
|
unset($this->runtime[$Model->alias]['restoreFields']);
|
|
|
|
}
|
|
|
|
|
2012-07-21 20:37:14 +00:00
|
|
|
$locale = $this->_getLocale($Model);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-07-21 20:37:14 +00:00
|
|
|
if (empty($locale) || empty($results) || empty($this->runtime[$Model->alias]['beforeFind'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $results;
|
|
|
|
}
|
2012-07-21 20:37:14 +00:00
|
|
|
$beforeFind = $this->runtime[$Model->alias]['beforeFind'];
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-10-26 04:10:24 +00:00
|
|
|
foreach ($results as $key => &$row) {
|
2012-07-21 20:37:14 +00:00
|
|
|
$results[$key][$Model->alias]['locale'] = (is_array($locale)) ? current($locale) : $locale;
|
2010-10-26 04:10:24 +00:00
|
|
|
foreach ($beforeFind as $_f => $field) {
|
2010-10-26 22:22:40 +00:00
|
|
|
$aliasField = is_numeric($_f) ? $field : $_f;
|
2012-07-21 20:37:14 +00:00
|
|
|
$aliasVirtual = "i18n_{$field}";
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($locale)) {
|
|
|
|
foreach ($locale as $_locale) {
|
2012-07-21 20:37:14 +00:00
|
|
|
$aliasVirtualLocale = "{$aliasVirtual}_{$_locale}";
|
|
|
|
if (!isset($row[$Model->alias][$aliasField]) && !empty($row[$Model->alias][$aliasVirtualLocale])) {
|
|
|
|
$row[$Model->alias][$aliasField] = $row[$Model->alias][$aliasVirtualLocale];
|
|
|
|
$row[$Model->alias]['locale'] = $_locale;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-07-21 20:37:14 +00:00
|
|
|
unset($row[$Model->alias][$aliasVirtualLocale]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2012-07-21 20:37:14 +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 = '';
|
2014-01-19 02:54:09 +00:00
|
|
|
if (isset($row[$Model->alias][$aliasVirtual])) {
|
2012-07-21 20:37:14 +00:00
|
|
|
$value = $row[$Model->alias][$aliasVirtual];
|
2008-07-30 20:34:01 +00:00
|
|
|
}
|
2012-07-21 20:37:14 +00:00
|
|
|
$row[$Model->alias][$aliasField] = $value;
|
|
|
|
unset($row[$Model->alias][$aliasVirtual]);
|
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
|
|
|
*
|
2012-07-21 20:37:14 +00:00
|
|
|
* @param Model $Model Model invalidFields was called on.
|
2013-09-05 13:38:29 +00:00
|
|
|
* @param array $options Options passed from Model::save().
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2013-09-05 13:38:29 +00:00
|
|
|
* @see Model::save()
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-09-05 13:38:29 +00:00
|
|
|
public function beforeValidate(Model $Model, $options = array()) {
|
2012-07-21 20:37:14 +00:00
|
|
|
unset($this->runtime[$Model->alias]['beforeSave']);
|
|
|
|
$this->_setRuntimeData($Model);
|
2012-05-07 01:29:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* beforeSave callback.
|
|
|
|
*
|
2012-05-08 01:36:28 +00:00
|
|
|
* Copies data into the runtime property when `$options['validate']` is
|
2012-12-22 22:48:15 +00:00
|
|
|
* disabled. Or the runtime data hasn't been set yet.
|
2012-05-08 01:36:28 +00:00
|
|
|
*
|
2012-07-21 20:37:14 +00:00
|
|
|
* @param Model $Model Model save was called on.
|
2013-09-05 13:38:29 +00:00
|
|
|
* @param array $options Options passed from Model::save().
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool true.
|
2013-09-05 13:38:29 +00:00
|
|
|
* @see Model::save()
|
2012-05-07 01:29:39 +00:00
|
|
|
*/
|
2012-07-21 20:37:14 +00:00
|
|
|
public function beforeSave(Model $Model, $options = array()) {
|
2012-09-14 17:26:30 +00:00
|
|
|
if (isset($options['validate']) && !$options['validate']) {
|
2012-07-21 20:37:14 +00:00
|
|
|
unset($this->runtime[$Model->alias]['beforeSave']);
|
2012-05-08 01:36:28 +00:00
|
|
|
}
|
2012-07-21 20:37:14 +00:00
|
|
|
if (isset($this->runtime[$Model->alias]['beforeSave'])) {
|
2012-05-08 01:36:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-21 20:37:14 +00:00
|
|
|
$this->_setRuntimeData($Model);
|
2012-05-07 01:29:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the runtime data.
|
|
|
|
*
|
|
|
|
* Used from beforeValidate() and beforeSave() for compatibility issues,
|
|
|
|
* and to allow translations to be persisted even when validation
|
|
|
|
* is disabled.
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior.
|
2012-05-07 01:29:39 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2012-07-21 20:37:14 +00:00
|
|
|
protected function _setRuntimeData(Model $Model) {
|
|
|
|
$locale = $this->_getLocale($Model);
|
2012-05-08 01:36:28 +00:00
|
|
|
if (empty($locale)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-21 20:37:14 +00:00
|
|
|
$fields = array_merge($this->settings[$Model->alias], $this->runtime[$Model->alias]['fields']);
|
2008-05-30 11:40:08 +00:00
|
|
|
$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
|
|
|
|
2012-07-21 20:37:14 +00:00
|
|
|
if (isset($Model->data[$Model->alias][$field])) {
|
|
|
|
$tempData[$field] = $Model->data[$Model->alias][$field];
|
|
|
|
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];
|
2008-06-14 23:21:09 +00:00
|
|
|
} else {
|
2012-07-21 20:37:14 +00:00
|
|
|
$values = array_values($Model->data[$Model->alias][$field]);
|
|
|
|
$Model->data[$Model->alias][$field] = $values[0];
|
2008-06-14 23:21:09 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-21 20:37:14 +00:00
|
|
|
$this->runtime[$Model->alias]['beforeSave'] = $tempData;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-11-01 03:13:56 +00:00
|
|
|
/**
|
|
|
|
* Restores model data to the original data.
|
|
|
|
* This solves issues with saveAssociated and validate = first.
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior.
|
2012-11-01 03:13:56 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function afterValidate(Model $Model) {
|
|
|
|
$Model->data[$Model->alias] = array_merge(
|
|
|
|
$Model->data[$Model->alias],
|
|
|
|
$this->runtime[$Model->alias]['beforeSave']
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2012-07-21 20:37:14 +00:00
|
|
|
* @param Model $Model Model the callback is called on
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $created Whether or not the save created a record.
|
2013-09-10 00:20:22 +00:00
|
|
|
* @param array $options Options passed from Model::save().
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-09-10 00:20:22 +00:00
|
|
|
public function afterSave(Model $Model, $created, $options = array()) {
|
2012-07-21 20:37:14 +00:00
|
|
|
if (!isset($this->runtime[$Model->alias]['beforeValidate']) && !isset($this->runtime[$Model->alias]['beforeSave'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-21 20:37:14 +00:00
|
|
|
if (isset($this->runtime[$Model->alias]['beforeValidate'])) {
|
|
|
|
$tempData = $this->runtime[$Model->alias]['beforeValidate'];
|
2012-05-08 01:36:28 +00:00
|
|
|
} else {
|
2012-07-21 20:37:14 +00:00
|
|
|
$tempData = $this->runtime[$Model->alias]['beforeSave'];
|
2012-05-08 01:36:28 +00:00
|
|
|
}
|
|
|
|
|
2012-07-21 20:37:14 +00:00
|
|
|
unset($this->runtime[$Model->alias]['beforeValidate'], $this->runtime[$Model->alias]['beforeSave']);
|
2013-06-02 17:48:17 +00:00
|
|
|
$conditions = array('model' => $Model->name, 'foreign_key' => $Model->id);
|
2012-07-21 20:37:14 +00:00
|
|
|
$RuntimeModel = $this->translateModel($Model);
|
2012-07-12 01:39:32 +00:00
|
|
|
|
2012-07-06 13:28:30 +00:00
|
|
|
if ($created) {
|
2012-08-04 02:39:51 +00:00
|
|
|
$tempData = $this->_prepareTranslations($Model, $tempData);
|
2012-07-06 13:28:30 +00:00
|
|
|
}
|
2012-08-04 02:39:51 +00:00
|
|
|
$locale = $this->_getLocale($Model);
|
2014-10-10 12:17:16 +00:00
|
|
|
$atomic = array();
|
|
|
|
if (isset($options['atomic'])) {
|
|
|
|
$atomic = array('atomic' => $options['atomic']);
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
2012-08-04 02:39:51 +00:00
|
|
|
$translations = $RuntimeModel->find('list', array(
|
|
|
|
'conditions' => $conditions,
|
|
|
|
'fields' => array(
|
|
|
|
$RuntimeModel->alias . '.locale',
|
|
|
|
$RuntimeModel->alias . '.id'
|
|
|
|
)
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($value as $_locale => $_value) {
|
|
|
|
$RuntimeModel->create();
|
|
|
|
$conditions['locale'] = $_locale;
|
|
|
|
$conditions['content'] = $_value;
|
|
|
|
if (array_key_exists($_locale, $translations)) {
|
2012-09-15 02:06:41 +00:00
|
|
|
$RuntimeModel->save(array(
|
|
|
|
$RuntimeModel->alias => array_merge(
|
|
|
|
$conditions, array('id' => $translations[$_locale])
|
2014-10-10 12:17:16 +00:00
|
|
|
),
|
|
|
|
$atomic
|
2012-09-15 02:06:41 +00:00
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2014-10-10 12:17:16 +00:00
|
|
|
$RuntimeModel->save(array($RuntimeModel->alias => $conditions), $atomic);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-08-04 02:39:51 +00:00
|
|
|
/**
|
|
|
|
* Prepares the data to be saved for translated records.
|
|
|
|
* Add blank fields, and populates data for multi-locale saves.
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior
|
2012-08-04 02:39:51 +00:00
|
|
|
* @param array $data The sparse data that was provided.
|
|
|
|
* @return array The fully populated data to save.
|
|
|
|
*/
|
|
|
|
protected function _prepareTranslations(Model $Model, $data) {
|
|
|
|
$fields = array_merge($this->settings[$Model->alias], $this->runtime[$Model->alias]['fields']);
|
|
|
|
$locales = array();
|
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
$locales = array_merge($locales, array_keys($value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$locales = array_unique($locales);
|
|
|
|
$hasLocales = count($locales) > 0;
|
|
|
|
|
|
|
|
foreach ($fields as $key => $field) {
|
|
|
|
if (!is_numeric($key)) {
|
|
|
|
$field = $key;
|
|
|
|
}
|
|
|
|
if ($hasLocales && !isset($data[$field])) {
|
|
|
|
$data[$field] = array_fill_keys($locales, '');
|
|
|
|
} elseif (!isset($data[$field])) {
|
|
|
|
$data[$field] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2012-07-21 20:37:14 +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
|
|
|
*/
|
2012-07-21 20:37:14 +00:00
|
|
|
public function afterDelete(Model $Model) {
|
|
|
|
$RuntimeModel = $this->translateModel($Model);
|
2013-06-02 17:48:17 +00:00
|
|
|
$conditions = array('model' => $Model->name, 'foreign_key' => $Model->id);
|
2008-05-30 11:40:08 +00:00
|
|
|
$RuntimeModel->deleteAll($conditions);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Get selected locale for model
|
|
|
|
*
|
2012-07-21 20:37:14 +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
|
|
|
|
*/
|
2012-07-21 20:37:14 +00:00
|
|
|
protected function _getLocale(Model $Model) {
|
2013-08-16 18:12:49 +00:00
|
|
|
if (!isset($Model->locale) || $Model->locale === null) {
|
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'));
|
2012-07-21 20:37:14 +00:00
|
|
|
$Model->locale = $I18n->l10n->locale;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-16 16:39:48 +00:00
|
|
|
|
2012-07-21 20:37:14 +00:00
|
|
|
return $Model->locale;
|
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-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
|
2012-12-22 22:48:15 +00:00
|
|
|
* name to find/use. If no translateModel property is found 'I18nModel' will be used.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-07-21 20:37:14 +00:00
|
|
|
* @param Model $Model Model to get a translatemodel for.
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return Model
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-07-21 20:37:14 +00:00
|
|
|
public function translateModel(Model $Model) {
|
|
|
|
if (!isset($this->runtime[$Model->alias]['model'])) {
|
|
|
|
if (!isset($Model->translateModel) || empty($Model->translateModel)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$className = 'I18nModel';
|
|
|
|
} else {
|
2012-07-21 20:37:14 +00:00
|
|
|
$className = $Model->translateModel;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 03:46:23 +00:00
|
|
|
$this->runtime[$Model->alias]['model'] = ClassRegistry::init($className);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-07-21 20:37:14 +00:00
|
|
|
if (!empty($Model->translateTable) && $Model->translateTable !== $this->runtime[$Model->alias]['model']->useTable) {
|
|
|
|
$this->runtime[$Model->alias]['model']->setSource($Model->translateTable);
|
|
|
|
} elseif (empty($Model->translateTable) && empty($Model->translateModel)) {
|
|
|
|
$this->runtime[$Model->alias]['model']->setSource('i18n');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-07-21 20:37:14 +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
|
2011-12-31 01:39:04 +00:00
|
|
|
* fake field.
|
|
|
|
*
|
|
|
|
* *Note* You should avoid binding translations that overlap existing model properties.
|
|
|
|
* This can cause un-expected and un-desirable behavior.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model using this behavior of model
|
2011-07-30 22:38:57 +00:00
|
|
|
* @param string|array $fields string with field or array(field1, field2=>AssocName, field3)
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $reset Leave true to have the fields only modified for the next operation.
|
2012-05-28 01:25:55 +00:00
|
|
|
* if false the field will be added for all future queries.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2012-12-22 22:48:15 +00:00
|
|
|
* @throws CakeException when attempting to bind a translating called name. This is not allowed
|
2012-03-04 19:18:04 +00:00
|
|
|
* as it shadows Model::$name.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-07-21 20:37:14 +00:00
|
|
|
public function bindTranslation(Model $Model, $fields, $reset = true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_string($fields)) {
|
|
|
|
$fields = array($fields);
|
|
|
|
}
|
|
|
|
$associations = array();
|
2012-07-21 20:37:14 +00:00
|
|
|
$RuntimeModel = $this->translateModel($Model);
|
2014-02-12 02:56:05 +00:00
|
|
|
$default = array(
|
|
|
|
'className' => $RuntimeModel->alias,
|
2014-10-25 02:21:06 +00:00
|
|
|
'foreignKey' => 'foreign_key',
|
|
|
|
'order' => 'id'
|
2014-02-12 02:56:05 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
foreach ($fields as $key => $value) {
|
|
|
|
if (is_numeric($key)) {
|
|
|
|
$field = $value;
|
|
|
|
$association = null;
|
|
|
|
} else {
|
|
|
|
$field = $key;
|
|
|
|
$association = $value;
|
|
|
|
}
|
2011-12-31 01:54:26 +00:00
|
|
|
if ($association === 'name') {
|
2011-12-31 01:39:04 +00:00
|
|
|
throw new CakeException(
|
|
|
|
__d('cake_dev', 'You cannot bind a translation named "name".')
|
|
|
|
);
|
|
|
|
}
|
2012-07-21 20:37:14 +00:00
|
|
|
$this->_removeField($Model, $field);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($association === null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($reset) {
|
2012-07-21 20:37:14 +00:00
|
|
|
$this->runtime[$Model->alias]['fields'][] = $field;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2012-07-21 20:37:14 +00:00
|
|
|
$this->settings[$Model->alias][] = $field;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($reset) {
|
2012-07-21 20:37:14 +00:00
|
|
|
$this->runtime[$Model->alias]['fields'][$field] = $association;
|
2014-02-12 02:56:05 +00:00
|
|
|
$this->runtime[$Model->alias]['restoreFields'][] = $field;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2012-07-21 20:37:14 +00:00
|
|
|
$this->settings[$Model->alias][$field] = $association;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (array('hasOne', 'hasMany', 'belongsTo', 'hasAndBelongsToMany') as $type) {
|
2012-07-21 20:37:14 +00:00
|
|
|
if (isset($Model->{$type}[$association]) || isset($Model->__backAssociation[$type][$association])) {
|
2008-11-03 23:58:44 +00:00
|
|
|
trigger_error(
|
2012-07-21 20:37:14 +00:00
|
|
|
__d('cake_dev', 'Association %s is already bound 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(
|
2013-06-02 17:48:17 +00:00
|
|
|
'model' => $Model->name,
|
2008-05-30 11:40:08 +00:00
|
|
|
$RuntimeModel->displayField => $field
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($associations)) {
|
2012-07-21 20:37:14 +00:00
|
|
|
$Model->bindModel(array('hasMany' => $associations), $reset);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-04-11 01:05:42 +00:00
|
|
|
/**
|
|
|
|
* Update runtime setting for a given field.
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior
|
2012-04-11 01:05:42 +00:00
|
|
|
* @param string $field The field to update.
|
2013-07-05 15:19:22 +00:00
|
|
|
* @return void
|
2012-04-11 01:05:42 +00:00
|
|
|
*/
|
2012-07-21 20:37:14 +00:00
|
|
|
protected function _removeField(Model $Model, $field) {
|
|
|
|
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($this->settings[$Model->alias], array($field)));
|
2012-04-11 01:05:42 +00:00
|
|
|
}
|
|
|
|
|
2012-07-21 20:37:14 +00:00
|
|
|
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($this->runtime[$Model->alias]['fields'], array($field)));
|
2012-04-11 01:05:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Unbind translation for fields, optionally unbinds hasMany association for
|
|
|
|
* fake field
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model using this behavior of model
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $fields string with field, or array(field1, field2=>AssocName, field3), or null for
|
2010-06-10 18:23:49 +00:00
|
|
|
* unbind all original translations
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-07-21 20:37:14 +00:00
|
|
|
public function unbindTranslation(Model $Model, $fields = null) {
|
|
|
|
if (empty($fields) && empty($this->settings[$Model->alias])) {
|
2010-02-12 03:03:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($fields)) {
|
2012-07-21 20:37:14 +00:00
|
|
|
return $this->unbindTranslation($Model, $this->settings[$Model->alias]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($fields)) {
|
|
|
|
$fields = array($fields);
|
|
|
|
}
|
|
|
|
$associations = array();
|
|
|
|
|
|
|
|
foreach ($fields as $key => $value) {
|
|
|
|
if (is_numeric($key)) {
|
|
|
|
$field = $value;
|
|
|
|
$association = null;
|
|
|
|
} else {
|
|
|
|
$field = $key;
|
|
|
|
$association = $value;
|
|
|
|
}
|
|
|
|
|
2012-07-21 20:37:14 +00:00
|
|
|
$this->_removeField($Model, $field);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($association !== null && (isset($Model->hasMany[$association]) || isset($Model->__backAssociation['hasMany'][$association]))) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$associations[] = $association;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($associations)) {
|
2012-07-21 20:37:14 +00:00
|
|
|
$Model->unbindModel(array('hasMany' => $associations), false);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-04 19:18:04 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|