123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace Ppb\View\Helper;
- use Cube\View\Helper\AbstractHelper,
- Cube\Controller\Front;
- class Countdown extends AbstractHelper
- {
-
- const DAY = 86400;
- const HOUR = 3600;
- const MIN = 60;
- const SEC = 1;
-
- protected $_intervals = array(
- self::DAY => array(self::DAY, 'days', 'day', ' ', ', '),
- self::HOUR => array(self::HOUR, 'hrs', 'hr', '', ''),
- self::MIN => array(0, 'mins', 'min', '', ''),
- self::SEC => array(0, 'secs', 'sec', '', ''),
- );
-
- protected $_baseUrl;
-
- protected $_date;
-
- public function __construct()
- {
- $this->_baseUrl = Front::getInstance()->getRequest()->getBaseUrl();
- }
-
- public function countdown($date)
- {
- if (!is_numeric($date)) {
- $date = strtotime($date);
- }
- $this->_date = $date;
- return $this;
- }
-
- public function timeLeft($secs = self::MIN)
- {
- $translate = $this->getTranslate();
- if (!$this->_date) {
- return $translate->_('n/a');
- }
- $output = null;
- $original = $countdown = $this->_date - time();
- if ($countdown > 0) {
- foreach ($this->_intervals as $seconds => $data) {
- $left = floor($countdown / $seconds);
- $countdown -= ($left * $seconds);
- list($min, $plural, $singular, $separator, $suffix) = $data;
- if ($original >= $min && $seconds >= $secs) {
- $output .= ' ' . $left . $separator . (($left > 1) ? $translate->_($plural) : $translate->_($singular)) . $suffix;
- }
- }
- }
- else {
- $output = '<span class="closed">' . $translate->_('Closed') . '</span>';
- }
- return $output;
- }
-
- public function timeElapsed($secs = self::MIN)
- {
- $translate = $this->getTranslate();
- $original = $elapsed = time() - $this->_date;
- if ($original <= 0 || $this->_date <= 0) {
- return $translate->_('n/a');
- }
- $output = null;
- foreach ($this->_intervals as $seconds => $data) {
- $left = floor($elapsed / $seconds);
- $elapsed -= ($left * $seconds);
- list($min, $plural, $singular, $separator, $suffix) = $data;
- if ($original >= $min && $seconds >= $secs) {
- $output .= $left . $separator . (($left > 1) ? $translate->_($plural) : $translate->_($singular)) . $suffix;
- }
- }
- return $output;
- }
- public function display()
- {
- $translate = $this->getTranslate();
- if (!$this->_date) {
- return $translate->_('n/a');
- }
- $this->_generateJavascript();
- $countdown = $this->_date . '000';
- return '<span data-countdown="' . $countdown . '"></span>';
- }
- protected function _generateJavascript()
- {
- $translate = $this->getTranslate();
-
- $scriptHelper = $this->getView()->getHelper('script');
- $scriptHelper->addBodyCode('<script type="text/javascript" src="' . $this->_baseUrl . '/js/jquery.countdown.min.js"></script>')
- ->addBodyCode("<script type=\"text/javascript\">" . "\n"
- . " $(document).ready(function() { " . "\n"
- . " $('[data-countdown]').each(function () {
- var element = $(this), finalDate = element.data('countdown');
- element.countdown(finalDate, function (event) {
- var format = '%M" . $translate->_('mins') . " %S" . $translate->_('secs') . "';
- if (event.offset.totalDays > 0 || event.offset.hours > 0) {
- format = '%-H" . $translate->_('hrs') . " ' + format;
- }
- if (event.offset.totalDays > 0) {
- format = '%-D %!D:" . $translate->_('day') . "," . $translate->_('days') . ";, ' + format;
- }
- element.html(event.strftime(format));
- }).on('finish.countdown', function(event) {
- element.html('<span class=\"closed\">" . $translate->_('Closed') . "</span>');
- });
- });
- }); " . "\n"
- . "</script>");
- }
- }
|