home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / HTML / Progress / DM.php next >
Encoding:
PHP Script  |  2004-03-24  |  16.9 KB  |  480 lines

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