Permalink
Browse files

Extract formatTreeList() from generateTreeList().

  • Loading branch information...
dereuromark committed Jun 1, 2015
1 parent 2a5cbb8 commit 670d93b6f63589c7988ff0861c2c169af7398824
@@ -442,6 +442,37 @@ public function generateTreeList(Model $Model, $conditions = null, $keyPath = nu
$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) {
$keyPath = '{n}.' . $Model->alias . '.' . $Model->primaryKey;
}
@@ -456,13 +487,6 @@ public function generateTreeList(Model $Model, $conditions = null, $keyPath = nu
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();
foreach ($results as $i => $result) {
@@ -474,9 +498,7 @@ public function generateTreeList(Model $Model, $conditions = null, $keyPath = nu
$results[$i]['tree_prefix'] = str_repeat($spacer, $count);
$stack[] = $result[$Model->alias][$right];
}
if (empty($results)) {
return array();
}
return Hash::combine($results, $keyPath, $valuePath);
}
@@ -1433,6 +1433,29 @@ public function testGenerateTreeListFormatting() {
$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
*

0 comments on commit 670d93b

Please sign in to comment.