You are here

function _hierarchical_select_process_get_db_selection in Hierarchical Select 5.3

Same name and namespace in other branches
  1. 6.3 hierarchical_select.module \_hierarchical_select_process_get_db_selection()
  2. 7.3 hierarchical_select.module \_hierarchical_select_process_get_db_selection()

Get the current (flat) selection of the dropbox.

This selection is not updatable by the user, because the values are retrieved from the hidden values in $element['dropbox']['hidden']['lineages_selections']. This selection can only be updated by the server, i.e. when the user clicks the "Add" button. But this selection can still be reduced in size if the user has marked dropbox entries (lineages) for removal.

Parameters

$element: A hierarchical_select form element.

Return value

An array (bag) containing the ids of the selected items in the dropbox.

1 call to _hierarchical_select_process_get_db_selection()
_hierarchical_select_process_calculate_selections in ./hierarchical_select.module
Calculates the flat selections of both the hierarchical select and the dropbox.

File

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

Code

function _hierarchical_select_process_get_db_selection($element) {
  $db_selection = array();
  if (count($element['#value']['dropbox']['hidden']['lineages_selections'])) {

    // This is only present in #value if at least one "Remove" checkbox was
    // checked, so ensure that we're doing something valid.
    $remove_from_db_selection = !isset($element['#value']['dropbox']['visible']['lineages']) ? array() : array_keys($element['#value']['dropbox']['visible']['lineages']);

    // Add all selections to the dropbox selection, except for the ones that
    // are scheduled for removal.
    foreach ($element['#value']['dropbox']['hidden']['lineages_selections'] as $x => $selection) {
      if (!in_array($x, $remove_from_db_selection)) {
        $db_selection = array_merge($db_selection, unserialize($selection));
      }
    }

    // Ensure that the last item of each selection that was scheduled for
    // removal is completely absent from the dropbox selection.
    // In case of a tree with multiple parents, the same item can exist in
    // different entries, and thus it would stay in the selection. When the
    // server then reconstructs all lineages, the lineage we're removing, will
    // also be reconstructed: it will seem as if the removing didn't work!
    // This will not break removing dropbox entries for hierarchies without
    // multiple parents, since items at the deepest level are always unique to
    // that specific lineage.
    // Easier explanation at http://drupal.org/node/221210#comment-733715.
    foreach ($remove_from_db_selection as $key => $x) {
      $item = end(unserialize($element['#value']['dropbox']['hidden']['lineages_selections'][$x]));
      $position = array_search($item, $db_selection);
      if ($position) {
        unset($db_selection[$position]);
      }
    }
    $db_selection = array_unique($db_selection);
  }
  return $db_selection;
}