From a0f3e5485c05610760c488b24abf4e4b6151f96d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stuardo=20Rodr=C3=ADguez?= Date: Wed, 13 Feb 2013 12:53:56 -0500 Subject: [PATCH] partial for #377 * adding TimeMachine to set time --- .../crunchbutton/api/TimeMachine/index.php | 46 ++++++++++++ include/library/Crunchbutton/Restaurant.php | 17 +++-- include/library/TimeMachine.php | 72 +++++++++++++++++++ .../default/crunchbutton/bundle/js.phtml | 3 +- .../crunchbutton/layout/html.bottom.phtml | 38 +++++----- www/assets/js/TimeMachine.js | 71 ++++++++++++++++++ 6 files changed, 225 insertions(+), 22 deletions(-) create mode 100644 include/controllers/default/crunchbutton/api/TimeMachine/index.php create mode 100644 include/library/TimeMachine.php create mode 100644 www/assets/js/TimeMachine.js diff --git a/include/controllers/default/crunchbutton/api/TimeMachine/index.php b/include/controllers/default/crunchbutton/api/TimeMachine/index.php new file mode 100644 index 000000000..e7ae76b2b --- /dev/null +++ b/include/controllers/default/crunchbutton/api/TimeMachine/index.php @@ -0,0 +1,46 @@ + '$_GET[\'time\'] was not set']; + + $DeLorean = new TimeMachine($timezone); + $DeLorean->travel($_GET['time']); + $DeLorean->toBeContinued(); + + $return = ['time' => $DeLorean->now()]; + + return $return; + } + + public function reset() + { + $return = null; + $timezone = (isset($_GET['timezone'])) ? $_GET['timezone'] : 'America/New_York'; + + $DeLorean = new TimeMachine($timezone); + $DeLorean->backToThePresent(); + $DeLorean->toBeContinued(); + + $return = ['time' => $DeLorean->now()]; + + return $return; + } + + /** + * Calls the public methodds as actions + */ + public function init() { + $action = c::getPagePiece(2); + if (is_callable(array($this, $action)) && ($action[0] != '_')) { + $return = $this->$action(); + } else { + $return = ['error' => 'invalid request']; + } + echo json_encode($return); + } +} diff --git a/include/library/Crunchbutton/Restaurant.php b/include/library/Crunchbutton/Restaurant.php index bdbb75faf..bc06c89f6 100644 --- a/include/library/Crunchbutton/Restaurant.php +++ b/include/library/Crunchbutton/Restaurant.php @@ -400,22 +400,31 @@ class Crunchbutton_Restaurant extends Cana_Table return $this->_hours[$gmt]; } + /** + * Confirms a restaurant is open + * + * Uses TimeMachine to test if the restaurant is open forcing time travel + * + * @link /api/TimeMachine/set?time=12:30am + * @link /api/TimeMachine/reset + */ public function open() { if (c::env() != 'live' && ($this->id_restaurant == 1 || $this->id_restaurant == 18)) { - return true; + // return true; } $hours = $this->hours(); - $today = new DateTime('now', new DateTimeZone($this->timezone)); - $day = strtolower($today->format('D')); + $DeLorean = new TimeMachine($this->timezone); + $today = $DeLorean->now(); + $day = strtolower($today->format('D')); foreach ($hours as $hour) { if ($hour->day != $day) { continue; } - $open = new DateTime('today '.$hour->time_open, new DateTimeZone($this->timezone)); + $open = new DateTime('today '.$hour->time_open, new DateTimeZone($this->timezone)); $close = new DateTime('today '.$hour->time_close, new DateTimeZone($this->timezone)); // if closeTime before openTime, then closeTime should be for tomorrow diff --git a/include/library/TimeMachine.php b/include/library/TimeMachine.php new file mode 100644 index 000000000..526c90a94 --- /dev/null +++ b/include/library/TimeMachine.php @@ -0,0 +1,72 @@ + + * $DeLorean = new TimeMachine(); + * var_log($DeLorean.now()); + * $DeLorean->travel('November 5, 1955'); + * var_log(DeLorean->now()); + * + * + * It's not called Tardis as it doesn't travel in space. + */ +class TimeMachine +{ + private $_fixedTime = null; + private $timezone = null; + + /** + * If there is any time traveled stored, load it. + * + * @param string $timezone The timezone to use. + * + * @return void + * + * @link http://www.php.net/manual/en/timezones.php + */ + public function __construct($timezone) + { + if ($_SESSION['TimeMachine']) { + $this->_fixedTime = $_SESSION['TimeMachine']; + } + $this->_timezone = new DateTimeZone($timezone); + } + + /** + * Returns the date of when the time machine has landed + * + * @return Date + */ + public function now() + { + return ($this->_fixedTime) ? $this->_fixedTime : new DateTime(); + } + + /** + * Returns the time machine to the present + * + * @return void + */ + function backToThePresent() + { + $this->_fixedTime = null; + } + + function toBeContinued() + { + $_SESSION['TimeMachine'] = $this->_fixedTime; + } + + /** + * Sets the time machine to the specific time + * + * @param string $newTime When to travel to + * + * @return void + */ + function travel($newTime) + { + $this->_fixedTime = new DateTime($newTime, $this->_timezone); + } +} \ No newline at end of file diff --git a/include/views/default/crunchbutton/bundle/js.phtml b/include/views/default/crunchbutton/bundle/js.phtml index 61a57b1de..ccede0569 100644 --- a/include/views/default/crunchbutton/bundle/js.phtml +++ b/include/views/default/crunchbutton/bundle/js.phtml @@ -4,6 +4,7 @@ + \ No newline at end of file + diff --git a/include/views/default/crunchbutton/layout/html.bottom.phtml b/include/views/default/crunchbutton/layout/html.bottom.phtml index 1c54c3dc2..e1b8a48c6 100644 --- a/include/views/default/crunchbutton/layout/html.bottom.phtml +++ b/include/views/default/crunchbutton/layout/html.bottom.phtml @@ -1,3 +1,6 @@ +restaurant)) && (!$this->restaurant->open()) ) :?> + + community->id_community) { @@ -12,6 +15,7 @@ if (!c::getPagePiece(0) || (c::getPagePiece(0) && !in_array(c::getPagePiece(0), $basicPages) && !$this->community)) { $e = ' short-meal-list'; } + ?>
@@ -67,30 +71,30 @@ window.fbAsyncInit = function() { xfbml: true, oauth: true }); - FB.getLoginStatus( App.signin.facebook.processStatus ); + FB.getLoginStatus( App.signin.facebook.processStatus ); FB.Event.subscribe('auth.statusChange', App.signin.facebook.processStatus ); }; }); (function(d, s, id) { - var js, fjs = d.getElementsByTagName(s)[0]; - if (d.getElementById(id)) return; - js = d.createElement(s); js.id = id; - js.src = "//connect.facebook.net/en_US/all.js"; - fjs.parentNode.insertBefore(js, fjs); +var js, fjs = d.getElementsByTagName(s)[0]; +if (d.getElementById(id)) return; +js = d.createElement(s); js.id = id; +js.src = "//connect.facebook.net/en_US/all.js"; +fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk')); - \ No newline at end of file + diff --git a/www/assets/js/TimeMachine.js b/www/assets/js/TimeMachine.js new file mode 100644 index 000000000..dea894ac0 --- /dev/null +++ b/www/assets/js/TimeMachine.js @@ -0,0 +1,71 @@ +/** + * Time traveling machine + * + * + * var DeLorean = new TimeMachine(); + * console.log(DeLorean.now()); + * DeLorean.travel('November 5, 1955'); + * console.log(DeLorean.now()); + * + * + * It's not called Tardis as it doesn't travel in space. + * + * @returns {TimeMachine} + */ +function TimeMachine() { + 'use strict'; + var _fixedTime = null; + + /** + * If there is any time traveled stored, load it. + * + * @return void + */ + this.__construct = function() + { + if ($.cookie('TimeMachine')) { + _fixedTime = $.cookie('TimeMachine'); + } + } + + /** + * Returns the date of when the time machine has landed + * + * @return Date + */ + this.now = function() + { + 'use strict'; + return (_fixedTime) ? _fixedTime : Date.now(); + } + + /** + * Returns the time machine to the present + * + * @return void + */ + this.returnToPresent = function() + { + 'use strict'; + _fixedTime = false; + } + + this.toBeContinued = function() + { + $.cookie('TimeMachine', _fixedTime); + } + + /** + * Sets the time machine to the specific time + * + * @return void + */ + this.travel = function(newTime) + { + 'use strict'; + _fixedTime = Date.parse(newTime); + } + + this.__construct(); +} +var DeLorean = new TimeMachine(); \ No newline at end of file