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 / DM.php next >
Encoding:
PHP Script  |  2008-07-02  |  16.5 KB  |  496 lines

  1. <?php
  2. /**
  3.  * HTML loading bar with only PHP and JS interface.
  4.  *
  5.  * PHP versions 4 and 5
  6.  *
  7.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  8.  * that is available through the world-wide-web at the following URI:
  9.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  10.  * the PHP License and are unable to obtain it through the web, please
  11.  * send a note to license@php.net so we can mail you a copy immediately.
  12.  *
  13.  * @category   HTML
  14.  * @package    HTML_Progress
  15.  * @subpackage Progress_DM
  16.  * @author     Laurent Laville <pear@laurent-laville.org>
  17.  * @copyright  1997-2005 The PHP Group
  18.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  19.  * @version    CVS: $Id: DM.php,v 1.9 2005/09/14 08:32:30 farell Exp $
  20.  * @link       http://pear.php.net/package/HTML_Progress
  21.  * @since      File available since Release 1.0
  22.  */
  23.  
  24. /**
  25.  * HTML loading bar with only PHP and JS interface.
  26.  *
  27.  * The HTML_Progress_DM class handles any mathematical issues
  28.  * arising from assigning faulty values.
  29.  *
  30.  * @category   HTML
  31.  * @package    HTML_Progress
  32.  * @subpackage Progress_DM
  33.  * @author     Laurent Laville <pear@laurent-laville.org>
  34.  * @copyright  1997-2005 The PHP Group
  35.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  36.  * @version    Release: @package_version@
  37.  * @link       http://pear.php.net/package/HTML_Progress
  38.  * @since      Class available since Release 1.0
  39.  */
  40.  
  41. class HTML_Progress_DM
  42. {
  43.     /**
  44.      * The progress bar's minimum value.
  45.      * The default is 0.
  46.      *
  47.      * @var        integer
  48.      * @since      1.0
  49.      * @access     private
  50.      * @see        getMinimum(), setMinimum()
  51.      */
  52.     var $_minimum;
  53.  
  54.     /**
  55.      * The progress bar's maximum value.
  56.      * The default is 100.
  57.      *
  58.      * @var        integer
  59.      * @since      1.0
  60.      * @access     private
  61.      * @see        getMaximum(), setMaximum()
  62.      */
  63.     var $_maximum;
  64.  
  65.     /**
  66.      * The progress bar's increment value.
  67.      * The default is +1.
  68.      *
  69.      * @var        integer
  70.      * @since      1.0
  71.      * @access     private
  72.      * @see        getIncrement(), setIncrement()
  73.      */
  74.     var $_increment;
  75.  
  76.     /**
  77.      * The progress bar's current value.
  78.      *
  79.      * @var        integer
  80.      * @since      1.0
  81.      * @access     private
  82.      * @see        getValue(), setvalue(), incValue()
  83.      */
  84.     var $_value;
  85.  
  86.  
  87.     /**
  88.      * The data model class constructor
  89.      *
  90.      * Constructor Summary
  91.      *
  92.      * o Creates a progress mathematical model with a minimum value set to 0,
  93.      *   a maximum value set to 100, and a increment value set to +1.
  94.      *   By default, the value is initialized to be equal to the minimum value.
  95.      *   <code>
  96.      *   $html = new HTML_Progress_DM();
  97.      *   </code>
  98.      *
  99.      * o Creates a progress mathematical model with minimum and maximum set to
  100.      *   specified values, and a increment value set to +1.
  101.      *   By default, the value is initialized to be equal to the minimum value.
  102.      *   <code>
  103.      *   $html = new HTML_Progress_DM($min, $max);
  104.      *   </code>
  105.      *
  106.      * o Creates a progress mathematical model with minimum, maximum and increment
  107.      *   set to specified values.
  108.      *   By default, the value is initialized to be equal to the minimum value.
  109.      *   <code>
  110.      *   $html = new HTML_Progress_DM($min, $max, $inc);
  111.      *   </code>
  112.      *
  113.      * @since      1.0
  114.      * @access     public
  115.      * @throws     HTML_PROGRESS_ERROR_INVALID_INPUT
  116.      */
  117.     function HTML_Progress_DM()
  118.     {
  119.         // if you've not yet created an instance of html_progress
  120.         if (!$GLOBALS['_HTML_PROGRESS_CALLBACK_ERRORHANDLER']) {
  121.             // init default error handling system,
  122.             HTML_Progress::_initErrorHandler();
  123.         }
  124.  
  125.         $this->_minimum = 0;
  126.         $this->_maximum = 100;
  127.         $this->_increment = +1;
  128.  
  129.         $args = func_get_args();
  130.  
  131.         switch (count($args)) {
  132.          case 2:
  133.             /*   int min, int max   */
  134.  
  135.             if (!is_int($args[0])) {
  136.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  137.                     array('var' => '$min',
  138.                           'was' => $args[0],
  139.                           'expected' => 'integer',
  140.                           'paramnum' => 1));
  141.  
  142.             } elseif ($args[0] < 0) {
  143.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  144.                     array('var' => '$min',
  145.                           'was' => $args[0],
  146.                           'expected' => 'positive',
  147.                           'paramnum' => 1));
  148.  
  149.             } elseif ($args[0] > $args[1]) {
  150.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  151.                     array('var' => '$min',
  152.                           'was' => $args[0],
  153.                           'expected' => 'less than $max = '.$args[1],
  154.                           'paramnum' => 1));
  155.             }
  156.             $this->_minimum = $args[0];
  157.  
  158.  
  159.             if (!is_int($args[1])) {
  160.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  161.                     array('var' => '$max',
  162.                           'was' => $args[1],
  163.                           'expected' => 'integer',
  164.                           'paramnum' => 2));
  165.  
  166.             } elseif ($args[1] < 0) {
  167.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  168.                     array('var' => '$max',
  169.                           'was' => $args[1],
  170.                           'expected' => 'positive',
  171.                           'paramnum' => 2));
  172.             }
  173.             $this->_maximum = $args[1];
  174.             break;
  175.          case 3:
  176.             /*   int min, int max, int inc   */
  177.  
  178.             if (!is_int($args[0])) {
  179.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  180.                     array('var' => '$min',
  181.                           'was' => $args[0],
  182.                           'expected' => 'integer',
  183.                           'paramnum' => 1));
  184.  
  185.             } elseif ($args[0] < 0) {
  186.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  187.                     array('var' => '$min',
  188.                           'was' => $args[0],
  189.                           'expected' => 'positive',
  190.                           'paramnum' => 1));
  191.  
  192.             } elseif ($args[0] > $args[1]) {
  193.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  194.                     array('var' => '$min',
  195.                           'was' => $args[0],
  196.                           'expected' => 'less than $max = '.$args[1],
  197.                           'paramnum' => 1));
  198.             }
  199.             $this->_minimum = $args[0];
  200.  
  201.             if (!is_int($args[1])) {
  202.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  203.                     array('var' => '$max',
  204.                           'was' => $args[1],
  205.                           'expected' => 'integer',
  206.                           'paramnum' => 2));
  207.  
  208.             } elseif ($args[1] < 0) {
  209.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  210.                     array('var' => '$max',
  211.                           'was' => $args[1],
  212.                           'expected' => 'positive',
  213.                           'paramnum' => 2));
  214.             }
  215.             $this->_maximum = $args[1];
  216.  
  217.             if (!is_int($args[2])) {
  218.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  219.                     array('var' => '$inc',
  220.                           'was' => $args[2],
  221.                           'expected' => 'integer',
  222.                           'paramnum' => 3));
  223.  
  224.             } elseif ($args[2] < 1) {
  225.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  226.                     array('var' => '$inc',
  227.                           'was' => $args[2],
  228.                           'expected' => 'greater than zero',
  229.                           'paramnum' => 3));
  230.             }
  231.             $this->_increment = $args[2];
  232.             break;
  233.          default:
  234.         }
  235.         $this->_value = $this->_minimum;
  236.     }
  237.  
  238.     /**
  239.      * Returns the progress bar's minimum value. The default value is 0.
  240.      *
  241.      * @return     integer
  242.      * @since      1.0
  243.      * @access     public
  244.      * @see        setMinimum()
  245.      */
  246.     function getMinimum()
  247.     {
  248.         return $this->_minimum;
  249.     }
  250.  
  251.     /**
  252.      * Sets the progress bar's minimum value.
  253.      *
  254.      * @param      integer   $min           progress bar's minimal value
  255.      *
  256.      * @return     void
  257.      * @since      1.0
  258.      * @access     public
  259.      * @throws     HTML_PROGRESS_ERROR_INVALID_INPUT
  260.      * @see        getMinimum()
  261.      */
  262.     function setMinimum($min)
  263.     {
  264.         if (!is_int($min)) {
  265.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  266.                 array('var' => '$min',
  267.                       'was' => gettype($min),
  268.                       'expected' => 'integer',
  269.                       'paramnum' => 1));
  270.  
  271.         } elseif ($min < 0) {
  272.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  273.                 array('var' => '$min',
  274.                       'was' => $min,
  275.                       'expected' => 'positive',
  276.                       'paramnum' => 1));
  277.  
  278.         } elseif ($min > $this->getMaximum()) {
  279.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  280.                 array('var' => '$min',
  281.                       'was' => $min,
  282.                       'expected' => 'less than $max = '.$this->getMaximum(),
  283.                       'paramnum' => 1));
  284.         }
  285.         $this->_minimum = $min;
  286.  
  287.         /* set current value to minimum if less than minimum */
  288.         if ($this->getValue() < $min) {
  289.             $this->setValue($min);
  290.         }
  291.     }
  292.  
  293.     /**
  294.      * Returns the progress bar's maximum value. The default value is 100.
  295.      *
  296.      * @return     integer
  297.      * @since      1.0
  298.      * @access     public
  299.      * @see        setMaximum()
  300.      */
  301.     function getMaximum()
  302.     {
  303.         return $this->_maximum;
  304.     }
  305.  
  306.     /**
  307.      * Sets the progress bar's maximum value.
  308.      *
  309.      * @param      integer   $max           progress bar's maximal value
  310.      *
  311.      * @return     void
  312.      * @since      1.0
  313.      * @access     public
  314.      * @throws     HTML_PROGRESS_ERROR_INVALID_INPUT
  315.      * @see        getMaximum()
  316.      */
  317.     function setMaximum($max)
  318.     {
  319.         if (!is_int($max)) {
  320.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  321.                 array('var' => '$max',
  322.                       'was' => gettype($max),
  323.                       'expected' => 'integer',
  324.                       'paramnum' => 1));
  325.  
  326.         } elseif ($max < 0) {
  327.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  328.                 array('var' => '$max',
  329.                       'was' => $max,
  330.                       'expected' => 'positive',
  331.                       'paramnum' => 1));
  332.  
  333.         } elseif ($max < $this->getMinimum()) {
  334.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  335.                 array('var' => '$max',
  336.                       'was' => $max,
  337.                       'expected' => 'greater than $min = '.$this->getMinimum(),
  338.                       'paramnum' => 1));
  339.         }
  340.         $this->_maximum = $max;
  341.  
  342.         /* set current value to maximum if greater to maximum */
  343.         if ($this->getValue() > $max) {
  344.             $this->setValue($max);
  345.         }
  346.     }
  347.  
  348.     /**
  349.      * Returns the progress bar's increment value. The default value is +1.
  350.      *
  351.      * @return     integer
  352.      * @since      1.0
  353.      * @access     public
  354.      * @see        setIncrement()
  355.      */
  356.     function getIncrement()
  357.     {
  358.         return $this->_increment;
  359.     }
  360.  
  361.     /**
  362.      * Sets the progress bar's increment value.
  363.      *
  364.      * @param      integer   $inc           progress bar's increment value
  365.      *
  366.      * @return     void
  367.      * @since      1.0
  368.      * @access     public
  369.      * @throws     HTML_PROGRESS_ERROR_INVALID_INPUT
  370.      * @see        getIncrement()
  371.      */
  372.     function setIncrement($inc)
  373.     {
  374.         if (!is_int($inc)) {
  375.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  376.                 array('var' => '$inc',
  377.                       'was' => gettype($inc),
  378.                       'expected' => 'integer',
  379.                       'paramnum' => 1));
  380.  
  381.         } elseif ($inc == 0) {
  382.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  383.                 array('var' => '$inc',
  384.                       'was' => $inc,
  385.                       'expected' => 'not equal zero',
  386.                       'paramnum' => 1));
  387.         }
  388.         $this->_increment = $inc;
  389.     }
  390.  
  391.     /**
  392.      * Returns the progress bar's current value. The value is always between
  393.      * the minimum and maximum values, inclusive.
  394.      * By default, the value is initialized with the minimum value.
  395.      *
  396.      * @return     integer
  397.      * @since      1.0
  398.      * @access     public
  399.      * @see        setValue()
  400.      */
  401.     function getValue()
  402.     {
  403.         return $this->_value;
  404.     }
  405.  
  406.     /**
  407.      * Sets the progress bar's current value.
  408.      * If the new value is different from previous value, all change listeners
  409.      * are notified.
  410.      *
  411.      * @param      integer   $val           progress bar's current value
  412.      *
  413.      * @return     void
  414.      * @since      1.0
  415.      * @access     public
  416.      * @throws     HTML_PROGRESS_ERROR_INVALID_INPUT
  417.      * @see        getValue()
  418.      */
  419.     function setValue($val)
  420.     {
  421.         if (!is_int($val)) {
  422.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  423.                 array('var' => '$val',
  424.                       'was' => gettype($val),
  425.                       'expected' => 'integer',
  426.                       'paramnum' => 1));
  427.  
  428.         } elseif ($val < $this->getMinimum()) {
  429.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  430.                 array('var' => '$val',
  431.                       'was' => $val,
  432.                       'expected' => 'greater than $min = '.$this->getMinimum(),
  433.                       'paramnum' => 1));
  434.  
  435.         } elseif ($val > $this->getMaximum()) {
  436.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  437.                 array('var' => '$val',
  438.                       'was' => $val,
  439.                       'expected' => 'less than $max = '.$this->getMaximum(),
  440.                       'paramnum' => 1));
  441.         }
  442.         $this->_value = $val;
  443.     }
  444.  
  445.     /**
  446.      * Updates the progress bar's current value by adding increment value.
  447.      *
  448.      * @return     void
  449.      * @since      1.0
  450.      * @access     public
  451.      * @see        getValue(), setValue()
  452.      */
  453.     function incValue()
  454.     {
  455.         $newVal = $this->getValue() + $this->getIncrement();
  456.         $newVal = min($this->getMaximum(), $newVal);
  457.         $this->setValue( $newVal );
  458.     }
  459.  
  460.     /**
  461.      * Returns the percent complete for the progress bar. Note that this number is
  462.      * between 0.00 and 1.00 or 0 and 100.
  463.      *
  464.      * @param      boolean   $float         (optional) float or integer format
  465.      *
  466.      * @return     mixed
  467.      * @since      1.0
  468.      * @access     public
  469.      * @throws     HTML_PROGRESS2_ERROR_INVALID_INPUT
  470.      * @see        getValue(), getMaximum()
  471.      */
  472.     function getPercentComplete($float = true)
  473.     {
  474.         if (!is_bool($float)) {
  475.             return HTML_Progress::raiseError(HTML_PROGRESS2_ERROR_INVALID_INPUT, 'exception',
  476.                 array('var' => '$float',
  477.                       'was' => gettype($float),
  478.                       'expected' => 'boolean',
  479.                       'paramnum' => 1));
  480.         }
  481.  
  482.         $min = $this->_minimum;
  483.         $max = $this->_maximum;
  484.         $val = $this->_value;
  485.  
  486.         $percent = round((($val - $min) / ($max - $min)), 4);
  487.  
  488.         if ($float) {
  489.             return $percent;
  490.         } else {
  491.             return intval($percent * 100);
  492.         }
  493.     }
  494. }
  495.  
  496. ?>