Examples TOCexamples

Complex ProgressBar Observer

$Date: 2005/07/25 12:27:28 $

 Table of contents

Introduction

This example requires :


This example will run a complex observer mechanism which run two progress bar. At each full loop of first bar, second bar is increment until this last one reach 100%.

All explains are in source code comments. Just a big warning: NEVER forget the line 144.

[Top]

 Render options

Here are options to build the 1st progress bar :
background-color = #e0e0e0
HTML_Progress_UI::setProgressAttributes()
Here are options to build the 1st progress bar string :
valign           = left
color            = red
background-color = lightblue
HTML_Progress_UI::setStringAttributes()
Here are options to build the 2nd progress bar border :
width = 1
style = solid
color = navy
HTML_Progress_UI::setBorderAttributes()
Here are options to build the 2nd progress bar cell :
active-color   = #3874B4
inactive-color = #EEEECC
HTML_Progress_UI::setCellAttributes()
Here are options to build the 2nd progress bar string :
width            = 100
align            = center
valign           = right
color            = yellow
background-color = lightblue
HTML_Progress_UI::setStringAttributes()

[Top]

 Output

Screenshot

[Top]

 PHP source syntax highlight

  1. <?php
  2. /**
  3.  * Observer ProgressBar example. Uses a custom observer class
  4.  * that handled progress of the second bar.
  5.  *
  6.  * @version    $Id: complex.php,v 1.2 2005/07/25 12:27:28 farell Exp $
  7.  * @author     Laurent Laville <pear@laurent-laville.org>
  8.  * @package    HTML_Progress
  9.  * @subpackage Examples
  10.  */
  11.  
  12. require_once 'HTML/Progress.php';
  13. require_once 'HTML/Progress/observer.php';
  14.  
  15. // 1. Defines ProgressBar observer
  16. class Bar1Observer extends HTML_Progress_Observer
  17. {
  18.     function Bar1Observer()
  19.     {
  20.         $this->HTML_Progress_Observer();
  21.     }
  22.  
  23.     function notify($event)
  24.     {
  25.         global $bar2;
  26.  
  27.         if (is_array($event)) {
  28.             $log = isset($event['log']) ? $event['log'] : "undefined event id.";
  29.             $val = isset($event['value']) ? $event['value'] : "unknown value";
  30.  
  31.             switch (strtolower($log)) {
  32.              case 'incvalue':
  33.                  // if you want to do special on each step of progress bar1; it's here !!!
  34.                  break;
  35.              case 'setvalue':
  36.                  if ($val == 0) {
  37.                      // updates $bar2 because $bar1 has completed a full loop
  38.                      $bar2->incValue();
  39.                      $bar2->display();
  40.                  }
  41.              default:
  42.             }
  43.         }
  44.     }
  45. }
  46.  
  47. // 2. Creates ProgressBar
  48. $bar1 = new HTML_Progress(HTML_PROGRESS_BAR_VERTICAL);
  49. $bar1->setAnimSpeed(50);
  50. $bar1->setIncrement(10);
  51. $bar1->setIdent('PB1');
  52.  
  53. $bar2 = new HTML_Progress(HTML_PROGRESS_BAR_VERTICAL);
  54. $bar2->setAnimSpeed(50);
  55. $bar2->setIncrement(25);
  56. $bar2->setIdent('PB2');
  57. $bar2->setBorderPainted(true);
  58.  
  59. // 3. Creates and attach a listener
  60. $observer = new Bar1Observer();
  61.  
  62. $ok = $bar1->addListener($observer);
  63. if (!$ok) {
  64.     die ("Cannot add a valid listener to progress bar !");
  65. }
  66.  
  67. // 4. Changes look-and-feel of ProgressBar
  68. $ui1 =& $bar1->getUI();
  69. $ui1->setComment('Complex Observer ProgressBar example');
  70. $ui1->setTabOffset(1);
  71. $ui1->setProgressAttributes(array(
  72.     'background-color' => '#e0e0e0'
  73. ));
  74. $ui1->setStringAttributes(array(
  75.     'valign' => 'left',
  76.     'color'  => 'red',
  77.     'background-color' => 'lightblue'
  78. ));
  79.  
  80. $ui2 =& $bar2->getUI();
  81. $ui2->setTabOffset(1);
  82. $ui2->setBorderAttributes(array(
  83.     'width' => 1,
  84.     'style' => 'solid',
  85.     'color' => 'navy'
  86. ));
  87. $ui2->setCellAttributes(array(
  88.     'active-color' => '#3874B4',
  89.     'inactive-color' => '#EEEECC'
  90. ));
  91. $ui2->setStringAttributes(array(
  92.     'width'  => '100',
  93.     'align'  => 'center',
  94.     'valign' => 'right',
  95.     'color'  => 'yellow',
  96.     'background-color' => 'lightblue'
  97. ));
  98. ?>
  99. <!DOCTYPE html
  100.     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  101.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  102. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  103. <head>
  104. <title>Complex Observer ProgressBar example</title>
  105. <style type="text/css">
  106. <!--
  107. <?php
  108. echo $bar1->getStyle();
  109. echo $bar2->getStyle();
  110. ?>
  111. table.container {
  112.         background-color: lightblue;
  113.         border: 2;
  114.         border-color: navy;
  115.         border-style: dashed;
  116.         cell-spacing: 4;
  117.         cell-padding: 8;
  118.         width: 50%;
  119. }
  120. // -->
  121. </style>
  122. <script type="text/javascript">
  123. <!--
  124. <?php echo $bar1->getScript(); ?>
  125. //-->
  126. </script>
  127. </head>
  128. <body>
  129.  
  130. <table class="container">
  131. <tr>
  132.     <td width="25%" align="center">
  133. <?php echo $bar1->toHtml(); ?>
  134.     </td>
  135.     <td width="25%" align="center">
  136. <?php echo $bar2->toHtml(); ?>
  137.     </td>
  138. </tr>
  139. </table>
  140.  
  141. <?php
  142. do {
  143.     $bar1->display();
  144.     $bar1->process();    // warning: don't forget it (even for a demo)
  145.     if ($bar1->getPercentComplete() == 1) {
  146.         $bar1->setValue(0);  // the 1st progress bar has reached 100%, do a new loop
  147.     } else {
  148.         $bar1->incValue();   // updates 1st progress bar
  149.     }
  150. } while($bar2->getPercentComplete() < 1);
  151. ?>
  152.  
  153. </body>
  154. </html>

[Top]