Skip to content

Commit

Permalink
Re-factor duplicated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Apr 11, 2012
1 parent 45e3414 commit 2333c8f
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions lib/Cake/Model/Behavior/TranslateBehavior.php
Expand Up @@ -471,17 +471,7 @@ public function bindTranslation(Model $model, $fields, $reset = true) {
);
}

if (array_key_exists($field, $this->settings[$model->alias])) {
unset($this->settings[$model->alias][$field]);
} elseif (in_array($field, $this->settings[$model->alias])) {
$this->settings[$model->alias] = array_merge(array_diff_assoc($this->settings[$model->alias], array($field)));
}

if (array_key_exists($field, $this->runtime[$model->alias]['fields'])) {
unset($this->runtime[$model->alias]['fields'][$field]);
} elseif (in_array($field, $this->runtime[$model->alias]['fields'])) {
$this->runtime[$model->alias]['fields'] = array_merge(array_diff_assoc($this->runtime[$model->alias]['fields'], array($field)));
}
$this->_updateSettings($model, $field);

if (is_null($association)) {
if ($reset) {
Expand Down Expand Up @@ -518,6 +508,25 @@ public function bindTranslation(Model $model, $fields, $reset = true) {
return true;
}

/**
* Update runtime setting for a given field.
*
* @param string $field The field to update.
*/
protected function _updateSettings(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_assoc($this->settings[$model->alias], array($field)));
}

if (array_key_exists($field, $this->runtime[$model->alias]['fields'])) {
unset($this->runtime[$model->alias]['fields'][$field]);
} elseif (in_array($field, $this->runtime[$model->alias]['fields'])) {
$this->runtime[$model->alias]['fields'] = array_merge(array_diff_assoc($this->runtime[$model->alias]['fields'], array($field)));
}
}

/**
* Unbind translation for fields, optionally unbinds hasMany association for
* fake field
Expand Down Expand Up @@ -550,17 +559,7 @@ public function unbindTranslation(Model $model, $fields = null) {
$association = $value;
}

if (array_key_exists($field, $this->settings[$model->alias])) {
unset($this->settings[$model->alias][$field]);
} elseif (in_array($field, $this->settings[$model->alias])) {
$this->settings[$model->alias] = array_merge(array_diff_assoc($this->settings[$model->alias], array($field)));
}

if (array_key_exists($field, $this->runtime[$model->alias]['fields'])) {
unset($this->runtime[$model->alias]['fields'][$field]);
} elseif (in_array($field, $this->runtime[$model->alias]['fields'])) {
$this->runtime[$model->alias]['fields'] = array_merge(array_diff_assoc($this->runtime[$model->alias]['fields'], array($field)));
}
$this->_updateSettings($model, $field);

if (!is_null($association) && (isset($model->hasMany[$association]) || isset($model->__backAssociation['hasMany'][$association]))) {
$associations[] = $association;
Expand Down

0 comments on commit 2333c8f

Please sign in to comment.