Login   Register  
PHP Classes
elePHPant
Icontem

File: tests/_test.php

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  >  tests/_test.php  >  Download  
File: tests/_test.php
Role: Example script
Content type: text/plain
Description: Test/example
Class: AJAX Locking
Lock server side resources from AJAX requests
Author: By
Last change: Now values are not hardcoded but can be inputed by a form
Date: 2006-11-26 10:49
Size: 2,884 bytes
 

Contents

Class file image Download
<?php
/**
 * AJAX_Locking test example
 *
 * @category   HTML
 * @package    AJAX_Locking
 * @author     Fabio Ambrosanio <fabio@ambrosanio.com>
 * @copyright  2007 Fabio Ambrosanio
 * @license    http://www.php.net/license/3_01.txt  PHP
 * @version    Release: @package_version@
 */
?>

<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', 'Error: cannot lock');
        }
    },
    unlock: function(result) {
        if (result === true) {
            h.status();
        } else {
            h.stop();
            HTML_AJAX_Util.setInnerHTML('status', 'Error: cannot 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>Editing';

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

            case 'locked':
                html = '<img src=locked.png align=absmiddle>Locked by ' + 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>Timeout expired';
                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;
}

function changeParams()
{
    var f = document.form;
    var user = f.user.value;
    var type = f.type.value;
    var id = f.id.value;
    h.setParams(user, type, id);
}
var h = new AJAX_Locking_Handler(lockingCallbacks, '<?=$_REQUEST['user']?>', '<?=$_REQUEST['type']?>' , '<?=$_REQUEST['id']?>');
</script>
</head>

<body onload="h.start();">
<form name="form">
User: <input type="text" name="user" value="<?=$_REQUEST['user']?>"><br>
Tipo <input type="text" name="type" value="<?=$_REQUEST['type']?>"><br>
Id: <input type="text" name="id" value="<?=$_REQUEST['id']?>"><br>
<button onClick="changeParams()" type="button">Set</button>
</form>
<div id = "count"></div>
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>