Skip to content

Commit

Permalink
Horaires d'ouverture : afficher un badge ouvert/fermé en fonction des…
Browse files Browse the repository at this point in the history
… données (#886)

* New OpeningHourService and OpeningHourKindService

* New open_closed_badge template

* Replace badge with header wrapper

* Show header only if enabled kind

* Add parameter to hide header: display_opening_hour_open_closed_header
  • Loading branch information
raphodn committed Sep 13, 2023
1 parent ee8f82f commit 3394172
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 10 deletions.
11 changes: 8 additions & 3 deletions app/Resources/views/_partial/header.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
</nav>
</div>
{% endif %}
{% include "admin/openinghour/_partial/open_closed_surheader.html.twig" %}
<nav class="main-navigation" role="navigation" style="background-color: {{ main_color }}">
<div class="nav-wrapper container">
<a id="logo-container" href="{{ path('homepage') }}" class="brand-logo hide-on-med-and-down">{{ site_name }}</a>
<a id="logo-container" href="{{ path('homepage') }}" class="brand-logo hide-on-large-only">{{ project_name }}</a>
<a id="logo-container" href="{{ path('homepage') }}" class="brand-logo hide-on-med-and-down">
{{ site_name }}
</a>
<a id="logo-container" href="{{ path('homepage') }}" class="brand-logo hide-on-large-only">
{{ project_name }}
</a>
<ul id="nav-desktop" class="right hide-on-med-and-down">
{% if is_granted("ROLE_ADMIN_PANEL") %}
<li class="highlight">
Expand All @@ -44,7 +49,7 @@
{% endif %}
<li>
<a href="{{ path('fos_user_security_logout') }}">
<i class="material-icons small right">exit_to_app</i>
<i class="material-icons small">exit_to_app</i>
</a>
</li>
{% else %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% if opening_hour_kind_service.hasEnabled %}
{% if opening_hour_service.isOpen %}
<span class="badge green white-text" style="font-family:'Raleway',sans-serif;float:unset;">Ouvert</span>
{% else %}
<span class="badge red white-text" style="font-family:'Raleway',sans-serif;float:unset;">Fermé</span>
{% endif %}
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% if display_opening_hour_open_closed_header %}
{% if opening_hour_kind_service.hasEnabled %}
<div class="wrapper center-align {% if opening_hour_service.isOpen %}green{% else %}red{% endif %}">
<div class="col s12">
{% if opening_hour_service.isOpen %}
<span class="white-text">Ouvert</span>
{% else %}
<span class="white-text">Fermé</span>
{% endif %}
</div>
</div>
{% endif %}
{% endif %}
7 changes: 4 additions & 3 deletions app/Resources/views/card_reader/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

{% block beforecontainer %}
<nav class="main-navigation" role="navigation" style="background-color: {{ main_color }}">
<a id="logo-container" class="brand-logo hide-on-med-and-down" style="margin-left: 10px">Lecteur carte membre</a>
<div class="nav-wrapper" style="margin-right: 20px; font-size: 16px; font-weight: 600">
<ul class="right">
<div class="nav-wrapper">
<a id="logo-container" class="brand-logo hide-on-med-and-down" style="margin-left: 10px">Lecteur carte membre</a>
<ul class="right" style="margin-right:10px;">
<li>{{ date('now') | date_fr_full_with_time }}</li>
<li>{% include "admin/openinghour/_partial/open_closed_badge.html.twig" %}</li>
</ul>
</div>
</nav>
Expand Down
4 changes: 4 additions & 0 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ twig:
fly_and_fixed_allow_fixed_shift_free: '%fly_and_fixed_allow_fixed_shift_free%'
# time log saving
time_log_saving_shift_free_min_time_in_advance_days: '%time_log_saving_shift_free_min_time_in_advance_days%'
# opening hours
display_opening_hour_open_closed_header: '%display_opening_hour_open_closed_header%'
# role names & icons
role_user_name: Utilisateur
role_user_material_icon: person
Expand All @@ -123,6 +125,8 @@ twig:
shift_service: "@shift_service"
period_service: "@period_service"
time_log_service: "@time_log_service"
opening_hour_service: "@opening_hour_service"
opening_hour_kind_service: "@opening_hour_kind_service"

# Doctrine Configuration
doctrine:
Expand Down
1 change: 1 addition & 0 deletions app/config/parameters.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,4 @@ parameters:
display_freeze_account: true
display_freeze_account_false_message: "Le gel de compte n'est pas autorisé."
display_keys_shop: true
display_opening_hour_open_closed_header: true
6 changes: 6 additions & 0 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ services:
event_service:
class: AppBundle\Service\EventService
public: true
opening_hour_service:
class: AppBundle\Service\OpeningHourService
public: true
opening_hour_kind_service:
class: AppBundle\Service\OpeningHourKindService
public: true

search_user_form_helper:
class: AppBundle\Service\SearchUserFormHelper
Expand Down
5 changes: 5 additions & 0 deletions src/AppBundle/Repository/OpeningHourKindRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ public function findAll()
{
return $this->findBy(array(), array('id' => 'ASC'));
}

public function findEnabled()
{
return $this->findBy(array('enabled' => 1));
}
}
12 changes: 8 additions & 4 deletions src/AppBundle/Repository/OpeningHourRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function findOngoing(OpeningHourKind $openingHourKind = null)
->getResult();
}

public function findByDay(\DateTime $date, OpeningHourKind $openingHourKind = null, bool $openingHourKindEnabled = null)
public function findByDay(\DateTime $date, OpeningHourKind $openingHourKind = null, bool $onlyOpeningHourKindEnabled = false, bool $excludeClosed = false)
{
$qb = $this->createQueryBuilder('oh')
->leftJoin('oh.kind', 'ohk')
Expand All @@ -71,10 +71,14 @@ public function findByDay(\DateTime $date, OpeningHourKind $openingHourKind = nu
->setParameter('kind', $openingHourKind);
}

if ($openingHourKindEnabled != null) {
if ($onlyOpeningHourKindEnabled) {
$qb
->andwhere('ohk.enabled = :enabled')
->setParameter('enabled', $openingHourKindEnabled);
->andwhere('ohk.enabled = 1');
}

if ($excludeClosed) {
$qb
->andwhere('oh.closed = 0');
}

$qb
Expand Down
23 changes: 23 additions & 0 deletions src/AppBundle/Service/OpeningHourKindService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace AppBundle\Service;

use DateTime;
use AppBundle\Entity\OpeningHour;
use Doctrine\ORM\EntityManagerInterface;

class OpeningHourKindService
{
protected $em;

public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}

public function hasEnabled()
{
$openingHourKindEnabled = $this->em->getRepository('AppBundle:OpeningHourKind')->findEnabled();
return count($openingHourKindEnabled) > 0;
}
}
51 changes: 51 additions & 0 deletions src/AppBundle/Service/OpeningHourService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace AppBundle\Service;

use DateTime;
use AppBundle\Entity\OpeningHour;
use Doctrine\ORM\EntityManagerInterface;

class OpeningHourService
{
protected $em;

public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}

public function isOpen(\DateTime $date = null)
{
if (!$date) {
$date = new \DateTime('now');
}

// filter on day
$openingHoursEnabledDay = $this->em->getRepository('AppBundle:OpeningHour')->findByDay($date, null, true, true);

// filter on time
if (count($openingHoursEnabledDay) > 0) {
$openingHoursEnabledDayTime = array_filter($openingHoursEnabledDay, function($openingHour) use ($date) {
$openingHourStart = $openingHour->getStart()->setDate($date->format('Y'), $date->format('m'), $date->format('d'));
$openingHourEnd = $openingHour->getEnd()->setDate($date->format('Y'), $date->format('m'), $date->format('d'));
return ($openingHourStart <= $date) && ($openingHourEnd >= $date);
});

// final check on closing exceptions
if (count($openingHoursEnabledDayTime) > 0) {
$closingExceptions = $this->em->getRepository('AppBundle:ClosingException')->findOngoing($date);
if (!$closingExceptions) {
return True;
}
}
}

return False;
}

public function isClosed(\DateTime $date = null)
{
return !$this->isOpen($date);
}
}

0 comments on commit 3394172

Please sign in to comment.