mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Use options array.
This commit is contained in:
parent
670d93b6f6
commit
4ad001b9ca
2 changed files with 25 additions and 18 deletions
|
@ -450,7 +450,7 @@ class TreeBehavior extends ModelBehavior {
|
||||||
$order = $Model->escapeField($left) . ' asc';
|
$order = $Model->escapeField($left) . ' asc';
|
||||||
$results = $Model->find('all', compact('conditions', 'fields', 'order', 'recursive'));
|
$results = $Model->find('all', compact('conditions', 'fields', 'order', 'recursive'));
|
||||||
|
|
||||||
return $this->formatTreeList($Model, $results, $keyPath, $valuePath);
|
return $this->formatTreeList($Model, $results, compact('keyPath', 'valuePath', 'spacer'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -461,30 +461,37 @@ class TreeBehavior extends ModelBehavior {
|
||||||
*
|
*
|
||||||
* @param Model $Model Model using this behavior
|
* @param Model $Model Model using this behavior
|
||||||
* @param array $results Result array of a find() call
|
* @param array $results Result array of a find() call
|
||||||
|
* @param array $options Options
|
||||||
* @param null $keyPath A string path to the key, i.e. "{n}.Post.id"
|
* @param null $keyPath A string path to the key, i.e. "{n}.Post.id"
|
||||||
* @param null $valuePath A string path to the value, i.e. "{n}.Post.title"
|
* @param null $valuePath A string path to the value, i.e. "{n}.Post.title"
|
||||||
* @param string $spacer The character or characters which will be repeated
|
* @param string $spacer The character or characters which will be repeated
|
||||||
* @return array An associative array of records, where the id is the key, and the display field is the value
|
* @return array An associative array of records, where the id is the key, and the display field is the value
|
||||||
*/
|
*/
|
||||||
public function formatTreeList(Model $Model, array $results, $keyPath = null, $valuePath = null, $spacer = '_') {
|
public function formatTreeList(Model $Model, array $results, array $options = array()) {
|
||||||
if (empty($results)) {
|
if (empty($results)) {
|
||||||
return array();
|
return array();
|
||||||
}
|
}
|
||||||
|
$defaults = array(
|
||||||
|
'keyPath' => null,
|
||||||
|
'valuePath' => null,
|
||||||
|
'spacer' => '_'
|
||||||
|
);
|
||||||
|
$options += $defaults;
|
||||||
|
|
||||||
extract($this->settings[$Model->alias]);
|
extract($this->settings[$Model->alias]);
|
||||||
|
|
||||||
if (!$keyPath) {
|
if (!$options['keyPath']) {
|
||||||
$keyPath = '{n}.' . $Model->alias . '.' . $Model->primaryKey;
|
$options['keyPath'] = '{n}.' . $Model->alias . '.' . $Model->primaryKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$valuePath) {
|
if (!$options['valuePath']) {
|
||||||
$valuePath = array('%s%s', '{n}.tree_prefix', '{n}.' . $Model->alias . '.' . $Model->displayField);
|
$options['valuePath'] = array('%s%s', '{n}.tree_prefix', '{n}.' . $Model->alias . '.' . $Model->displayField);
|
||||||
|
|
||||||
} elseif (is_string($valuePath)) {
|
} elseif (is_string($options['valuePath'])) {
|
||||||
$valuePath = array('%s%s', '{n}.tree_prefix', $valuePath);
|
$options['valuePath'] = array('%s%s', '{n}.tree_prefix', $options['valuePath']);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
array_unshift($valuePath, '%s' . $valuePath[0], '{n}.tree_prefix');
|
array_unshift($options['valuePath'], '%s' . $options['valuePath'][0], '{n}.tree_prefix');
|
||||||
}
|
}
|
||||||
|
|
||||||
$stack = array();
|
$stack = array();
|
||||||
|
@ -495,11 +502,11 @@ class TreeBehavior extends ModelBehavior {
|
||||||
array_pop($stack);
|
array_pop($stack);
|
||||||
$count--;
|
$count--;
|
||||||
}
|
}
|
||||||
$results[$i]['tree_prefix'] = str_repeat($spacer, $count);
|
$results[$i]['tree_prefix'] = str_repeat($options['spacer'], $count);
|
||||||
$stack[] = $result[$Model->alias][$right];
|
$stack[] = $result[$Model->alias][$right];
|
||||||
}
|
}
|
||||||
|
|
||||||
return Hash::combine($results, $keyPath, $valuePath);
|
return Hash::combine($results, $options['keyPath'], $options['valuePath']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1446,14 +1446,14 @@ class TreeBehaviorNumberTest extends CakeTestCase {
|
||||||
$options = array('order' => array('lft' => 'asc'));
|
$options = array('order' => array('lft' => 'asc'));
|
||||||
$records = $this->Tree->find('all', $options);
|
$records = $this->Tree->find('all', $options);
|
||||||
|
|
||||||
$result = $this->Tree->formatTreeList(
|
$options = array(
|
||||||
$records,
|
'keyPath' => "{n}.$modelClass.id",
|
||||||
"{n}.$modelClass.id",
|
'valuePath' => array('%s - %s', "{n}.$modelClass.id", "{n}.$modelClass.name"),
|
||||||
array('%s - %s', "{n}.$modelClass.id", "{n}.$modelClass.name")
|
'spacer' => '--');
|
||||||
);
|
$result = $this->Tree->formatTreeList($records, $options);
|
||||||
$this->assertEquals('1 - 1. Root', $result[1]);
|
$this->assertEquals('1 - 1. Root', $result[1]);
|
||||||
$this->assertEquals('_2 - 1.1', $result[2]);
|
$this->assertEquals('--2 - 1.1', $result[2]);
|
||||||
$this->assertEquals('__3 - 1.1.1', $result[3]);
|
$this->assertEquals('----3 - 1.1.1', $result[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue