CoolCMS API

TreeStructure
in package

Table of Contents

Methods

buildTree()  : array<int, T, children: list}>
Converts flat objects with `lft`, `rgt` and `level` into a nested array.
findNodeContext()  : array{parent: object|null, children: array}|null
Finds the node of the given item in the tree and returns its children and parent.
prepareForTemplate()  : array<int, T, children: list}>
Returns data prepared for recursive rendering in Latte.
withoutInactiveBranches()  : array<int, T>
Drops nodes that are not active together with their whole subtree (nested set: lft/rgt inside the interval of a dropped node). A node without `getActive()` counts as active.
planTree()  : array{roots: list, children: array>}|null
Plans the tree in one pass with a stack of ancestors, O(n) instead of O(n^2).

Methods

buildTree()

Converts flat objects with `lft`, `rgt` and `level` into a nested array.

public static buildTree(array<string|int, T> $items) : array<int, T, children: list}>
Parameters
$items : array<string|int, T>

nested-set nodes (lft, rgt, level, id)

Tags
template
Return values
array<int, T, children: list}> —

nested structure (children has the same shape)

findNodeContext()

Finds the node of the given item in the tree and returns its children and parent.

public static findNodeContext(array<string|int, mixed> $nodes, object|null $current[, object|null $parent = null ]) : array{parent: object|null, children: array}|null

Shared implementation for category lists (Blog, Bazaar, Comcat, Discussion, Eshop).

The parent comes from the traversal, not from getParentId(): category entities have no getParent(), and looking it up separately would cost a second query over data already in the tree.

IMPORTANT: returns null, not an array of empty values, when no item is given or it is not found in the tree. That lets the template tell a state without context (show roots) from a leaf category with nothing to show (children => []).

NOTE: for a root item parent is null, because a root has nothing above it.

NOTE: nodes that are not arrays or carry no object in item are skipped (the helper returns an untyped array, so the shape cannot be relied on statically).

Parameters
$nodes : array<string|int, mixed>

nodes from prepareForTemplate(), i.e. ['item' => object, 'children' => [...]]

$current : object|null

item to find (typically the current category)

$parent : object|null = null

node one level up; passed by the recursion, callers leave the default

Return values
array{parent: object|null, children: array}|null

prepareForTemplate()

Returns data prepared for recursive rendering in Latte.

public static prepareForTemplate(array<string|int, T> $items) : array<int, T, children: list}>
Parameters
$items : array<string|int, T>

nested-set nodes (lft, rgt, level, id)

Tags
template
Return values
array<int, T, children: list}> —

data prepared for rendering

withoutInactiveBranches()

Drops nodes that are not active together with their whole subtree (nested set: lft/rgt inside the interval of a dropped node). A node without `getActive()` counts as active.

public static withoutInactiveBranches(array<string|int, T> $items) : array<int, T>

A filter active = 1 alone would keep the active children of a disabled parent, and buildTree() would then attach them to an unrelated node.

Parameters
$items : array<string|int, T>
Tags
template
Return values
array<int, T>

planTree()

Plans the tree in one pass with a stack of ancestors, O(n) instead of O(n^2).

private static planTree(array<int, int> $lft, array<int, int> $rgt, array<int, int> $level) : array{roots: list, children: array>}|null

The slow path scans all items backwards for every node to find its parent. On properly nested intervals the parent is always among the ancestors on the stack.

The result matches the slow path including its quirks (the first item is a root regardless of level, a node without a parent one level up is dropped). When the data is not a clean nested set it returns null and buildTree() takes the slow path.

Parameters
$lft : array<int, int>

values sorted by lft, indexed 0..n-1

$rgt : array<int, int>
$level : array<int, int>
Return values
array{roots: list, children: array>}|null
On this page

Search results