MRU Manager class

Almost any program with "friendly" user interface gives a possibility to open one of last viewed/edited files, projects, etc. Last open file moves on the top of the list, the length of a list is usually not greater than 4-8 items. It's called MRU - Most Recently Used files.
PHP module mru_manager.php (and class CMRUManager) adds this feature to the web applications. Currently it can:

Using the class

Don't forget to include() or require() module as_dbutils.php, as it's used by mru_manager to operate with MySQL data. It should be included in distributive.

Using example (We suggest that sessions is turned ON, and $_SESSION['user_id'] contains logged user unique ID.)
$as_dbparam = array(
  'server'   => 'localhost
  'dbname'   => 'test'
  'username' => '',
  'password' => '',
); # used by as_dbutils.php to establish MySQL connection
require_once('as_dbutils.php'); # class for working with MySQL data, used by mru_manager


$mru_params = array(
    'mrulength' => 8
   ,'userid'    => $_SESSION['user_id']

$mru = new CMRUManager($mru_params);
# equivalent call : $mru = new CMRUManager($_SESSION['user_id'], 8);

# ...

# Registering object that user opens for reading/editing:
$mru->RegisterEvent('My favorite films.txt', 'text-file');

# ... Get the MRU list, to render menu items:
# (Suppose we have a special module edit-text-file.php to view/edit text files in our application)

$mruList = $mru->GetMRUList('text-file', 'edit-text-file.php?file={id}');

print_r($mruList); # just show returned array
Here we pretend that user opens for reading/editing a file 'My favorite films.txt'; in the real application You just insert this operation in the "editing" module, and pass real file name (or object ID) that is to be opened.

Somewhere in the "building user menu" block You should create CMRUManager object again, retrieve MRU items list by calling GetMRUList(), and build from it all menu items for "Open last edited..." submenu.

All methods description

CMRUManager([$options]) - Class constructor can be called without parameters, or with passed associative array, containg one or more following items:

'tablename' => 'MRU_table_name' : by default MRU data is stored in automatically generated table mru_data. If You want use different name, pass it here.
'mrulength' => default MRU list length for all object types. By default it's 4, so only four last opened objects are stored im MRU data.
'mrulengths' => $array , associative array that should contain desired MRU lengths for all "object types" in Your web application.
For example: we want to remember in MRU last eight 'text files', and ten 'xml files'. In that case we should pass an array('text files'=>8, 'xml files'=>10).
'userid' => user_id, authenticated user unique code (ID or login). All consequent calls will be done for this user.

It's possible to pass "scalar" parameters - user_id in the first parameter, and MRU length in the second one. So these two constructor calls are equivalent:
$mru = new CMRUManager(array('userid'=>'test_user', 'mrulength'=>8);
#  AND
$mru = new CMRUManager('test_user', 8);

NOTE: All Following methods will alwais use pre-configured user-id, passed in constructor, unless non-empty $userid exists in their calls.

RegisterEvent($itemid, $itemtype='', $userid=false) - Registers an "opening object" event, placing it on the top of MRU list for passed user, and object type.
If passed object Id (parameter $itemid, it can be a file name or what else, but it's length is limited to 80 chars) is already somewhere in the MRU stack, it will be shifted to the top of it. Otherwise it's placed on the top with pushing out the last one (when maximum length for this object type reached).

GetMRUList($itemtype='', $uri_pattern=false, $userid='') - returns current MRU list for desired user and/or object type.
If optional second parameter $uri_pattern passed, it should contain URI pattern with "{id}" string for substituting with real item id/file name. In that case returned array will have pre-built 'uri' in every row, so you'll have ready-to use link for re-opening that "document". Returned result is an associative array, where every row contains following elements:
Element Descritpion
$result[$row]['itemid'] Item id (or file name if it's used as ID), For example, "My favorite films.txt"
$result[$row]['itemtype'] Type of found item. If You passed non-empty $itemtype when calling GetMRUList(), all these values of course will be the same
$result[$row]['eventdate'] Date and time of a last opening event. String format depends of MySQL version (TIMESTAMP field in MySQL 3.x can look like "20101229102713", but in MySQL 4 and greater it is returned like DATETIME fields : "2010-12-29 10:27:13")
$result[$row]['uri'] This element created only if you passed $uri_pattern parameter. String "{id}" in that pattern will be replaced with url-encoded value of $result[$row]['itemid'], to construct address (URI) for (re)opening respective document. This URI can be used for building "MRU" HTML code in your application's popup-menu or somewhere else.

ClearMRUData($itemtype=false, $itemid=false, $userid=false) - deletes data from MRU table. Any parameter can be empty, so "group" deleting is possible. For example, to clear all "txt-files" MRU memory for current user, you could call ClearMRUData('txt-files').

ATTENTION: Automatically created MRU data table has a CHAR(80) field for storing itemid. If you expect items with name/ID longer than 80 chars (for example You're going to save in MRU "long" file names as Item ID), don't forget to modify structure of created table ("mru_data" by default). Check out other fields too, ( userid and itemtype are CHAR(20) ).
Distributed under BSD License

Change log

1.00.000 (12/30/2010)

Copyright © 2010 Alexander Selifonov,