You are here

function privatemsg_get_setting in Privatemsg 7.2

Same name and namespace in other branches
  1. 6.2 privatemsg.module \privatemsg_get_setting()

Retrieve a user setting.

First, the entries in {pm_setting} are loaded. If there is no value for the global, a variable with the name privatemsg_setting_$setting is also checked.

Parameters

$setting: Name of the setting.

$ids: For which ids should be looked. Keyed by the type, the value is an array of ids for that type. The first key is the most specific (typically user), followed by optional others, ordered by importance. For example roles and then global.

$default: The default value if none was found. Defaults to NULL.

Return value

The most specific value found.

See also

privatemsg_get_default_settings_ids().

5 calls to privatemsg_get_setting()
PrivatemsgAPITestCase::testSettings in ./privatemsg.test
Tests for the privatemsg_*_setting() functions.
privatemsg_is_disabled in ./privatemsg.module
Checks the status of private messaging for provided user.
_pm_email_notify_only_user in pm_email_notify/pm_email_notify.module
Checks if a user should only be notified when addressed directly.
_pm_email_notify_show_sender in pm_email_notify/pm_email_notify.module
Checks if a message author wants his email address as a sender.
_pm_email_notify_user_level in pm_email_notify/pm_email_notify.module
Retrieve notification level of a user.

File

./privatemsg.module, line 3467
Allows users to send private messages to other users.

Code

function privatemsg_get_setting($setting, $ids = NULL, $default = NULL) {
  $cache =& drupal_static('privatemsg_settings', array());
  if (empty($ids)) {
    $ids = privatemsg_get_default_setting_ids();
  }

  // First, try the static cache with the most specific type only. Do not check
  // others since there might be a more specific setting which is not yet
  // cached.
  $type_ids = reset($ids);
  $type = key($ids);
  foreach ($type_ids as $type_id) {
    if (isset($cache[$setting][$type][$type_id]) && $cache[$setting][$type][$type_id] !== FALSE && $cache[$setting][$type][$type_id] >= 0) {
      return $cache[$setting][$type][$type_id];
    }
  }

  // Second, look for all uncached settings.
  $query_ids = array();
  foreach ($ids as $type => $type_ids) {
    foreach ($type_ids as $type_id) {
      if (!isset($cache[$setting][$type][$type_id])) {
        $query_ids[$type][] = $type_id;

        // Default to FALSE for that value in case nothing can be found.
        $cache[$setting][$type][$type_id] = FALSE;
      }
    }
  }

  // If there are any, query them.
  if (!empty($query_ids)) {

    // Build the query and execute it.
    $query = _privatemsg_assemble_query('settings', $setting, $query_ids);
    foreach ($query
      ->execute() as $row) {
      $cache[$setting][$row->type][$row->id] = $row->value;
    }

    // If there is no global default in the database, try to get one with
    // variable_get().
    if ($cache[$setting]['global'][0] === FALSE) {
      $cache[$setting]['global'][0] = variable_get('privatemsg_setting_' . $setting, FALSE);
    }
  }

  // Now, go over all cached settings and return the first match.
  foreach ($ids as $type => $type_ids) {
    foreach ($type_ids as $type_id) {
      if (isset($cache[$setting][$type][$type_id]) && $cache[$setting][$type][$type_id] !== FALSE && $cache[$setting][$type][$type_id] >= 0) {
        return $cache[$setting][$type][$type_id];
      }
    }
  }

  // Nothing matched, return the provided default.
  return $default;
}