Advertisement
ayukawaa

Multiple progress bars during long PHP script execution

Mar 11th, 2014
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.82 KB | None | 0 0
  1. <?php
  2. /**
  3.  * MULTIPLE PROGRESS BARS DURING LONG PHP SCRIPT EXECUTION
  4.  *
  5.  * based on http://spidgorny.blogspot.com/2012/02/divcolored-for-lengthy-php-process.html
  6.  * modified by ayukawaa@gmail.com (because I need it ^_^)
  7.  * pure js, tested on IE 11, Firefox 27, Chrome 33
  8.  * can be easely changed to jquery,...
  9.  *
  10.  * 2014-03-11
  11.  */
  12. class ProgressBar
  13. {
  14.     # default values
  15.     var $set_time_limit                 = 0;// do we really need to touch this?
  16.     var $uniqueid                       = '';// for multiple progress bars on same page
  17.     var $width                          = '300px';
  18.     var $percent                        = 0;
  19.     var $decimals                       = 0;
  20.     var $hide_when_done                 = false;
  21.     var $txt_pre                        = '';// text to put before the caption (%)
  22.     var $txt_post                       = '';// text to put after the caption (%)
  23.     var $initial_display                = false;//show text when rendering the progress but BEFORE starting process
  24.     # default names of each <div =id"">
  25.     var $divmain                        = 'divmain';
  26.     var $divtext                        = 'divtext';
  27.     var $divsubdiv                      = 'divsubdiv';
  28.     var $divcolored                     = 'divcolored';
  29.     var $divtransparent                 = 'divtransparent';
  30.     # default css of each <div =id"">
  31.     var $css_divmain                    = 'position:relative;';
  32.     var $css_divtext                    = 'color:#000000;position:absolute;width:100%;height:16px;text-align:center;font-size:9px;font-weight:700;font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;height: 16px;line-height: 16px;vertical-align: middle;';
  33.     var $css_divsubdiv                  = 'width:100%;height:16px;border:1px solid #a9ddef;border-radius:5px 5px 5px 5px;';
  34.     var $css_divcolored                 = 'float:left;height:16px;background-color:#43b6df;border-radius: 5px 5px 5px 5px;';
  35.     var $css_divtransparent             = 'float:left;border-radius: 0 5px 5px 0;';
  36.     var $css_progressbar_finished       = 'border-radius: 5px 5px 5px 5px !important;background-color:#e8e8e8 !important;';
  37.     # js to do when progressbar starts/ends
  38.     var $js_prehook                     = '';
  39.     var $js_posthook                    = '';
  40.  
  41.  
  42.  
  43.     /**
  44.      * class constructor
  45.      * @param array $config array with the options we want overwritten
  46.      * @return nothing OUTPUT TO SCREEN!
  47.      */
  48.     function __construct($config=null)
  49.     {
  50.         # save config array
  51.         if(is_array($config))
  52.         {
  53.             foreach ($config as $key => $value)
  54.             {
  55.                 $this->$key             = $value;
  56.             }
  57.         }
  58.         #1 parameter=uniqueid
  59.         elseif(!is_null($config))
  60.         {
  61.             $this->uniqueid             = $config;
  62.         }
  63.         # should be always 0?
  64.         set_time_limit($this->set_time_limit);
  65.         # write inmediatly all pending: <html><head></head><body>...
  66.         flush();
  67.         ob_flush();
  68.         # get ready for progressbar action!
  69.         ob_end_clean();
  70.         ini_set('output_buffering', '0');
  71.     }
  72.  
  73.     /**
  74.      * draw on screen the progress bar without doing any command yet
  75.      * @param string $width optional|width of the desired progress bar
  76.      * @return nothing OUTPUT TO SCREEN!
  77.      */
  78.     public function render($width='')
  79.     {
  80.         if(!empty($width))
  81.         {
  82.             $this->width                = $width;
  83.         }
  84.         //echo $GLOBALS['CONTENT'];$GLOBALS['CONTENT']='';
  85.         echo $this->getContent();
  86.         $this->flush();
  87.         //$this->progress(0);
  88.     }
  89.  
  90.     /**
  91.      * do the REAL drawing on screen of the progress bar without doing any command yet
  92.      * @return string the html+js+css needed
  93.      */
  94.     private function getContent()
  95.     {
  96.         $value                          = floatval($this->percent);
  97.         $hide_when_done                 = $this->hide_when_done ? '1' : '0';
  98.         $percent                        = ($this->initial_display) ? number_format($this->percent, $this->decimals, '.', '') .'%' : '';
  99.         $content .= '
  100. <div id="PBCONTAINER'.$this->uniqueid.'">
  101.     <div id="'.$this->divmain.$this->uniqueid.'">
  102.         <div id="'.$this->divtext.$this->uniqueid.'">'.$percent.'</div>
  103.         <div id="'.$this->divsubdiv.$this->uniqueid.'">
  104.             <div id="'.$this->divcolored.$this->uniqueid.'" style="width:'.$value.'%;"></div>
  105.             <div id="'.$this->divtransparent.$this->uniqueid.'"></div>
  106.         </div>
  107.     </div>
  108. </div>
  109. <style>
  110. #PBCONTAINER'.$this->uniqueid.'{width:'.$this->width.';}
  111. #'.$this->divmain.$this->uniqueid.'{'.$this->css_divmain.'}
  112. #'.$this->divtext.$this->uniqueid.'{'.$this->css_divtext.'}
  113. #'.$this->divsubdiv.$this->uniqueid.'{'.$this->css_divsubdiv.'}
  114. #'.$this->divcolored.$this->uniqueid.'{'.$this->css_divcolored.'}
  115. #'.$this->divtransparent.$this->uniqueid.'{'.$this->css_divtransparent.'}
  116. .progressbar_finished'.$this->uniqueid.'{'.$this->css_progressbar_finished.'}
  117. </style>
  118. <script>
  119. var jspbar_cache'.$this->uniqueid.'=false;
  120. var jspbar_txt_pre'.$this->uniqueid.'="'.$this->txt_pre.'";
  121. var jspbar_txt_post'.$this->uniqueid.'="'.$this->txt_post.'";
  122. var jspbar_hide_when_done'.$this->uniqueid.'='.$hide_when_done.';
  123. var jspbar_'.$this->divcolored.$this->uniqueid.'=document.getElementById("'.$this->divcolored.$this->uniqueid.'");
  124. var jspbar_'.$this->divmain.$this->uniqueid.'=document.getElementById("'.$this->divmain.$this->uniqueid.'");
  125. var jspbar_'.$this->divtransparent.$this->uniqueid.'=document.getElementById("'.$this->divtransparent.$this->uniqueid.'");
  126. var jspbar_'.$this->divtext.$this->uniqueid.'=document.getElementById("'.$this->divtext.$this->uniqueid.'");
  127. //cache dom
  128. function jspbar_cache_dom'.$this->uniqueid.'()
  129. {
  130.     jspbar_'.$this->divcolored.$this->uniqueid.'=document.getElementById("'.$this->divcolored.$this->uniqueid.'");
  131.     jspbar_'.$this->divmain.$this->uniqueid.'=document.getElementById("'.$this->divmain.$this->uniqueid.'");
  132.     jspbar_'.$this->divtransparent.$this->uniqueid.'=document.getElementById("'.$this->divtransparent.$this->uniqueid.'");
  133.     jspbar_'.$this->divtext.$this->uniqueid.'=document.getElementById("'.$this->divtext.$this->uniqueid.'");
  134.     jspbar_cache'.$this->uniqueid.'=true;
  135.     jspbar_prehook'.$this->uniqueid.'("'.$this->uniqueid.'");
  136. }
  137. //called each time
  138. function jspbar_run'.$this->uniqueid.'(progress, text, end)
  139. {
  140.     if(!jspbar_cache'.$this->uniqueid.')
  141.     {
  142.         jspbar_cache_dom'.$this->uniqueid.'();
  143.     }
  144.     if(jspbar_'.$this->divcolored.$this->uniqueid.')
  145.     {
  146.         jspbar_'.$this->divcolored.$this->uniqueid.'.style.width=progress+"%";
  147.         if(end==1)
  148.         {
  149.             if(jspbar_hide_when_done'.$this->uniqueid.'==1)
  150.             {
  151.                 jspbar_'.$this->divmain.$this->uniqueid.'.style.display="none";
  152.             }
  153.             jspbar_'.$this->divcolored.$this->uniqueid.'.style.width="100%";//the end can be 100% or 0%
  154.             jspbar_'.$this->divcolored.$this->uniqueid.'.className+=jspbar_'.$this->divcolored.$this->uniqueid.'.className ? "progressbar_finished'.$this->uniqueid.'" : " progressbar_finished'.$this->uniqueid.'";
  155.             jspbar_'.$this->divtext.$this->uniqueid.'.innerHTML=text;//without PRE or POST!!!
  156.             jspbar_posthook'.$this->uniqueid.'("'.$this->uniqueid.'");
  157.         }
  158.         else
  159.         {
  160.             var last_percent=100-progress;
  161.             jspbar_'.$this->divtransparent.$this->uniqueid.'.style.width=last_percent+"%";
  162.             if (text!="")
  163.             {
  164.                 jspbar_'.$this->divtext.$this->uniqueid.'.innerHTML=jspbar_txt_pre'.$this->uniqueid.'+text+jspbar_txt_post'.$this->uniqueid.';
  165.             }
  166.         }
  167.     }
  168. }
  169. //called before start
  170. function jspbar_prehook'.$this->uniqueid.'(uniqueid)
  171. {
  172.     '.$this->js_prehook.'
  173. }
  174. //called when finished
  175. function jspbar_posthook'.$this->uniqueid.'(uniqueid)
  176. {
  177.     '.$this->js_posthook.'
  178. }
  179. </script>';
  180.         return $content.PHP_EOL;
  181.     }
  182.     /**
  183.      * write and do the specific progress
  184.      * @param string $percent percent done
  185.      * @param string $text optional|string to display (values=null:write nothing, empty:write default %, else:write 'as is')
  186.      * @param bool $end optional|end of progress bar reached (100% or 0%)
  187.      */
  188.     function progress($percent, $text = null, $end=null)
  189.     {
  190.  
  191.         # only send END parameter when really ends (100% or 0%)
  192.         $this->percent                  = number_format($percent, $this->decimals, '.', '');
  193.         if(is_null($text))
  194.         {
  195.             $text_parameter             = '';
  196.         }
  197.         elseif(empty($text))
  198.         {
  199.             $text_parameter             = $this->percent.'%';
  200.         }
  201.         else
  202.         {
  203.             $text_parameter             = $text;
  204.         }
  205.         $end_parameter                  = is_null($end) ? '' : ',1';
  206.         echo '<script>jspbar_run'.$this->uniqueid.'("'.$percent.'","'.htmlspecialchars($text_parameter).'"'.$end_parameter.');</script>';
  207.         $this->flush();
  208.     }
  209.  
  210.     function flush()
  211.     {
  212.         print str_pad('', intval(ini_get('output_buffering'))).PHP_EOL;
  213.         //ob_end_flush();
  214.         flush();
  215.     }
  216.  
  217. }
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225. /* EOF */
  226. ?><!doctype html>
  227. <html>
  228. <head>
  229. <meta charset="utf-8">
  230. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  231. <meta name="description" content="Multiple progress bars during long PHP script execution">
  232. <meta name="author" content="ayukawaa">
  233. <title>TEST • Multiple progress bars during long PHP script execution by ayukawaa</title>
  234. </head>
  235. <body><h1 id="title">Multiple progress bars during long PHP script execution</h1>
  236. <hr>
  237. <?php
  238. /**
  239.  * MULTIPLE PROGRESS BARS DURING LONG PHP SCRIPT EXECUTION
  240.  *
  241.  * based on http://spidgorny.blogspot.com/2012/02/divcolored-for-lengthy-php-process.html
  242.  * modified by ayukawaa@gmail.com (because I need it ^_^)
  243.  * pure js, tested on IE 11, Firefox 27, Chrome 33
  244.  * can be easely changed to jquery,...
  245.  *
  246.  * 2014-03-11
  247.  */
  248.  
  249. /*
  250.  
  251.  
  252. require_once('class.progressbar.php');
  253.  
  254.  
  255.  
  256. */
  257.  
  258.  
  259.     # --- PROGRESS BAR ---
  260.     $config                             = array();
  261.     $config['width']                    = '600px';
  262.     $config['uniqueid']                 = '_0';
  263.     $config['txt_pre']                  = 'UN-DOING SOME NASTY ACTION: ';
  264.     $config['txt_post']                 = '... ha,ha,ha';
  265.     $config['percent']                  = 100;//initial
  266.     $config['decimals']                 = 2;
  267.     $config['css_divmain']              = 'position:relative;';
  268.     $config['css_divtext']              = 'position:absolute;width:400px;padding-top:6px;height:6px;text-align:left;font-size:9px;font-family: Verdana;';
  269.     $config['css_divsubdiv']            = 'width:100%;height:4px;border:1px solid #a9ddef;';
  270.     $config['css_divcolored']           = 'float:left;height:4px;background-color:#43b6df;';
  271.     $config['css_divtransparent']       = 'float:left;';
  272.     $config['css_progressbar_finished'] = 'background-color:#eeeeee !important;';
  273.     $config['js_posthook']              = 'document.getElementById("dom_write").innerHTML="ProgressBar ["+uniqueid+"] finished!!!";';
  274.     $pb0                                = new ProgressBar($config);
  275.    
  276.     # --- PROGRESS BAR ---
  277.     $config                             = array();
  278.     $config['width']                    = '600px';
  279.     $config['uniqueid']                 = '_9';
  280.     $config['txt_pre']                  = 'REAL PROCESS: ';
  281.     $config['decimals']                 = 0;
  282.     $config['css_divmain']              = 'position:relative;';
  283.     $config['css_divtext']              = 'color:#86610b;position:absolute;width:100%;height:40px;text-align:center;font-size:22px;font-weight:700;font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;line-height: 40px;vertical-align: middle;';
  284.     $config['css_divsubdiv']            = 'width:100%;height:40px;border:1px solid #86610b;';
  285.     $config['css_divcolored']           = 'float:left;height:40px;background-color:#f5cb68;';
  286.     $config['css_divtransparent']       = 'float:left;';
  287.     $config['css_progressbar_finished'] = 'background-color:#86610b !important;';
  288.     $config['js_prehook']               = 'document.getElementById("title").innerHTML="PRE HOOK: Starting 2nd process";';
  289.     $config['js_posthook']              = 'alert("REAL PROCESS: ["+uniqueid+"] finished!!!");document.getElementById("title").innerHTML="Multiple progress bars during long PHP script execution";';
  290.     $pb9                                = new ProgressBar($config);
  291.  
  292.     # --- PROGRESS BAR ---
  293.     $config                             = array();
  294.     $config['width']                    = '600px';
  295.     $config['uniqueid']                 = '_1';
  296.     $config['txt_pre']                  = 'Nested ';
  297.     $config['txt_post']                 = ' (+rounded borders)';
  298.     $config['percent']                  = 0;//initial
  299.     $config['decimals']                 = 1;
  300.     $config['css_divmain']              = 'position:relative;';
  301.     $config['css_divtext']              = 'color:#0002ff;position:absolute;width:100%;height:40px;text-align:center;font-size:18px;font-family: times, serif;line-height: 40px;vertical-align: middle;';
  302.     $config['css_divsubdiv']            = 'width:100%;height:40px;border:1px solid #a9ddef;border-radius: 10px 10px 10px 10px;';
  303.     $config['css_divcolored']           = 'float:left;height:40px;background-color:#43b6df;border-radius: 10px 10px 10px 10px;';
  304.     $config['css_divtransparent']       = 'float:left;border-radius: 0 10px 10px 0;';
  305.     $config['css_progressbar_finished'] = 'border-radius: 10px 10px 10px 10px !important;background-color:#43b6df !important;';
  306.     $pb1                                = new ProgressBar($config);
  307.  
  308.     # --- PROGRESS BAR ---
  309.     $config                             = array();
  310.     $config['uniqueid']                 = '_2';
  311.     $config['width']                    = '400px';
  312.     $config['txt_pre']                  = 'Getting clients for emailing ';
  313.     $config['txt_post']                 = '...';
  314.     $config['percent']                  = 0;
  315.     $config['decimals']                 = 3;
  316.     $config['hide_when_done']           = true;
  317.     $config['css_divmain']              = 'position:relative;padding-left:50px;';
  318.     $config['css_divtext']              = 'position:absolute;width:100%;height: 16px;text-align:center;font-size:9px;font-weight:700;font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;line-height: 16px;vertical-align: middle;';
  319.     $config['css_divsubdiv']            = 'width:100%;height:16px;border:1px solid #ffddef;border-radius:5px 5px 5px 5px;';
  320.     $config['css_divcolored']           = 'float:left;height:16px;background-color:#ffb6df;border-radius: 5px 5px 5px 5px;';
  321.     $config['css_divtransparent']       = 'float:left;border-radius: 0 5px 5px 0;';
  322.     $config['css_progressbar_finished'] = 'border-radius: 5px 5px 5px 5px !important;background-color:#ffaaaa !important;';
  323.     $pb2                                = new ProgressBar($config);
  324.    
  325.     # --- PROGRESS BAR ---
  326.     $config                             = array();
  327.     $config['uniqueid']                 = '_3';
  328.     $config['width']                    = '400px';
  329.     $config['txt_pre']                  = 'Sending email to clients ';
  330.     $config['txt_post']                 = '';
  331.     $config['percent']                  = 0;
  332.     $config['decimals']                 = 2;
  333.     $config['hide_when_done']           = true;
  334.     $config['css_divmain']              = 'position:relative;padding-left:100px;';
  335.     $config['css_divtext']              = 'position:absolute;width:100%;height:10px;text-align:center;font-size:8px;font-weight:700;font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;line-height:10px;vertical-align: middle;';
  336.     $config['css_divsubdiv']            = 'width:100%;height:10px;border:1px solid #9acd32;';
  337.     $config['css_divcolored']           = 'float:left;height:10px;background-color:#e0eebb;';
  338.     $config['css_divtransparent']       = 'float:left;';
  339.     $config['css_progressbar_finished'] = 'background-color:#ffaaaa !important;';
  340.     $pb3                                = new ProgressBar($config);
  341.  
  342.     # --- PROGRESS BAR ---
  343.     $pb4                                = new ProgressBar('_MyNewUniqueID');//all default
  344.    
  345.     # write headers and functions
  346.     echo 'ALL OPTIONS BY DEFAULT:<br />';
  347.     $pb4->render();
  348.     echo '<br /><p>DOM element written at end: <strong><span id="dom_write">Waiting...</span></strong></p>';
  349.     $pb0->render();
  350.     echo '<br />';
  351.     $pb9->render();
  352.     echo '<br />';
  353.  
  354.  
  355.     # start procesing
  356.     $max_value_for_pbar_0               = 305;
  357.     $max_value_for_pbar_1               = 7;
  358.     $max_value_for_pbar_2               = 3;
  359.     $max_value_for_pbar_3               = 53;
  360.     $max_value_for_pbar_4               = 7000;
  361.    
  362.     # ----------------------------------
  363.     # progressbar 4: ALL OPTIONS BY DEFAULT
  364.     # ----------------------------------
  365.     for ($x4 = 0; $x4 < $max_value_for_pbar_4; $x4++)
  366.     {
  367.         $percent_value_pbar_4           = $x4*100/$max_value_for_pbar_4;
  368.         $text4                          = number_format($percent_value_pbar_4, 2, '.', '').'%';
  369.         $pb4->progress($percent_value_pbar_4, $text4);
  370.         usleep(100);
  371.     }
  372.     $pb4->progress(100,null,true);
  373.  
  374.     # ----------------------------------
  375.     # progressbar 0
  376.     # ----------------------------------
  377.     for ($x0 = $max_value_for_pbar_0; $x0 >0 ; $x0--)
  378.     {
  379.         $percent_value_pbar_0           = $x0*100/$max_value_for_pbar_0;
  380.         $text0                          = number_format($percent_value_pbar_0, 2, '.', '').'%';
  381.         $pb0->progress($percent_value_pbar_0, $text0);
  382.         usleep(10000);
  383.     }
  384.     $pb0->progress(0,null,true);//<- countdown to 0
  385.    
  386.     # ----------------------------------
  387.     # progressbar 9
  388.     # ----------------------------------
  389.     $array                              = array('Starting job','Selecting clients','Reading accounts','Procesing orders','Taking the money ^_^','Sending emails','Deleting trash');
  390.     $max_value_for_pbar_9               = count($array);
  391.     for ($x9 =0;$x9 < $max_value_for_pbar_9; $x9++)
  392.     {
  393.         $percent_value_pbar_9           = $x9*100/$max_value_for_pbar_9;
  394.         $text9                          = $array[$x9];
  395.         $pb9->progress($percent_value_pbar_9, $text9);
  396.         usleep(1000000);
  397.     }
  398.     $pb9->progress(100,null,true);
  399.    
  400.     # ----------------------------------
  401.     # progressbars 1-2-3 nested
  402.     # ----------------------------------
  403.     // draw progressbars now!
  404.     $pb1->render();
  405.     echo '<br />';
  406.     $pb2->render();
  407.     echo '<br />';
  408.     $pb3->render('100%');//width !!!
  409.     for ($x1 = 0; $x1 < $max_value_for_pbar_1; $x1++)
  410.     {
  411.         $percent_value_pbar_1           = $x1*100/$max_value_for_pbar_1;
  412.         $text1                          = number_format($percent_value_pbar_1, 2, '.', '').'%';
  413.         $pb1->progress($percent_value_pbar_1, $text1);
  414.         usleep(100000);
  415.         for ($x2 = 0; $x2 < $max_value_for_pbar_2; $x2++)
  416.         {
  417.             $percent_value_pbar_2       = $x2*100/$max_value_for_pbar_2;
  418.             $text2                      = number_format($percent_value_pbar_2, 2, '.', '').'%';
  419.             $pb2->progress($percent_value_pbar_2, $text2);
  420.             usleep(100000);
  421.             for ($x3 = 0; $x3 < $max_value_for_pbar_3; $x3++)
  422.             {
  423.                 $percent_value_pbar_3   = $x3*100/$max_value_for_pbar_3;
  424.                 $text3                  = number_format($percent_value_pbar_3, 2, '.', '').'%';
  425.                 $pb3->progress($percent_value_pbar_3, $text3);
  426.                 usleep(10000);
  427.             }
  428.         }
  429.     }
  430.     $pb3->progress(100,null,true);
  431.     $pb2->progress(100,null,true);
  432.     $pb1->progress(100,'All process done!',true);
  433.  
  434.  
  435. echo '<hr>Done.<br />';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement