Custom Generator

Overview

This example will run a custom Progress2 Generator Wizard that will help you to build your own progress bar.
display and process actions have been modified to allows IT dynamic QF renderer usage and download source code rather than print on standard output.

Screenshot

sample screenshot

Demonstration

Give it a try

Dependencies

This example requires mandatory resources :

Explains step by step

The wizard windows (PBwizard) has default preview action, then display and process actions are overloaded by user callback (lines 42-80, 101-133): see class constructor parameters (lines 137-139).

Attribute Value Default
preview   ActionPreview =
HTML/Progress2/Generator/Preview.php
display MyDisplayHandler ActionDisplay =
HTML/Progress2/Generator/Display.php
process MyProcessHandler ActionProcess =
HTML/Progress2/Generator/Process.php

Source Code

  1. <?php
  2. /**
  3. * HTML_Progress2_Generator embedded into existing html page
  4. * and allows php/css source-code download.
  5. *
  6. * @version    $Id: generatorcus.php,v 1.4 2006/12/20 10:40:18 farell Exp $
  7. * @author     Laurent Laville <pear@laurent-laville.org>
  8. * @package    HTML_Progress2
  9. * @subpackage Examples
  10. * @access     public
  11. * @example    examples/generator/generatorcus.php
  12. *             generatorcus source code
  13. * @link       http://www.laurent-laville.org/img/progress/screenshot/generatorcus.png
  14. *             screenshot (Image PNG, 720x204 pixels) 25.2 Kb
  15. */
  16. require_once 'HTML/Progress2/Generator.php';
  17.  
  18. /* 1. Choose between standard renderers (default, HTMLPage2, ITDynamic).
  19.       If none is selected, then 'default' will be used.
  20.       It can be automatically loaded and added by the controller
  21. */
  22. require_once 'HTML/Progress2/Generator/ITDynamic.php';
  23.  
  24. /* 2. 'ActionDisplay' is default classname that should exists
  25.       to manage wizard/tabbed display. But you can also create
  26.       your own class under a new name. Then you've to give
  27.       the new name to HTML_Progress2_Generator.
  28.       For example:
  29.  
  30.       class MyDisplayHandler extends HTML_QuickForm_Action_Display
  31.       {
  32.            ...
  33.       }
  34.       If your 'MyDisplayHandler' class is not defined, then default
  35.       'ActionDisplay' ('HTML/Progress2/Generator/Default.php')
  36.       will be used.
  37. */
  38.  
  39. /**
  40. * @ignore
  41. */
  42. class MyDisplayHandler extends ActionDisplay
  43. {
  44.     function MyDisplayHandler($css = null)
  45.     {
  46.         // when no user-styles defined, used the default values
  47.         parent::ActionDisplay($css);
  48.     }
  49.  
  50.     function _renderForm(&$page)
  51.     {
  52.         $tpl =& new HTML_Template_Sigma('.', 'cache/');
  53.         $tpl->loadTemplateFile('itdynamic.html');
  54.  
  55.         $styles = $this->getStyleSheet();
  56.         $js     = '';
  57.  
  58.         // on preview tab, add progress bar javascript and stylesheet
  59.         if ($page->getAttribute('id') == 'Preview') {
  60.             $pb = $page->controller->createProgressBar();
  61.  
  62.             $styles .= $pb->getStyle();
  63.             $js      = $pb->getScript();
  64.  
  65.             $pbElement =& $page->getElement('progressBar');
  66.             $pbElement->setText($pb->toHtml() . '<br /><br />');
  67.         }
  68.         $tpl->setVariable(array(
  69.             'qf_style'  => $styles,
  70.             'qf_script' => $js
  71.             )
  72.         );
  73.  
  74.         $renderer =& new HTML_QuickForm_Renderer_ITDynamic($tpl);
  75.         $renderer->setElementBlock(array('buttons' => 'qf_buttons'));
  76.  
  77.         $page->accept($renderer);
  78.         $tpl->show();
  79.     }
  80. }
  81.  
  82. /* 3. 'ActionProcess' is default classname that should exists
  83.       to save your progress bar php/css source-code. But you can also create
  84.       your own class under a new name. Then you've to give
  85.       the new name to HTML_Progress2_Generator.
  86.       For example:
  87.  
  88.       class MyProcessHandler extends HTML_QuickForm_Action
  89.       {
  90.            ...
  91.       }
  92.       If your 'MyProcessHandler' class is not defined, then default
  93.       'ActionProcess' ('HTML/Progress2/Generator/Process.php')
  94.       will be used.
  95. */
  96. require_once 'HTML/Progress2/Generator/Process.php';
  97.  
  98. /**
  99. * @ignore
  100. */
  101. class MyProcessHandler extends ActionProcess
  102. {
  103.     function perform(&$page, $actionName)
  104.     {
  105.         if ($actionName == 'cancel') {
  106.             echo '<h1>Progress2 Generator Demonstration is Over</h1>';
  107.             echo '<p>Hope you\'ve enjoyed. See you later!</p>';
  108.         } else {
  109.             // Checks whether the pages of the controller are valid
  110.             $page->isFormBuilt() or $page->buildForm();
  111.             $page->controller->isValid();
  112.  
  113.             // what kind of source code is requested
  114.             $code = $page->exportValue('phpcss');
  115.             $pb = $page->controller->createProgressBar();
  116.  
  117.             $phpCode = (isset($code['P']) === true);
  118.             $cssCode = (isset($code['C']) === true);
  119.  
  120.             if ($cssCode && !$phpCode) {
  121.                 $strCSS = $this->sprintCSS($pb);
  122.                 $this->exportOutput($strCSS, 'text/css');
  123.             }
  124.             if ($phpCode) {
  125.                 $strPHP = $this->sprintPHP($pb, $cssCode);
  126.                 $this->exportOutput($strPHP, 'text/php');
  127.             }
  128.  
  129.             // reset session data
  130.             $page->controller->container(true);
  131.         }
  132.     }
  133. }
  134.  
  135. session_start();
  136.  
  137. $tabbed = new HTML_Progress2_Generator('PBwizard',
  138.     array('display' => 'MyDisplayHandler', 'process' => 'MyProcessHandler')
  139. );
  140. $tabbed->run();
  141. ?>