You are here

user_variable.module in User variable 7

Same filename and directory in other branches
  1. 6 user_variable.module

User variable - Creating and working with user variables.

@author Oleg Sidorenko <orb@vseok.org.ua>

File

user_variable.module
View source
<?php

/**
 * @file
 * User variable - Creating and working with user variables.
 *
 * @author
 * Oleg Sidorenko <orb@vseok.org.ua>
 */

/**
 * Set a variable if it does not exist, then create.
 * @param $name
 *   Variable name.
 * @param $value
 *   Variable value.
 * @param $common
 *   If TRUE then the variable does not depend on user ID.
 * @param $uid
 *   User ID.
 * @param $expired
 *   The lifetime of a variable (in seconds)
 *   if equal 0 then the variable is not deleted automatically.
 * @param $session
 *   If TRUE then the variable is tied to the session
 *   when set, the variable is tied to the value (eg, session ID).
 */
function user_variable_set($name, $value, $common = FALSE, $uid = 0, $expired = 0, $session = FALSE) {
  global $user_variables_array, $user;
  $serialized_value = serialize($value);
  $expired = $expired > 0 ? time() + $expired : 0;
  if ($session === TRUE) {
    $session = session_id();
  }
  elseif ($session === FALSE) {
    $session = '';
  }
  if ($common) {
    $uid = $user->uid;
  }
  else {
    if (!$uid) {
      $uid = $user->uid;
    }
  }
  $query = db_merge('user_variable')
    ->fields(array(
    'sid' => $session,
    'value' => $serialized_value,
    'expired' => $expired,
    'common' => $common ? 1 : 0,
    'name' => $name,
    'uid' => $uid,
  ));
  if ($common) {
    $query
      ->key(array(
      'name' => $name,
      'common' => 1,
      'sid' => $session,
    ))
      ->execute();
    if ($session == '') {
      $user_variables_array['common_session'][$name] = $value;
    }
    else {
      $user_variables_array['common'][$session][$name] = $value;
    }
  }
  else {
    $query
      ->key(array(
      'name' => $name,
      'common' => 0,
      'sid' => $session,
      'uid' => $uid,
    ))
      ->execute();
    if ($session == '') {
      $user_variables_array['uid_session'][$uid][$name] = $value;
    }
    else {
      $user_variables_array['uid'][$uid][$session][$name] = $value;
    }
  }
}

/**
 * Get variable.
 * @param $name
 *   Variable name.
 * @param $common
 *   If TRUE then the variable does not depend on user ID.
 * @param $uid
 *   User ID.
 * @param $session
 *   Session ID.
 * 
 * @return
 *   Variable value.
 */
function user_variable_get($name, $common = FALSE, $uid = 0, $session = '') {
  global $user_variables_array, $user;
  if ($session === TRUE) {
    $session = session_id();
  }
  elseif ($session === FALSE) {
    $session = '';
  }
  if ($common) {
    if ($session == '') {
      if (isset($user_variables_array['common_session'][$name])) {
        return $user_variables_array['common_session'][$name];
      }
    }
    else {
      if (isset($user_variables_array['common'][$session][$name])) {
        return $user_variables_array['common'][$session][$name];
      }
    }

    //Get from DB
    $result = db_query_range("SELECT value FROM {user_variable}\n      WHERE common = 1 AND name = :name AND sid = :sid", 0, 1, array(
      ':name' => $name,
      ':sid' => $session,
    ))
      ->fetchObject();
    if ($session == '') {
      return $user_variables_array['common_session'][$name] = isset($result->value) && $result->value ? unserialize($result->value) : FALSE;
    }
    else {
      return $user_variables_array['common'][$session][$name] = isset($result->value) && $result->value ? unserialize($result->value) : FALSE;
    }
  }
  else {

    //$common = FALSE
    if (!$uid) {
      $uid = $user->uid;
    }
    if ($session == '') {
      if (isset($user_variables_array['uid_session'][$uid][$name])) {
        return $user_variables_array['uid_session'][$uid][$name];
      }
    }
    else {
      if (isset($user_variables_array['uid'][$uid][$session][$name])) {
        return $user_variables_array['uid'][$uid][$session][$name];
      }
    }

    //Get from DB
    $result = db_query_range("SELECT value FROM {user_variable}\n      WHERE name = :name AND common = 0 AND uid = :uid AND sid = :sid", 0, 1, array(
      ':name' => $name,
      ':uid' => $uid,
      ':sid' => $session,
    ))
      ->fetchObject();
    if ($session == '') {
      return $user_variables_array['uid_session'][$uid][$name] = isset($result->value) && $result->value ? unserialize($result->value) : FALSE;
    }
    else {
      return $user_variables_array['uid'][$uid][$session][$name] = isset($result->value) && $result->value ? unserialize($result->value) : FALSE;
    }
  }
}

/**
 * Remove the variable with the given name.
 * @param $name
 *   Variable name.
 * @param $common
 *   If TRUE then the variable does not depend on user ID.
 * @param $uid
 *   User ID.
 * @param $session
 *   Session ID.
 */
function user_variable_del($name, $common = FALSE, $uid = 0, $session = '') {
  global $user_variables_array, $user;
  if ($session === TRUE) {
    $session = session_id();
  }
  elseif ($session === FALSE) {
    $session = '';
  }
  if ($common) {
    db_query("DELETE FROM {user_variable}\n      WHERE common = 1 AND name = :name AND sid = :sid", array(
      ':name' => $name,
      ':sid' => $session,
    ));
    if ($session == '') {
      unset($user_variables_array['common_session'][$name]);
    }
    else {
      unset($user_variables_array['common'][$session][$name]);
    }
  }
  else {
    if (!$uid) {
      $uid = $user->uid;
    }
    db_query("DELETE FROM {user_variable}\n      WHERE common = 0 AND name = :name AND uid = :uid AND sid = :sid", array(
      ':name' => $name,
      ':uid' => $uid,
      ':sid' => $session,
    ));
    if ($session == '') {
      unset($user_variables_array['uid_session'][$uid][$name]);
    }
    else {
      unset($user_variables_array['uid'][$uid][$session][$name]);
    }
  }
}

/**
 * Implementation of hook_cron().
 */
function user_variable_cron() {
  db_query("DELETE FROM {user_variable} WHERE expired < :time AND expired <> 0", array(
    ':time' => time(),
  ));
}

Functions

Namesort descending Description
user_variable_cron Implementation of hook_cron().
user_variable_del Remove the variable with the given name.
user_variable_get Get variable.
user_variable_set Set a variable if it does not exist, then create.