Location: PHPKode > scripts > MRU manager > mru_manager.en.htm
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<head><title>MRU Manager class</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
body     { font-family:verdana,arial,helvetica;font-size:12px; }
pre { font-family: verdana,arial; font-size:11px; color:#0000FF;
      background-color: #EFEFF0; border: 1px solid #D0D0F0; padding:6px;
td   { font-family:arial,helvetica;font-size:12px;}
td.head  { background-color: #FBFBFE; border: 1px solid #ccf; font-weight:bold; padding-left:6px;}
tr.odd   { background-color: #F0F0F8; color:#000000; }
tr.even  { background-color: #E0E0F0; color:#000000; }
h4       { background-color: #E0E0F0; color:#000000;
           text-align: left; font-size:12px; }
h5       { background-color: #E0E0F0; color:#000000;
           text-align: left; font-size:11px; padding-left:8px;}
hr		{ height:1px; border:0; color: #aae;}

<!-- DOC BEGIN -->

<h3>MRU Manager class</h3>

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:
  <li>Register "opening object" event and store it in the MRU data, realized as DB table (MySQL)</li>
  <li>Automatically create MRU table if not exist,  during first "Register event" method calling ( RegisterEvent() )</li>
  <li>Manage events order, with deleting (pushing out) from MRU all events that become beyong configured MRU max length per object type</li>
  <li>Save MRU with user credentials (if multi-user MRU needed, your WEB application should store something like User ID's, or unique logins for logged-in users)</li>
  <li>Save separated MRU list for different types of viewed objects, so every user can have many independent MRU lists
    (for example for text files, html files, XML files, "policies" etc.)</li>
  <li>Retrieve MRU list for objects of any "registered" type, with optional building of ready-to-use URI that will re-open referenced document</li>
  <li>Delete unneeded MRU records programmatically (for example, if some object is deleted from disc, and there is no reason to have a respective record in MRU list)</li>

<h4>Using the class</h4>

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

<br /><br />
<b>Using example</b>

(We suggest that sessions is turned ON, and $_SESSION['user_id'] contains logged user unique ID.)
$as_dbparam = array(
  'server'   =&gt; 'localhost
  'dbname'   =&gt; 'test'
  'username' =&gt; '',
  'password' =&gt; '',
); # 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' =&gt; 8
   ,'userid'    =&gt; $_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-&gt;RegisterEvent('My favorite films.txt', 'text-file');

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

$mruList = $mru-&gt;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.
<br><br>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.

<h4>All methods description</h4>

<b>CMRUManager([$options])</b> - Class constructor can be called without parameters, or with passed associative array, containg one or more following items:
'<b>tablename</b>' =&gt; 'MRU_table_name' : by default MRU data is stored in automatically generated table <b>mru_data</b>. If You want use different name, pass it here.
<br />
'<b>mrulength</b>' =&gt; default MRU list length for all object types. By default it's 4, so only four last opened objects are stored im MRU data.
<br />
'<b>mrulengths</b>' =&gt; $array , associative array that should contain desired MRU lengths for all "object types" in Your web application.
<br />For example: we want to remember in MRU last eight 'text files', and ten 'xml files'. In that case we should pass an <b>array('text files'=&gt;8, 'xml files'=&gt;10)</b>.
<br />
'<b>userid</b>' =&gt; user_id, authenticated user unique code (ID or login). All consequent calls will be done for this user.
<br /><br />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'=&gt;'test_user', 'mrulength'=&gt;8);
#  AND
$mru = new CMRUManager('test_user', 8);

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

<br /><br />
<b>RegisterEvent($itemid, $itemtype='', $userid=false)</b> - Registers an "opening object" event, placing it on the top of MRU list for passed user, and object type.
<br />

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).

<br /><br />
<b>GetMRUList($itemtype='', $uri_pattern=false, $userid='')</b> - returns current MRU list for desired user and/or object type.
<br />If optional second parameter <b>$uri_pattern</b> 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:
<table border=0>
    <td class="head">Element</td>
    <td class="head">Descritpion</td>
  <tr class="odd">
    <td nowrap="nowrap">$result[$row]['itemid']</td>
    <td>Item id (or file name if it's used as ID), For example, "My favorite films.txt"</td>
  <tr class="even">
    <td nowrap="nowrap">$result[$row]['itemtype']</td>
    <td>Type of found item. If You passed non-empty $itemtype when calling  GetMRUList(), all these values of course will be the same</td>
  <tr class="odd">
    <td nowrap="nowrap">$result[$row]['eventdate']</td>
    <td>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")
  <tr class="even">
    <td nowrap="nowrap">$result[$row]['uri']</td>
    <td>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.


<br /><br />
<b>ClearMRUData($itemtype=false, $itemid=false, $userid=false)</b> - 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').

<br /><br /><b>ATTENTION</b>: 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) ).

<div style="text-align:right">Distributed under BSD License</div>

<h4>Change log</h4>

<h5>1.00.000 (12/30/2010)</h5>
  <li>First release published

<!-- DOC END -->
<br />
<div align=center><font size=-2>Copyright &copy; 2010 Alexander Selifonov, <a href="http://www.selifan.ru">www.selifan.ru</a>
Return current item: MRU manager