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

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