Automate email notification to managers when approved by admin
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
include __DIR__ . '/../../config/koneksi.php';
|
include __DIR__ . '/../../config/koneksi.php';
|
||||||
include __DIR__ . '/../../config/auth.php';
|
include __DIR__ . '/../../config/auth.php';
|
||||||
|
include_once __DIR__ . '/../auth/mail_helper.php';
|
||||||
header('Content-Type: application/json; charset=utf-8');
|
header('Content-Type: application/json; charset=utf-8');
|
||||||
|
|
||||||
require_role('admin');
|
require_role('admin');
|
||||||
@@ -9,6 +10,23 @@ $data = json_decode(file_get_contents('php://input'), true);
|
|||||||
$id = isset($data['id']) ? intval($data['id']) : 0;
|
$id = isset($data['id']) ? intval($data['id']) : 0;
|
||||||
if(!$id){ http_response_code(400); echo json_encode(['success'=>false,'error'=>'missing_id']); exit; }
|
if(!$id){ http_response_code(400); echo json_encode(['success'=>false,'error'=>'missing_id']); exit; }
|
||||||
|
|
||||||
|
// 1. Fetch user email and name first
|
||||||
|
$email = null;
|
||||||
|
$name = null;
|
||||||
|
$uStmt = $conn->prepare("SELECT email, name FROM users WHERE id = ?");
|
||||||
|
if($uStmt){
|
||||||
|
$uStmt->bind_param('i', $id);
|
||||||
|
$uStmt->execute();
|
||||||
|
$uRes = $uStmt->get_result();
|
||||||
|
$uRow = $uRes ? $uRes->fetch_assoc() : null;
|
||||||
|
if($uRow){
|
||||||
|
$email = $uRow['email'];
|
||||||
|
$name = $uRow['name'];
|
||||||
|
}
|
||||||
|
$uStmt->close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Perform the update
|
||||||
$st = $conn->prepare('UPDATE users SET status = "active", approved_by = ?, approved_at = NOW() WHERE id = ?');
|
$st = $conn->prepare('UPDATE users SET status = "active", approved_by = ?, approved_at = NOW() WHERE id = ?');
|
||||||
if(!$st){ http_response_code(500); echo json_encode(['success'=>false,'error'=>$conn->error]); exit; }
|
if(!$st){ http_response_code(500); echo json_encode(['success'=>false,'error'=>$conn->error]); exit; }
|
||||||
$adminId = intval(get_current_user_info()['id']);
|
$adminId = intval(get_current_user_info()['id']);
|
||||||
@@ -17,5 +35,19 @@ $res = $st->execute();
|
|||||||
$st->close();
|
$st->close();
|
||||||
if(!$res){ http_response_code(500); echo json_encode(['success'=>false,'error'=>$conn->error]); exit; }
|
if(!$res){ http_response_code(500); echo json_encode(['success'=>false,'error'=>$conn->error]); exit; }
|
||||||
|
|
||||||
|
// 3. Send email notification if user has email
|
||||||
|
if($email){
|
||||||
|
try {
|
||||||
|
$subject = 'Akun Pengelola Telah Aktif - WebGIS';
|
||||||
|
$message = "Halo " . ($name ? $name : 'Pengelola') . ",\n\n" .
|
||||||
|
"Akun pengelola Anda pada sistem WebGIS Poverty Mapping telah disetujui dan diaktifkan oleh administrator.\n\n" .
|
||||||
|
"Silakan login menggunakan email dan password yang telah Anda daftarkan.\n\n" .
|
||||||
|
"Terima kasih.";
|
||||||
|
send_mail_notify($email, $subject, $message, false);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
error_log("Failed to send approval email: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
echo json_encode(['success'=>true]);
|
echo json_encode(['success'=>true]);
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
include __DIR__ . '/../../config/koneksi.php';
|
include __DIR__ . '/../../config/koneksi.php';
|
||||||
include __DIR__ . '/../../config/auth.php';
|
include __DIR__ . '/../../config/auth.php';
|
||||||
|
include_once __DIR__ . '/mail_helper.php';
|
||||||
header('Content-Type: application/json; charset=utf-8');
|
header('Content-Type: application/json; charset=utf-8');
|
||||||
|
|
||||||
$admin = require_bearer_login();
|
$admin = require_bearer_login();
|
||||||
@@ -10,12 +11,40 @@ $data = json_decode(file_get_contents('php://input'), true);
|
|||||||
if(!$data || !isset($data['id'])){ http_response_code(400); echo json_encode(['success'=>false,'error'=>'missing_id']); exit; }
|
if(!$data || !isset($data['id'])){ http_response_code(400); echo json_encode(['success'=>false,'error'=>'missing_id']); exit; }
|
||||||
$id = intval($data['id']);
|
$id = intval($data['id']);
|
||||||
|
|
||||||
|
// Fetch user email and name first
|
||||||
|
$email = null;
|
||||||
|
$name = null;
|
||||||
|
$uStmt = $conn->prepare("SELECT email, name FROM users WHERE id = ?");
|
||||||
|
if($uStmt){
|
||||||
|
$uStmt->bind_param('i', $id);
|
||||||
|
$uStmt->execute();
|
||||||
|
$uRes = $uStmt->get_result();
|
||||||
|
$uRow = $uRes ? $uRes->fetch_assoc() : null;
|
||||||
|
if($uRow){
|
||||||
|
$email = $uRow['email'];
|
||||||
|
$name = $uRow['name'];
|
||||||
|
}
|
||||||
|
$uStmt->close();
|
||||||
|
}
|
||||||
|
|
||||||
$st = $conn->prepare('UPDATE users SET status = ?, approved_by = ?, approved_at = NOW() WHERE id = ? AND role = "manager"');
|
$st = $conn->prepare('UPDATE users SET status = ?, approved_by = ?, approved_at = NOW() WHERE id = ? AND role = "manager"');
|
||||||
if(!$st){ http_response_code(500); echo json_encode(['success'=>false,'error'=>$conn->error]); exit; }
|
if(!$st){ http_response_code(500); echo json_encode(['success'=>false,'error'=>$conn->error]); exit; }
|
||||||
$status = 'active'; $adminId = intval($admin['id']); $st->bind_param('sii', $status, $adminId, $id);
|
$status = 'active'; $adminId = intval($admin['id']); $st->bind_param('sii', $status, $adminId, $id);
|
||||||
$res = $st->execute(); $st->close();
|
$res = $st->execute(); $st->close();
|
||||||
if(!$res){ http_response_code(500); echo json_encode(['success'=>false,'error'=>$conn->error]); exit; }
|
if(!$res){ http_response_code(500); echo json_encode(['success'=>false,'error'=>$conn->error]); exit; }
|
||||||
|
|
||||||
// optional: send notification email
|
if($email){
|
||||||
|
try {
|
||||||
|
$subject = 'Akun Pengelola Telah Aktif - WebGIS';
|
||||||
|
$message = "Halo " . ($name ? $name : 'Pengelola') . ",\n\n" .
|
||||||
|
"Akun pengelola Anda pada sistem WebGIS Poverty Mapping telah disetujui dan diaktifkan oleh administrator.\n\n" .
|
||||||
|
"Silakan login menggunakan email dan password yang telah Anda daftarkan.\n\n" .
|
||||||
|
"Terima kasih.";
|
||||||
|
send_mail_notify($email, $subject, $message, false);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
error_log("Failed to send approval email: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
echo json_encode(['success'=>true]);
|
echo json_encode(['success'=>true]);
|
||||||
?>
|
?>
|
||||||
Reference in New Issue
Block a user