You are here

function global_filter_set_on_session in Views Global Filter 7

Same name and namespace in other branches
  1. 8 global_filter.storage.inc \global_filter_set_on_session()

Sets the filter of the supplied name to the supplied value.

Parameters

string $name: eg 'field_country', must not be empty

mixed $value: to be set to filter of $name

Return value

bool TRUE if $value was set, FALSE if it wasn't

7 calls to global_filter_set_on_session()
global_filter_action_set_filter in ./global_filter.rules.inc
The action function for the 'global_filter_action_set_filter'.
global_filter_clear_filters in ./global_filter.module
Set all or the supplied global filters back to their global defaults.
global_filter_get_view_next_value in ./global_filter.module
In the supplied view return the successor to the supplied reference value.
global_filter_init in ./global_filter.module
Implements hook_init().
global_filter_set_form_on_session in ./global_filter.widgets.inc
Stashes the selected global filter value(s) in the user's session.

... See full list

File

./global_filter.storage.inc, line 181
global_filter.storage.inc

Code

function global_filter_set_on_session($name, $value) {

  // Provide hook_global_filter_value_alter(), so $value can be altered by
  // other modules.
  drupal_alter('global_filter_value', $name, $value);
  if (empty($name)) {
    drupal_set_message(t('Global Filter: filter name empty. Cannot set value.'), 'warning');
    return FALSE;
  }
  if (is_array($value)) {

    // For e.g., hierarchical select.
    $value = global_filter_array_flatten($value);
    if (empty($value)) {
      $value = '';
    }
  }

  // Gets and caches all filters.
  $prev_value = global_filter_get_session_value($name);
  if ($prev_value == $value) {
    global_filter_debug(t('Global Filter @name: no change in value. No need to set.', array(
      '@name' => $name,
    )));
    return FALSE;
  }

  // May return '' or NULL.
  $filters = global_filter_get_session_value();
  if (empty($filters)) {
    $filters = array();
  }

  // Update the selected filter with the new value.
  $filters[$name] = $value;

  // For Context Session module, http://drupal.org/project/context_session
  // @todo refactor into its own ugly function.
  if (module_exists('context_session') && !empty($_SESSION)) {

    // context_session does not support session_cache, so use $_SESSION.
    // Note that context_session only offers boolean support: it sets a
    // context based on the fact whether a key is present or not. So we have
    // to fake name=value pairs. We start by clearing out old values.
    foreach ($_SESSION as $key => $v) {
      if (strpos($key, "global_filter:{$name}=") === 0) {
        unset($_SESSION[$key]);
      }
    }

    // Now set the new value. Support the AND operator. ORs are achieved by
    // the user adding multiple session conditions.
    $val = is_array($value) ? implode(',', array_filter($value)) : $value;
    $_SESSION["global_filter:{$name}={$val}"] = TRUE;

    // If in the future Context Session accepts name|value pairs we can do this:
    // $_SESSION["global_filter:$name"] = $val;
  }
  $prev_value = !isset($prev_value) || $prev_value == '' ? t('all') : (is_array($prev_value) ? implode('+', $prev_value) : print_r($prev_value, TRUE));
  $value = !isset($value) || $value == '' ? t('all') : (is_array($value) ? implode('+', $value) : print_r($value, TRUE));
  global_filter_debug(t('Global Filter @name: changing from %prev_value to %value', array(
    '@name' => $name,
    '%prev_value' => $prev_value,
    '%value' => $value,
  )));
  if ($name != 'view_autocycle' && global_filter_get_module_parameter('mutex_mode', FALSE)) {
    global_filter_debug(t('Global Filter mutex mode is on, so setting other filters back to their global defaults...'));
    foreach (global_filter_get_parameter(NULL) as $filter) {
      $nm = $filter['name'];
      if ($nm && $nm != $name && $nm != 'view_autocycle') {
        $filters[$nm] = $val = global_filter_get_global_default($nm);
        $val = is_array($val) ? implode('+', array_filter($val)) : $val;
        if (module_exists('context_var')) {
          variable_set($nm, $val);
        }
        global_filter_debug(t('Global Filter %name: mutex reset to global default: "%value"', array(
          '%name' => $nm,
          '%value' => print_r($val, TRUE),
        )));
      }
    }
  }

  // Set all updated filters.
  session_cache_set('global_filter', $filters);

  // Invoke the Rules event: "A Views Global Filter has been set".
  if (module_exists('rules')) {
    rules_invoke_event('global_filter_set_filter_done', $name, $value);
  }
  return TRUE;
}