diff --git a/VERSIONS_WIP/44522 b/VERSIONS_WIP/44522 index 7c01312abfd5d941d4547eb9ed006aa8eb52e22a..8a3cedf3c6f91a9d4103275c42bf4bffb76fe7d6 100644 --- a/VERSIONS_WIP/44522 +++ b/VERSIONS_WIP/44522 @@ -1 +1 @@ - - ticket #44522 : (bonus) Evolution : Ouverture aujourd'hui, et maintenant \ No newline at end of file + - ticket #44522 : Boîte bibliothèque: ajout de la facette "Ouvert maintenant" \ No newline at end of file diff --git a/library/Class/Bib.php b/library/Class/Bib.php index be77082bb7889d0f549e51991f378b72bd4d8236..7f542146aae550ea884c5290dcee7be345da1f6a 100644 --- a/library/Class/Bib.php +++ b/library/Class/Bib.php @@ -941,20 +941,9 @@ class Class_Bib extends Storm_Model_Abstract { if (!$ouverture = $this->getOuvertureOnDate($this->getCurrentTime())) return false; - $hour = date('H:i', $this->getCurrentTime()); - - $next_open_hour = ($hour >= $ouverture->getFinMatin()) - ? $ouverture->getDebutApresMidi() - : $ouverture->getDebutMatin(); - - if ($next_open_hour > $hour) - return false; - - $next_close_hour = ($ouverture->isJourneeContinue() || $hour >= $ouverture->getDebutApresMidi()) - ? max($ouverture->getFinMatin(), $ouverture->getFinApresMidi()) - : $ouverture->getFinMatin(); - - return ($next_close_hour > $hour); + return Class_Ouverture_State::from($ouverture) + ->onIsOpenedUntil(true) + ->evaluateAt($this->getCurrentTime()); } diff --git a/library/Class/Ouverture/State.php b/library/Class/Ouverture/State.php new file mode 100644 index 0000000000000000000000000000000000000000..20bb674c391923a2f66beaa0ff916dc801c211b1 --- /dev/null +++ b/library/Class/Ouverture/State.php @@ -0,0 +1,107 @@ +<?php +/** + * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with BOKEH; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +class Class_Ouverture_State { + protected + $_ouverture, + $_on_will_open_at, + $_on_is_opened_until, + $_on_is_closed_since; + + public static function from($ouverture) { + return new static($ouverture); + } + + + public function __construct($ouverture) { + $this->_ouverture = $ouverture; + $this->_on_will_open_at = + $this->_on_is_opened_until = + $this->_on_is_closed_since = [$this, '_returnFalse']; + } + + + public function onWillOpenAt($closure_or_value) { + $this->_on_will_open_at = $this->_closureOrValue($closure_or_value); + return $this; + } + + + public function onIsOpenedUntil($closure_or_value) { + $this->_on_is_opened_until = $this->_closureOrValue($closure_or_value); + return $this; + } + + + public function onIsClosedSince($closure_or_value) { + $this->_on_is_closed_since = $this->_closureOrValue($closure_or_value); + return $this; + } + + + protected function _returnFalse() { return false; } + + + protected function _closureOrValue($closure_or_value) { + return is_callable($closure_or_value) + ? $closure_or_value + : function () use($closure_or_value) { return $closure_or_value; }; + } + + + protected function _doOnWillOpenAt($open_hour) { + return call_user_func($this->_on_will_open_at, $open_hour); + } + + + protected function _doOnIsOpenedUntil($close_hour) { + return call_user_func($this->_on_is_opened_until, $close_hour); + } + + + protected function _doOnIsClosedSince($close_hour) { + return call_user_func($this->_on_is_closed_since, $close_hour); + } + + + public function evaluateAt($time) { + $hour = date('H:i', $time); + + $next_open_hour = ($hour >= $this->_ouverture->getFinMatin()) + ? $this->_ouverture->getDebutApresMidi() + : $this->_ouverture->getDebutMatin(); + + if ($next_open_hour > $hour) + return $this->_doOnWillOpenAt($next_open_hour); + + $next_close_hour = ($this->_ouverture->isJourneeContinue() || $hour >= $this->_ouverture->getDebutApresMidi()) + ? max($this->_ouverture->getFinMatin(), $this->_ouverture->getFinApresMidi()) + : $this->_ouverture->getFinMatin(); + + + return ($next_close_hour > $hour) + ? $this->_doOnIsOpenedUntil($next_close_hour) + : $this->_doOnIsClosedSince($next_close_hour); + } + +} +?> \ No newline at end of file diff --git a/library/ZendAfi/View/Helper/RenderLibraryOpening.php b/library/ZendAfi/View/Helper/RenderLibraryOpening.php index 11da998af127296cb0b823378aa1c8fcd0e1f264..299bdecda6a1d6ad5c40ff58dea964ae30aefae7 100644 --- a/library/ZendAfi/View/Helper/RenderLibraryOpening.php +++ b/library/ZendAfi/View/Helper/RenderLibraryOpening.php @@ -37,24 +37,30 @@ class ZendAfi_View_Helper_RenderLibraryOpening extends ZendAfi_View_Helper_BaseH return $this->renderClosed($this->_('Fermé.') . $this->renderNextOuvertureForLibrary($library)); - $hour = date('H:i', $this->getCurrentTime()); - $next_open_hour = ($hour >= $ouverture->getFinMatin()) - ? $ouverture->getDebutApresMidi() - : $ouverture->getDebutMatin(); + return Class_Ouverture_State::from($ouverture) + ->onWillOpenAt([$this, 'renderShouldOpenAt']) + ->onIsOpenedUntil([$this, 'renderIsOpenedUntil']) + ->onIsClosedSince(function($close_hour) use ($library) + { + return $this->renderIsClosedSince($close_hour, $library); + }) + ->evaluateAt($this->getCurrentTime()); + } + - if ($next_open_hour > $hour) - return $this->renderClosed($this->_('Ouvrira à %s', $next_open_hour)); + public function renderShouldOpenAt($open_hour) { + return $this->renderClosed($this->_('Ouvrira à %s', $open_hour)); + } - $next_close_hour = ($ouverture->isJourneeContinue() || $hour >= $ouverture->getDebutApresMidi()) - ? max($ouverture->getFinMatin(), $ouverture->getFinApresMidi()) - : $ouverture->getFinMatin(); + public function renderIsOpenedUntil($close_hour) { + return $this->renderOpened($this->_('Ouvert jusqu\'à %s', $close_hour)); + } - if ($next_close_hour > $hour) - return $this->renderOpened($this->_('Ouvert jusqu\'à %s', $next_close_hour)); - return $this->renderClosed($this->_('Fermé depuis %s.', $next_close_hour) + public function renderIsClosedSince($close_hour, $library) { + return $this->renderClosed($this->_('Fermé depuis %s.', $close_hour) . $this->renderNextOuvertureForLibrary($library)); }