JsPush.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  16. * @license http://framework.zend.com/license/new-bsd New BSD License
  17. * @version $Id$
  18. */
  19. /**
  20. * @see Zend_Json
  21. */
  22. #require_once 'Zend/Json.php';
  23. /**
  24. * @see Zend_ProgressBar_Adapter
  25. */
  26. #require_once 'Zend/ProgressBar/Adapter.php';
  27. /**
  28. * Zend_ProgressBar_Adapter_JsPush offers a simple method for updating a
  29. * progressbar in a browser.
  30. *
  31. * @category Zend
  32. * @package Zend_ProgressBar
  33. * @uses Zend_ProgressBar_Adapter_Interface
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_ProgressBar_Adapter_JsPush extends Zend_ProgressBar_Adapter
  38. {
  39. /**
  40. * Name of the JavaScript method to call on update
  41. *
  42. * @var string
  43. */
  44. protected $_updateMethodName = 'Zend_ProgressBar_Update';
  45. /**
  46. * Name of the JavaScript method to call on finish
  47. *
  48. * @var string
  49. */
  50. protected $_finishMethodName;
  51. /**
  52. * Set the update method name
  53. *
  54. * @param string $methodName
  55. * @return Zend_ProgressBar_Adapter_JsPush
  56. */
  57. public function setUpdateMethodName($methodName)
  58. {
  59. $this->_updateMethodName = $methodName;
  60. return $this;
  61. }
  62. /**
  63. * Set the finish method name
  64. *
  65. * @param string $methodName
  66. * @return Zend_ProgressBar_Adapter_JsPush
  67. */
  68. public function setFinishMethodName($methodName)
  69. {
  70. $this->_finishMethodName = $methodName;
  71. return $this;
  72. }
  73. /**
  74. * Defined by Zend_ProgressBar_Adapter_Interface
  75. *
  76. * @param float $current Current progress value
  77. * @param float $max Max progress value
  78. * @param float $percent Current percent value
  79. * @param integer $timeTaken Taken time in seconds
  80. * @param integer $timeRemaining Remaining time in seconds
  81. * @param string $text Status text
  82. * @return void
  83. */
  84. public function notify($current, $max, $percent, $timeTaken, $timeRemaining, $text)
  85. {
  86. $arguments = array(
  87. 'current' => $current,
  88. 'max' => $max,
  89. 'percent' => ($percent * 100),
  90. 'timeTaken' => $timeTaken,
  91. 'timeRemaining' => $timeRemaining,
  92. 'text' => $text
  93. );
  94. $data = '<script type="text/javascript">'
  95. . 'parent.' . $this->_updateMethodName . '(' . Zend_Json::encode($arguments) . ');'
  96. . '</script>';
  97. // Output the data
  98. $this->_outputData($data);
  99. }
  100. /**
  101. * Defined by Zend_ProgressBar_Adapter_Interface
  102. *
  103. * @return void
  104. */
  105. public function finish()
  106. {
  107. if ($this->_finishMethodName === null) {
  108. return;
  109. }
  110. $data = '<script type="text/javascript">'
  111. . 'parent.' . $this->_finishMethodName . '();'
  112. . '</script>';
  113. $this->_outputData($data);
  114. }
  115. /**
  116. * Outputs given data the user agent.
  117. *
  118. * This split-off is required for unit-testing.
  119. *
  120. * @param string $data
  121. * @return void
  122. */
  123. protected function _outputData($data)
  124. {
  125. // 1024 padding is required for Safari, while 256 padding is required
  126. // for Internet Explorer. The <br /> is required so Safari actually
  127. // executes the <script />
  128. echo str_pad($data . '<br />', 1024, ' ', STR_PAD_RIGHT) . "\n";
  129. flush();
  130. ob_flush();
  131. }
  132. }