PEAR Progress2 logo

HTML_Progress2 : The Definitive Guide

How to observe events ?

Suppose we decide to send a mail after a process is over (progress meter reach 100%) and keep aware some users. We will see how to do this now.

First step is to declare a function (user callback) that will observe all progress meter events.

Interface of such function (callback) is pretty easy. It requires only one argument passed by reference ($notification) and that should contains a PEAR::Event_Dispatcher instance.

Here are, in purpose of our tutorial, a function that will observe events and send a mail, at end of process, to webmaster@site.com with a short message that gave time elapsed.

Second step is to attach this function (user callback) to the progress meter with the addListener() method.

<?php
require_once 'HTML/Progress2.php';

function getmicrotime($time)
{
    list($usec, $sec) = explode(' ', $time);
    return ((float)$usec + (float)$sec);

}

function myObserver(&$notification) 5
{
    static $time_start;

    $notifyName = $notification->getNotificationName(); 1
    $notifyInfo = $notification->getNotificationInfo(); 2

    switch ($notifyName) {
        case 'onSubmit':  3
            $time_start = getmicrotime($notifyInfo['time']);
            break;
        case 'onLoad':    4
            $time_elapse = getmicrotime($notifyInfo['time']) - $time_start;
            error_log ('process ID=5 is over (elapse time = '. $time_elapse . ' sec.)',
                       3, 'progress_observer.log');
            break;
    }
}

$pb = new HTML_Progress2();
$pb->setAnimSpeed(200);
$pb->setIncrement(10);
$pb->addListener('myObserver'); 5
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Observer Pattern</title>

<?php
echo $pb->getStyle(false);
echo $pb->getScript(false);
?>
</head>
<body>
<?php
$pb->display();
$pb->run();  
?>
</body>
</html>
   

1

$notifyName contains event name triggered. Three issues : onSubmit, onLoad, onChange.

[Caution] Caution
Event name is case sensitive: onsubmit does not match onSubmit.

2

$notifyInfo contains array of additional informations.

For example :

Array
(
    [handler] => run
    [value] => 100
    [sender] => html_progress2
    [time] => 0.26838700 1127382686
)
      

3

When progress meter start, it trigger a onSubmit event through the run() method. This event is catched into user callback and with help of additionnal info we compute and store start time.

4

When progress meter reach 100%, it trigger a onLoad event through the run() method. This event is catched into user callback. We compute elapse time and send final result by mail to the webmaster of site.com

5

All observers attached, are notified by each event. It's up to you to decide if you have to process an event or not. Here we will proceed only two events: onSubmit, onLoad.

Third step (optional) is stop listen events and then detach user function (callback) to the progress meter with the removeListener() method.

[Note] Note
While there are any observers attached, progress meter will continue to notify them of each event when they occured.

HTML_Progress2 : The Definitive Guide v 1.0.0 : September 23, 2005