web/lib/Zend/ProgressBar/Adapter/JsPush.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * LICENSE
       
     4  *
       
     5  * This source file is subject to the new BSD license that is bundled
       
     6  * with this package in the file LICENSE.txt.
       
     7  * It is also available through the world-wide-web at this URL:
       
     8  * http://framework.zend.com/license/new-bsd
       
     9  * If you did not receive a copy of the license and are unable to
       
    10  * obtain it through the world-wide-web, please send an email
       
    11  * to license@zend.com so we can send you a copy immediately.
       
    12  *
       
    13  * @category   Zend
       
    14  * @package    Zend_ProgressBar
       
    15  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    16  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    17  * @version    $Id: JsPush.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    18  */
       
    19 
       
    20 /**
       
    21  * @see Zend_Json
       
    22  */
       
    23 require_once 'Zend/Json.php';
       
    24 
       
    25 /**
       
    26  * @see Zend_ProgressBar_Adapter
       
    27  */
       
    28 require_once 'Zend/ProgressBar/Adapter.php';
       
    29 
       
    30 /**
       
    31  * Zend_ProgressBar_Adapter_JsPush offers a simple method for updating a
       
    32  * progressbar in a browser.
       
    33  *
       
    34  * @category  Zend
       
    35  * @package   Zend_ProgressBar
       
    36  * @uses      Zend_ProgressBar_Adapter_Interface
       
    37  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    38  * @license   http://framework.zend.com/license/new-bsd     New BSD License
       
    39  */
       
    40 class Zend_ProgressBar_Adapter_JsPush extends Zend_ProgressBar_Adapter
       
    41 {
       
    42     /**
       
    43      * Name of the JavaScript method to call on update
       
    44      *
       
    45      * @var string
       
    46      */
       
    47     protected $_updateMethodName = 'Zend_ProgressBar_Update';
       
    48 
       
    49     /**
       
    50      * Name of the JavaScript method to call on finish
       
    51      *
       
    52      * @var string
       
    53      */
       
    54     protected $_finishMethodName;
       
    55 
       
    56     /**
       
    57      * Set the update method name
       
    58      *
       
    59      * @param  string $methodName
       
    60      * @return Zend_ProgressBar_Adapter_JsPush
       
    61      */
       
    62     public function setUpdateMethodName($methodName)
       
    63     {
       
    64         $this->_updateMethodName = $methodName;
       
    65 
       
    66         return $this;
       
    67     }
       
    68 
       
    69     /**
       
    70      * Set the finish method name
       
    71      *
       
    72      * @param  string $methodName
       
    73      * @return Zend_ProgressBar_Adapter_JsPush
       
    74      */
       
    75     public function setFinishMethodName($methodName)
       
    76     {
       
    77         $this->_finishMethodName = $methodName;
       
    78 
       
    79         return $this;
       
    80     }
       
    81 
       
    82     /**
       
    83      * Defined by Zend_ProgressBar_Adapter_Interface
       
    84      *
       
    85      * @param  float   $current       Current progress value
       
    86      * @param  float   $max           Max progress value
       
    87      * @param  float   $percent       Current percent value
       
    88      * @param  integer $timeTaken     Taken time in seconds
       
    89      * @param  integer $timeRemaining Remaining time in seconds
       
    90      * @param  string  $text          Status text
       
    91      * @return void
       
    92      */
       
    93     public function notify($current, $max, $percent, $timeTaken, $timeRemaining, $text)
       
    94     {
       
    95         $arguments = array(
       
    96             'current'       => $current,
       
    97             'max'           => $max,
       
    98             'percent'       => ($percent * 100),
       
    99             'timeTaken'     => $timeTaken,
       
   100             'timeRemaining' => $timeRemaining,
       
   101             'text'          => $text
       
   102         );
       
   103 
       
   104         $data = '<script type="text/javascript">'
       
   105               . 'parent.' . $this->_updateMethodName . '(' . Zend_Json::encode($arguments) . ');'
       
   106               . '</script>';
       
   107 
       
   108         // Output the data
       
   109         $this->_outputData($data);
       
   110     }
       
   111 
       
   112     /**
       
   113      * Defined by Zend_ProgressBar_Adapter_Interface
       
   114      *
       
   115      * @return void
       
   116      */
       
   117     public function finish()
       
   118     {
       
   119         if ($this->_finishMethodName === null) {
       
   120             return;
       
   121         }
       
   122 
       
   123         $data = '<script type="text/javascript">'
       
   124               . 'parent.' . $this->_finishMethodName . '();'
       
   125               . '</script>';
       
   126 
       
   127         $this->_outputData($data);
       
   128     }
       
   129 
       
   130     /**
       
   131      * Outputs given data the user agent.
       
   132      *
       
   133      * This split-off is required for unit-testing.
       
   134      *
       
   135      * @param  string $data
       
   136      * @return void
       
   137      */
       
   138     protected function _outputData($data)
       
   139     {
       
   140         // 1024 padding is required for Safari, while 256 padding is required
       
   141         // for Internet Explorer. The <br /> is required so Safari actually
       
   142         // executes the <script />
       
   143         echo str_pad($data . '<br />', 1024, ' ', STR_PAD_RIGHT) . "\n";
       
   144 
       
   145         flush();
       
   146         ob_flush();
       
   147     }
       
   148 }