| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentBoards\App\Services\NotificationService; |
| 6 |
use FluentBoards\Framework\Http\Request\Request; |
| 7 |
|
| 8 |
class NotificationController extends Controller |
| 9 |
{ |
| 10 |
private $notificationService; |
| 11 |
|
| 12 |
public function __construct(NotificationService $notificationService) |
| 13 |
{ |
| 14 |
parent::__construct(); |
| 15 |
$this->notificationService = $notificationService; |
| 16 |
} |
| 17 |
public function getAllNotifications(Request $request) |
| 18 |
{ |
| 19 |
try { |
| 20 |
$per_page = $request->getSafe('per_page', 'intval', 20); |
| 21 |
$page = $request->getSafe('page', 'intval', 1); |
| 22 |
$action = $request->getSafe('action', 'sanitize_text_field', 'all'); |
| 23 |
$data = $this->notificationService->getAllNotifications($per_page, $page, $action); |
| 24 |
|
| 25 |
return $this->sendSuccess($data, 200); |
| 26 |
} catch (\Exception $e) { |
| 27 |
return $this->sendError($e->getMessage(), 400); |
| 28 |
} |
| 29 |
} |
| 30 |
public function getAllUnreadNotifications(Request $request) |
| 31 |
{ |
| 32 |
try { |
| 33 |
$per_page = $request->getSafe('per_page', 'intval', 20); |
| 34 |
$page = $request->getSafe('page', 'intval', 1); |
| 35 |
return $this->sendSuccess( |
| 36 |
$this->notificationService->getAllUnreadNotifications($per_page, $page), |
| 37 |
200); |
| 38 |
} catch (\Exception $e) { |
| 39 |
return $this->sendError($e->getMessage(), 400); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
public function newNotificationNumber() |
| 44 |
{ |
| 45 |
try { |
| 46 |
$unread_notifications = $this->notificationService->newNotificationNumber(); |
| 47 |
|
| 48 |
return $this->sendSuccess([ |
| 49 |
'total_unread' => $unread_notifications, |
| 50 |
], 200); |
| 51 |
} catch (\Exception $e) { |
| 52 |
return $this->sendError($e->getMessage(), 400); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Return the current user's board notification settings with safe defaults. |
| 58 |
*/ |
| 59 |
public function getBoardNotificationSettings($board_id) |
| 60 |
{ |
| 61 |
$board_id = absint($board_id); |
| 62 |
try { |
| 63 |
$userId = get_current_user_id(); |
| 64 |
$currentSettings = $this->notificationService->getBoardNotificationSettingsWithDefaults($board_id, $userId); |
| 65 |
|
| 66 |
return $this->sendSuccess([ |
| 67 |
'currentSettings' => $currentSettings, |
| 68 |
], 200); |
| 69 |
} catch (\Exception $e) { |
| 70 |
return $this->sendError($e->getMessage(), 400); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
public function updateBoardNotificationSettings(Request $request, $board_id) |
| 75 |
{ |
| 76 |
$board_id = absint($board_id); |
| 77 |
try { |
| 78 |
// updatedSettings is an array, sanitize each element |
| 79 |
$rawSettings = $request->getSafe('updatedSettings'); |
| 80 |
$newSettings = []; |
| 81 |
if (is_array($rawSettings)) { |
| 82 |
foreach ($rawSettings as $key => $value) { |
| 83 |
// Sanitize key and value |
| 84 |
$sanitizedKey = sanitize_text_field($key); |
| 85 |
$sanitizedValue = sanitize_text_field($value); |
| 86 |
$newSettings[$sanitizedKey] = $sanitizedValue; |
| 87 |
} |
| 88 |
} |
| 89 |
$this->notificationService->updateBoardNotificationSettings($newSettings, $board_id); |
| 90 |
return $this->sendSuccess([ |
| 91 |
'message' => __('Board notification settings have been updated', 'fluent-boards'), |
| 92 |
], 200); |
| 93 |
} catch (\Exception $e) { |
| 94 |
return $this->sendError($e->getMessage(), 400); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
public function readNotification(Request $request) |
| 99 |
{ |
| 100 |
try { |
| 101 |
$notificationId = $request->getSafe('notification_id', 'intval'); |
| 102 |
|
| 103 |
if ($notificationId) { |
| 104 |
$notification = $this->notificationService->markNotificationRead($notificationId); |
| 105 |
} else { |
| 106 |
$notification = $this->notificationService->markAllRead(); |
| 107 |
} |
| 108 |
|
| 109 |
return $this->sendSuccess([ |
| 110 |
'message' => __('Notification is updated', 'fluent-boards'), |
| 111 |
'notification' => $notification |
| 112 |
], 200); |
| 113 |
|
| 114 |
} catch (\Exception $e) { |
| 115 |
return $this->sendError($e->getMessage(), 400); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|