Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UserNotification] Add more configuration options #176

Merged
merged 17 commits into from
Dec 12, 2023
137 changes: 127 additions & 10 deletions src/Event/NotificationEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,151 @@
namespace KevinPapst\TablerBundle\Event;

use KevinPapst\TablerBundle\Model\NotificationInterface;
use KevinPapst\TablerBundle\Model\NotificationV2Interface;

class NotificationEvent extends ThemeEvent
{
private ?string $title = null;

private ?string $titleEmpty = null;

private ?string $titleHtml = null;

private bool $withArrow = true;

private string $badgeColor = 'red';

private bool $showBadgeTotal = true;

private int $maxDisplay = 10;

/**
* @var array<NotificationInterface>
* @var array<int,NotificationInterface | NotificationV2Interface>
*/
private array $notifications = [];

public function getTotal(): int
{
return \count($this->getNotifications(null));
}

public function isVisible(): bool
{
return $this->getTotal() > 0 || ($this->getTotal() === 0 && $this->showIfEmpty);
}

public function __construct(
private bool $showIfEmpty = false,
) {
}

public function isShowIfEmpty(): bool
{
return $this->showIfEmpty;
}

public function setShowIfEmpty(bool $showIfEmpty): void
{
$this->showIfEmpty = $showIfEmpty;
}

public function getTitle(): ?string
{
return $this->title;
}

public function setTitle(?string $title): void
{
$this->title = $title;
}

public function getTitleEmpty(): ?string
{
return $this->titleEmpty;
}

public function setTitleEmpty(?string $titleEmpty): void
{
$this->titleEmpty = $titleEmpty;
}

public function getTitleHtml(): ?string
{
return $this->titleHtml;
}

public function setTitleHtml(?string $titleHtml): void
{
$this->titleHtml = $titleHtml;
}

public function isWithArrow(): bool
{
return $this->withArrow;
}

public function setWithArrow(bool $withArrow): void
{
$this->withArrow = $withArrow;
}

public function getBadgeColor(): string
{
return $this->badgeColor;
}

public function setBadgeColor(string $badgeColor): void
{
$this->badgeColor = $badgeColor;
}

public function isShowBadgeTotal(): bool
{
return $this->showBadgeTotal;
}

public function setShowBadgeTotal(bool $showBadgeTotal): void
{
$this->showBadgeTotal = $showBadgeTotal;
}

public function getMaxDisplay(): int
{
return $this->maxDisplay;
}

public function setMaxDisplay(int $maxDisplay): void
{
$this->maxDisplay = $maxDisplay;
}

/**
* @param int|null $max
* @return NotificationInterface[]
* @return array<int,NotificationInterface | NotificationV2Interface>
*/
public function getNotifications(?int $max = 10): array
{
if (null !== $max) {
return \array_slice($this->notifications, 0, $max);
if ($max === null) {
return $this->notifications;
} elseif ($max !== 10) {
trigger_deprecation('kevinpapst/tabler-bundle', '1.1.0', 'Setting `$max` parameter is deprecated. Use setMaxDisplay() instead!');
}

return $this->notifications;
return \array_slice($this->notifications, 0, $this->maxDisplay);
}

public function addNotification(NotificationInterface $notificationInterface): void
public function addNotification(NotificationV2Interface|NotificationInterface $notification): void
{
$this->notifications[] = $notificationInterface;
// if (($notification instanceof NotificationV2Interface) === false) {
// trigger_deprecation('kevinpapst/tabler-bundle', '1.2.0', 'Notification should implement NotificationV2Interface::class!');
// }

$this->notifications[] = $notification;
}

public function getTotal(): int
public function removeNotification(NotificationInterface $notification): void
{
return \count($this->notifications);
if (($key = array_search($notification, $this->notifications)) !== false) {
unset($this->notifications[$key]);
}
}
}
41 changes: 41 additions & 0 deletions src/Event/NotificationEventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Tabler bundle, created by Kevin Papst (www.kevinpapst.de).
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KevinPapst\TablerBundle\Event;

use KevinPapst\TablerBundle\Model\NotificationInterface;
use KevinPapst\TablerBundle\Model\NotificationV2Interface;

interface NotificationEventInterface
{
public function getTotal(): int;

public function isVisible(): bool;

public function isShowIfEmpty(): bool;

public function getTitle(): ?string;

public function getTitleEmpty(): ?string;

public function getTitleHtml(): ?string;

public function isWithArrow(): bool;

public function getBadgeColor(): string;

public function isShowBadgeTotal(): bool;

public function getMaxDisplay(): int;

/**
* @return array<int,NotificationInterface | NotificationV2Interface>
*/
public function getNotifications(?int $max = 10): array;
}
86 changes: 75 additions & 11 deletions src/Model/NotificationModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,105 @@

use KevinPapst\TablerBundle\Helper\Constants;

class NotificationModel implements NotificationInterface
class NotificationModel implements NotificationV2Interface
{
private ?string $url = null;
private bool $active = false;
private bool $disabled = false;
private bool $withBadge = true;
private bool $badgeAnimated = true;

public function __construct(private string $id, private string $message, private string $type = Constants::TYPE_INFO)
{
private bool $html = false;

public function __construct(
private readonly string $id,
private string $message,
private string $type = Constants::TYPE_INFO
) {
}

public function getIdentifier(): string
{
return $this->id;
}

public function getType(): string
public function getUrl(): ?string
{
return $this->type;
return $this->url;
}

public function setMessage(string $message): void
public function setUrl(?string $url): void
{
$this->message = $message;
$this->url = $url;
}

public function isActive(): bool
{
return $this->active;
}

public function setActive(bool $active): void
{
$this->active = $active;
}

public function isDisabled(): bool
{
return $this->disabled;
}

public function setDisabled(bool $disabled): void
{
$this->disabled = $disabled;
}

public function isWithBadge(): bool
{
return $this->withBadge;
}

public function setWithBadge(bool $withBadge): void
{
$this->withBadge = $withBadge;
}

public function isBadgeAnimated(): bool
{
return $this->badgeAnimated;
}

public function setBadgeAnimated(bool $badgeAnimated): void
{
$this->badgeAnimated = $badgeAnimated;
}

public function isHtml(): bool
{
return $this->html;
}

public function setHtml(bool $html): void
{
$this->html = $html;
}

public function getMessage(): string
{
return $this->message;
}

public function setUrl(?string $url): void
public function setMessage(string $message): void
{
$this->url = $url;
$this->message = $message;
}

public function getUrl(): ?string
public function getType(): string
{
return $this->url;
return $this->type;
}

public function setType(string $type): void
{
$this->type = $type;
}
}
28 changes: 28 additions & 0 deletions src/Model/NotificationV2Interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Tabler bundle, created by Kevin Papst (www.kevinpapst.de).
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KevinPapst\TablerBundle\Model;

/**
* TODO : Implement these in NotificationInterface later
* @internal THIS WILL BE REMOVED WITH THE NEXT MAJOR RELEASE, NO BC PROMISE GIVEN
* @deprecated
*/
interface NotificationV2Interface extends NotificationInterface
{
public function isActive(): bool;

public function isDisabled(): bool;

public function isWithBadge(): bool;

public function isBadgeAnimated(): bool;

public function isHtml(): bool;
}
17 changes: 9 additions & 8 deletions src/Twig/RuntimeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ final class RuntimeExtension implements RuntimeExtensionInterface
* @param array<string, string|null> $routes
* @param array<string, string> $icons
*/
public function __construct(private EventDispatcherInterface $eventDispatcher, private ContextHelper $helper, private array $routes, private array $icons)
{
public function __construct(
private EventDispatcherInterface $eventDispatcher,
private ContextHelper $helper,
private array $routes,
private array $icons
) {
}

public function getRouteByAlias(string $routeName): string
Expand Down Expand Up @@ -68,6 +72,7 @@ public function containerClass(string $class = ''): string

/**
* @param Request $request
*
* @return MenuItemInterface[]|null
*/
public function getMenu(Request $request): ?array
Expand All @@ -82,19 +87,15 @@ public function getMenu(Request $request): ?array
return $event->getItems();
}

public function getNotifications(): ?NotificationEvent
public function getNotifications(): NotificationEvent
{
if (!$this->eventDispatcher->hasListeners(NotificationEvent::class)) {
return null;
return new NotificationEvent(false);
}

/** @var NotificationEvent $listEvent */
$listEvent = $this->eventDispatcher->dispatch(new NotificationEvent());

if ($listEvent->getTotal() === 0) {
return null;
}

return $listEvent;
}

Expand Down
Loading
Loading