| 1 |
<?php |
| 2 |
|
| 3 |
use AcyMailing\Helpers\HeaderHelper; |
| 4 |
|
| 5 |
function acym_enqueueMessage($message, string $type = 'success', bool $addNotification = true, array $addDashboardNotification = [], bool $addHeaderNotification = true): void |
| 6 |
{ |
| 7 |
$type = str_replace(['notice', 'message'], ['info', 'success'], $type); |
| 8 |
$message = is_array($message) ? implode('<br/>', $message) : $message; |
| 9 |
|
| 10 |
$notification = new stdClass(); |
| 11 |
$notification->message = $message; |
| 12 |
$notification->date = time(); |
| 13 |
$notification->read = false; |
| 14 |
$notification->level = $type; |
| 15 |
|
| 16 |
$handledTypes = ['info', 'warning', 'error']; |
| 17 |
|
| 18 |
if ($addNotification && acym_isAdmin()) { |
| 19 |
$helperHeader = new HeaderHelper(); |
| 20 |
$notification->id = $helperHeader->addNotification($notification); |
| 21 |
} else { |
| 22 |
$handledTypes[] = 'success'; |
| 23 |
} |
| 24 |
|
| 25 |
if (in_array($type, $handledTypes) && $addHeaderNotification) { |
| 26 |
acym_session(); |
| 27 |
if (empty($_SESSION['acymessage'.$type]) || !in_array($message, $_SESSION['acymessage'.$type])) { |
| 28 |
if (empty($notification->id)) { |
| 29 |
$_SESSION['acymessage'.$type][] = $message; |
| 30 |
} else { |
| 31 |
$_SESSION['acymessage'.$type][$notification->id] = $message; |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
if (!empty($addDashboardNotification)) { |
| 37 |
$config = acym_config(); |
| 38 |
$notRemindable = json_decode($config->get('remindme'), true); |
| 39 |
$existingNotifications = json_decode($config->get('dashboard_notif', '[]'), true); |
| 40 |
|
| 41 |
foreach ($addDashboardNotification as &$dashboardNotification) { |
| 42 |
if (in_array($dashboardNotification['name'], $notRemindable)) { |
| 43 |
continue; |
| 44 |
} |
| 45 |
$dashboardNotification['date'] = time(); |
| 46 |
$dashboardNotification['level'] = $type; |
| 47 |
$dashboardNotification['message'] = $message; |
| 48 |
|
| 49 |
$found = false; |
| 50 |
foreach ($existingNotifications as &$existingNotification) { |
| 51 |
if ($existingNotification['name'] === $dashboardNotification['name']) { |
| 52 |
$existingNotification = $dashboardNotification; |
| 53 |
$found = true; |
| 54 |
break; |
| 55 |
} |
| 56 |
} |
| 57 |
if (!$found) { |
| 58 |
$existingNotifications[] = $dashboardNotification; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
$config->saveConfig(['dashboard_notif' => json_encode($existingNotifications)], false); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
function acym_displayMessages(): void |
| 67 |
{ |
| 68 |
$types = ['success', 'info', 'warning', 'error']; |
| 69 |
acym_session(); |
| 70 |
foreach ($types as $type) { |
| 71 |
if (empty($_SESSION['acymessage'.$type])) continue; |
| 72 |
|
| 73 |
acym_display($_SESSION['acymessage'.$type], $type); |
| 74 |
unset($_SESSION['acymessage'.$type]); |
| 75 |
} |
| 76 |
} |
| 77 |
|