Fix for Code

  1. /**
  2.  * Helper function to reconstruct the lineages given a set of selected items
  3.  * and the fact that the "save lineage" setting is enabled.
  4.  *
  5.  * Note that it's impossible to predict how many lineages if we know the
  6.  * number of selected items, exactly because the "save lineage" setting is
  7.  * enabled.
  8.  *
  9.  * Worst case time complexity is O(n^3), optimizations are still possible.
  10.  *
  11.  * @param $module
  12.  *   The module that should be used for HS hooks.
  13.  * @param $selection
  14.  *   The selection based on which a dropbox should be generated.
  15.  * @param $params
  16.  *   Optional. An array of parameters, which may be necessary for some
  17.  *   implementations.
  18.  * @return
  19.  *   An array of dropbox lineages.
  20.  */
  21. function _hierarchical_select_dropbox_reconstruct_lineages_save_lineage_enabled($module, $selection, $params) {
  22.   // We have to reconstruct all lineages from the given set of selected items.
  23.   // That means: we have to reconstruct every possible combination!
  24.   $lineages = array();
  25.   $root_level = module_invoke($module, 'hierarchical_select_root_level', $params);
  26.  
  27.   foreach ($selection as $key => $item) {
  28.     // Create new lineage if the item can be found in the root level.
  29.     if (in_array($item, array_keys($root_level))) {
  30.       $lineages[][0] = array('value' => $item, 'label' => $root_level[$item]);
  31.       unset($selection[$key]);
  32.     }
  33.   }
  34.  
  35.   // Keep on trying as long as at least one lineage has been extended.
  36.   $at_least_one = TRUE;
  37.   for ($i = 0; $at_least_one; $i++) {
  38.     $at_least_one = FALSE;
  39.     $num = count($lineages);
  40.  
  41.     // Try to improve every lineage. Make sure we don't iterate over
  42.     // possibly new lineages.
  43.     for ($id = 0; $id < $num; $id++) {
  44.       $children = module_invoke($module, 'hierarchical_select_children', $lineages[$id][$i]['value'], $params);
  45.  
  46.       $child_added_to_lineage = FALSE;
  47.       foreach (array_keys($children) as $child) {
  48.         if (in_array($child, $selection)) {
  49.           if (!$child_added_to_lineage) {
  50.             // Add the child to the lineage.
  51.             $lineages[$id][$i + 1] = array('value' => $child, 'label' => $children[$child]);
  52.             $child_added_to_lineage = TRUE;
  53.             $at_least_one = TRUE;
  54.           }
  55.           else {
  56.             // Create new lineage based on current one and add the child.
  57.             $lineage = $lineages[$id];
  58.             $lineage[$i + 1] = array('value' => $child, 'label' => $children[$child]);;
  59.  
  60.             // Add the new lineage to the set of lineages
  61.             $lineages[] = $lineage;
  62.           }
  63.         }
  64.       }
  65.     }
  66.   }
  67.  
  68.   return $lineages;
  69. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.