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