home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / Progress2 / Generator / Process.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  10.6 KB  |  283 lines

  1. <?php
  2. /**
  3.  * Copyright (c) 2005-2008, Laurent Laville <pear@laurent-laville.org>
  4.  *
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  *     * Redistributions of source code must retain the above copyright
  12.  *       notice, this list of conditions and the following disclaimer.
  13.  *     * Redistributions in binary form must reproduce the above copyright
  14.  *       notice, this list of conditions and the following disclaimer in the
  15.  *       documentation and/or other materials provided with the distribution.
  16.  *     * Neither the name of the authors nor the names of its contributors
  17.  *       may be used to endorse or promote products derived from this software
  18.  *       without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  24.  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * PHP versions 4 and 5
  33.  *
  34.  * @category  HTML
  35.  * @package   HTML_Progress2
  36.  * @author    Laurent Laville <pear@laurent-laville.org>
  37.  * @copyright 2005-2008 Laurent Laville
  38.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  39.  * @version   CVS: $Id: Process.php,v 1.9 2008/03/20 21:27:55 farell Exp $
  40.  * @link      http://pear.php.net/package/HTML_Progress2
  41.  * @since     File available since Release 2.0.0RC1
  42.  */
  43.  
  44.  
  45. /**
  46.  * The ActionProcess class provides final step of ProgressBar creation.
  47.  * Manage php/css source-code save and cancel action.
  48.  *
  49.  * @category  HTML
  50.  * @package   HTML_Progress2
  51.  * @author    Laurent Laville <pear@laurent-laville.org>
  52.  * @copyright 2005-2008 Laurent Laville
  53.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  54.  * @version   Release: 2.4.0
  55.  * @link      http://pear.php.net/package/HTML_Progress2
  56.  * @since     Class available since Release 2.0.0RC1
  57.  */
  58.  
  59. class ActionProcess extends HTML_QuickForm_Action
  60. {
  61.     /**
  62.      * Performs an action on a page of the controller (wizard)
  63.      *
  64.      * @param string &$page      current page displayed by the controller
  65.      * @param string $actionName page action asked
  66.      *
  67.      * @return     void
  68.      * @since      version 2.0.0 (2005-10-01)
  69.      * @access     public
  70.      */
  71.     function perform(&$page, $actionName)
  72.     {
  73.         if ($actionName == 'cancel') {
  74.             echo '<h1>Progress2 Generator Task was canceled</h1>';
  75.             echo '<p>None (PHP/CSS) source codes are available.</p>';
  76.         } else {
  77.             // Checks whether the pages of the controller are valid
  78.             $page->isFormBuilt() or $page->buildForm();
  79.             $page->controller->isValid();
  80.  
  81.             // what kind of source code is requested
  82.             $code = $page->exportValue('phpcss');
  83.             $pb   = $page->controller->createProgressBar();
  84.  
  85.             $phpCode = (isset($code['P']) === true);
  86.             $cssCode = (isset($code['C']) === true);
  87.  
  88.             if ($cssCode) {
  89.                 $strCSS = $this->sprintCSS($pb);
  90.                 $this->exportOutput($strCSS);
  91.             }
  92.             if ($phpCode) {
  93.                 $strPHP = $this->sprintPHP($pb, $cssCode);
  94.                 $this->exportOutput($strPHP);
  95.             }
  96.  
  97.             // reset session data
  98.             $page->controller->container(true);
  99.         }
  100.     }
  101.  
  102.     /**
  103.      * Returns a formatted string of the progress meter stylesheet
  104.      *
  105.      * @param object  &$pBar progress meter object reference
  106.      * @param boolean $raw   (optional) decides whether to put html tags or not
  107.      *
  108.      * @return     string
  109.      * @since      version 2.0.0 (2005-10-01)
  110.      * @access     public
  111.      */
  112.     function sprintCSS(&$pBar, $raw = false)
  113.     {
  114.         return $pBar->getStyle($raw);
  115.     }
  116.  
  117.     /**
  118.      * Returns a formatted string of the progress meter php code
  119.      *
  120.      * @param object  &$pBar   progress meter object reference
  121.      * @param boolean $cssCode returns css source code
  122.      * @param boolean $raw     (optional) decides whether to put php tags or not
  123.      *
  124.      * @return     string
  125.      * @since      version 2.0.0 (2005-10-01)
  126.      * @access     public
  127.      */
  128.     function sprintPHP(&$pBar, $cssCode, $raw = false)
  129.     {
  130.         $structure = $pBar->toArray();
  131.  
  132.         if ($raw) {
  133.             $strPHP = PHP_EOL;
  134.         } else {
  135.             $strPHP = '<?php'
  136.                     . PHP_EOL;
  137.         }
  138.         $strPHP .= 'require_once \'HTML/Progress2.php\';'
  139.                 . PHP_EOL . PHP_EOL;
  140.         $strPHP .= '$pb = new HTML_Progress2();'
  141.                 . PHP_EOL;
  142.         $strPHP .= '$pb->setIdent(\'PB1\');'
  143.                 . PHP_EOL;
  144.  
  145.         if ($pBar->isIndeterminate()) {
  146.             $strPHP .= '$pb->setIndeterminate(true);'
  147.                     . PHP_EOL;
  148.         }
  149.         if ($pBar->isBorderPainted()) {
  150.             $strPHP .= '$pb->setBorderPainted(true);'
  151.                     . PHP_EOL;
  152.         }
  153.         if ($structure['animspeed'] > 0) {
  154.             $strPHP .= '$pb->setAnimSpeed(' . $structure['animspeed'] . ');'
  155.                     . PHP_EOL;
  156.         }
  157.         if ($structure['minimum'] != 0) {
  158.             $strPHP .= '$pb->setMinimum(' . $structure['minimum'] . ');'
  159.                     . PHP_EOL;
  160.         }
  161.         if ($structure['maximum'] != 100) {
  162.             $strPHP .= '$pb->setMaximum(' . $structure['maximum'] . ');'
  163.                     . PHP_EOL;
  164.         }
  165.         if ($structure['increment'] != 1) {
  166.             $strPHP .= '$pb->setIncrement(' . $structure['increment'] . ');'
  167.                     . PHP_EOL;
  168.         }
  169.         if ($structure['orientation'] == '2') {
  170.             $strPHP .= '$pb->setOrientation(HTML_PROGRESS2_BAR_VERTICAL);'
  171.                     . PHP_EOL;
  172.         }
  173.         if ($structure['fillway'] != 'natural') {
  174.             $strPHP .= '$pb->setFillWay(\'' . $structure['fillway'] . '\');'
  175.                     . PHP_EOL;
  176.         }
  177.  
  178.         /* Page 1: Progress attributes ****************************************/
  179.         $strPHP .= $this->_attributesArray('$pb->setProgressAttributes(',
  180.                                            $structure['progress']);
  181.         $strPHP .= PHP_EOL;
  182.  
  183.         /* Page 2: Cell attributes ********************************************/
  184.         $strPHP .= '$pb->setCellCount(' . $structure['cellcount'] . ');'
  185.                 . PHP_EOL;
  186.         $strPHP .= $this->_attributesArray('$pb->setCellAttributes(',
  187.                                            $structure['cell']);
  188.         $strPHP .= PHP_EOL;
  189.  
  190.         /* Page 3: Border attributes ******************************************/
  191.         $strPHP .= $this->_attributesArray('$pb->setBorderAttributes(',
  192.                                            $structure['border']);
  193.         $strPHP .= PHP_EOL;
  194.  
  195.         /* Page 4: Label attributes *******************************************/
  196.         foreach ($structure['label'] as $name => $data) {
  197.             if ($data['type'] == HTML_PROGRESS2_LABEL_TEXT) {
  198.                 $strPHP .= '$pb->addLabel(HTML_PROGRESS2_LABEL_TEXT, \''
  199.                         . $name .'\');';
  200.                 $strPHP .= PHP_EOL;
  201.             }
  202.             unset($data['type']);
  203.             $strPHP .= $this->_attributesArray('$pb->setLabelAttributes(\''
  204.                     . $name . '\', ', $data);
  205.             $strPHP .= PHP_EOL;
  206.         }
  207.  
  208.         $strPHP .= PHP_EOL;
  209.         $strPHP .= '// code below is only for run demo;'
  210.         $strPHP .= PHP_EOL;
  211.         $strPHP .= '// its not nececessary to create progress bar';
  212.         $strPHP .= PHP_EOL;
  213.         if (!$cssCode) {
  214.             $strPHP .= 'echo \'<head>\' . PHP_EOL;' . PHP_EOL;
  215.             $strPHP .= 'echo $pb->getStyle(false) . PHP_EOL;' . PHP_EOL;
  216.         }
  217.         $strPHP .= 'echo $pb->getScript(false) . PHP_EOL;' . PHP_EOL;
  218.         if (!$cssCode) {
  219.             $strPHP .= 'echo \'</head>\' . PHP_EOL;' . PHP_EOL;
  220.             $strPHP .= 'echo \'<body>\' . PHP_EOL;' . PHP_EOL;
  221.         }
  222.         $strPHP .= '$pb->display();' . PHP_EOL;
  223.         $strPHP .= '$pb->run();' . PHP_EOL;
  224.         if (!$cssCode) {
  225.             $strPHP .= 'echo \'</body>\' . PHP_EOL;' . PHP_EOL;
  226.         }
  227.         if (!$raw) {
  228.             $strPHP .= '?>';
  229.         }
  230.         return $strPHP;
  231.     }
  232.  
  233.     /**
  234.      * Prints a string to standard output, with http headers if necessary
  235.      *
  236.      * @param string $str     string to print
  237.      * @param string $mime    (optional) mime description
  238.      * @param string $charset (optional) charset to use
  239.      *
  240.      * @return     void
  241.      * @since      version 2.0.0 (2005-10-01)
  242.      * @access     public
  243.      */
  244.     function exportOutput($str, $mime = 'text/plain', $charset = 'iso-8859-1')
  245.     {
  246.         if (!headers_sent()) {
  247.             header("Expires: Tue, 1 Jan 1980 12:00:00 GMT");
  248.             header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  249.             header("Cache-Control: no-cache");
  250.             header("Pragma: no-cache");
  251.             header("Content-Type: $mime; charset=$charset");
  252.         }
  253.         print $str;
  254.     }
  255.  
  256.     /**
  257.      * Complete a php function arguments line with appropriate attributes
  258.      *
  259.      * @param string $str        php function to complete
  260.      * @param array  $attributes function arguments list of values
  261.      *
  262.      * @return     string
  263.      * @since      version 2.0.0 (2005-10-01)
  264.      * @access     private
  265.      */
  266.     function _attributesArray($str, $attributes)
  267.     {
  268.         $strPHP = $str . 'array(';
  269.         foreach ($attributes as $attr => $val) {
  270.             if (is_integer($val)) {
  271.                 $strPHP .= "'$attr'=>$val, ";
  272.             } elseif (is_bool($val)) {
  273.                 $strPHP .= "'$attr'=>" . ($val ? 'true' : 'false') . ', ';
  274.             } else {
  275.                 $strPHP .= "'$attr'=>'$val', ";
  276.             }
  277.         }
  278.         $strPHP  = ereg_replace(', $', '', $strPHP);
  279.         $strPHP .= '));';
  280.         return $strPHP;
  281.     }
  282. }
  283. ?>