home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / indeterminate.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  6.1 KB  |  221 lines

  1. <?php 
  2. /**
  3.  * Horizontal ProgressBar in indeterminate mode.
  4.  * 
  5.  * @version    $Id: indeterminate.php,v 1.1 2003/11/15 18:27:10 thesaur Exp $
  6.  * @author     Laurent Laville <pear@laurent-laville.org>
  7.  * @package    HTML_Progress
  8.  */
  9.  
  10. require_once ('HTML/Progress/monitor.php');
  11.  
  12. class Task extends HTML_Progress_Monitor
  13. {
  14.     var $_current;
  15.     
  16.     function Task()
  17.     {
  18.         $this->_current = 0;
  19.         $this->_id = md5(microtime());
  20.  
  21.         $this->_form = new HTML_QuickForm('ProgressBarDialog');
  22.  
  23.         $renderer =& $this->_form->defaultRenderer();
  24.         $renderer->setFormTemplate('
  25.             <table width="450" border="0" cellpadding="3" cellspacing="2" bgcolor="#CCCC99">
  26.             <form{attributes}>{content}
  27.             </form>
  28.             </table>
  29.             ');
  30.         $renderer->setHeaderTemplate('
  31.             <tr>
  32.         <td style="white-space:nowrap;background:#996;color:#ffc;" align="left" colspan="2"><b>{header}</b></td>
  33.         </tr>
  34.         ');
  35.         
  36.         $this->_form->addElement('header', 'windowsname', 'Progress...');
  37.         $this->_form->addElement('static', 'progress');
  38.         $this->_form->addElement('static', 'status');
  39.  
  40.         $buttons[] = &HTML_QuickForm::createElement('submit', 'start',  'Start',  'style="width:80px;"');
  41.         $buttons[] = &HTML_QuickForm::createElement('submit', 'cancel', 'Cancel', 'style="width:80px;"');
  42.         $this->_form->addGroup($buttons);
  43.  
  44.         
  45.         $this->_progress = new HTML_Progress();
  46.         $this->_progress->setIncrement(10);
  47.         $this->_progress->setStringPainted(true);     // get space for the string
  48.         $this->_progress->setString("");              // but don't paint it
  49.  
  50.         $ui = & $this->_progress->getUI();
  51.         $ui->setProgressAttributes(array(
  52.             'background-color' => '#e0e0e0'
  53.                 ));        
  54.         $ui->setStringAttributes(array(
  55.                 'color'  => '#996',
  56.             'background-color' => '#CCCC99'
  57.                 ));        
  58.         $ui->setCellAttributes(array(
  59.                 'active-color' => '#996'
  60.                 ));
  61.  
  62.         $bar =& $this->_form->getElement('progress');
  63.         $bar->setText( $this->_progress->toHtml() );
  64.  
  65.         $str =& $this->_form->getElement('status');
  66.         $str->setText('<div id="status" style="color:#000000; font-size:10px;"> </div>');
  67.  
  68.         $this->_progress->addListener($this);
  69.     }
  70.  
  71.     function notify($event)
  72.     {
  73.         if (is_array($event)) {
  74.             $log = strtolower($event['log']);
  75.             $val = $event['value'];
  76.             
  77.             switch (strtolower($log)) {
  78.              case 'incvalue':
  79.              case 'setvalue':
  80.                  $this->_current = $this->getCurrent() + 16;
  81.                  $s = $this->getMessage();
  82.                  if (!is_null($s)) {
  83.                      if ($this->_progress->isIndeterminate()) {
  84.                          $this->_progress->setIndeterminate(false);
  85.                          $this->_progress->setString(null);      // display % string
  86.                          $this->_progress->setValue(0);
  87.                      }
  88.                      if ($this->isDone()) {
  89.                          $this->_progress->removeListener($this);
  90.                          $this->_progress->setString("");       // hide % string
  91.                      }
  92.                  }
  93.                  $this->_progress->display();
  94.                  
  95.                  if ($this->_progress->getPercentComplete() == 1) {
  96.                      if ($this->_progress->isIndeterminate()) {
  97.                          $this->_progress->setValue(0);
  98.                      }
  99.                  } else {
  100.                      $this->_progress->incValue();
  101.                  }
  102.                  break;
  103.              default:
  104.             }
  105.         }
  106.     }
  107.  
  108.     function getCurrent()
  109.     {
  110.         return $this->_current;
  111.     }
  112.  
  113.     function getMessage()
  114.     {
  115.         $c = $this->getCurrent();
  116.         $s = "completed $c out of 416";
  117.  
  118.         if (function_exists('ob_get_clean')) {
  119.             $status  = ob_get_clean();      // use for PHP 4.3+
  120.         } else {
  121.             $status  = ob_get_contents();   // use for PHP 4.2+
  122.             ob_end_clean();
  123.         }
  124.         $status = '<script type="text/javascript">self.setStatus("'.$s.'"); </script>';
  125.         echo $status;
  126.         ob_start();
  127.         
  128.         if ($c >= 240 ) {
  129.             return $s;
  130.         } else {
  131.             return null;
  132.         }
  133.     }
  134.  
  135.     function isDone()
  136.     {
  137.         return ( ($this->_progress->getPercentComplete() == 1) &&
  138.                  ($this->_progress->isIndeterminate() == false) );
  139.     }
  140.  
  141.  
  142.     function isStarted()
  143.     {
  144.         $action = $this->_form->getSubmitValues();
  145.  
  146.         if (isset($action['start'])) {
  147.             return true;
  148.         }
  149.  
  150.         return false;
  151.     }
  152.  
  153.     function run()
  154.     {
  155.         if ($this->isStarted()) {
  156.             $this->_progress->setIndeterminate(true);
  157.             $this->_progress->incValue();
  158.             
  159.         } else {
  160.             $abort = $this->isCanceled();
  161.         }
  162.     }
  163.  
  164. }
  165.  
  166. $task = new Task();
  167.  
  168. ?>
  169. <!DOCTYPE html
  170.     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  171.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  172.  
  173. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  174. <head>
  175. <title>Indeterminate Mode Progress example</title>
  176. <style type="text/css">
  177. <!--
  178. <?php echo $task->getStyle(); ?>
  179.  
  180. body {
  181.     background-color: #444444;
  182.     color: #EEEEEE;
  183.     font-family: Verdana, Arial;
  184. }
  185.  
  186. a:visited, a:active, a:link {
  187.     color: yellow;
  188. }
  189. // -->
  190. </style>
  191. <script type="text/javascript">
  192. <!--
  193. <?php echo $task->getScript(); ?>
  194.  
  195. function setStatus(pString)
  196. {
  197.         if (isDom)
  198.             prog = document.getElementById('status');
  199.         if (isIE)
  200.             prog = document.all['status'];
  201.         if (isNS4)
  202.             prog = document.layers['status'];
  203.     if (prog != null) 
  204.         prog.innerHTML = pString;
  205. }
  206. //-->
  207. </script>
  208. </head>
  209. <body>
  210. <h1><?php echo basename(__FILE__); ?></h1>
  211.  
  212. <?php 
  213. echo $task->toHtml(); 
  214.  
  215. $task->run();
  216. ?>
  217.  
  218. <p><< <a href="index.html">Back examples TOC</a></p>
  219.  
  220. </body>
  221. </html>