Restore getUserMemberStatus function to original implementation and fix database queries
This commit is contained in:
121
functions.php
121
functions.php
@@ -171,6 +171,31 @@ function checkSuperAdmin()
|
||||
return $service->requireSuperAdmin();
|
||||
}
|
||||
|
||||
function checkUserSession()
|
||||
{
|
||||
// Redirect to login if user is not logged in
|
||||
if (!isset($_SESSION['user_id'])) {
|
||||
header('Location: login.php');
|
||||
exit;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely execute a database query
|
||||
* Returns false if database is unavailable, otherwise returns query result
|
||||
*/
|
||||
function safeQuery($sql)
|
||||
{
|
||||
global $conn;
|
||||
|
||||
if (!$conn || isset($GLOBALS['_DB_ERROR'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $conn->query($sql);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// USER INFORMATION FUNCTIONS - Delegates to UserService
|
||||
// =============================================================================
|
||||
@@ -277,27 +302,87 @@ function getEFTDetails($eft_id)
|
||||
|
||||
function getUserMemberStatus($user_id)
|
||||
{
|
||||
|
||||
$conn = openDatabaseConnection();
|
||||
$stmt = $conn->prepare("
|
||||
SELECT COUNT(*) as total FROM membership_application
|
||||
WHERE user_id = ?
|
||||
AND payment_status = 'PAID'
|
||||
AND accept_indemnity = 1
|
||||
LIMIT 1
|
||||
");
|
||||
|
||||
if (!$stmt) {
|
||||
error_log("getUserMemberStatus prepare error: " . $conn->error);
|
||||
|
||||
// Step 1: Check if the user is a member
|
||||
$queryUser = "SELECT member FROM users WHERE user_id = ?";
|
||||
$stmtUser = $conn->prepare($queryUser);
|
||||
if (!$stmtUser) {
|
||||
error_log("Failed to prepare user query: " . $conn->error);
|
||||
return false;
|
||||
}
|
||||
|
||||
$stmt->bind_param("i", $user_id);
|
||||
$stmt->execute();
|
||||
$stmt->bind_result($count);
|
||||
$stmt->fetch();
|
||||
$stmt->close();
|
||||
|
||||
return $count > 0;
|
||||
|
||||
$stmtUser->bind_param('i', $user_id);
|
||||
$stmtUser->execute();
|
||||
$resultUser = $stmtUser->get_result();
|
||||
$stmtUser->close();
|
||||
|
||||
if ($resultUser->num_rows === 0) {
|
||||
error_log("User not found for user_id: $user_id");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Step 3: Check the membership_application table for accept_indemnity status
|
||||
$queryApplication = "SELECT accept_indemnity FROM membership_application WHERE user_id = ?";
|
||||
$stmtApplication = $conn->prepare($queryApplication);
|
||||
if (!$stmtApplication) {
|
||||
error_log("Failed to prepare application query: " . $conn->error);
|
||||
return false;
|
||||
}
|
||||
|
||||
$stmtApplication->bind_param('i', $user_id);
|
||||
$stmtApplication->execute();
|
||||
$resultApplication = $stmtApplication->get_result();
|
||||
$stmtApplication->close();
|
||||
|
||||
if ($resultApplication->num_rows === 0) {
|
||||
error_log("No membership application found for user_id: $user_id");
|
||||
return false;
|
||||
}
|
||||
|
||||
$application = $resultApplication->fetch_assoc();
|
||||
$accept_indemnity = $application['accept_indemnity'];
|
||||
|
||||
// Validate accept_indemnity
|
||||
if ($accept_indemnity !== 1) {
|
||||
error_log("User has not accepted indemnity for user_id: $user_id");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Step 2: Check membership fees table for valid payment status and membership_end_date
|
||||
$queryFees = "SELECT payment_status, membership_end_date FROM membership_fees WHERE user_id = ?";
|
||||
$stmtFees = $conn->prepare($queryFees);
|
||||
if (!$stmtFees) {
|
||||
error_log("Failed to prepare fees query: " . $conn->error);
|
||||
return false;
|
||||
}
|
||||
|
||||
$stmtFees->bind_param('i', $user_id);
|
||||
$stmtFees->execute();
|
||||
$resultFees = $stmtFees->get_result();
|
||||
$stmtFees->close();
|
||||
|
||||
if ($resultFees->num_rows === 0) {
|
||||
error_log("Membership fees not found for user_id: $user_id");
|
||||
return false;
|
||||
}
|
||||
|
||||
$fees = $resultFees->fetch_assoc();
|
||||
$payment_status = $fees['payment_status'];
|
||||
$membership_end_date = $fees['membership_end_date'];
|
||||
|
||||
// Validate payment status and membership_end_date
|
||||
$current_date = new DateTime();
|
||||
$membership_end_date_obj = DateTime::createFromFormat('Y-m-d', $membership_end_date);
|
||||
|
||||
if ($payment_status === "PAID" && $current_date <= $membership_end_date_obj) {
|
||||
return true; // Membership is active
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false; // Membership is not active
|
||||
}
|
||||
|
||||
function getUserMemberStatusPending($user_id)
|
||||
|
||||
Reference in New Issue
Block a user