Initial commit

This commit is contained in:
mareksebera 2014-09-10 20:20:58 +02:00
commit 3b93da31de
1004 changed files with 265840 additions and 0 deletions

View file

@ -0,0 +1,144 @@
<?php
/**
* ACL behavior class.
*
* Enables objects to easily tie into an ACL system
*
* CakePHP : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP Project
* @package Cake.Model.Behavior
* @since CakePHP v 1.2.0.4487
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('ModelBehavior', 'Model');
App::uses('AclNode', 'Model');
App::uses('Hash', 'Utility');
/**
* ACL behavior
*
* Enables objects to easily tie into an ACL system
*
* @package Cake.Model.Behavior
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/acl.html
*/
class AclBehavior extends ModelBehavior {
/**
* Maps ACL type options to ACL models
*
* @var array
*/
protected $_typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco', 'both' => array('Aro', 'Aco'));
/**
* Sets up the configuration for the model, and loads ACL models if they haven't been already
*
* @param Model $model Model using this behavior.
* @param array $config Configuration options.
* @return void
*/
public function setup(Model $model, $config = array()) {
if (isset($config[0])) {
$config['type'] = $config[0];
unset($config[0]);
}
$this->settings[$model->name] = array_merge(array('type' => 'controlled'), $config);
$this->settings[$model->name]['type'] = strtolower($this->settings[$model->name]['type']);
$types = $this->_typeMaps[$this->settings[$model->name]['type']];
if (!is_array($types)) {
$types = array($types);
}
foreach ($types as $type) {
$model->{$type} = ClassRegistry::init($type);
}
if (!method_exists($model, 'parentNode')) {
trigger_error(__d('cake_dev', 'Callback %s not defined in %s', 'parentNode()', $model->alias), E_USER_WARNING);
}
}
/**
* Retrieves the Aro/Aco node for this model
*
* @param Model $model Model using this behavior.
* @param string|array|Model $ref Array with 'model' and 'foreign_key', model object, or string value
* @param string $type Only needed when Acl is set up as 'both', specify 'Aro' or 'Aco' to get the correct node
* @return array
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/acl.html#node
*/
public function node(Model $model, $ref = null, $type = null) {
if (empty($type)) {
$type = $this->_typeMaps[$this->settings[$model->name]['type']];
if (is_array($type)) {
trigger_error(__d('cake_dev', 'AclBehavior is setup with more then one type, please specify type parameter for node()'), E_USER_WARNING);
return null;
}
}
if (empty($ref)) {
$ref = array('model' => $model->name, 'foreign_key' => $model->id);
}
return $model->{$type}->node($ref);
}
/**
* Creates a new ARO/ACO node bound to this record
*
* @param Model $model Model using this behavior.
* @param bool $created True if this is a new record
* @param array $options Options passed from Model::save().
* @return void
*/
public function afterSave(Model $model, $created, $options = array()) {
$types = $this->_typeMaps[$this->settings[$model->name]['type']];
if (!is_array($types)) {
$types = array($types);
}
foreach ($types as $type) {
$parent = $model->parentNode();
if (!empty($parent)) {
$parent = $this->node($model, $parent, $type);
}
$data = array(
'parent_id' => isset($parent[0][$type]['id']) ? $parent[0][$type]['id'] : null,
'model' => $model->name,
'foreign_key' => $model->id
);
if (!$created) {
$node = $this->node($model, null, $type);
$data['id'] = isset($node[0][$type]['id']) ? $node[0][$type]['id'] : null;
}
$model->{$type}->create();
$model->{$type}->save($data);
}
}
/**
* Destroys the ARO/ACO node bound to the deleted record
*
* @param Model $model Model using this behavior.
* @return void
*/
public function afterDelete(Model $model) {
$types = $this->_typeMaps[$this->settings[$model->name]['type']];
if (!is_array($types)) {
$types = array($types);
}
foreach ($types as $type) {
$node = Hash::extract($this->node($model, null, $type), "0.{$type}.id");
if (!empty($node)) {
$model->{$type}->delete($node);
}
}
}
}

View file

@ -0,0 +1,431 @@
<?php
/**
* Behavior for binding management.
*
* Behavior to simplify manipulating a model's bindings when doing a find operation
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Model.Behavior
* @since CakePHP(tm) v 1.2.0.5669
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('ModelBehavior', 'Model');
/**
* Behavior to allow for dynamic and atomic manipulation of a Model's associations
* used for a find call. Most useful for limiting the amount of associations and
* data returned.
*
* @package Cake.Model.Behavior
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
*/
class ContainableBehavior extends ModelBehavior {
/**
* Types of relationships available for models
*
* @var array
*/
public $types = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
/**
* Runtime configuration for this behavior
*
* @var array
*/
public $runtime = array();
/**
* Initiate behavior for the model using specified settings.
*
* Available settings:
*
* - recursive: (boolean, optional) set to true to allow containable to automatically
* determine the recursiveness level needed to fetch specified models,
* and set the model recursiveness to this level. setting it to false
* disables this feature. DEFAULTS TO: true
* - notices: (boolean, optional) issues E_NOTICES for bindings referenced in a
* containable call that are not valid. DEFAULTS TO: true
* - autoFields: (boolean, optional) auto-add needed fields to fetch requested
* bindings. DEFAULTS TO: true
*
* @param Model $Model Model using the behavior
* @param array $settings Settings to override for model.
* @return void
*/
public function setup(Model $Model, $settings = array()) {
if (!isset($this->settings[$Model->alias])) {
$this->settings[$Model->alias] = array('recursive' => true, 'notices' => true, 'autoFields' => true);
}
$this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], $settings);
}
/**
* Runs before a find() operation. Used to allow 'contain' setting
* as part of the find call, like this:
*
* `Model->find('all', array('contain' => array('Model1', 'Model2')));`
*
* {{{
* Model->find('all', array('contain' => array(
* 'Model1' => array('Model11', 'Model12'),
* 'Model2',
* 'Model3' => array(
* 'Model31' => 'Model311',
* 'Model32',
* 'Model33' => array('Model331', 'Model332')
* )));
* }}}
*
* @param Model $Model Model using the behavior
* @param array $query Query parameters as set by cake
* @return array
*/
public function beforeFind(Model $Model, $query) {
$reset = (isset($query['reset']) ? $query['reset'] : true);
$noContain = false;
$contain = array();
if (isset($this->runtime[$Model->alias]['contain'])) {
$noContain = empty($this->runtime[$Model->alias]['contain']);
$contain = $this->runtime[$Model->alias]['contain'];
unset($this->runtime[$Model->alias]['contain']);
}
if (isset($query['contain'])) {
$noContain = $noContain || empty($query['contain']);
if ($query['contain'] !== false) {
$contain = array_merge($contain, (array)$query['contain']);
}
}
$noContain = $noContain && empty($contain);
if ($noContain || empty($contain)) {
if ($noContain) {
$query['recursive'] = -1;
}
return $query;
}
if ((isset($contain[0]) && is_bool($contain[0])) || is_bool(end($contain))) {
$reset = is_bool(end($contain))
? array_pop($contain)
: array_shift($contain);
}
$containments = $this->containments($Model, $contain);
$map = $this->containmentsMap($containments);
$mandatory = array();
foreach ($containments['models'] as $model) {
$instance = $model['instance'];
$needed = $this->fieldDependencies($instance, $map, false);
if (!empty($needed)) {
$mandatory = array_merge($mandatory, $needed);
}
if ($contain) {
$backupBindings = array();
foreach ($this->types as $relation) {
if (!empty($instance->__backAssociation[$relation])) {
$backupBindings[$relation] = $instance->__backAssociation[$relation];
} else {
$backupBindings[$relation] = $instance->{$relation};
}
}
foreach ($this->types as $type) {
$unbind = array();
foreach ($instance->{$type} as $assoc => $options) {
if (!isset($model['keep'][$assoc])) {
$unbind[] = $assoc;
}
}
if (!empty($unbind)) {
if (!$reset && empty($instance->__backOriginalAssociation)) {
$instance->__backOriginalAssociation = $backupBindings;
}
$instance->unbindModel(array($type => $unbind), $reset);
}
foreach ($instance->{$type} as $assoc => $options) {
if (isset($model['keep'][$assoc]) && !empty($model['keep'][$assoc])) {
if (isset($model['keep'][$assoc]['fields'])) {
$model['keep'][$assoc]['fields'] = $this->fieldDependencies($containments['models'][$assoc]['instance'], $map, $model['keep'][$assoc]['fields']);
}
if (!$reset && empty($instance->__backOriginalAssociation)) {
$instance->__backOriginalAssociation = $backupBindings;
} elseif ($reset) {
$instance->__backAssociation[$type] = $backupBindings[$type];
}
$instance->{$type}[$assoc] = array_merge($instance->{$type}[$assoc], $model['keep'][$assoc]);
}
if (!$reset) {
$instance->__backInnerAssociation[] = $assoc;
}
}
}
}
}
if ($this->settings[$Model->alias]['recursive']) {
$query['recursive'] = (isset($query['recursive'])) ? max($query['recursive'], $containments['depth']) : $containments['depth'];
}
$autoFields = ($this->settings[$Model->alias]['autoFields']
&& !in_array($Model->findQueryType, array('list', 'count'))
&& !empty($query['fields']));
if (!$autoFields) {
return $query;
}
$query['fields'] = (array)$query['fields'];
foreach (array('hasOne', 'belongsTo') as $type) {
if (!empty($Model->{$type})) {
foreach ($Model->{$type} as $assoc => $data) {
if ($Model->useDbConfig === $Model->{$assoc}->useDbConfig && !empty($data['fields'])) {
foreach ((array)$data['fields'] as $field) {
$query['fields'][] = (strpos($field, '.') === false ? $assoc . '.' : '') . $field;
}
}
}
}
}
if (!empty($mandatory[$Model->alias])) {
foreach ($mandatory[$Model->alias] as $field) {
if ($field === '--primaryKey--') {
$field = $Model->primaryKey;
} elseif (preg_match('/^.+\.\-\-[^-]+\-\-$/', $field)) {
list($modelName, $field) = explode('.', $field);
if ($Model->useDbConfig === $Model->{$modelName}->useDbConfig) {
$field = $modelName . '.' . (
($field === '--primaryKey--') ? $Model->$modelName->primaryKey : $field
);
} else {
$field = null;
}
}
if ($field !== null) {
$query['fields'][] = $field;
}
}
}
$query['fields'] = array_unique($query['fields']);
return $query;
}
/**
* Unbinds all relations from a model except the specified ones. Calling this function without
* parameters unbinds all related models.
*
* @param Model $Model Model on which binding restriction is being applied
* @return void
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#using-containable
*/
public function contain(Model $Model) {
$args = func_get_args();
$contain = call_user_func_array('am', array_slice($args, 1));
$this->runtime[$Model->alias]['contain'] = $contain;
}
/**
* Permanently restore the original binding settings of given model, useful
* for restoring the bindings after using 'reset' => false as part of the
* contain call.
*
* @param Model $Model Model on which to reset bindings
* @return void
*/
public function resetBindings(Model $Model) {
if (!empty($Model->__backOriginalAssociation)) {
$Model->__backAssociation = $Model->__backOriginalAssociation;
unset($Model->__backOriginalAssociation);
}
$Model->resetAssociations();
if (!empty($Model->__backInnerAssociation)) {
$assocs = $Model->__backInnerAssociation;
$Model->__backInnerAssociation = array();
foreach ($assocs as $currentModel) {
$this->resetBindings($Model->$currentModel);
}
}
}
/**
* Process containments for model.
*
* @param Model $Model Model on which binding restriction is being applied
* @param array $contain Parameters to use for restricting this model
* @param array $containments Current set of containments
* @param bool $throwErrors Whether non-existent bindings show throw errors
* @return array Containments
*/
public function containments(Model $Model, $contain, $containments = array(), $throwErrors = null) {
$options = array('className', 'joinTable', 'with', 'foreignKey', 'associationForeignKey', 'conditions', 'fields', 'order', 'limit', 'offset', 'unique', 'finderQuery');
$keep = array();
if ($throwErrors === null) {
$throwErrors = (empty($this->settings[$Model->alias]) ? true : $this->settings[$Model->alias]['notices']);
}
foreach ((array)$contain as $name => $children) {
if (is_numeric($name)) {
$name = $children;
$children = array();
}
if (preg_match('/(?<!\.)\(/', $name)) {
$name = str_replace('(', '.(', $name);
}
if (strpos($name, '.') !== false) {
$chain = explode('.', $name);
$name = array_shift($chain);
$children = array(implode('.', $chain) => $children);
}
$children = (array)$children;
foreach ($children as $key => $val) {
if (is_string($key) && is_string($val) && !in_array($key, $options, true)) {
$children[$key] = (array)$val;
}
}
$keys = array_keys($children);
if ($keys && isset($children[0])) {
$keys = array_merge(array_values($children), $keys);
}
foreach ($keys as $i => $key) {
if (is_array($key)) {
continue;
}
$optionKey = in_array($key, $options, true);
if (!$optionKey && is_string($key) && preg_match('/^[a-z(]/', $key) && (!isset($Model->{$key}) || !is_object($Model->{$key}))) {
$option = 'fields';
$val = array($key);
if ($key{0} === '(') {
$val = preg_split('/\s*,\s*/', substr($key, 1, -1));
} elseif (preg_match('/ASC|DESC$/', $key)) {
$option = 'order';
$val = $Model->{$name}->alias . '.' . $key;
} elseif (preg_match('/[ =!]/', $key)) {
$option = 'conditions';
$val = $Model->{$name}->alias . '.' . $key;
}
$children[$option] = is_array($val) ? $val : array($val);
$newChildren = null;
if (!empty($name) && !empty($children[$key])) {
$newChildren = $children[$key];
}
unset($children[$key], $children[$i]);
$key = $option;
$optionKey = true;
if (!empty($newChildren)) {
$children = Hash::merge($children, $newChildren);
}
}
if ($optionKey && isset($children[$key])) {
if (!empty($keep[$name][$key]) && is_array($keep[$name][$key])) {
$keep[$name][$key] = array_merge((isset($keep[$name][$key]) ? $keep[$name][$key] : array()), (array)$children[$key]);
} else {
$keep[$name][$key] = $children[$key];
}
unset($children[$key]);
}
}
if (!isset($Model->{$name}) || !is_object($Model->{$name})) {
if ($throwErrors) {
trigger_error(__d('cake_dev', 'Model "%s" is not associated with model "%s"', $Model->alias, $name), E_USER_WARNING);
}
continue;
}
$containments = $this->containments($Model->{$name}, $children, $containments);
$depths[] = $containments['depth'] + 1;
if (!isset($keep[$name])) {
$keep[$name] = array();
}
}
if (!isset($containments['models'][$Model->alias])) {
$containments['models'][$Model->alias] = array('keep' => array(), 'instance' => &$Model);
}
$containments['models'][$Model->alias]['keep'] = array_merge($containments['models'][$Model->alias]['keep'], $keep);
$containments['depth'] = empty($depths) ? 0 : max($depths);
return $containments;
}
/**
* Calculate needed fields to fetch the required bindings for the given model.
*
* @param Model $Model Model
* @param array $map Map of relations for given model
* @param array|bool $fields If array, fields to initially load, if false use $Model as primary model
* @return array Fields
*/
public function fieldDependencies(Model $Model, $map, $fields = array()) {
if ($fields === false) {
foreach ($map as $parent => $children) {
foreach ($children as $type => $bindings) {
foreach ($bindings as $dependency) {
if ($type === 'hasAndBelongsToMany') {
$fields[$parent][] = '--primaryKey--';
} elseif ($type === 'belongsTo') {
$fields[$parent][] = $dependency . '.--primaryKey--';
}
}
}
}
return $fields;
}
if (empty($map[$Model->alias])) {
return $fields;
}
foreach ($map[$Model->alias] as $type => $bindings) {
foreach ($bindings as $dependency) {
$innerFields = array();
switch ($type) {
case 'belongsTo':
$fields[] = $Model->{$type}[$dependency]['foreignKey'];
break;
case 'hasOne':
case 'hasMany':
$innerFields[] = $Model->$dependency->primaryKey;
$fields[] = $Model->primaryKey;
break;
}
if (!empty($innerFields) && !empty($Model->{$type}[$dependency]['fields'])) {
$Model->{$type}[$dependency]['fields'] = array_unique(array_merge($Model->{$type}[$dependency]['fields'], $innerFields));
}
}
}
return array_unique($fields);
}
/**
* Build the map of containments
*
* @param array $containments Containments
* @return array Built containments
*/
public function containmentsMap($containments) {
$map = array();
foreach ($containments['models'] as $name => $model) {
$instance = $model['instance'];
foreach ($this->types as $type) {
foreach ($instance->{$type} as $assoc => $options) {
if (isset($model['keep'][$assoc])) {
$map[$name][$type] = isset($map[$name][$type]) ? array_merge($map[$name][$type], (array)$assoc) : (array)$assoc;
}
}
}
}
return $map;
}
}

View file

@ -0,0 +1,707 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Model.Behavior
* @since CakePHP(tm) v 1.2.0.4525
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('ModelBehavior', 'Model');
App::uses('I18n', 'I18n');
App::uses('I18nModel', 'Model');
/**
* Translate behavior
*
* @package Cake.Model.Behavior
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/translate.html
*/
class TranslateBehavior extends ModelBehavior {
/**
* Used for runtime configuration of model
*
* @var array
*/
public $runtime = array();
/**
* Stores the joinTable object for generating joins.
*
* @var object
*/
protected $_joinTable;
/**
* Stores the runtime model for generating joins.
*
* @var Model
*/
protected $_runtimeModel;
/**
* 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
*
* @param Model $Model Model the behavior is being attached to.
* @param array $config Array of configuration information.
* @return mixed
*/
public function setup(Model $Model, $config = array()) {
$db = ConnectionManager::getDataSource($Model->useDbConfig);
if (!$db->connected) {
trigger_error(
__d('cake_dev', 'Datasource %s for TranslateBehavior of model %s is not connected', $Model->useDbConfig, $Model->alias),
E_USER_ERROR
);
return false;
}
$this->settings[$Model->alias] = array();
$this->runtime[$Model->alias] = array('fields' => array());
$this->translateModel($Model);
return $this->bindTranslation($Model, $config, false);
}
/**
* Cleanup Callback unbinds bound translations and deletes setting information.
*
* @param Model $Model Model being detached.
* @return void
*/
public function cleanup(Model $Model) {
$this->unbindTranslation($Model);
unset($this->settings[$Model->alias]);
unset($this->runtime[$Model->alias]);
}
/**
* beforeFind Callback
*
* @param Model $Model Model find is being run on.
* @param array $query Array of Query parameters.
* @return array Modified query
*/
public function beforeFind(Model $Model, $query) {
$this->runtime[$Model->alias]['virtualFields'] = $Model->virtualFields;
$locale = $this->_getLocale($Model);
if (empty($locale)) {
return $query;
}
$db = $Model->getDataSource();
$RuntimeModel = $this->translateModel($Model);
if (!empty($RuntimeModel->tablePrefix)) {
$tablePrefix = $RuntimeModel->tablePrefix;
} else {
$tablePrefix = $db->config['prefix'];
}
$joinTable = new StdClass();
$joinTable->tablePrefix = $tablePrefix;
$joinTable->table = $RuntimeModel->table;
$joinTable->schemaName = $RuntimeModel->getDataSource()->getSchemaName();
$this->_joinTable = $joinTable;
$this->_runtimeModel = $RuntimeModel;
if (is_string($query['fields']) && $query['fields'] === "COUNT(*) AS {$db->name('count')}") {
$query['fields'] = "COUNT(DISTINCT({$db->name($Model->escapeField())})) {$db->alias}count";
$query['joins'][] = array(
'type' => 'INNER',
'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;
} elseif (is_string($query['fields'])) {
$query['fields'] = String::tokenize($query['fields']);
}
$fields = array_merge(
$this->settings[$Model->alias],
$this->runtime[$Model->alias]['fields']
);
$addFields = array();
if (empty($query['fields'])) {
$addFields = $fields;
} elseif (is_array($query['fields'])) {
$isAllFields = (
in_array($Model->alias . '.' . '*', $query['fields']) ||
in_array($Model->escapeField('*'), $query['fields'])
);
foreach ($fields as $key => $value) {
$field = (is_numeric($key)) ? $value : $key;
if (
$isAllFields ||
in_array($Model->alias . '.' . $field, $query['fields']) ||
in_array($field, $query['fields'])
) {
$addFields[] = $field;
}
}
}
$this->runtime[$Model->alias]['virtualFields'] = $Model->virtualFields;
if ($addFields) {
foreach ($addFields as $_f => $field) {
$aliasField = is_numeric($_f) ? $field : $_f;
foreach (array($aliasField, $Model->alias . '.' . $aliasField) as $_field) {
$key = array_search($_field, (array)$query['fields']);
if ($key !== false) {
unset($query['fields'][$key]);
}
}
$query = $this->_addJoin($Model, $query, $field, $aliasField, $locale);
}
}
$this->runtime[$Model->alias]['beforeFind'] = $addFields;
unset($this->_joinTable, $this->_runtimeModel);
return $query;
}
/**
* Check a query's conditions for translated fields.
* Return an array of translated fields found in the conditions.
*
* @param Model $Model The model being read.
* @param array $query The query array.
* @return array The list of translated fields that are in the conditions.
*/
protected function _checkConditions(Model $Model, $query) {
$conditionFields = array();
if (empty($query['conditions']) || (!empty($query['conditions']) && !is_array($query['conditions']))) {
return $conditionFields;
}
foreach ($query['conditions'] as $col => $val) {
foreach ($this->settings[$Model->alias] as $field => $assoc) {
if (is_numeric($field)) {
$field = $assoc;
}
if (strpos($col, $field) !== false) {
$conditionFields[] = $field;
}
}
}
return $conditionFields;
}
/**
* Appends a join for translated fields.
*
* @param Model $Model The model being worked on.
* @param array $query The query array to append a join to.
* @param string $field The field name being joined.
* @param string $aliasField The aliased field name being joined.
* @param string|array $locale The locale(s) having joins added.
* @return array The modified query
*/
protected function _addJoin(Model $Model, $query, $field, $aliasField, $locale) {
$db = ConnectionManager::getDataSource($Model->useDbConfig);
$RuntimeModel = $this->_runtimeModel;
$joinTable = $this->_joinTable;
$aliasVirtual = "i18n_{$field}";
$alias = "I18n__{$field}";
if (is_array($locale)) {
foreach ($locale as $_locale) {
$aliasVirtualLocale = "{$aliasVirtual}_{$_locale}";
$aliasLocale = "{$alias}__{$_locale}";
$Model->virtualFields[$aliasVirtualLocale] = "{$aliasLocale}.content";
if (!empty($query['fields']) && is_array($query['fields'])) {
$query['fields'][] = $aliasVirtualLocale;
}
$query['joins'][] = array(
'type' => 'LEFT',
'alias' => $aliasLocale,
'table' => $joinTable,
'conditions' => array(
$Model->escapeField() => $db->identifier("{$aliasLocale}.foreign_key"),
"{$aliasLocale}.model" => $Model->name,
"{$aliasLocale}.{$RuntimeModel->displayField}" => $aliasField,
"{$aliasLocale}.locale" => $_locale
)
);
}
} else {
$Model->virtualFields[$aliasVirtual] = "{$alias}.content";
if (!empty($query['fields']) && is_array($query['fields'])) {
$query['fields'][] = $aliasVirtual;
}
$query['joins'][] = array(
'type' => 'INNER',
'alias' => $alias,
'table' => $joinTable,
'conditions' => array(
"{$Model->alias}.{$Model->primaryKey}" => $db->identifier("{$alias}.foreign_key"),
"{$alias}.model" => $Model->name,
"{$alias}.{$RuntimeModel->displayField}" => $aliasField,
"{$alias}.locale" => $locale
)
);
}
return $query;
}
/**
* afterFind Callback
*
* @param Model $Model Model find was run on
* @param array $results Array of model results.
* @param bool $primary Did the find originate on $model.
* @return array Modified results
*/
public function afterFind(Model $Model, $results, $primary = false) {
$Model->virtualFields = $this->runtime[$Model->alias]['virtualFields'];
$this->runtime[$Model->alias]['virtualFields'] = $this->runtime[$Model->alias]['fields'] = array();
if (!empty($this->runtime[$Model->alias]['restoreFields'])) {
$this->runtime[$Model->alias]['fields'] = $this->runtime[$Model->alias]['restoreFields'];
unset($this->runtime[$Model->alias]['restoreFields']);
}
$locale = $this->_getLocale($Model);
if (empty($locale) || empty($results) || empty($this->runtime[$Model->alias]['beforeFind'])) {
return $results;
}
$beforeFind = $this->runtime[$Model->alias]['beforeFind'];
foreach ($results as $key => &$row) {
$results[$key][$Model->alias]['locale'] = (is_array($locale)) ? current($locale) : $locale;
foreach ($beforeFind as $_f => $field) {
$aliasField = is_numeric($_f) ? $field : $_f;
$aliasVirtual = "i18n_{$field}";
if (is_array($locale)) {
foreach ($locale as $_locale) {
$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;
}
unset($row[$Model->alias][$aliasVirtualLocale]);
}
if (!isset($row[$Model->alias][$aliasField])) {
$row[$Model->alias][$aliasField] = '';
}
} else {
$value = '';
if (isset($row[$Model->alias][$aliasVirtual])) {
$value = $row[$Model->alias][$aliasVirtual];
}
$row[$Model->alias][$aliasField] = $value;
unset($row[$Model->alias][$aliasVirtual]);
}
}
}
return $results;
}
/**
* beforeValidate Callback
*
* @param Model $Model Model invalidFields was called on.
* @param array $options Options passed from Model::save().
* @return bool
* @see Model::save()
*/
public function beforeValidate(Model $Model, $options = array()) {
unset($this->runtime[$Model->alias]['beforeSave']);
$this->_setRuntimeData($Model);
return true;
}
/**
* beforeSave callback.
*
* Copies data into the runtime property when `$options['validate']` is
* disabled. Or the runtime data hasn't been set yet.
*
* @param Model $Model Model save was called on.
* @param array $options Options passed from Model::save().
* @return bool true.
* @see Model::save()
*/
public function beforeSave(Model $Model, $options = array()) {
if (isset($options['validate']) && !$options['validate']) {
unset($this->runtime[$Model->alias]['beforeSave']);
}
if (isset($this->runtime[$Model->alias]['beforeSave'])) {
return true;
}
$this->_setRuntimeData($Model);
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.
*
* @param Model $Model Model using this behavior.
* @return void
*/
protected function _setRuntimeData(Model $Model) {
$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) {
$field = (is_numeric($key)) ? $value : $key;
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];
} else {
$values = array_values($Model->data[$Model->alias][$field]);
$Model->data[$Model->alias][$field] = $values[0];
}
}
}
}
$this->runtime[$Model->alias]['beforeSave'] = $tempData;
}
/**
* Restores model data to the original data.
* This solves issues with saveAssociated and validate = first.
*
* @param Model $Model Model using this behavior.
* @return void
*/
public function afterValidate(Model $Model) {
$Model->data[$Model->alias] = array_merge(
$Model->data[$Model->alias],
$this->runtime[$Model->alias]['beforeSave']
);
return true;
}
/**
* afterSave Callback
*
* @param Model $Model Model the callback is called on
* @param bool $created Whether or not the save created a record.
* @param array $options Options passed from Model::save().
* @return void
*/
public function afterSave(Model $Model, $created, $options = array()) {
if (!isset($this->runtime[$Model->alias]['beforeValidate']) && !isset($this->runtime[$Model->alias]['beforeSave'])) {
return true;
}
if (isset($this->runtime[$Model->alias]['beforeValidate'])) {
$tempData = $this->runtime[$Model->alias]['beforeValidate'];
} else {
$tempData = $this->runtime[$Model->alias]['beforeSave'];
}
unset($this->runtime[$Model->alias]['beforeValidate'], $this->runtime[$Model->alias]['beforeSave']);
$conditions = array('model' => $Model->name, 'foreign_key' => $Model->id);
$RuntimeModel = $this->translateModel($Model);
if ($created) {
$tempData = $this->_prepareTranslations($Model, $tempData);
}
$locale = $this->_getLocale($Model);
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));
}
}
}
}
/**
* Prepares the data to be saved for translated records.
* Add blank fields, and populates data for multi-locale saves.
*
* @param Model $Model Model using this behavior
* @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;
}
/**
* afterDelete Callback
*
* @param Model $Model Model the callback was run on.
* @return void
*/
public function afterDelete(Model $Model) {
$RuntimeModel = $this->translateModel($Model);
$conditions = array('model' => $Model->name, 'foreign_key' => $Model->id);
$RuntimeModel->deleteAll($conditions);
}
/**
* Get selected locale for model
*
* @param Model $Model Model the locale needs to be set/get on.
* @return mixed string or false
*/
protected function _getLocale(Model $Model) {
if (!isset($Model->locale) || $Model->locale === null) {
$I18n = I18n::getInstance();
$I18n->l10n->get(Configure::read('Config.language'));
$Model->locale = $I18n->l10n->locale;
}
return $Model->locale;
}
/**
* 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.
*
* @param Model $Model Model to get a translatemodel for.
* @return Model
*/
public function translateModel(Model $Model) {
if (!isset($this->runtime[$Model->alias]['model'])) {
if (!isset($Model->translateModel) || empty($Model->translateModel)) {
$className = 'I18nModel';
} else {
$className = $Model->translateModel;
}
$this->runtime[$Model->alias]['model'] = ClassRegistry::init($className);
}
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');
}
return $this->runtime[$Model->alias]['model'];
}
/**
* Bind translation for fields, optionally with hasMany association for
* fake field.
*
* *Note* You should avoid binding translations that overlap existing model properties.
* This can cause un-expected and un-desirable behavior.
*
* @param Model $Model using this behavior of model
* @param string|array $fields string with field or array(field1, field2=>AssocName, field3)
* @param bool $reset Leave true to have the fields only modified for the next operation.
* if false the field will be added for all future queries.
* @return bool
* @throws CakeException when attempting to bind a translating called name. This is not allowed
* as it shadows Model::$name.
*/
public function bindTranslation(Model $Model, $fields, $reset = true) {
if (is_string($fields)) {
$fields = array($fields);
}
$associations = array();
$RuntimeModel = $this->translateModel($Model);
$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 ($association === 'name') {
throw new CakeException(
__d('cake_dev', 'You cannot bind a translation named "name".')
);
}
$this->_removeField($Model, $field);
if ($association === null) {
if ($reset) {
$this->runtime[$Model->alias]['fields'][] = $field;
} else {
$this->settings[$Model->alias][] = $field;
}
} else {
if ($reset) {
$this->runtime[$Model->alias]['fields'][$field] = $association;
$this->runtime[$Model->alias]['restoreFields'][] = $field;
} 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])) {
trigger_error(
__d('cake_dev', 'Association %s is already bound to model %s', $association, $Model->alias),
E_USER_ERROR
);
return false;
}
}
$associations[$association] = array_merge($default, array('conditions' => array(
'model' => $Model->name,
$RuntimeModel->displayField => $field
)));
}
}
if (!empty($associations)) {
$Model->bindModel(array('hasMany' => $associations), $reset);
}
return true;
}
/**
* Update runtime setting for a given field.
*
* @param Model $Model Model using this behavior
* @param string $field The field to update.
* @return void
*/
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)));
}
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)));
}
}
/**
* Unbind translation for fields, optionally unbinds hasMany association for
* fake field
*
* @param Model $Model using this behavior of model
* @param string|array $fields string with field, or array(field1, field2=>AssocName, field3), or null for
* unbind all original translations
* @return bool
*/
public function unbindTranslation(Model $Model, $fields = null) {
if (empty($fields) && empty($this->settings[$Model->alias])) {
return false;
}
if (empty($fields)) {
return $this->unbindTranslation($Model, $this->settings[$Model->alias]);
}
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;
}
$this->_removeField($Model, $field);
if ($association !== null && (isset($Model->hasMany[$association]) || isset($Model->__backAssociation['hasMany'][$association]))) {
$associations[] = $association;
}
}
if (!empty($associations)) {
$Model->unbindModel(array('hasMany' => $associations), false);
}
return true;
}
}

File diff suppressed because it is too large Load diff