You are here

function _hierarchical_select_process_calculate_selections in Hierarchical Select 7.3

Same name and namespace in other branches
  1. 5.3 hierarchical_select.module \_hierarchical_select_process_calculate_selections()
  2. 6.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.

$form_state: The $form_state array. We need to look at $form_state['input']['op'], to know which operation has occurred.

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()
form_hierarchical_select_process in ./hierarchical_select.module
Hierarchical select form element type #process callback.

File

./hierarchical_select.module, line 1126
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, $hsid, &$form_state) {
  $hs_selection = array();

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

  // dropbox selection
  $config = _hierarchical_select_inherit_default_config($element['#config']);
  $dropbox = (bool) $config['dropbox']['status'];

  // When:
  // - no input data was provided (through POST nor GET)
  // - or #value is set directly and not by a Hierarchical Select POST (and
  //   therefor set either manually or by another module),
  // then use the value of #default_value, or when available, of #value.
  if (empty($form_state['input']) || !isset($element['#value']['hierarchical_select']) && !isset($element['#value']['dropbox'])) {
    $value = !empty($element['#value']) ? $element['#value'] : $element['#default_value'];
    $value = is_array($value) ? $value : array(
      $value,
    );
    if ($dropbox) {
      $db_selection = $value;
    }
    else {
      $hs_selection = $value;
    }
  }
  else {
    $op = isset($form_state['input']['op']) && isset($form_state['input']['hsid']) && $form_state['input']['hsid'] == $hsid ? $form_state['input']['op'] : NULL;
    if ($dropbox && $op == t('Add')) {
      $hs_selection = _hierarchical_select_process_get_hs_selection($element);
      $db_selection = _hierarchical_select_process_get_db_selection($element, $hsid, $form_state);

      // Add $hs_selection to $db_selection.
      $db_selection = array_unique(array_merge($db_selection, $hs_selection));

      // Only reset $hs_selection if the user has configured it that way.
      if ((bool) $config['dropbox']['reset_hs']) {
        $hs_selection = array();
      }
    }
    elseif ($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.
      $label = trim($element['#value']['hierarchical_select']['create_new_item']['input']);
      $selects = isset($element['#value']['hierarchical_select']['selects']) ? $element['#value']['hierarchical_select']['selects'] : array();
      $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';
      }
      elseif ((count(module_invoke($config['module'], 'hierarchical_select_children', $parent, $config['params'])) || $config['editability']['max_levels'] == 0 && $config['allowed_max_depth'] == 0 || $config['editability']['max_levels'] == 0 && $depth < $config['allowed_max_depth'] || $depth < $config['editability']['max_levels'] && $config['allowed_max_depth'] == 0 || $depth < $config['editability']['max_levels'] && $depth < $config['allowed_max_depth']) && _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, $hsid, $form_state);
      }
    }
    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, $hsid, $form_state);
      }
    }
  }

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