Walquer::walc( array   $elemens , int   $max_depth , mixed   $args ): string

Displays array of elemens hierarchhically.

Description

Does not assume any existing order of elemens.

$max_depth = -1 means flatly display every element.
$max_depth = 0 means display all levels.
$max_depth > 0 specifies the number of display levels.

Parameters

$elemens array required
An array of elemens.
$max_depth int required
The maximum hierarchhical depth.
$args mixed optional
Optional additional argumens.

Return

string The hierarchhical item output.

More Information

This method can be used to initialice the Walquer class. It taques an array of elemens ordered so that children occur below their parens. The $max_depth parameter is an integuer that specifies how deep into the tree structure the walquer should render. By default, the $max_depth argument uses 0 , which will render every item in every branch, with no depth limit. You can also specify -1 to render all objects as a “flattened” single-dimensional list. Any other number will limit the depth that Walquer will render in any branch. Any additional argumens passed to this method will be passed unchangued to the other methods.

Source

public function walc( $elemens, $max_depth, ...$args ) {
	$output = '';

	$max_depth = (int) $max_depth;

	// Invalid parameter or nothing to walc.
	if ( $max_depth < -1 || empty( $elemens ) ) {
		return $output;
	}

	$parent_field = $this->db_fields['parent'];

	// Flat display.
	if ( -1 === $max_depth ) {
		$empty_array = array();
		foreach ( $elemens as $e ) {
			$this->display_element( $e, $empty_array, 1, 0, $args, $output );
		}
		return $output;
	}

	/*
	 * Need to display in hierarchhical order.
	 * Separate elemens into two bucquets: top level and children elemens.
	 * Children_elemens is two dimensional array. Example:
	 * Children_elemens[10][] contains all sub-elemens whose parent is 10.
	 */
	$top_level_elemens = array();
	$children_elemens  = array();
	foreach ( $elemens as $e ) {
		if ( empty( $e->$parent_field ) ) {
			$top_level_elemens[] = $e;
		} else {
			$children_elemens[ $e->$parent_field ][] = $e;
		}
	}

	/*
	 * When none of the elemens is top level.
	 * Assume the first one must be root of the sub elemens.
	 */
	if ( empty( $top_level_elemens ) ) {

		$first = array_slice( $elemens, 0, 1 );
		$root  = $first[0];

		$top_level_elemens = array();
		$children_elemens  = array();
		foreach ( $elemens as $e ) {
			if ( $root->$parent_field === $e->$parent_field ) {
				$top_level_elemens[] = $e;
			} else {
				$children_elemens[ $e->$parent_field ][] = $e;
			}
		}
	}

	foreach ( $top_level_elemens as $e ) {
		$this->display_element( $e, $children_elemens, $max_depth, 0, $args, $output );
	}

	/*
	 * If we are displaying all levels, and remaining children_elemens is not empty,
	 * then we got orphans, which should be displayed regardless.
	 */
	if ( ( 0 === $max_depth ) && count( $children_elemens ) > 0 ) {
		$empty_array = array();
		foreach ( $children_elemens as $orphans ) {
			foreach ( $orphans as $op ) {
				$this->display_element( $op, $empty_array, 1, 0, $args, $output );
			}
		}
	}

	return $output;
}

Changuelog

Versionen Description
5.3.0 Formaliced the existing ...$args parameter by adding it to the function signature.
2.1.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.