Files
2026-04-28 21:15:09 +03:00

85 lines
2.2 KiB
PHP

<?php
use App\Models\FeedbackReport;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
$title = $_POST['title'] ?? '';
$description = $_POST['description'] ?? '';
$screenshot = $_POST['screenshot'] ?? '';
$user_id = Auth::id();
if (!$title && !$description) {
echo json_encode(['status' => 'error', 'message' => 'Incomplete data sent!']);
exit();
}
try {
$screenshot_path = null;
if (!empty($screenshot)) {
$screenshot = preg_replace('#^data:image/\w+;base64,#i', '', $screenshot);
$decoded = base64_decode($screenshot);
if (strlen($decoded) > 5 * 1024 * 1024) {
echo json_encode(['status' => 'error', 'message' => 'Screenshot too large!']);
exit();
}
$folder = base_path('storage/feedback/Images');
if (!file_exists($folder)) mkdir($folder, 0777, true);
$fileName = 'feedback_' . time() . '_' . uniqid() . '.jpg';
file_put_contents("$folder/$fileName", $decoded);
$screenshot_path = "storage/feedback/Images/$fileName";
}
$user = Auth::user();
$username = $user ? $user->name : 'Unknown User';
// SAVE TO DB
$feedback = FeedbackReport::create([
'user_id' => $user_id,
'username' => $username,
'title' => $title,
'description' => $description,
'screenshot_path' => $screenshot_path,
'status' => 'submitted',
]);
// Send notification to users with feedback permission
$notificationCode = 'notification_feedback_report';
$notificationTitle = 'New Feedback';
$notificationMessage = sprintf(
'%s submitted a new feedback with the title "%s".',
$username,
$title
);
$notificationLink = '/admin/types/feedback-reports';
sendNotification(
$notificationCode,
$notificationMessage,
$notificationLink,
$notificationTitle,
null,
1
);
echo json_encode([
'status' => 'success',
'message' => 'Your feedback has been saved successfully!'
]);
} catch (Exception $e) {
echo json_encode([
'status' => 'error',
'message' => 'Server error: ' . $e->getMessage()
]);
}
exit();
?>