2020-03-01 18:21:16 +01:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-03-01 18:21:16 +01:00
|
|
|
namespace Engelsystem\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
|
|
trait HasUserNotifications
|
|
|
|
{
|
2023-02-02 22:53:51 +01:00
|
|
|
protected function addNotification(string|array $value, NotificationType $type = NotificationType::MESSAGE): void
|
2020-03-01 18:21:16 +01:00
|
|
|
{
|
2023-02-02 22:53:51 +01:00
|
|
|
$type = 'messages.' . $type->value;
|
2020-03-01 18:21:16 +01:00
|
|
|
session()->set(
|
|
|
|
$type,
|
2023-02-02 22:53:51 +01:00
|
|
|
array_merge_recursive(session()->get($type, []), (array) $value)
|
2020-03-01 18:21:16 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-02 22:53:51 +01:00
|
|
|
/**
|
|
|
|
* @param NotificationType[]|null $types
|
|
|
|
* @return array<string,Collection|array<string>>
|
|
|
|
*/
|
|
|
|
protected function getNotifications(array $types = null): array
|
2020-03-01 18:21:16 +01:00
|
|
|
{
|
|
|
|
$return = [];
|
2023-02-02 22:53:51 +01:00
|
|
|
$types = $types ?: [
|
|
|
|
NotificationType::ERROR,
|
|
|
|
NotificationType::WARNING,
|
|
|
|
NotificationType::INFORMATION,
|
|
|
|
NotificationType::MESSAGE,
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($types as $type) {
|
|
|
|
$type = $type->value;
|
|
|
|
$path = 'messages.' . $type;
|
|
|
|
$return[$type] = Collection::make(
|
|
|
|
session()->get($path, [])
|
|
|
|
)->flatten();
|
|
|
|
session()->remove($path);
|
2020-03-01 18:21:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
}
|