Commit 44a07e04 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Notion d'emplacement (table ) permettant de restreindre l'affichage d'une...

Notion d'emplacement (table ) permettant de restreindre l'affichage d'une alerte selon la route/URL de la page courante.
parent 248e9d7d
Loading
Loading
Loading
Loading
Loading
+1 −0
Changes for CHANGELOG.md: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ CHANGELOG
4.0.0
-----
- Le respect du **planning** d'affichage d'une alerte puisée en base de données devient incontournable (cf. [README.md](README.md#ruptures-de-compatibilité).
- Notion d'emplacement (table `unicaen_alerte_alerte_location`) permettant de restreindre l'affichage d'une alerte selon la route/URL de la page courante.

3.2.0
-----
+14 −1
Changes for README.md: 14 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -46,6 +46,18 @@ càd une ou plusieurs périodes précises (date de début, date de fin) pendant

Si aucun planning n'est spécifié, l'alerte sera prise en compte systématiquement.

#### Emplacements

Une alerte stockée en bdd peut avoir un ou plusieurs **emplacements** (table `unicaen_alerte_alerte_location`), 
spécifiés par :
  - une expression régulière matchant une route Laminas 
    (colonne `route_name_pattern`, ex : `#^home|these|soutenance|formation\/.+#`) ;
    l'alerte ne sera prise en compte que si le `RouteMatch::getMatchedRouteName()` courant satisfait la regexp ;
  - une expression régulière matchant une URL 
    (colonne `url_pattern`, ex : `#\/these\/archivage#`) ;
    l'alerte ne sera prise en compte que si le `$_SERVER['REQUEST_URI']` satisfait la regexp.
  
Si aucun emplacement particulier n'est spécifié, l'alerte sera prise en compte systématiquement.
  
    
Exemples d'utilisation
@@ -64,7 +76,8 @@ echo $this->alertes()addAlerteNew("Youpi !!", "Félicitations", 'success', 3000)

```php
// Affichage sous forme de Toasts Bootstrap de *toutes* les alertes puisées en bdd :
//   - n'ayant aucun planning d'affichage particulier *ou* ayant au moins un planning incluant la date du jour :
//   - n'ayant aucun planning d'affichage particulier *ou* ayant au moins un planning incluant la date du jour,
//   - n'ayant aucun emplacement particulier :
echo $this->alertes()->addAlertesFromDatabase()->renderAsBootstrapToasts();
// Idem, mais sous forme d'Alerts Bootstrap classiques :
echo $this->alertes()->addAlertesFromDatabase()->renderAsBootstrapAlerts();
+18 −0
Changes for data/sql/schema.pgsql.sql: 18 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -51,9 +51,25 @@ create index unicaen_alerte_alerte_planning__idx on unicaen_alerte_alerte_planni
create index unicaen_alerte_alerte_planning__alerte__idx on unicaen_alerte_alerte_planning (alerte_id);


create table unicaen_alerte_alerte_location
(
    id bigserial constraint unicaen_alerte_alerte_location__pk primary key,
    alerte_id bigint not null constraint unicaen_alerte_alerte_location__unicaen_alerte__fk references unicaen_alerte_alerte,
    route_name_pattern varchar(512),
    url_pattern varchar(512)
);

comment on table unicaen_alerte_alerte_location is 'Emplacement d''affichage des alertes';
comment on column unicaen_alerte_alerte_location.alerte_id is 'Identifiant de l''alerte concernée';
comment on column unicaen_alerte_alerte_location.route_name_pattern is 'Expression régulière décrivant les routes pour lesquelles l''alerte doit s''afficher';
comment on column unicaen_alerte_alerte_location.url_pattern is 'Expression régulière décrivant les URL pour lesquelles l''alerte doit s''afficher';

create index unicaen_alerte_alerte_location__alerte__idx on unicaen_alerte_alerte_location (alerte_id);
create index unicaen_alerte_alerte_location__idx on unicaen_alerte_alerte_location (id);


/*

insert into unicaen_alerte_alerte(id, code, title, text, severity, duration, dismissible)
select nextval('unicaen_alerte_alerte_id_seq'),
       'ALERTE_SOUTENANCE',
@@ -68,3 +84,5 @@ select nextval('unicaen_alerte_alerte_planning_id_seq'),
       currval('unicaen_alerte_alerte_id_seq'),
       to_date('20220701','YYYYMMDD'),
       to_date('20220831','YYYYMMDD');

*/
 No newline at end of file
+2 −1
Changes for src/UnicaenAlerte/Container/Database/DatabaseContainer.php: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -18,7 +18,8 @@ class DatabaseContainer implements FilterableContainerInterface
    public function fetchAlertes(): array
    {
        $qb = $this->alerteRepository->createQueryBuilder('a')
            ->leftJoin('a.plannings', 'p')->addSelect('p');
            ->leftJoin('a.plannings', 'p')->addSelect('p')
            ->leftJoin('a.locations', 'l')->addSelect('l');

        $ors = [];
        foreach ($this->withCodes as $code) {
+34 −4
Changes for src/UnicaenAlerte/Entity/Db/Alerte.php: 34 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -35,10 +35,15 @@ class Alerte
    private bool $dismissible = true;

    /**
     * @var \UnicaenAlerte\Entity\Db\AlertePlanning[]|\Doctrine\Common\Collections\Collection
     * @var \Doctrine\Common\Collections\Collection<\UnicaenAlerte\Entity\Db\AlertePlanning>
     */
    private Collection $plannings;

    /**
     * @var \Doctrine\Common\Collections\Collection<\UnicaenAlerte\Entity\Db\AlerteLocation>
     */
    private Collection $locations;

    /**
     * Instancie une alerte ayant les propriétés spécifiées.
     *
@@ -51,7 +56,7 @@ class Alerte
     *
     * @return \UnicaenAlerte\Entity\Db\Alerte
     */
    static public function create(string $code,
    public static function create(string $code,
                                  string $text,
                                  string $title,
                                  string $severity = Alerte::SEVERITY_INFO,
@@ -68,7 +73,7 @@ class Alerte
        return $alerte;
    }

    static public function sorter(?array $severitiesOrders = null): callable
    public static function sorter(?array $severitiesOrders = null): callable
    {
        if (!$severitiesOrders) {
            $severitiesOrders = static::SEVERITIES_ORDERS_DEFAULTS;
@@ -80,6 +85,7 @@ class Alerte
    public function __construct()
    {
        $this->plannings = new ArrayCollection();
        $this->locations = new ArrayCollection();
    }

    public function __toString(): string
@@ -201,7 +207,7 @@ class Alerte
    }

    /**
     * @return \Doctrine\Common\Collections\Collection|\UnicaenAlerte\Entity\Db\AlertePlanning[]
     * @return Collection<\UnicaenAlerte\Entity\Db\AlertePlanning>
     */
    public function getPlannings(): Collection
    {
@@ -223,4 +229,28 @@ class Alerte

        return $this;
    }

    /**
     * @return Collection<\UnicaenAlerte\Entity\Db\AlerteLocation>
     */
    public function getLocations(): Collection
    {
        return $this->locations;
    }

    public function addLocations(iterable $locations): self
    {
        foreach ($locations as $avisComplem)
            $this->locations->add($avisComplem);

        return $this;
    }

    public function removeLocations(iterable $locations): self
    {
        foreach ($locations as $avisComplem)
            $this->locations->removeElement($avisComplem);

        return $this;
    }
}
Loading