You are here

function fieldset_helper_state_manager_get_lookup_id in Fieldset helper 7.2

Same name and namespace in other branches
  1. 6.2 fieldset_helper.module \fieldset_helper_state_manager_get_lookup_id()
  2. 6 fieldset_helper.module \fieldset_helper_state_manager_get_lookup_id()

Get the lookup id for the $element_id in the current path.

Parameters

$element_id: The DOM element id.

Return value

The numeric auto generated look up id for the $element_id. If $element_id is not set then the entire lookup id table for the current page will returned.

2 calls to fieldset_helper_state_manager_get_lookup_id()
fieldset_helper_js_alter in ./fieldset_helper.module
Implements hook_js_alter().
fieldset_helper_state_manager_get_state in ./fieldset_helper.module
Get fieldset's collapsed state.

File

./fieldset_helper.module, line 286

Code

function fieldset_helper_state_manager_get_lookup_id($element_id = NULL) {
  static $lookup_id_table;
  $path = fieldset_helper_state_manager_get_path();

  // Load existing lookup ids for the current path from the database.
  if (!isset($lookup_id_table)) {
    $lookup_id_table = array();

    // Fetch lookup records for the current path. Use sorting to make sure global path (*) are last.
    $result = db_query("SELECT id, element_id FROM {fieldset_helper_state_manager} WHERE path=:path OR path='*' ORDER BY path DESC", array(
      ':path' => $path,
    ));
    foreach ($result as $record) {
      $lookup_id_table[$record->element_id] = $record->id;
    }
  }

  // Create a new lookup id for element_id's not associated with the lookup id table.
  if ($element_id != NULL && !isset($lookup_id_table[$element_id])) {

    // Get element path.
    $element_path = fieldset_helper_state_manager_get_element_path($element_id, $path);

    // Get id for path and element_id combination.
    $lookup_id = db_insert('fieldset_helper_state_manager')
      ->fields(array(
      'path' => $element_path,
      'element_id' => $element_id,
    ))
      ->execute();
    $lookup_id_table[$element_id] = $lookup_id;
  }

  // Return the look up id for the element id.
  return $element_id == NULL ? $lookup_id_table : $lookup_id_table[$element_id];
}