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 / Progress / monitor.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  13.4 KB  |  429 lines

  1. <?php
  2. /**
  3.  * The HTML_Progress_Monitor class allow an easy way to display progress
  4.  * in a dialog. The user can cancel the task.
  5.  *
  6.  * PHP versions 4 and 5
  7.  *
  8.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  9.  * that is available through the world-wide-web at the following URI:
  10.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  11.  * the PHP License and are unable to obtain it through the web, please
  12.  * send a note to license@php.net so we can mail you a copy immediately.
  13.  *
  14.  * @category   HTML
  15.  * @package    HTML_Progress
  16.  * @subpackage Progress_Observer
  17.  * @author     Laurent Laville <pear@laurent-laville.org>
  18.  * @copyright  1997-2005 The PHP Group
  19.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  20.  * @version    CVS: $Id: monitor.php,v 1.7 2005/07/25 13:06:13 farell Exp $
  21.  * @link       http://pear.php.net/package/HTML_Progress
  22.  */
  23.  
  24. require_once 'HTML/Progress.php';
  25. require_once 'HTML/QuickForm.php';
  26.  
  27. /**
  28.  * The HTML_Progress_Monitor class allow an easy way to display progress
  29.  * in a dialog. The user can cancel the task.
  30.  *
  31.  * PHP versions 4 and 5
  32.  *
  33.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  34.  * that is available through the world-wide-web at the following URI:
  35.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  36.  * the PHP License and are unable to obtain it through the web, please
  37.  * send a note to license@php.net so we can mail you a copy immediately.
  38.  *
  39.  * @category   HTML
  40.  * @package    HTML_Progress
  41.  * @subpackage Progress_Observer
  42.  * @author     Laurent Laville <pear@laurent-laville.org>
  43.  * @copyright  1997-2005 The PHP Group
  44.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  45.  * @version    Release: 1.2.5
  46.  * @link       http://pear.php.net/package/HTML_Progress
  47.  */
  48.  
  49. class HTML_Progress_Monitor
  50. {
  51.     /**
  52.      * Instance-specific unique identification number.
  53.      *
  54.      * @var        integer
  55.      * @since      1.0
  56.      * @access     private
  57.      */
  58.     var $_id;
  59.  
  60.     /**#@+
  61.      * Attributes of monitor form.
  62.      *
  63.      * @var        string
  64.      * @since      1.1
  65.      * @access     public
  66.      */
  67.     var $windowname;
  68.     var $buttonStart;
  69.     var $buttonCancel;
  70.     /**#@-*/
  71.  
  72.     /**
  73.      * The progress object renders into this monitor.
  74.      *
  75.      * @var        object
  76.      * @since      1.0
  77.      * @access     private
  78.      */
  79.     var $_progress;
  80.  
  81.     /**
  82.      * The quickform object that allows the presentation.
  83.      *
  84.      * @var        object
  85.      * @since      1.0
  86.      * @access     private
  87.      */
  88.     var $_form;
  89.  
  90.  
  91.     /**
  92.      * Constructor Summary
  93.      *
  94.      * o Creates a standard progress bar into a dialog box (QuickForm).
  95.      *   Form name, buttons 'start', 'cancel' labels and style, and
  96.      *   title of dialog box may also be changed.
  97.      *   <code>
  98.      *   $monitor = new HTML_Progress_Monitor();
  99.      *   </code>
  100.      *
  101.      * o Creates a progress bar into a dialog box, with only a new
  102.      *   form name.
  103.      *   <code>
  104.      *   $monitor = new HTML_Progress_Monitor($formName);
  105.      *   </code>
  106.      *
  107.      * o Creates a progress bar into a dialog box, with a new form name,
  108.      *   new buttons name and style, and also a different title box.
  109.      *   <code>
  110.      *   $monitor = new HTML_Progress_Monitor($formName, $attributes);
  111.      *   </code>
  112.      *
  113.      * @param      string    $formName      (optional) Name of monitor dialog box (QuickForm)
  114.      * @param      array     $attributes    (optional) List of renderer options
  115.      * @param      array     $errorPrefs    (optional) Hash of params to configure error handler
  116.      *
  117.      * @since      1.0
  118.      * @access     public
  119.      * @throws     HTML_PROGRESS_ERROR_INVALID_INPUT
  120.      */
  121.     function HTML_Progress_Monitor($formName = 'ProgressMonitor', $attributes = array(), $errorPrefs = array())
  122.     {
  123.         $bar = new HTML_Progress($errorPrefs);
  124.         $this->_progress = $bar;
  125.  
  126.         if (!is_string($formName)) {
  127.             return $this->_progress->raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  128.                 array('var' => '$formName',
  129.                       'was' => gettype($formName),
  130.                       'expected' => 'string',
  131.                       'paramnum' => 1));
  132.  
  133.         } elseif (!is_array($attributes)) {
  134.             return $this->_progress->raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  135.                 array('var' => '$attributes',
  136.                       'was' => gettype($attributes),
  137.                       'expected' => 'array',
  138.                       'paramnum' => 2));
  139.         }
  140.  
  141.         $this->_id = md5(microtime());
  142.  
  143.         $this->_form = new HTML_QuickForm($formName);
  144.         $this->_form->removeAttribute('name');        // XHTML compliance
  145.  
  146.         $this->windowname   = isset($attributes['title'])  ? $attributes['title']  : 'In progress ...';
  147.         $this->buttonStart  = isset($attributes['start'])  ? $attributes['start']  : 'Start';
  148.         $this->buttonCancel = isset($attributes['cancel']) ? $attributes['cancel'] : 'Cancel';
  149.         $buttonAttr         = isset($attributes['button']) ? $attributes['button'] : '';
  150.  
  151.         $this->_form->addElement('header', 'windowname', $this->windowname);
  152.         $this->_form->addElement('static', 'progressBar');
  153.         $this->_form->addElement('static', 'progressStatus');
  154.  
  155.         $style = $this->isStarted() ? array('disabled'=>'true') : null;
  156.  
  157.         $buttons[] =& $this->_form->createElement('submit', 'start',  $this->buttonStart, $style);
  158.         $buttons[] =& $this->_form->createElement('submit', 'cancel', $this->buttonCancel);
  159.  
  160.         $buttons[0]->updateAttributes($buttonAttr);
  161.         $buttons[1]->updateAttributes($buttonAttr);
  162.  
  163.         $this->_form->addGroup($buttons, 'buttons', '', ' ', false);
  164.  
  165.         // default embedded progress element with look-and-feel
  166.         $this->setProgressElement($bar);
  167.  
  168.         $str =& $this->_form->getElement('progressStatus');
  169.         $str->setText('<div id="status" class="progressStatus"> </div>');
  170.     }
  171.  
  172.     /**
  173.      * Listens all progress events from this monitor.
  174.      *
  175.      * @param      mixed     $event         A hash describing the progress event.
  176.      *
  177.      * @return     void
  178.      * @since      1.0
  179.      * @access     public
  180.      * @throws     HTML_PROGRESS_ERROR_INVALID_INPUT
  181.      * @see        HTML_Progress::process()
  182.      * @deprecated
  183.      */
  184.     function notify($event)
  185.     {
  186.     }
  187.  
  188.     /**
  189.      * Sets a user-defined progress handler function.
  190.      *
  191.      * @param      mixed     $handler       Name of function or a class-method.
  192.      *
  193.      * @return     void
  194.      * @since      1.1
  195.      * @access     public
  196.      * @throws     HTML_PROGRESS_ERROR_INVALID_CALLBACK
  197.      * @see        HTML_Progress::setProgressHandler()
  198.      */
  199.     function setProgressHandler($handler)
  200.     {
  201.         if (!is_callable($handler)) {
  202.             return $this->raiseError(HTML_PROGRESS_ERROR_INVALID_CALLBACK, 'warning',
  203.                 array('var' => '$handler',
  204.                       'element' => 'valid Class-Method/Function',
  205.                       'was' => 'element',
  206.                       'paramnum' => 1));
  207.         }
  208.         $this->_progress->setProgressHandler($handler);
  209.     }
  210.  
  211.     /**
  212.      * Calls a user-defined progress handler function.
  213.      *
  214.      * @param      integer   $arg           Current value of the progress bar.
  215.      *
  216.      * @return     void
  217.      * @since      1.1
  218.      * @access     public
  219.      * @deprecated
  220.      */
  221.     function callProgressHandler($arg)
  222.     {
  223.         $this->_progress->process();
  224.     }
  225.  
  226.     /**
  227.      * Returns TRUE if progress was started by user, FALSE otherwise.
  228.      *
  229.      * @return     bool
  230.      * @since      1.1
  231.      * @access     public
  232.      */
  233.     function isStarted()
  234.     {
  235.         $action = $this->_form->getSubmitValues();
  236.         return isset($action['start']);
  237.     }
  238.  
  239.     /**
  240.      * Returns TRUE if progress was canceled by user, FALSE otherwise.
  241.      *
  242.      * @return     bool
  243.      * @since      1.0
  244.      * @access     public
  245.      */
  246.     function isCanceled()
  247.     {
  248.         $action = $this->_form->getSubmitValues();
  249.         return isset($action['cancel']);
  250.     }
  251.  
  252.     /**
  253.      * Display Monitor and catch user action (cancel button).
  254.      *
  255.      * @return     void
  256.      * @since      1.0
  257.      * @access     public
  258.      */
  259.     function run()
  260.     {
  261.         if ($this->isStarted()) {
  262.             $this->_progress->run();
  263.         }
  264.     }
  265.  
  266.     /**
  267.      * Attach a progress bar to this monitor.
  268.      *
  269.      * @param      object    $bar           a html_progress instance
  270.      *
  271.      * @return     void
  272.      * @since      1.1
  273.      * @access     public
  274.      * @throws     HTML_PROGRESS_ERROR_INVALID_INPUT
  275.      * @see        getProgressElement()
  276.      */
  277.     function setProgressElement($bar)
  278.     {
  279.         if (!is_a($bar, 'HTML_Progress')) {
  280.             return $this->_progress->raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  281.                 array('var' => '$bar',
  282.                       'was' => gettype($bar),
  283.                       'expected' => 'HTML_Progress object',
  284.                       'paramnum' => 1));
  285.         }
  286.         $this->_progress = $bar;
  287.  
  288.         $bar =& $this->_form->getElement('progressBar');
  289.         $bar->setText( $this->_progress->toHtml() );
  290.     }
  291.  
  292.     /**
  293.      * Returns a reference to the progress bar object
  294.      * used with the monitor.
  295.      *
  296.      * @return     object
  297.      * @since      1.1
  298.      * @access     public
  299.      * @see        setProgressElement()
  300.      */
  301.     function &getProgressElement()
  302.     {
  303.         return $this->_progress;
  304.     }
  305.  
  306.     /**
  307.      * Returns progress styles (StyleSheet).
  308.      *
  309.      * @return     string
  310.      * @since      1.0
  311.      * @access     public
  312.      */
  313.     function getStyle()
  314.     {
  315.         return $this->_progress->getStyle();
  316.     }
  317.  
  318.     /**
  319.      * Returns progress javascript.
  320.      *
  321.      * @return     string
  322.      * @since      1.0
  323.      * @access     public
  324.      */
  325.     function getScript()
  326.     {
  327.         $js = "
  328. function setStatus(pString)
  329. {
  330.     if (isDom) {
  331.         prog = document.getElementById('status');
  332.     } else if (isIE) {
  333.         prog = document.all['status'];
  334.     } else if (isNS4) {
  335.         prog = document.layers['status'];
  336.     }
  337.     if (prog != null)  {
  338.     prog.innerHTML = pString;
  339.     }
  340. }
  341. ";
  342.         return $this->_progress->getScript() . $js;
  343.     }
  344.  
  345.     /**
  346.      * Returns Monitor forms as a Html string.
  347.      *
  348.      * @return     string
  349.      * @since      1.0
  350.      * @access     public
  351.      */
  352.     function toHtml()
  353.     {
  354.         return $this->_form->toHtml();
  355.     }
  356.  
  357.     /**
  358.      * Accepts a renderer
  359.      *
  360.      * @param      object    $renderer      An HTML_QuickForm_Renderer object
  361.      *
  362.      * @return     void
  363.      * @since      1.1
  364.      * @access     public
  365.      */
  366.     function accept(&$renderer)
  367.     {
  368.         if (!is_a($renderer, 'HTML_QuickForm_Renderer')) {
  369.             return $this->_progress->raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  370.                 array('var' => '$renderer',
  371.                       'was' => gettype($renderer),
  372.                       'expected' => 'HTML_QuickForm_Renderer object',
  373.                       'paramnum' => 1));
  374.         }
  375.         $this->_form->accept($renderer);
  376.     }
  377.  
  378.     /**
  379.      * Display a caption on action in progress.
  380.      *
  381.      * The idea of a simple utility function for replacing variables
  382.      * with values in an message template, come from sprintfErrorMessage
  383.      * function of Error_Raise package by Greg Beaver.
  384.      *
  385.      * This simple str_replace-based function can be used to have an
  386.      * order-independent sprintf, so messages can be passed in
  387.      * with different grammar ordering, or other possibilities without
  388.      * changing the source code.
  389.      *
  390.      * Variables should simply be surrounded by % as in %varname%
  391.      *
  392.      * @param      string    $caption       (optional) message template
  393.      * @param      array     $args          (optional) associative array of
  394.      *                                      template var -> message text
  395.      * @since      1.1
  396.      * @access     public
  397.      */
  398.     function setCaption($caption = ' ', $args = array() )
  399.     {
  400.         if (!is_string($caption)) {
  401.             return $this->_progress->raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  402.                 array('var' => '$caption',
  403.                       'was' => gettype($caption),
  404.                       'expected' => 'string',
  405.                       'paramnum' => 1));
  406.  
  407.         } elseif (!is_array($args)) {
  408.             return $this->_progress->raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  409.                 array('var' => '$args',
  410.                       'was' => gettype($args),
  411.                       'expected' => 'array',
  412.                       'paramnum' => 2));
  413.         }
  414.  
  415.         foreach($args as $name => $value) {
  416.             $caption = str_replace("%$name%", $value, $caption);
  417.         }
  418.         if (function_exists('ob_get_clean')) {
  419.             $status  = ob_get_clean();      // use for PHP 4.3+
  420.         } else {
  421.             $status  = ob_get_contents();   // use for PHP 4.2+
  422.             ob_end_clean();
  423.         }
  424.         $status = '<script type="text/javascript">self.setStatus(\''.$caption.'\'); </script>';
  425.         echo $status;
  426.         ob_start();
  427.     }
  428. }
  429. ?>