You are here

function _hierarchical_select_process_calculate_selections in Hierarchical Select 5.3

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

Calculates the flat selections of both the hierarchical select and the dropbox.

Parameters

$element: A hierarchical_select form element.

Return value

An array of the following structure: array( $hierarchical_select_selection = array(), // Flat list of selected ids. $dropbox_selection = array(), ) with both of the subarrays flat lists of selected ids. The _hierarchical_select_hierarchy_generate() and _hierarchical_select_dropbox_generate() functions should be applied on these respective subarrays.

See also

_hierarchical_select_hierarchy_generate()

_hierarchical_select_dropbox_generate()

1 call to _hierarchical_select_process_calculate_selections()
hierarchical_select_process in ./hierarchical_select.module
Hierarchical select form element type #process callback.

File

./hierarchical_select.module, line 881
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_calculate_selections(&$element) {
  $hs_selection = array();

  // hierarchical select selection
  $db_selection = array();

  // dropbox selection
  $config = _hierarchical_select_inherit_default_config($element['#config']);
  $dropbox = (bool) $config['dropbox']['status'];
  $op = $element['#post']['op'];
  if (empty($element['#post'])) {
    $value = isset($element['#value']) ? $element['#value'] : $element['#default_value'];
    $value = is_array($value) ? $value : array(
      $value,
    );
    if ($dropbox) {
      $db_selection = $value;
    }
    else {
      $hs_selection = $value;
    }
  }
  else {
    if ($dropbox && $op == t('Add')) {
      $hs_selection = _hierarchical_select_process_get_hs_selection($element);
      $db_selection = _hierarchical_select_process_get_db_selection($element);

      // Add $hs_selection to $db_selection (automatically filters to keep
      // only the unique ones).
      $db_selection = array_merge($db_selection, $hs_selection);

      // Only reset $hs_selection if the user has configured it that way.
      if ($config['dropbox']['reset_hs']) {
        $hs_selection = array();
      }
    }
    else {
      if ($op == t('Create')) {

        // This code handles both the creation of a new item in an existing
        // level and the creation of an item that also creates a new level.
        // TODO: http://drupal.org/node/253868
        // TODO: http://drupal.org/node/253869
        $label = trim($element['#value']['hierarchical_select']['create_new_item']['input']);
        $selects = $element['#value']['hierarchical_select']['selects'];
        $depth = count($selects);
        $parent = $depth > 0 ? end($selects) : 0;

        // Disallow items with empty labels; allow the user again to create a
        // (proper) new item.
        if (empty($label)) {
          $element['#value']['hierarchical_select']['selects'][count($selects)] = 'create_new_item';
        }
        else {
          if ((count(module_invoke($config['module'], 'hierarchical_select_children', $parent, $config['params'])) || $config['editability']['max_levels'] == 0 || $depth < $config['editability']['max_levels']) && _hierarchical_select_create_new_item_is_allowed($config, $depth)) {

            // Create the new item in the hierarchy and retrieve its value.
            $value = module_invoke($config['module'], 'hierarchical_select_create_item', check_plain($label), $parent, $config['params']);

            // Ensure the newly created item will be selected after rendering.
            if ($value) {

              // Pretend there was a select where the "create new item" section
              // was, and assign it the value of the item that was just created.
              $element['#value']['hierarchical_select']['selects'][count($selects)] = $value;
            }
          }
        }
        $hs_selection = _hierarchical_select_process_get_hs_selection($element);
        if ($dropbox) {
          $db_selection = _hierarchical_select_process_get_db_selection($element);
        }
      }
      else {

        // This handles the cases of:
        // - $op == t('Update')
        // - $op == t('Cancel') (used when creating a new item or a new level)
        // - any other submit button, e.g. the "Preview" button
        $hs_selection = _hierarchical_select_process_get_hs_selection($element);
        if ($dropbox) {
          $db_selection = _hierarchical_select_process_get_db_selection($element);
        }
      }
    }
  }

  // Prevent doubles in either array.
  $hs_selection = array_unique($hs_selection);
  $db_selection = array_unique($db_selection);
  return array(
    $hs_selection,
    $db_selection,
  );
}