Login   Register  
PHP Classes
elePHPant
Icontem

File: README.txt

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Fabio Ambrosanio  >  AJAX Locking  >  README.txt  >  Download  
File: README.txt
Role: Documentation
Content type: text/plain
Description: README
Class: AJAX Locking
Lock server side resources from AJAX requests
Author: By
Last change:
Date: 2006-11-13 01:57
Size: 3,603 bytes
 

Contents

Class file image Download
AJAX_Locking is a framework in AJAX to manage locking of object in a web application.

The idea is "stolen" from David Perelman-Hall's column appeared in DDJ issue on 8th September 2006
http://www.ddj.com/showArticle.jhtml?articleID=192700218

The idea is to use AJAX to lock, unlock and get status of records/objects, avoiding use of locks on db (or transactions).

AJAX_Locking is a class that extends HTML_AJAX_Server and implements itself the features to lock, unlock and get status via a "driver":
the default driver (AJAX_Locking_Driver_SharedMemory) uses PEAR's package System_SharedMemory as repository of locks,
you can write your own driver to achieve the same purpose, using a db or the filesystem.

To use AJAX_Locking you have to write a "server" page like the following, that instantiate the Ajax remote object:

server.php
<?php
require_once ('AJAX_Locking/AJAX_Locking.php');

require_once('AJAX_Locking/Driver/SharedMemory.php');
$driver = new AJAX_Locking_Driver_SharedMemory(10, 'file');
$server = new AJAX_Locking($driver);
$server->handleRequest();
?>

and in your client page you have to use HTML_Ajax framework to call lock, unlock and status methods provided from server.
AJAX_Locking provides its own javascript library to help to write client-side management:

test.php
<?php
$user='fabamb';
$type='test';
$id  =1;
?>

<html>
<head>
<script type = 'text/javascript' src = "server.php?client=Locking,all&stub=all"></script>
<script type = "text/javascript">
var lockingCallbacks = { 
	count: 0, 
	lock: function(result) {
		if (result === true) {
			h.status();
		} else {
			h.stop();
			HTML_AJAX_Util.setInnerHTML('status', 'Errore durante il lock');
		}
	},                   
	unlock: function(result) {
		if (result === true) {
			h.status();
		} else {
			h.stop();
			HTML_AJAX_Util.setInnerHTML('status', 'Errore durante l\'unlock');
		}
	},                   
	status: function(result) {
		HTML_AJAX_Util.setInnerHTML('count', this.count++);
		var values = result.split('~');

		switch (values[0]) {
			case 'owned':
				html = '<img src=owned.png align=absmiddle>In fase di modifica';

				enableButton('editButton', false);
				enableButton('saveButton', true);
				break;

			case 'locked':
				html = '<img src=locked.png align=absmiddle>Bloccato da ' + values[1];

				enableButton('editButton', false);
				enableButton('saveButton', false);
				break;

			case 'unlocked':
				html = '<img src=unlocked.png align=absmiddle>';

				enableButton('editButton', true);
				enableButton('saveButton', false);
				break;

			case 'timeout':
				h.stop();

				html = '<img src=timeout.png align=absmiddle>Fase di modifica scaduta';
				enableButton('editButton', false);
				enableButton('saveButton', false);
				break;
		}

		HTML_AJAX_Util.setInnerHTML('status', html);
	}
}

function enableButton(id, enabled) {
	var button = document.getElementById(id);
	
	if (button)	button.disabled = !enabled;
}

var h = new AJAX_Locking_Handler(lockingCallbacks, '<?=$user?>', '<?=$type?>', <?=$id?>);
</script>
</head>

<body onload = 'h.start();'>
<div id = "count">
</div>

User: <?= $user ?><br>
Tipo <?= $type ?><br>
Id: <?= $id ?><br>
Status: <span id = "status"></span>

<br>
<button id = "editButton" type = "button" onClick = "h.lock()">Edit</button>
<button id = "saveButton" type = "button" onClick = "h.unlock()" disabled>Save</button>
<button id = "statusButton" type = "button" onClick = "h.status()">Status</button>

<br>
</body>
</html>