Source for file ProgressBar.class.php

Documentation is available at ProgressBar.class.php

  1. <?php
  2. /**
  3. * Class ProgressBar
  4. * Easy to use progress bar in html and css.
  5. *
  6. * @author David Bongard (mail@bongard.net | www.bongard.net | www.pinkorange.
  7. * at)
  8. * @version 1.0 - 20070418
  9. * @licence http://www.opensource.org/licenses/mit-license.php MIT License
  10. *
  11. * Copyright (c) 2007 David Bongard
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. * THE SOFTWARE.
  30. *
  31. * Example of usage:
  32. * <code>
  33. * require_once 'ProgressBar.class.php';
  34. * $bar = new ProgressBar();
  35. *
  36. * $elements = 100000; //total number of elements to process
  37. * $bar->initialize($elements); //print the empty bar
  38. *
  39. * for($i=0;$i<$elements;$i++){
  40. * //do something here...
  41. * $bar->increase(); //calls the bar with every processed element
  42. * }
  43. * </code>
  44. *
  45. * Another example:
  46. * <code>
  47. * require_once 'ProgressBar.class.php'; $bar = new ProgressBar();
  48. *
  49. * $bar->initialize(3); //initialize the bar with the total number of elements to process
  50. *
  51. * //do something time consuming here...
  52. * $bar->increase(); //call for first element
  53. *
  54. * //do something time consuming here...
  55. * $bar->increase(); //call for second element
  56. *
  57. * //do something time consuming here...
  58. * $bar->increase(); //call for third element. end of bar...
  59. * </code>
  60. */
  61. class ProgressBar {
  62.  
  63. /**
  64. * Constructor
  65. *
  66. * @param str $message Message shown above the bar eg. "Please wait...". Default: ''
  67. * @param bool $hide Hide the bar after completion (with JavaScript).
  68. * Default: false
  69. * @param int $sleepOnFinish Seconds to sleep after bar completion. Default: 0
  70. * @param int $barLength Length in pixels. Default: 200
  71. * @param int $precision Desired number of steps to show. Default: 20. Precision will become $numElements when greater than $numElements. $barLength will increase if $precision is greater than $barLength.
  72. * @param str $backgroundColor Color of the bar background
  73. * @param str $foregroundColor Color of the actual progress-bar
  74. * @param str $domID Html-Attribute "id" for the bar
  75. * @param str $stepElement Element the bar is build from
  76. */
  77. function ProgressBar($message='Working...', $hide=false, $sleepOnFinish=0, $barLength=300, $precision=30,
  78. $backgroundColor='#cccccc', $foregroundColor='#0A0', $domID='progressbar',
  79. $stepElement='<div style="width:%spx;height:20px;float:left;"></div>'
  80. ){
  81.  
  82. //increase time limit
  83. if(!ini_get('safe_mode')){
  84. set_time_limit(0);
  85. }
  86.  
  87. $this->hide = (bool) $hide;
  88. $this->sleepOnFinish = (int) $sleepOnFinish;
  89. $this->domID = strip_tags($domID);
  90. $this->message = $message;
  91. $this->stepElement = $stepElement;
  92. $this->barLength = (int) $barLength;
  93. $this->precision = (int) $precision;
  94. $this->backgroundColor = strip_tags($backgroundColor);
  95. $this->foregroundColor = strip_tags($foregroundColor);
  96. if($this->barLength < $this->precision){
  97. $this->barLength = $this->precision;
  98. }
  99.  
  100. $this->StepCount = 0;
  101. $this->CallCount = 0;
  102. }
  103.  
  104. /**
  105. * Print the empty progress bar
  106. * @param int $numElements Number of Elements to be processed and number of times $bar->initialize() will be called while processing
  107. */
  108. function initialize($numElements)
  109. {
  110. $numElements = (int) $numElements ;
  111. if($numElements == 0){
  112. $numElements = 1;
  113. }
  114. //calculate the number of calls for one step
  115. $this->CallsPerStep = ceil(($numElements/$this->precision)); // eg. 1000/200 = 100
  116.  
  117. //calculate the total number of steps
  118. if($numElements >= $this->CallsPerStep){
  119. $this->numSteps = round($numElements/$this->CallsPerStep);
  120. }else{
  121. $this->numSteps = round($numElements);
  122. }
  123.  
  124. //calculate the length of one step
  125. $stepLength = floor($this->barLength/$this->numSteps); // eg. 100/10 = 10
  126.  
  127. //the rest is the first step
  128. $this->rest = $this->barLength-($stepLength*$this->numSteps);
  129. if($this->rest > 0){
  130. $this->firstStep = sprintf($this->stepElement,$this->rest);
  131. }
  132.  
  133. //build the basic step-element
  134. $this->oneStep = sprintf($this->stepElement,$stepLength);
  135.  
  136. //build bar background
  137. $backgroundLength = $this->rest+($stepLength*$this->numSteps);
  138. $this->backgroundBar = sprintf($this->stepElement,$backgroundLength);
  139.  
  140. //stop buffering
  141. ob_end_flush();
  142. //start buffering
  143. ob_start();
  144.  
  145. echo '<div id="'.$this->domID.'">'.
  146. $this->message.'<br/>'.
  147. '<div style="position:absolute;color:'.$this->backgroundColor.';background-color:'.$this->backgroundColor.'">'.$this->backgroundBar.'</div>' .
  148. '<div style="position:absolute;color:'.$this->foregroundColor.';background-color:'.$this->foregroundColor.'">';
  149.  
  150. ob_flush();
  151. flush();
  152. }
  153.  
  154. /**
  155. * Count steps and increase bar length
  156. *
  157. */
  158. function increase()
  159. {
  160. $this->CallCount++;
  161.  
  162. if(!$this->started){
  163. //rest output
  164. echo $this->firstStep;
  165. ob_flush();
  166. flush();
  167. }
  168.  
  169. if($this->StepCount < $this->numSteps
  170. &&(!$this->started || $this->CallCount == $this->CallsPerStep)){
  171.  
  172. //add a step
  173. echo $this->oneStep;
  174. ob_flush();
  175. flush();
  176.  
  177. $this->StepCount++;
  178. $this->CallCount=0;
  179. }
  180. $this->started = true;
  181.  
  182. if(!$this->finished && $this->StepCount == $this->numSteps){
  183.  
  184. // close the bar
  185. echo '</div></div><br/>';
  186. ob_flush();
  187. flush();
  188.  
  189. //sleep x seconds before ending the script
  190. if($this->sleepOnFinish > 0){
  191. sleep($this->sleepOnFinish);
  192. }
  193.  
  194. //hide the bar
  195. if($this->hide){
  196. echo '<script type="text/javascript">document.getElementById("'.$this->domID.'").style.display = "none";</script>';
  197. ob_flush();
  198. flush();
  199. }
  200. $this->finished = true;
  201. }
  202. }
  203. }
  204. ?>

Documentation generated on Mon, 31 Mar 2008 18:08:58 +0200 by phpDocumentor 1.3.0RC3