mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Extract formatTreeList() from generateTreeList().
This commit is contained in:
parent
2a5cbb8037
commit
670d93b6f6
2 changed files with 55 additions and 10 deletions
|
@ -442,6 +442,37 @@ class TreeBehavior extends ModelBehavior {
|
||||||
$fields = array($Model->primaryKey, $Model->displayField, $left, $right);
|
$fields = array($Model->primaryKey, $Model->displayField, $left, $right);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$conditions = (array)$conditions;
|
||||||
|
if ($scope) {
|
||||||
|
$conditions[] = $scope;
|
||||||
|
}
|
||||||
|
|
||||||
|
$order = $Model->escapeField($left) . ' asc';
|
||||||
|
$results = $Model->find('all', compact('conditions', 'fields', 'order', 'recursive'));
|
||||||
|
|
||||||
|
return $this->formatTreeList($Model, $results, $keyPath, $valuePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats result of a find() call to a hierarchical array used for HTML select boxes.
|
||||||
|
*
|
||||||
|
* Note that when using your own find() call this expects the order to be "left" field asc in order
|
||||||
|
* to generate the same result as using generateTreeList() directly.
|
||||||
|
*
|
||||||
|
* @param Model $Model Model using this behavior
|
||||||
|
* @param array $results Result array of a find() call
|
||||||
|
* @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 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
|
||||||
|
*/
|
||||||
|
public function formatTreeList(Model $Model, array $results, $keyPath = null, $valuePath = null, $spacer = '_') {
|
||||||
|
if (empty($results)) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
|
extract($this->settings[$Model->alias]);
|
||||||
|
|
||||||
if (!$keyPath) {
|
if (!$keyPath) {
|
||||||
$keyPath = '{n}.' . $Model->alias . '.' . $Model->primaryKey;
|
$keyPath = '{n}.' . $Model->alias . '.' . $Model->primaryKey;
|
||||||
}
|
}
|
||||||
|
@ -456,13 +487,6 @@ class TreeBehavior extends ModelBehavior {
|
||||||
array_unshift($valuePath, '%s' . $valuePath[0], '{n}.tree_prefix');
|
array_unshift($valuePath, '%s' . $valuePath[0], '{n}.tree_prefix');
|
||||||
}
|
}
|
||||||
|
|
||||||
$conditions = (array)$conditions;
|
|
||||||
if ($scope) {
|
|
||||||
$conditions[] = $scope;
|
|
||||||
}
|
|
||||||
|
|
||||||
$order = $Model->escapeField($left) . " asc";
|
|
||||||
$results = $Model->find('all', compact('conditions', 'fields', 'order', 'recursive'));
|
|
||||||
$stack = array();
|
$stack = array();
|
||||||
|
|
||||||
foreach ($results as $i => $result) {
|
foreach ($results as $i => $result) {
|
||||||
|
@ -474,9 +498,7 @@ class TreeBehavior extends ModelBehavior {
|
||||||
$results[$i]['tree_prefix'] = str_repeat($spacer, $count);
|
$results[$i]['tree_prefix'] = str_repeat($spacer, $count);
|
||||||
$stack[] = $result[$Model->alias][$right];
|
$stack[] = $result[$Model->alias][$right];
|
||||||
}
|
}
|
||||||
if (empty($results)) {
|
|
||||||
return array();
|
|
||||||
}
|
|
||||||
return Hash::combine($results, $keyPath, $valuePath);
|
return Hash::combine($results, $keyPath, $valuePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1433,6 +1433,29 @@ class TreeBehaviorNumberTest extends CakeTestCase {
|
||||||
$this->assertEquals('__3 - 1.1.1', $result[3]);
|
$this->assertEquals('__3 - 1.1.1', $result[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the formatting options of formatTreeList()
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testFormatTreeList() {
|
||||||
|
extract($this->settings);
|
||||||
|
$this->Tree = new $modelClass();
|
||||||
|
$this->Tree->initialize(2, 2);
|
||||||
|
|
||||||
|
$options = array('order' => array('lft' => 'asc'));
|
||||||
|
$records = $this->Tree->find('all', $options);
|
||||||
|
|
||||||
|
$result = $this->Tree->formatTreeList(
|
||||||
|
$records,
|
||||||
|
"{n}.$modelClass.id",
|
||||||
|
array('%s - %s', "{n}.$modelClass.id", "{n}.$modelClass.name")
|
||||||
|
);
|
||||||
|
$this->assertEquals('1 - 1. Root', $result[1]);
|
||||||
|
$this->assertEquals('_2 - 1.1', $result[2]);
|
||||||
|
$this->assertEquals('__3 - 1.1.1', $result[3]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* testArraySyntax method
|
* testArraySyntax method
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue