You are here

function _hierarchical_select_reconstruct_lineages_save_lineage_enabled in Hierarchical Select 5.2

Helper function to reconstruct the lineages given a set of selected items and the fact that the "save lineage" option is enabled.

Note that it's impossible to predict how many lineages if we know the number of selected items, exactly because the "save lineage" option is enabled.

Worst case time complexity is O(n^3), optimizations are still possible.

Parameters

$module: The module that should be used for HS hooks.

$selection: The selection based on which a dropbox should be generated.

$params: An array of parameters, which may be necessary for some implementations. Optional.

Return value

An array of dropbox lineages.

1 call to _hierarchical_select_reconstruct_lineages_save_lineage_enabled()
hierarchical_select_get_dropbox in ./hierarchical_select.module
Generate the dropbox object.

File

./hierarchical_select.module, line 604
This module defines the "hierarchical_select" form element, which is a greatly enhanced way for letting the user select an option in a hierarchy.

Code

function _hierarchical_select_reconstruct_lineages_save_lineage_enabled($module, $selection, $params) {

  // We have to reconstruct all lineages from the given set of selected
  // items. That means: we have to reconstruct every possible combination!
  $lineages = array();
  $root_level = module_invoke($module, 'hierarchical_select_root_level', $params);
  foreach ($selection as $key => $item) {

    // Create new lineage if the item can be found in the root level.
    if (in_array($item, array_keys($root_level))) {
      $lineages[][0] = array(
        'value' => $item,
        'label' => $root_level[$item],
      );
      unset($selection[$key]);
    }
  }

  // Keep on trying as long as at least one lineage has been extended.
  $at_least_one = TRUE;
  for ($i = 0; $at_least_one; $i++) {
    $at_least_one = FALSE;
    $num = count($lineages);

    // Try to improve every lineage. Make sure we don't iterate over
    // possibly new lineages.
    for ($id = 0; $id < $num; $id++) {
      $children = module_invoke($module, 'hierarchical_select_children', $lineages[$id][$i]['value'], $params);
      $child_added_to_lineage = FALSE;
      foreach (array_keys($children) as $child) {
        if (in_array($child, $selection)) {
          if (!$child_added_to_lineage) {

            // Add the child to the lineage.
            $lineages[$id][$i + 1] = array(
              'value' => $child,
              'label' => $children[$child],
            );
            $child_added_to_lineage = TRUE;
            $at_least_one = TRUE;
          }
          else {

            // Create new lineage based on current one and add the child.
            $lineage = $lineages[$id];
            $lineage[$i + 1] = array(
              'value' => $child,
              'label' => $children[$child],
            );

            // Add the new lineage to the set of lineages
            $lineages[] = $lineage;
          }
        }
      }
    }
  }
  return $lineages;
}