Examples TOCexamples

Embedded ProgressBar Generator

$Date: 2005/07/25 12:52:15 $

 Table of contents

Introduction

This example requires :

Be aware that:


This example will run a custom ProgressBar Controller Wizard integrated in your own existing pages. As other examples it will help you to create your own progress bar. You may even save the CSS and/or PHP code at end !

This example will show you how to customize all actions (display, preview, process). We will use IT template engine family, but you may use engine such as Smarty or even many others.

At line 12 is included the IT template engine ressource for default class name 'ActionDisplay'.

But we have choosen to include the generator in our own pages (itdynamic_generator.html) set at line 43.

For preview tab, we have to make it ourself. This is done on lines 46 to 57. CSS and Javascript code required for progress bar are merge into html page on lines 49 to 53.

The progress bar generated with all your options at line 47 is finally included into html page on lines 55, 56.

On lines 84 to 208, class MyProcessHandler is defined to replace default 'ActionProcess', that will help you to download the file that contents your progress bar PHP/CSS code (see option 'text/php' at line 172, rather than default 'text/plain').

Don't forget also on lines 88 to 90 code to manage action when user click on Cancel button.

And finally, the generator is create at lines 213 to 216, and run on line 218.

[Top]

 Render options

Uses defaults: and replace :

[Top]

 Output

Screenshot

[Top]

 PHP source syntax highlight

  1. <?php
  2. /**
  3.  * How to embedded HTML_Progress_Generator into existing html page
  4.  * and allows php/css source-code download.
  5.  *
  6.  * @version    $Id: embedded.php,v 1.3 2005/07/25 12:52:15 farell Exp $
  7.  * @author     Laurent Laville <pear@laurent-laville.org>
  8.  * @package    HTML_Progress
  9.  * @subpackage Examples
  10.  */
  11.  
  12. require_once 'HTML/Progress/generator.php';
  13.  
  14. /* 1. Choose between standard renderers (default, HTMLPage, ITDynamic).
  15.       If none is selected, then 'default' will be used.
  16.       It can be automatically loaded and added by the controller
  17.  */
  18. require_once 'HTML/Progress/generator/ITDynamic.php';
  19.  
  20. /* 2. 'ActionDisplay' is default classname that should exists
  21.       to manage wizard/tabbed display. But you can also create
  22.       your own class under a new name. Then you've to give
  23.       the new name to HTML_Progress_Generator.
  24.       For example:
  25.  
  26.       class MyDisplayHandler extends HTML_QuickForm_Action_Display
  27.       {
  28.            ...
  29.       }
  30.       If your 'MyDisplayHandler' class is not defined, then default
  31.       'ActionDisplay' ('HTML/Progress/generator/default.php')
  32.       will be used.
  33.  */
  34. class MyDisplayHandler extends HTML_QuickForm_Action_Display
  35. {
  36.     function _renderForm(&$page)
  37.     {
  38.         $pageName = $page->getAttribute('name');
  39.         $tabPreview = array_slice ($page->controller->_tabs, -2, 1);
  40.  
  41.         $tpl =& new HTML_Template_ITX('../templates');
  42.  
  43.         $tpl->loadTemplateFile('itdynamic_generator.html');
  44.  
  45.         // on preview tab, add progress bar javascript and stylesheet
  46.         if ($pageName == $tabPreview[0][0]) {
  47.             $bar = $page->controller->createProgressBar();
  48.  
  49.             $tpl->setVariable(array(
  50.                 'qf_style'  => $bar->getStyle(),
  51.                 'qf_script' => $bar->getScript()
  52.                 )
  53.             );
  54.  
  55.             $barElement =& $page->getElement('progressBar');
  56.             $barElement->setText( $bar->toHtml() );
  57.         }
  58.  
  59.         $renderer =& new HTML_QuickForm_Renderer_ITDynamic($tpl);
  60.         $renderer->setElementBlock(array(
  61.             'buttons'     => 'qf_buttons'
  62.         ));
  63.  
  64.         $page->accept($renderer);
  65.  
  66.         $tpl->show();
  67.     }
  68. }
  69.  
  70. /* 3. 'ActionProcess' is default classname that should exists
  71.       to save your progress bar php/css source-code. But you can also create
  72.       your own class under a new name. Then you've to give
  73.       the new name to HTML_Progress_Generator.
  74.       For example:
  75.  
  76.       class MyProcessHandler extends HTML_QuickForm_Action
  77.       {
  78.            ...
  79.       }
  80.       If your 'MyProcessHandler' class is not defined, then default
  81.       'ActionProcess' ('HTML/Progress/generator/process.php')
  82.       will be used.
  83.  */
  84. class MyProcessHandler extends HTML_QuickForm_Action
  85. {
  86.     function perform(&$page, $actionName)
  87.     {
  88.         if ($actionName == 'cancel') {
  89.             echo '<h1>Progress Generator Demonstration is Over</h1>';
  90.             echo '<p>Hope you\'ve enjoyed. See you later!</p>';
  91.         } else {
  92.             // Checks whether the pages of the controller are valid
  93.             $page->isFormBuilt() or $page->buildForm();
  94.             $page->controller->isValid();
  95.  
  96.             // what kind of source code is requested
  97.             $code = $page->exportValue('phpcss');
  98.             $bar = $page->controller->createProgressBar();
  99.  
  100.             $lineEnd = OS_WINDOWS ? "\r\n" : "\n";
  101.  
  102.             if (isset($code['C']) && !isset($code['P'])) {
  103.                 $this->exportOutput($bar->getStyle(), 'text/css');
  104.             }
  105.  
  106.             if (isset($code['P'])) {
  107.                 $structure = $bar->toArray();
  108.  
  109.                 if (isset($code['C'])) {
  110.                     $strCSS  = '<style type="text/css">'.$lineEnd;
  111.                     $strCSS .= '<!--'.$lineEnd;
  112.                     $strCSS .= $bar->getStyle();
  113.                     $strCSS .= '// -->'.$lineEnd;
  114.                     $strCSS .= '</style>'.$lineEnd;
  115.                     $strPHP  = $strCSS;
  116.                 } else {
  117.                     $strPHP  = '';
  118.                 }
  119.                 $strPHP .= '<?php'.$lineEnd;
  120.                 $strPHP .= 'require_once \'HTML/Progress.php\';'.$lineEnd.$lineEnd;
  121.                 $strPHP .= '$progress = new HTML_Progress();'.$lineEnd;
  122.                 $strPHP .= '$progress->setIdent(\'PB1\');'.$lineEnd;
  123.  
  124.                 if ($bar->isIndeterminate()) {
  125.                     $strPHP .= '$progress->setIndeterminate(true);'.$lineEnd;
  126.                 }
  127.                 if ($bar->isBorderPainted()) {
  128.                     $strPHP .= '$progress->setBorderPainted(true);'.$lineEnd;
  129.                 }
  130.                 if ($bar->isStringPainted()) {
  131.                     $strPHP .= '$progress->setStringPainted(true);'.$lineEnd;
  132.                 }
  133.                 if (is_null($structure['string'])) {
  134.                     $strPHP .= '$progress->setString(null);';
  135.                 } else {
  136.                     $strPHP .= '$progress->setString('.$structure['string'].');';
  137.                 }
  138.                 $strPHP .= $lineEnd;
  139.                 if ($structure['animspeed'] > 0) {
  140.                     $strPHP .= '$progress->setAnimSpeed('.$structure['animspeed'].');'.$lineEnd;
  141.                 }
  142.                 if ($structure['dm']['minimum'] != 0) {
  143.                     $strPHP .= '$progress->setMinimum('.$structure['dm']['minimum'].');'.$lineEnd;
  144.                 }
  145.                 if ($structure['dm']['maximum'] != 100) {
  146.                     $strPHP .= '$progress->setMaximum('.$structure['dm']['maximum'].');'.$lineEnd;
  147.                 }
  148.                 if ($structure['dm']['increment'] != 1) {
  149.                     $strPHP .= '$progress->setIncrement('.$structure['dm']['increment'].');'.$lineEnd;
  150.                 }
  151.                 $strPHP .= $lineEnd;
  152.                 $strPHP .= '$ui =& $progress->getUI();'.$lineEnd;
  153.  
  154.                 $orient = ($structure['ui']['orientation'] == '1') ? 'HTML_PROGRESS_BAR_HORIZONTAL' : 'HTML_PROGRESS_BAR_VERTICAL';
  155.                 $strPHP .= '$ui->setOrientation('.$orient.');'.$lineEnd;
  156.                 $strPHP .= '$ui->setFillWay(\''.$structure['ui']['fillway'].'\');'.$lineEnd;
  157.  
  158.             /* Page 1: Progress attributes **************************************************/
  159.                 $strPHP .= $this->_attributesArray('$ui->setProgressAttributes(', $structure['ui']['progress']);
  160.                 $strPHP .= $lineEnd;
  161.  
  162.             /* Page 2: Cell attributes ******************************************************/
  163.                 $strPHP .= '$ui->setCellCount('.$structure['ui']['cell']['count'].');'.$lineEnd;
  164.                 unset($structure['ui']['cell']['count']);  // to avoid dupplicate entry in attributes
  165.                 $strPHP .= $this->_attributesArray('$ui->setCellAttributes(', $structure['ui']['cell']);
  166.                 $strPHP .= $lineEnd;
  167.  
  168.             /* Page 3: Border attributes ****************************************************/
  169.                 $strPHP .= $this->_attributesArray('$ui->setBorderAttributes(', $structure['ui']['border']);
  170.                 $strPHP .= $lineEnd;
  171.  
  172.             /* Page 4: String attributes ****************************************************/
  173.                 $strPHP .= $this->_attributesArray('$ui->setStringAttributes(', $structure['ui']['string']);
  174.                 $strPHP .= $lineEnd.$lineEnd;
  175.  
  176.                 $strPHP .= '// code below is only for run demo; its not nececessary to create progress bar'.$lineEnd;
  177.                 if (!isset($code['C'])) {
  178.                     $strPHP .= 'echo "<style type=\"text/css\">\n";'.$lineEnd;
  179.                     $strPHP .= 'echo "<!--\n";'.$lineEnd;
  180.                     $strPHP .= 'echo $progress->getStyle();'.$lineEnd;
  181.                     $strPHP .= 'echo "// -->\n";'.$lineEnd;
  182.                     $strPHP .= 'echo "</style>\n";'.$lineEnd;
  183.                 }
  184.                 $strPHP .= 'echo "<script type=\"text/javascript\">\n";'.$lineEnd;
  185.                 $strPHP .= 'echo "<!--\n";'.$lineEnd;
  186.                 $strPHP .= 'echo $progress->getScript();'.$lineEnd;
  187.                 $strPHP .= 'echo "//-->\n";'.$lineEnd;
  188.                 $strPHP .= 'echo "</script>\n";'.$lineEnd;
  189.                 $strPHP .= 'echo $progress->toHtml();'.$lineEnd;
  190.                 $strPHP .= '$progress->run();'.$lineEnd;
  191.                 $strPHP .= '?>';
  192.                 $this->exportOutput($strPHP, 'text/php');
  193.             }
  194.  
  195.             // reset session data
  196.             $page->controller->container(true);
  197.         }
  198.     }
  199.  
  200.     function exportOutput($str, $mime = 'text/plain', $charset = 'iso-8859-1')
  201.     {
  202.         if (!headers_sent()) {
  203.             header("Expires: Tue, 1 Jan 1980 12:00:00 GMT");
  204.             header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  205.             header("Cache-Control: no-cache");
  206.             header("Pragma: no-cache");
  207.             header("Content-Type: $mime; charset=$charset");
  208.         }
  209.         print $str;
  210.     }
  211.  
  212.     function _attributesArray($str, $attributes)
  213.     {
  214.         $strPHP = $str . 'array(';
  215.         foreach ($attributes as $attr => $val) {
  216.             if (is_integer($val)) {
  217.                 $strPHP .= "'$attr'=>$val, ";
  218.             } elseif (is_bool($val)) {
  219.                 $strPHP .= "'$attr'=>".($val?'true':'false').', ';
  220.             } else {
  221.                 $strPHP .= "'$attr'=>'$val', ";
  222.             }
  223.         }
  224.         $strPHP = ereg_replace(', $', '', $strPHP);
  225.         $strPHP .= '));';
  226.         return $strPHP;
  227.     }
  228. }
  229.  
  230.  
  231. session_start();
  232.  
  233. $tabbed = new HTML_Progress_Generator('PBwizard', array(
  234.                                       'display' => 'MyDisplayHandler',
  235.                                       'process' => 'MyProcessHandler'
  236.                                       ));
  237.  
  238. $tabbed->run();
  239. ?>

[Top]