HTML_Progress
[ class tree: HTML_Progress ] [ index: HTML_Progress ] [ all elements ]
Prev Next
Monitoring functions v2
displays a progress bar inside a dialog box

Table of Contents

Introduction

On February 15, 2004 and for release 1.1 of HTML_Progress, the HTML_Progress_Monitor class improved a lot. Features are :

  • both modes determinate/indeterminate supported for the progress meter
  • easy communication with your user-process
  • follow standard of QuickForm rendering
  • buttons size, dialog box and buttons title, all are customizable
  • different captions on user-process progress are allowed
User-end command actions to begin and cancel process. The progress bar won't start if the user did not click on the 'Start' button. Once the process begun the start button is disabled.

Basic concept

Form and progress bar output are now contained in classes that extend HTML_QuickForm_Renderer. Design pattern is much easier to manage and change, and you can choose your favorite template engine.

There are 8 renderers available since release 3.1.1 of QuickForm. The following template engines are directly suported: Smarty, HTML_Template_Sigma, HTML_Template_IT, HTML_Template_Flexy. Among these 8 renderers, we will have a look on 3 examples in next chapters : 'default', 'default improved', 'template integration'.

Using Progress Monitor

Default renderer

Without any changes, you may obtains a decent dialog box with a default progress meter such as :


Default renderer

  1. <?php
  2. require_once 'HTML/Progress/monitor.php';
  3.  
  4. $monitor = new HTML_Progress_Monitor();
  5.  
  6. $bar =& $monitor->getProgressElement();
  7. $bar->setAnimSpeed(50);
  8. $bar->setIncrement(10);
  9. ?>
  10. <html>
  11. <head>
  12. <title>Progress Monitor - default renderer </title>
  13. <style type="text/css">
  14. <!--
  15. .progressStatus {
  16. color:#000000;
  17. font-size:10px;
  18. }
  19. <?php echo $monitor->getStyle(); ?>
  20. // -->
  21. </style>
  22. <script type="text/javascript">
  23. <!--
  24. <?php echo $monitor->getScript(); ?>
  25. //-->
  26. </script>
  27. </head>
  28. <body>
  29.  
  30. <?php
  31. echo $monitor->toHtml();
  32. $monitor->run();
  33. ?>
  34.  
  35. </body>
  36. </html>

CSS class 'progressStatus' is for caption display (see HTML_Progress_Monitor::setCaption).


Default improved renderer

With some class constructor options and a QF renderer, we will improves a lot in few lines, the decent but poor graphic first example. Enjoy the new result :


Default improved renderer

  1. <?php
  2. require_once 'HTML/Progress/monitor.php';
  3.  
  4. function myFunctionHandler($progressValue, &$obj)
  5. {
  6. $bar =& $obj->getProgressElement();
  7. $bar->sleep();
  8. if (!$bar->isIndeterminate()) {
  9. if (fmod($progressValue,10) == 0) {
  10. $obj->setCaption("myFunctionHandler -> progress value is = $progressValue");
  11. }
  12. } else {
  13. /* in case we have attached an indeterminate progress bar to the monitor ($obj)
  14. after a first pass that reached 60%,
  15. we swap from indeterminate mode to determinate mode
  16. and run a standard progress bar from 0 to 100%
  17. */
  18. if ($progressValue == 60) {
  19. $bar->setIndeterminate(false);
  20. $bar->setString(null); // show percent-info
  21. $bar->setValue(0);
  22. }
  23. }
  24. }
  25.  
  26. $monitor = new HTML_Progress_Monitor('frmMonitor4', array(
  27. 'button' => array('style' => 'width:80px;')
  28. )
  29. );
  30. $monitor->setProgressHandler('myFunctionHandler');
  31.  
  32. $progress = new HTML_Progress();
  33. $progress->setAnimSpeed(20);
  34. $progress->setStringPainted(true); // get space for the string
  35. $progress->setString(""); // but don't paint it
  36. $progress->setIndeterminate(true); // Progress start in indeterminate mode
  37.  
  38. $ui =& $progress->getUI();
  39. $ui->setCellCount(20);
  40. $ui->setProgressAttributes('background-color=#EEE');
  41. $ui->setStringAttributes('background-color=#EEE color=navy');
  42. $ui->setCellAttributes('inactive-color=#FFF active-color=#444444');
  43.  
  44. $monitor->setProgressElement($progress);
  45. ?>
  46. <html>
  47. <head>
  48. <title>Progress Monitor - default improved renderer </title>
  49. <style type="text/css">
  50. <!--
  51. body {
  52. background-color: lightgrey;
  53. font-family: Verdana, Arial;
  54. }
  55. .progressStatus {
  56. color: navy;
  57. font-size:10px;
  58. }
  59. <?php echo $monitor->getStyle(); ?>
  60. // -->
  61. </style>
  62. <script type="text/javascript">
  63. <!--
  64. <?php echo $monitor->getScript(); ?>
  65. //-->
  66. </script>
  67. </head>
  68. <body>
  69.  
  70. <?php
  71. $renderer =& HTML_QuickForm::defaultRenderer();
  72. $renderer->setFormTemplate('
  73. <table width="450" border="0" cellpadding="3" cellspacing="2" bgcolor="#EEEEEE">
  74. <form{attributes}>{content}
  75. </form>
  76. </table>
  77. ');
  78. $renderer->setHeaderTemplate('
  79. <tr>
  80. <td style="white-space:nowrap;background:#7B7B88;color:#ffc;" align="left" colspan="2">
  81. <b>{header}</b>
  82. </td>
  83. </tr>
  84. ');
  85. $monitor->accept($renderer);
  86.  
  87. echo $renderer->toHtml();
  88. $monitor->run();
  89. ?>
  90.  
  91. </body>
  92. </html>

To make it possible, we have changed buttons size and internal QuickForm name on lines 26 to 29 (class constructor options), and written the QF renderer on lines 71 to 84. The progress bar look and feel is defined on lines 38 to 42. Our user-process which run an indeterminate progress switching back to determinate mode is defined by the 'myFunctionHandler' function (lines 4 to 24), linked to the monitor on line 30.


Template engine integration

Using Progress Monitor with a template engine supported by QF rendering system is very easy. We will show you how to do with ITx (http://pear.php.net/package/HTML_Template_IT). Example below will simulate a picture upload operation (3 steps: picture 1 at 10%, picture 2 at 45%, picture 3 at 70%).


ITDynamic renderer

  1. <?php
  2. require_once 'HTML/Progress/monitor.php';
  3. require_once 'HTML/QuickForm/Renderer/ITDynamic.php';
  4. require_once 'HTML/Template/ITX.php';
  5.  
  6. class my2ClassHandler
  7. {
  8. function my1Method($progressValue, &$obj)
  9. {
  10. switch ($progressValue) {
  11. case 10:
  12. $pic = 'picture1.jpg';
  13. break;
  14. case 45:
  15. $pic = 'picture2.jpg';
  16. break;
  17. case 70:
  18. $pic = 'picture3.jpg';
  19. break;
  20. default:
  21. $pic = null;
  22. }
  23. if (!is_null($pic)) {
  24. $obj->setCaption('upload <b>%file%</b> in progress ... Start at %percent%%',
  25. array('file'=>$pic, 'percent'=>$progressValue)
  26. );
  27. }
  28. $bar =& $obj->getProgressElement();
  29. $bar->sleep(); // slow animation because we do noting else
  30. }
  31. }
  32.  
  33. $monitor = new HTML_Progress_Monitor('frmMonitor5', array(
  34. 'title' => 'Upload your pictures',
  35. 'start' => 'Upload',
  36. 'cancel' => 'Stop',
  37. 'button' => array('style' => 'width:80px;')
  38. )
  39. );
  40. $monitor->setProgressHandler(array('my2ClassHandler','my1Method'));
  41.  
  42. $progress = new HTML_Progress();
  43. $progress->setAnimSpeed(50);
  44.  
  45. $ui =& $progress->getUI();
  46. $ui->setCellCount(20);
  47. $ui->setProgressAttributes('background-color=#EEE');
  48. $ui->setStringAttributes('background-color=#EEE color=navy');
  49. $ui->setCellAttributes('inactive-color=#FFF active-color=#444444');
  50.  
  51. $monitor->setProgressElement($progress);
  52.  
  53. $tpl =& new HTML_Template_ITX('.');
  54. $tpl->loadTemplateFile('monitor_itdynamic.html');
  55. $tpl->setVariable(array(
  56. 'qf_style' => "body {font-family: Verdana, Arial; } \n" . $monitor->getStyle(),
  57. 'qf_script' => $monitor->getScript()
  58. )
  59. );
  60.  
  61. $renderer =& new HTML_QuickForm_Renderer_ITDynamic($tpl);
  62. $renderer->setElementBlock(array(
  63. 'buttons' => 'qf_buttons'
  64. ));
  65.  
  66. $monitor->accept($renderer);
  67. $tpl->show();
  68.  
  69. $monitor->run();
  70. ?>

HTML_Progress_Monitor class constructor options (lines 33 to 39) allows to change buttons name and size, the dialog box title, and the internal QuickForm name. User-process is now defined by a method-class on lines 6 to 31, and linked to the monitor on line 40.
The ITx template engine is initialized line 53, and loaded with template monitor_itdynamic.html (line 54).
Finally we get the QF renderer on lines 61 to 64.


Prev Up Next
Observer pattern Getting Started Reference Guide

Documentation generated on Sun, 12 Sep 2004 20:22:55 +0200 by phpDocumentor 1.3.0RC3