PEAR Progress2 logo

HTML_Progress2 : The Definitive Guide

Observer usage

Figure 21.4. Progress as a listener to Event_Dispatcher

Progress as a listener to Event_Dispatcher

This example show how to implement the ProgressMeter Event_Dispatcher to listen user-process (simulation of mail send) that decide to refresh itself the progress bar.

[Warning] Warning
This example requires at least the version 2.1.0 of HTML_Progress2

Example 21.4. Progress bar listen to user process

  1. <?php
  2. require_once 'HTML/Progress2.php';
  3.  
  4. class myClass
  5. {
  6.     function doSomething()
  7.     {
  8.        $usecs = 1000 * 1000;  // wait 1 seconde
  9.        $dispatch =& Event_Dispatcher::getInstance('ProgressMeter');
  10.  
  11.        // A long task to do ... for example sending 10 mails
  12.        for ($i = 0; $i < 10; ++$i) {
  13.  
  14.            // simulate by a sleep 1 sec function
  15.            if ((substr(PHP_OS, 0, 3) == 'WIN') && (substr(PHP_VERSION,0,1) < '5') ){
  16.                for ($w = 0; $w < $usecs; $w++) { }
  17.            } else {
  18.                usleep($usecs);
  19.            } //
  20.  
  21.            $dispatch->post($this, 'onTick', array($i));
  22.        }
  23.     }
  24. }
  25.  
  26. class myBar extends HTML_Progress2
  27. {
  28.     function myBar()
  29.     {
  30.         parent::HTML_Progress2();
  31.         $this->setIncrement(10);
  32.  
  33.         $this->addLabel(HTML_PROGRESS2_LABEL_TEXT, 'txt1');
  34.         $this->setLabelAttributes('txt1', array(
  35.             'valign' => 'top',
  36.             'left' => 0,
  37.             ));
  38.     }
  39.  
  40.     function notify(&$notification)
  41.     {
  42.         $notifyName = $notification->getNotificationName();
  43.  
  44.         if (strcasecmp($notifyName, 'onTick') == 0) {
  45.             $notifyInfo = $notification->getNotificationInfo();
  46.  
  47.             if ($notifyInfo[0] == 0) {
  48.                 $this->setLabelAttributes('txt1', array('value' => 'Sending mail ...'));
  49.  
  50.             } elseif ($notifyInfo[0] == 9) {
  51.                 $this->setLabelAttributes('txt1', array('value' => 'Mail sent with success.'));
  52.             }
  53.             $this->moveNext();
  54.         }
  55.     }
  56.  
  57. }
  58.  
  59. $bar = new myBar();
  60. $bar->addListener(array(&$bar, 'notify'), 'onTick');
  61.  
  62. ?>
  63. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  64.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  65. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  66. <head>
  67. <title>doSomething</title>
  68. <style type="text/css">
  69. <!--
  70. <?php echo $bar->getStyle(); ?>
  71.  
  72. body {
  73.     background-color: #FFFFFF;
  74. }
  75.  -->
  76. </style>
  77. <?php echo $bar->getScript(false); ?>
  78. </head>
  79. <body>
  80.  
  81. <?php
  82. $bar->display();
  83.  
  84. $process = new myClass();
  85. $process->doSomething();
  86. ?>
  87.  
  88. </body>
  89. </html>
HTML_Progress2 : The Definitive Guide v 2.4.0 : April 20, 2007