| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Controllers\Configuration; |
| 4 |
|
| 5 |
use AcyMailing\Controllers\DashboardController; |
| 6 |
use AcyMailing\Helpers\HeaderHelper; |
| 7 |
|
| 8 |
trait Subscription |
| 9 |
{ |
| 10 |
public function removeNotification(): void |
| 11 |
{ |
| 12 |
$whichNotification = acym_getVar('string', 'id'); |
| 13 |
|
| 14 |
if ($whichNotification !== '0' && empty($whichNotification)) { |
| 15 |
acym_sendAjaxResponse(acym_translation('ACYM_NOTIFICATION_NOT_FOUND'), [], false); |
| 16 |
} |
| 17 |
|
| 18 |
$notifications = json_decode($this->config->get('notifications', '[]'), true) ?? []; |
| 19 |
$dashboardNotifications = json_decode($this->config->get('dashboard_notif', '[]'), true) ?? []; |
| 20 |
|
| 21 |
if ('all' === $whichNotification) { |
| 22 |
$notifications = []; |
| 23 |
} else { |
| 24 |
$notifications = array_values(array_filter($notifications, fn($notif) => !isset($notif['id']) || $notif['id'] !== $whichNotification)); |
| 25 |
|
| 26 |
$dashboardNotifications = array_values(array_filter($dashboardNotifications, fn($notif) => !isset($notif['name']) || $notif['name'] !== $whichNotification)); |
| 27 |
|
| 28 |
if (is_numeric($whichNotification)) { |
| 29 |
$notifications = array_values(array_filter($notifications, fn($notif, $key) => $key != $whichNotification, ARRAY_FILTER_USE_BOTH)); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
$this->config->save(['notifications' => json_encode($notifications)]); |
| 34 |
$this->config->save(['dashboard_notif' => json_encode($dashboardNotifications)], false); |
| 35 |
|
| 36 |
$helperHeader = new HeaderHelper(); |
| 37 |
$dashboardController = new DashboardController(); |
| 38 |
|
| 39 |
$data = [ |
| 40 |
'notifications' => $dashboardNotifications, |
| 41 |
]; |
| 42 |
$dashboardController->getDashboardNotifications($data); |
| 43 |
|
| 44 |
acym_sendAjaxResponse( |
| 45 |
'', |
| 46 |
[ |
| 47 |
'headerHtml' => $helperHeader->getNotificationCenterInner($notifications), |
| 48 |
'dashboardHtml' => $data['dashboardNotifications'], |
| 49 |
] |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
public function markNotificationRead(): void |
| 54 |
{ |
| 55 |
$which = acym_getVar('string', 'id'); |
| 56 |
|
| 57 |
$notifications = json_decode($this->config->get('notifications', '[]'), true); |
| 58 |
if (empty($notifications)) { |
| 59 |
acym_sendAjaxResponse('', []); |
| 60 |
} |
| 61 |
|
| 62 |
if (empty($which)) { |
| 63 |
foreach ($notifications as $key => $notification) { |
| 64 |
$notifications[$key]['read'] = true; |
| 65 |
} |
| 66 |
} else { |
| 67 |
foreach ($notifications as $key => $notification) { |
| 68 |
if ($notification['id'] != $which) continue; |
| 69 |
$notifications[$key]['read'] = true; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
$this->config->save(['notifications' => json_encode($notifications)]); |
| 75 |
|
| 76 |
acym_sendAjaxResponse('', []); |
| 77 |
} |
| 78 |
|
| 79 |
public function addNotification(): void |
| 80 |
{ |
| 81 |
$message = acym_getVar('string', 'message'); |
| 82 |
$level = acym_getVar('string', 'level'); |
| 83 |
|
| 84 |
if (empty($message) || empty($level)) { |
| 85 |
acym_sendAjaxResponse(acym_translation('ACYM_ERROR'), [], false); |
| 86 |
} |
| 87 |
|
| 88 |
$helperHeader = new HeaderHelper(); |
| 89 |
|
| 90 |
$newNotification = new \stdClass(); |
| 91 |
$newNotification->message = $message; |
| 92 |
$newNotification->level = $level; |
| 93 |
$newNotification->read = false; |
| 94 |
$newNotification->date = time(); |
| 95 |
|
| 96 |
$helperHeader->addNotification($newNotification); |
| 97 |
|
| 98 |
acym_sendAjaxResponse('', ['notificationCenter' => $helperHeader->getNotificationCenter()]); |
| 99 |
} |
| 100 |
|
| 101 |
private function loadSurveyAnswers(array &$data): void |
| 102 |
{ |
| 103 |
$surveyTexts = $this->config->get('unsub_survey', '{}'); |
| 104 |
$data['surveyAnswers'] = json_decode($surveyTexts, true); |
| 105 |
} |
| 106 |
} |
| 107 |
|