File indexing completed on 2024-12-22 05:36:57

0001 <?php
0002 /**
0003  * LICENSE
0004  *
0005  * This source file is subject to the new BSD license that is bundled
0006  * with this package in the file LICENSE.txt.
0007  * It is also available through the world-wide-web at this URL:
0008  * http://framework.zend.com/license/new-bsd
0009  * If you did not receive a copy of the license and are unable to
0010  * obtain it through the world-wide-web, please send an email
0011  * to license@zend.com so we can send you a copy immediately.
0012  *
0013  * @category   Zend
0014  * @package    Zend_ProgressBar
0015  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0016  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0017  * @version    $Id$
0018  */
0019 
0020 /**
0021  * @see Zend_Json
0022  */
0023 // require_once 'Zend/Json.php';
0024 
0025 /**
0026  * @see Zend_ProgressBar_Adapter
0027  */
0028 // require_once 'Zend/ProgressBar/Adapter.php';
0029 
0030 /**
0031  * Zend_ProgressBar_Adapter_JsPush offers a simple method for updating a
0032  * progressbar in a browser.
0033  *
0034  * @category  Zend
0035  * @package   Zend_ProgressBar
0036  * @uses      Zend_ProgressBar_Adapter_Interface
0037  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0038  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0039  */
0040 class Zend_ProgressBar_Adapter_JsPush extends Zend_ProgressBar_Adapter
0041 {
0042     /**
0043      * Name of the JavaScript method to call on update
0044      *
0045      * @var string
0046      */
0047     protected $_updateMethodName = 'Zend_ProgressBar_Update';
0048 
0049     /**
0050      * Name of the JavaScript method to call on finish
0051      *
0052      * @var string
0053      */
0054     protected $_finishMethodName;
0055 
0056     /**
0057      * Set the update method name
0058      *
0059      * @param  string $methodName
0060      * @return Zend_ProgressBar_Adapter_JsPush
0061      */
0062     public function setUpdateMethodName($methodName)
0063     {
0064         $this->_updateMethodName = $methodName;
0065 
0066         return $this;
0067     }
0068 
0069     /**
0070      * Set the finish method name
0071      *
0072      * @param  string $methodName
0073      * @return Zend_ProgressBar_Adapter_JsPush
0074      */
0075     public function setFinishMethodName($methodName)
0076     {
0077         $this->_finishMethodName = $methodName;
0078 
0079         return $this;
0080     }
0081 
0082     /**
0083      * Defined by Zend_ProgressBar_Adapter_Interface
0084      *
0085      * @param  float   $current       Current progress value
0086      * @param  float   $max           Max progress value
0087      * @param  float   $percent       Current percent value
0088      * @param  integer $timeTaken     Taken time in seconds
0089      * @param  integer $timeRemaining Remaining time in seconds
0090      * @param  string  $text          Status text
0091      * @return void
0092      */
0093     public function notify($current, $max, $percent, $timeTaken, $timeRemaining, $text)
0094     {
0095         $arguments = array(
0096             'current'       => $current,
0097             'max'           => $max,
0098             'percent'       => ($percent * 100),
0099             'timeTaken'     => $timeTaken,
0100             'timeRemaining' => $timeRemaining,
0101             'text'          => $text
0102         );
0103 
0104         $data = '<script type="text/javascript">'
0105               . 'parent.' . $this->_updateMethodName . '(' . Zend_Json::encode($arguments) . ');'
0106               . '</script>';
0107 
0108         // Output the data
0109         $this->_outputData($data);
0110     }
0111 
0112     /**
0113      * Defined by Zend_ProgressBar_Adapter_Interface
0114      *
0115      * @return void
0116      */
0117     public function finish()
0118     {
0119         if ($this->_finishMethodName === null) {
0120             return;
0121         }
0122 
0123         $data = '<script type="text/javascript">'
0124               . 'parent.' . $this->_finishMethodName . '();'
0125               . '</script>';
0126 
0127         $this->_outputData($data);
0128     }
0129 
0130     /**
0131      * Outputs given data the user agent.
0132      *
0133      * This split-off is required for unit-testing.
0134      *
0135      * @param  string $data
0136      * @return void
0137      */
0138     protected function _outputData($data)
0139     {
0140         // 1024 padding is required for Safari, while 256 padding is required
0141         // for Internet Explorer. The <br /> is required so Safari actually
0142         // executes the <script />
0143         echo str_pad($data . '<br />', 1024, ' ', STR_PAD_RIGHT) . "\n";
0144 
0145         flush();
0146         ob_flush();
0147     }
0148 }