Countdown.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ KI64uEYmMEgvYPCMy0OQC4r5UQp1ZdYbOXSi8D0FgHI=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.6
  11. */
  12. /**
  13. * live countdown / timer view helper class
  14. */
  15. namespace Ppb\View\Helper;
  16. use Cube\View\Helper\AbstractHelper,
  17. Cube\Controller\Front;
  18. class Countdown extends AbstractHelper
  19. {
  20. /**
  21. * constants in seconds
  22. */
  23. const DAY = 86400;
  24. const HOUR = 3600;
  25. const MIN = 60;
  26. const SEC = 1;
  27. /**
  28. *
  29. * intervals array
  30. * values mean:
  31. * [0] minimum value in seconds needed for the result to display (eg. from 1 day)
  32. * [1] plural string
  33. * [2] singular string
  34. * [3] separator
  35. * [4] suffix
  36. *
  37. * @var array
  38. */
  39. protected $_intervals = array(
  40. self::DAY => array(self::DAY, 'days', 'day', ' ', ', '),
  41. self::HOUR => array(self::HOUR, 'hrs', 'hr', '', ''),
  42. self::MIN => array(0, 'mins', 'min', '', ''),
  43. self::SEC => array(0, 'secs', 'sec', '', ''),
  44. );
  45. /**
  46. *
  47. * base url of the application
  48. *
  49. * @var string
  50. */
  51. protected $_baseUrl;
  52. /**
  53. *
  54. * date timestamp
  55. *
  56. * @var int
  57. */
  58. protected $_date;
  59. /**
  60. *
  61. * class constructor
  62. */
  63. public function __construct()
  64. {
  65. $this->_baseUrl = Front::getInstance()->getRequest()->getBaseUrl();
  66. }
  67. /**
  68. *
  69. * main method, will only store the date in the helper
  70. *
  71. * @param string|int $date
  72. *
  73. * @return string
  74. */
  75. public function countdown($date)
  76. {
  77. if (!is_numeric($date)) {
  78. $date = strtotime($date);
  79. }
  80. $this->_date = $date;
  81. return $this;
  82. }
  83. /**
  84. *
  85. * display the time left w/o javascript component
  86. *
  87. * @param int $secs minimum number of seconds to display
  88. *
  89. * @return string
  90. */
  91. public function timeLeft($secs = self::MIN)
  92. {
  93. $translate = $this->getTranslate();
  94. if (!$this->_date) {
  95. return $translate->_('n/a');
  96. }
  97. $output = null;
  98. $original = $countdown = $this->_date - time();
  99. if ($countdown > 0) {
  100. foreach ($this->_intervals as $seconds => $data) {
  101. $left = floor($countdown / $seconds);
  102. $countdown -= ($left * $seconds);
  103. list($min, $plural, $singular, $separator, $suffix) = $data;
  104. if ($original >= $min && $seconds >= $secs) {
  105. $output .= ' ' . $left . $separator . (($left > 1) ? $translate->_($plural) : $translate->_($singular)) . $suffix;
  106. }
  107. }
  108. }
  109. else {
  110. $output = '<span class="closed">' . $translate->_('Closed') . '</span>';
  111. }
  112. return $output;
  113. }
  114. /**
  115. *
  116. * display the time elapsed w/o javascript component
  117. *
  118. * @param int $secs minimum time interval to display (by key)
  119. *
  120. * @return string
  121. */
  122. public function timeElapsed($secs = self::MIN)
  123. {
  124. $translate = $this->getTranslate();
  125. $original = $elapsed = time() - $this->_date;
  126. if ($original <= 0 || $this->_date <= 0) {
  127. return $translate->_('n/a');
  128. }
  129. $output = null;
  130. foreach ($this->_intervals as $seconds => $data) {
  131. $left = floor($elapsed / $seconds);
  132. $elapsed -= ($left * $seconds);
  133. list($min, $plural, $singular, $separator, $suffix) = $data;
  134. if ($original >= $min && $seconds >= $secs) {
  135. $output .= $left . $separator . (($left > 1) ? $translate->_($plural) : $translate->_($singular)) . $suffix;
  136. }
  137. }
  138. return $output;
  139. }
  140. public function display()
  141. {
  142. $translate = $this->getTranslate();
  143. if (!$this->_date) {
  144. return $translate->_('n/a');
  145. }
  146. $this->_generateJavascript();
  147. $countdown = $this->_date . '000'; // workaround for 32 bit systems
  148. return '<span data-countdown="' . $countdown . '"></span>';
  149. }
  150. protected function _generateJavascript()
  151. {
  152. $translate = $this->getTranslate();
  153. /** @var \Cube\View\Helper\Script $scriptHelper */
  154. $scriptHelper = $this->getView()->getHelper('script');
  155. $scriptHelper->addBodyCode('<script type="text/javascript" src="' . $this->_baseUrl . '/js/jquery.countdown.min.js"></script>')
  156. ->addBodyCode("<script type=\"text/javascript\">" . "\n"
  157. . " $(document).ready(function() { " . "\n"
  158. . " $('[data-countdown]').each(function () {
  159. var element = $(this), finalDate = element.data('countdown');
  160. element.countdown(finalDate, function (event) {
  161. var format = '%M" . $translate->_('mins') . " %S" . $translate->_('secs') . "';
  162. if (event.offset.totalDays > 0 || event.offset.hours > 0) {
  163. format = '%-H" . $translate->_('hrs') . " ' + format;
  164. }
  165. if (event.offset.totalDays > 0) {
  166. format = '%-D %!D:" . $translate->_('day') . "," . $translate->_('days') . ";, ' + format;
  167. }
  168. element.html(event.strftime(format));
  169. }).on('finish.countdown', function(event) {
  170. element.html('<span class=\"closed\">" . $translate->_('Closed') . "</span>');
  171. });
  172. });
  173. }); " . "\n"
  174. . "</script>");
  175. }
  176. }