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.

  1. <?php
  2. require_once 'HTML/Progress2.php';
  3.  
  4. function getmicrotime($time)
  5. {
  6.     list($usec, $sec) = explode(' ', $time);
  7.     return ((float)$usec + (float)$sec);
  8.  
  9. }
  10.  
  11. function myObserver(&$notification)
  12. {
  13.     static $time_start;
  14.  
  15.     $notifyName = $notification->getNotificationName();
  16.     $notifyInfo = $notification->getNotificationInfo();
  17.  
  18.     switch ($notifyName) {
  19.         case 'onSubmit':
  20.             $time_start = getmicrotime($notifyInfo['time']);
  21.             break;
  22.         case 'onLoad':
  23.             $time_elapse = getmicrotime($notifyInfo['time']) - $time_start;
  24.             error_log ('process ID=5 is over (elapse time = '. $time_elapse . ' sec.)',
  25.                        3, 'progress_observer.log');
  26.             break;
  27.     }
  28. }
  29.  
  30. $pb = new HTML_Progress2();
  31. $pb->setAnimSpeed(200);
  32. $pb->setIncrement(10);
  33. $pb->addListener('myObserver');
  34. ?>
  35. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  36.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  37. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  38. <head>
  39. <title>Observer Pattern</title>
  40.  
  41. <?php
  42. echo $pb->getStyle(false);
  43. echo $pb->getScript(false);
  44. ?>
  45. </head>
  46. <body>
  47. <?php
  48. $pb->display();
  49. $pb->run();
  50. ?>
  51. </body>
  52. </html>
Line 15 :

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

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

$notifyInfo contains array of additional informations.

For example :

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

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.

Line 22 :

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

Lines 11-28, 33 :

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 2.4.0 : April 20, 2007