You are here

user_variable.module in User variable 6

Same filename and directory in other branches
  1. 7 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;
    }
  }
  $o_variable = new stdClass();
  $o_variable->sid = $session;
  $o_variable->value = $serialized_value;
  $o_variable->expired = $expired;
  $o_variable->common = $common ? 1 : 0;
  $o_variable->name = $name;
  $o_variable->uid = $uid;
  if ($common) {
    drupal_write_record('user_variable', $o_variable, array(
      'name',
      'common',
      'sid',
    ));
    if ($session == '') {
      $user_variables_array['common_session'][$name] = $value;
    }
    else {
      $user_variables_array['common'][$session][$name] = $value;
    }
  }
  else {
    drupal_write_record('user_variable', $o_variable, array(
      'name',
      'common',
      'uid',
      'sid',
    ));
    if ($session == '') {
      $user_variables_array['uid_session'][$uid][$name] = $value;
    }
    else {
      $user_variables_array['uid'][$uid][$session][$name] = $value;
    }
  }
  if (!db_affected_rows()) {
    drupal_write_record('user_variable', $o_variable);
  }
}

/**
 * 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_fetch_object(db_query_range("SELECT value FROM {user_variable}\n      WHERE common = 1 AND name = '%s' AND sid = '%s'", $name, $session, 0, 1));
    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_fetch_object(db_query_range("SELECT value FROM {user_variable}\n      WHERE name = '%s' AND common = 0 AND uid = %d AND sid = '%s'", $name, $uid, $session, 0, 1));
    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 = '%s' AND sid = '%s'", $name, $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 = '%s' AND uid = %d AND sid = '%s'", $name, $uid, $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 < %d AND expired <> 0", 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.