xaraya
(3,428 views)

Managing user variables

There are a bunch of different ways to keep data and pass them around between modules, pages, and sessions. I've collected my notes in one place, here.

Xaraya uses different kinds of variables for different purposes:

  • Configuration vars - site wide configuration options
  • Module vars - module specific configuration options
  • Theme variables - theme-dependent variables
  • Module User vars - user-dependent overrides for the module variables above
  • User variables - user-dependent values (must be defined in roles/DD)
  • Session variables - preferences etc specific for one user esssion
  • Cached variables - temporary values cached for the duration of a page request (replaces GLOBALS in php)

I won't further describe the first 3 because I dont see anyone using them via templates or API (just the default admin templates). The others may be useful in various applications.

Deciding which variable type to use depends on your situation and "who owns" the variable.

 

Cached Vars

For sharing variables between modules and templates for the duration of a single page request (replaces GLOBALS in php).

Cached vars are organized into separate 'collections'. Each module for example may have its own variable cache. You too can make your own variable cache, or just use an existing one( like 'base').

xarVarSetCached( 'mycache', 'varname', $val )

$val = xarVarGetCached( 'mycache', 'varname' )

Note, when using this its important to keep in mind the order in which templates are processed. The current order of execution is

  1. module function
  2. module function template
  3. page template
  4. blocks in page template (by group)

Session Vars

If you want variables to persist across pages within the current session

xarSessionSetVar( 'varname', $val )

$val = xarSessionGetVar( 'varname' )

xarSessionDelVar( 'varname' )

 

User Vars

for accessing values associated with the current user as stored in the Roles module, including vars in the default roles profile, and any extended with dynamicdata hooks.

xarUserSetVar( 'varname', $val )

$val = xarUserGetVar( 'varame' )

Examples:

$uid = xarUserGetVar( 'uid' );

Default roles vars:

  • uid - id number of the user
  • uname - handle or nick, must be unique
  • name - display name, eg full name shown in user list
  • email
  • userhome - users home page, shown when logs in
  • primaryparent - main parent group takes precedence for group home page etc
  • passwordupdate - date time of password last changed
  • userlastlogin - date time of last login

 

Module User Vars

User-specific variables can be stored on a module by module basis.  This is basically the same as xarModSetVar, but this allows for setting variable values which are tied to a specific user for a certain module.  Typical usage is storing user preferences. Only deviations from the module vars are stored.

First, you have to create the variable in the db for the module, e.g.

xarModSetUserVar( 'roles', 'formcompleted', 0 )

call this only once for each new user. Then to access it,

$val = xarModGetUserVar( 'modname', 'varname' )

xarModSetUserVar( 'modname', 'varname', $val )

function xarModSetUserVar($modName, $name, $value, $uid=NULL)

function xarModGetUserVar($modName, $name, $uid = NULL, $prep = NULL) 

function xarModDelUserVar($modName, $name, $uid=NULL) 

* @param modName The name of the module to set a user variable for
 * @param name    The name of the variable to set
 * @param value   Value to set the variable to.
 * @param uid     User id for which value needs to be set
 * @return bool true on success false on failure
 

 

 

Comments

New Comment

markdown formatting permitted