Upgrade CakePHP from 2.2.5 to 2.9.5

This commit is contained in:
Brm Ko 2017-02-26 15:29:44 +01:00
parent 5a580df460
commit 235a541597
793 changed files with 60746 additions and 23753 deletions

View file

@ -1,18 +1,20 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.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 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @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 MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('ModelBehavior', 'Model');
App::uses('I18n', 'I18n');
App::uses('I18nModel', 'Model');
@ -119,7 +121,7 @@ class TranslateBehavior extends ModelBehavior {
$this->_joinTable = $joinTable;
$this->_runtimeModel = $RuntimeModel;
if (is_string($query['fields']) && "COUNT(*) AS {$db->name('count')}" == $query['fields']) {
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',
@ -128,7 +130,7 @@ class TranslateBehavior extends ModelBehavior {
'conditions' => array(
$Model->escapeField() => $db->identifier($RuntimeModel->escapeField('foreign_key')),
$RuntimeModel->escapeField('model') => $Model->name,
$RuntimeModel->escapeField('locale') => $locale
$RuntimeModel->escapeField('locale') => $locale
)
);
$conditionFields = $this->_checkConditions($Model, $query);
@ -137,6 +139,8 @@ class TranslateBehavior extends ModelBehavior {
}
unset($this->_joinTable, $this->_runtimeModel);
return $query;
} elseif (is_string($query['fields'])) {
$query['fields'] = CakeText::tokenize($query['fields']);
}
$fields = array_merge(
@ -153,8 +157,7 @@ class TranslateBehavior extends ModelBehavior {
);
foreach ($fields as $key => $value) {
$field = (is_numeric($key)) ? $value : $key;
if (
$isAllFields ||
if ($isAllFields ||
in_array($Model->alias . '.' . $field, $query['fields']) ||
in_array($field, $query['fields'])
) {
@ -193,7 +196,7 @@ class TranslateBehavior extends ModelBehavior {
*/
protected function _checkConditions(Model $Model, $query) {
$conditionFields = array();
if (empty($query['conditions']) || (!empty($query['conditions']) && !is_array($query['conditions'])) ) {
if (empty($query['conditions']) || (!empty($query['conditions']) && !is_array($query['conditions']))) {
return $conditionFields;
}
foreach ($query['conditions'] as $col => $val) {
@ -213,12 +216,11 @@ class TranslateBehavior extends ModelBehavior {
* Appends a join for translated fields.
*
* @param Model $Model The model being worked on.
* @param object $joinTable The jointable object.
* @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 modfied query
* @return array The modified query
*/
protected function _addJoin(Model $Model, $query, $field, $aliasField, $locale) {
$db = ConnectionManager::getDataSource($Model->useDbConfig);
@ -271,12 +273,18 @@ class TranslateBehavior extends ModelBehavior {
*
* @param Model $Model Model find was run on
* @param array $results Array of model results.
* @param boolean $primary Did the find originate on $model.
* @param bool $primary Did the find originate on $model.
* @return array Modified results
*/
public function afterFind(Model $Model, $results, $primary) {
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'])) {
@ -304,7 +312,7 @@ class TranslateBehavior extends ModelBehavior {
}
} else {
$value = '';
if (!empty($row[$Model->alias][$aliasVirtual])) {
if (isset($row[$Model->alias][$aliasVirtual])) {
$value = $row[$Model->alias][$aliasVirtual];
}
$row[$Model->alias][$aliasField] = $value;
@ -319,9 +327,11 @@ class TranslateBehavior extends ModelBehavior {
* beforeValidate Callback
*
* @param Model $Model Model invalidFields was called on.
* @return boolean
* @param array $options Options passed from Model::save().
* @return bool
* @see Model::save()
*/
public function beforeValidate(Model $Model) {
public function beforeValidate(Model $Model, $options = array()) {
unset($this->runtime[$Model->alias]['beforeSave']);
$this->_setRuntimeData($Model);
return true;
@ -331,13 +341,15 @@ class TranslateBehavior extends ModelBehavior {
* beforeSave callback.
*
* Copies data into the runtime property when `$options['validate']` is
* disabled. Or the runtime data hasn't been set yet.
* disabled. Or the runtime data hasn't been set yet.
*
* @param Model $Model Model save was called on.
* @return boolean true.
* @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'] == false) {
if (isset($options['validate']) && !$options['validate']) {
unset($this->runtime[$Model->alias]['beforeSave']);
}
if (isset($this->runtime[$Model->alias]['beforeSave'])) {
@ -354,7 +366,7 @@ class TranslateBehavior extends ModelBehavior {
* and to allow translations to be persisted even when validation
* is disabled.
*
* @param Model $Model
* @param Model $Model Model using this behavior.
* @return void
*/
protected function _setRuntimeData(Model $Model) {
@ -387,7 +399,7 @@ class TranslateBehavior extends ModelBehavior {
* Restores model data to the original data.
* This solves issues with saveAssociated and validate = first.
*
* @param Model $model
* @param Model $Model Model using this behavior.
* @return void
*/
public function afterValidate(Model $Model) {
@ -402,14 +414,14 @@ class TranslateBehavior extends ModelBehavior {
* afterSave Callback
*
* @param Model $Model Model the callback is called on
* @param boolean $created Whether or not the save created a record.
* @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) {
public function afterSave(Model $Model, $created, $options = array()) {
if (!isset($this->runtime[$Model->alias]['beforeValidate']) && !isset($this->runtime[$Model->alias]['beforeSave'])) {
return true;
}
$locale = $this->_getLocale($Model);
if (isset($this->runtime[$Model->alias]['beforeValidate'])) {
$tempData = $this->runtime[$Model->alias]['beforeValidate'];
} else {
@ -417,23 +429,16 @@ class TranslateBehavior extends ModelBehavior {
}
unset($this->runtime[$Model->alias]['beforeValidate'], $this->runtime[$Model->alias]['beforeSave']);
$conditions = array('model' => $Model->alias, 'foreign_key' => $Model->id);
$conditions = array('model' => $Model->name, 'foreign_key' => $Model->id);
$RuntimeModel = $this->translateModel($Model);
$fields = array_merge(
$this->settings[$Model->alias],
$this->runtime[$Model->alias]['fields']
);
if ($created) {
// set each field value to an empty string
foreach ($fields as $key => $field) {
if (!is_numeric($key)) {
$field = $key;
}
if (!isset($tempData[$field])) {
$tempData[$field] = '';
}
}
$tempData = $this->_prepareTranslations($Model, $tempData);
}
$locale = $this->_getLocale($Model);
$atomic = array();
if (isset($options['atomic'])) {
$atomic = array('atomic' => $options['atomic']);
}
foreach ($tempData as $field => $value) {
@ -451,7 +456,10 @@ class TranslateBehavior extends ModelBehavior {
}
$translations = $RuntimeModel->find('list', array(
'conditions' => $conditions,
'fields' => array($RuntimeModel->alias . '.locale', $RuntimeModel->alias . '.id')
'fields' => array(
$RuntimeModel->alias . '.locale',
$RuntimeModel->alias . '.id'
)
));
foreach ($value as $_locale => $_value) {
$RuntimeModel->create();
@ -461,15 +469,48 @@ class TranslateBehavior extends ModelBehavior {
$RuntimeModel->save(array(
$RuntimeModel->alias => array_merge(
$conditions, array('id' => $translations[$_locale])
)
),
$atomic
));
} else {
$RuntimeModel->save(array($RuntimeModel->alias => $conditions));
$RuntimeModel->save(array($RuntimeModel->alias => $conditions), $atomic);
}
}
}
}
/**
* 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
*
@ -478,10 +519,7 @@ class TranslateBehavior extends ModelBehavior {
*/
public function afterDelete(Model $Model) {
$RuntimeModel = $this->translateModel($Model);
$conditions = array(
'model' => $Model->alias,
'foreign_key' => $Model->id
);
$conditions = array('model' => $Model->name, 'foreign_key' => $Model->id);
$RuntimeModel->deleteAll($conditions);
}
@ -492,7 +530,7 @@ class TranslateBehavior extends ModelBehavior {
* @return mixed string or false
*/
protected function _getLocale(Model $Model) {
if (!isset($Model->locale) || is_null($Model->locale)) {
if (!isset($Model->locale) || $Model->locale === null) {
$I18n = I18n::getInstance();
$I18n->l10n->get(Configure::read('Config.language'));
$Model->locale = $I18n->l10n->locale;
@ -505,7 +543,7 @@ class TranslateBehavior extends ModelBehavior {
* 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.
* 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
@ -518,7 +556,7 @@ class TranslateBehavior extends ModelBehavior {
$className = $Model->translateModel;
}
$this->runtime[$Model->alias]['model'] = ClassRegistry::init($className, 'Model');
$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);
@ -535,12 +573,12 @@ class TranslateBehavior extends ModelBehavior {
* *Note* You should avoid binding translations that overlap existing model properties.
* This can cause un-expected and un-desirable behavior.
*
* @param Model $Model instance of model
* @param Model $Model using this behavior of model
* @param string|array $fields string with field or array(field1, field2=>AssocName, field3)
* @param boolean $reset Leave true to have the fields only modified for the next operation.
* @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 boolean
* @throws CakeException when attempting to bind a translating called name. This is not allowed
* @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) {
@ -551,7 +589,8 @@ class TranslateBehavior extends ModelBehavior {
$RuntimeModel = $this->translateModel($Model);
$default = array(
'className' => $RuntimeModel->alias,
'foreignKey' => 'foreign_key'
'foreignKey' => 'foreign_key',
'order' => 'id'
);
foreach ($fields as $key => $value) {
@ -567,10 +606,9 @@ class TranslateBehavior extends ModelBehavior {
__d('cake_dev', 'You cannot bind a translation named "name".')
);
}
$this->_removeField($Model, $field);
if (is_null($association)) {
if ($association === null) {
if ($reset) {
$this->runtime[$Model->alias]['fields'][] = $field;
} else {
@ -579,6 +617,7 @@ class TranslateBehavior extends ModelBehavior {
} else {
if ($reset) {
$this->runtime[$Model->alias]['fields'][$field] = $association;
$this->runtime[$Model->alias]['restoreFields'][] = $field;
} else {
$this->settings[$Model->alias][$field] = $association;
}
@ -593,7 +632,7 @@ class TranslateBehavior extends ModelBehavior {
}
}
$associations[$association] = array_merge($default, array('conditions' => array(
'model' => $Model->alias,
'model' => $Model->name,
$RuntimeModel->displayField => $field
)));
}
@ -608,7 +647,9 @@ class TranslateBehavior extends ModelBehavior {
/**
* 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])) {
@ -628,10 +669,10 @@ class TranslateBehavior extends ModelBehavior {
* Unbind translation for fields, optionally unbinds hasMany association for
* fake field
*
* @param Model $Model instance of model
* @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 boolean
* @return bool
*/
public function unbindTranslation(Model $Model, $fields = null) {
if (empty($fields) && empty($this->settings[$Model->alias])) {
@ -657,7 +698,7 @@ class TranslateBehavior extends ModelBehavior {
$this->_removeField($Model, $field);
if (!is_null($association) && (isset($Model->hasMany[$association]) || isset($Model->__backAssociation['hasMany'][$association]))) {
if ($association !== null && (isset($Model->hasMany[$association]) || isset($Model->__backAssociation['hasMany'][$association]))) {
$associations[] = $association;
}
}