Skip to content

Commit

Permalink
simplify method
Browse files Browse the repository at this point in the history
  • Loading branch information
raideus committed Jun 22, 2015
1 parent 324fd9b commit 679aaa1
Showing 1 changed file with 14 additions and 48 deletions.
62 changes: 14 additions & 48 deletions src/TermsWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TermsWalker extends StdObject {
private $taxonomy;
private $term_identifier;
private $elements;
private $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
private static $db_fields = array ('parent' => 'parent', 'id' => 'term_id');

function __construct( $args = array(), $term_args ) {
$defaults = array( 'taxonomy' => 'category',
Expand Down Expand Up @@ -49,6 +49,7 @@ function __construct( $args = array(), $term_args ) {
* @return array An Array of nested taxonomy terms
*/
public function build_nested_terms_array( $max_depth ) {
$parent_field = self::$db_fields['parent'];
$parent_element_array = array();
$value_array = array();

Expand All @@ -58,56 +59,21 @@ public function build_nested_terms_array( $max_depth ) {
if (empty($this->elements)) //nothing to walk
return $value_array;

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

// Max depth of -1 means no hierarchy
if ( -1 == $max_depth ) {
$empty_array = array();
foreach ( $this->elements as $e ) {
$this->add_element( $e, $value_array,
$parent_element_array,
$empty_array, 1, 0);
}
return $value_array;
return $this->build_basic_terms_array();
}

/*
* Separate elements into two buckets: top level and children elements.
* Children_elements is two dimensional array, eg.
* Children_elements[10][] contains all sub-elements whose parent is 10.
*/
$top_level_elements = array();
$children_elements = array();
$elements_table = array(0 => array());
foreach ( $this->elements as $e) {
if ( 0 == $e->$parent_field )
$top_level_elements[] = $e;
else
$children_elements[ $e->$parent_field ][] = $e;
$elements_table[ $e->$parent_field ][] = $e;
}

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

$first = array_slice( $this->elements, 0, 1 );
$root = $first[0];

$top_level_elements = array();
$children_elements = array();
foreach ( $this->elements as $e) {
if ( $root->$parent_field == $e->$parent_field )
$top_level_elements[] = $e;
else
$children_elements[ $e->$parent_field ][] = $e;
}
foreach ( $elements_table[0] as $e ) {
$this->add_element($e, $value_array, $parent_element_array,
$elements_table, $max_depth, 0);
}

foreach ( $top_level_elements as $e )
$this->add_element( $e, $value_array, $parent_element_array,
$children_elements, $max_depth, 0);

return $value_array;
}

Expand All @@ -120,14 +86,14 @@ public function build_nested_terms_array( $max_depth ) {
* @param array $element Term element to be added to the array
* @param array $value_array Master array to add the element to
* @param array $parent_element_array Parent term of the current element
* @param array $children_elements Array of all child elements
* @param array $elements_table Table of all elements index by parent_id
* @param int $max_depth The maximum number of nested levels to recurse
* @param int $depth The current depth of recursion
*/
public function add_element($element, &$value_array, &$parent_element_array,
&$children_elements, $max_depth, $depth) {
&$elements_table, $max_depth, $depth) {

$id_field = $this->db_fields['id'];
$id_field = self::$db_fields['id'];
$id = $element->$id_field;
$term_identifier = $this->term_identifier;

Expand All @@ -138,13 +104,13 @@ public function add_element($element, &$value_array, &$parent_element_array,
);

$has_children = false;
if (!empty($children_elements) && !empty($children_elements[$id])) {
if (!empty($elements_table) && !empty($elements_table[$id])) {
$has_children = true;
}

if ( ($max_depth == 0 || $max_depth > $depth+1 ) && $has_children ) {
foreach($children_elements[$element->term_id] as $child) {
$this->add_element($child, $value_array, $el, $children_elements,
foreach($elements_table[$element->term_id] as $child) {
$this->add_element($child, $value_array, $el, $elements_table,
$max_depth, $depth+1);
}
}
Expand Down

0 comments on commit 679aaa1

Please sign in to comment.